led7seg.s
changeset 89 1b3cea759eff
parent 88 c923295ee520
child 90 13c2deb919d1
equal deleted inserted replaced
88:c923295ee520 89:1b3cea759eff
     1 .nolist
       
     2 .include "m168def.inc"      ; Same family as 328P
       
     3 .list
       
     4 
       
     5 
       
     6 ;; Interrupt Vector
       
     7 .org 0x00
       
     8         rjmp        init
       
     9 
       
    10 .org OC1Aaddr
       
    11 		; Timer/Counter1 Compare Output A
       
    12 		rjmp		Timer_OC1A
       
    13 
       
    14 .org OC0Aaddr
       
    15         ; Timer/Counter0 Compare Output A
       
    16         rjmp        Timer_OC0A
       
    17 
       
    18 .org SPIaddr
       
    19         rjmp        SPI_Interrupt
       
    20 
       
    21 .org ADCCaddr
       
    22         rjmp        ADC_Interrupt
       
    23 
       
    24 .org 0x40
       
    25 
       
    26 ;; Syntax
       
    27 .include "macros.inc"
       
    28 
       
    29 ;; SPI
       
    30 .include "spi.inc"
       
    31 
       
    32 ;; LCD
       
    33 .include "led7seg.inc"
       
    34 
       
    35 ;; DIPs
       
    36 .include "dip.inc"
       
    37 
       
    38 ;; ADC
       
    39 
       
    40 ; ADC Interrupt handler
       
    41 On_ADC:
       
    42     ; DEBUG
       
    43 		sbi			PIND, PORTD7
       
    44 
       
    45     ; Check timer, from r16
       
    46         rcall       ADC_Read8
       
    47 
       
    48         ldi         r17, 64
       
    49         mul         r16, r17
       
    50         mov         XL, r0
       
    51         mov         XH, r1 
       
    52 
       
    53         rjmp       Timer_Update
       
    54 
       
    55 .set ADC_Handler = On_ADC
       
    56 
       
    57 .include "adc.inc"
       
    58 
       
    59 ;; Timer
       
    60 .set TIMER0_CB_A = SPI_Update
       
    61 
       
    62 .include "timer.inc"
       
    63 
       
    64 ;; Utils
       
    65 .include "delay.inc"
       
    66 .include "div.inc"
       
    67 
       
    68 Sleep_ADC:
       
    69         ; delay
       
    70         rcall       ADC_Read8 
       
    71         ; rcall       DIP_Read8
       
    72         
       
    73         ; Sleep for 64 * var timer cycles
       
    74         ldi         r17, 64
       
    75         mul         r16, r17
       
    76         mov         XL, r0
       
    77         mov         XH, r1 
       
    78 
       
    79         rjmp       Timer_Sleep
       
    80 
       
    81 ;; Show value
       
    82 Main_ShowValue:
       
    83         ; load value to r16
       
    84         ; rcall       DIP_Read8
       
    85         rcall       ADC_Read8
       
    86         ; rcall       Timer0_Read8
       
    87 
       
    88         ; display from r16
       
    89         rcall       LED7_ShowHex
       
    90         
       
    91         ; wait
       
    92         rcall       Sleep_ADC
       
    93 
       
    94         ; ldi         XL, 0
       
    95         ; ldi         XH, 16
       
    96         ; rcall       Timer_Sleep
       
    97         
       
    98         ; rcall       ADC_Read8
       
    99         ; mov         r20, r16
       
   100         ; rcall       VarDelay
       
   101 
       
   102         ; continue
       
   103         rjmp        Main_ShowValue
       
   104 
       
   105 ;; Count down from F
       
   106 ; Returns once we've hit zero
       
   107 Main_Countdown:
       
   108         ; init from F
       
   109         ldi         r24, LED7_F
       
   110 
       
   111 _count_loop:
       
   112         ; display
       
   113         mov         r16, r24
       
   114         mov         r16, r24
       
   115         rcall       LED7_Show
       
   116 
       
   117         ; exit if zero
       
   118         tst         r24
       
   119         breq        _count_end
       
   120 
       
   121         ; count down
       
   122         dec         r24
       
   123 
       
   124         ; wait...
       
   125         rcall       Sleep_ADC
       
   126 
       
   127         ; next
       
   128         rjmp        _count_loop
       
   129 
       
   130 _count_end:
       
   131         ; done
       
   132         ret
       
   133 
       
   134 ;; Count up from 00 -> 255
       
   135 ; Returns once done
       
   136 Main_CountUp:
       
   137         ; init from 0
       
   138         ldi         r24, 0
       
   139 
       
   140 _countup_loop:
       
   141         ; display
       
   142         mov         r16, r24
       
   143         rcall       LED7_ShowDec
       
   144 
       
   145         ; wait...
       
   146         rcall       Sleep_ADC
       
   147 
       
   148         ; exit if zero
       
   149         cpi         r24, 255
       
   150         breq        _countup_end
       
   151 
       
   152         ; count up
       
   153         inc         r24
       
   154 
       
   155         ; continue
       
   156         rjmp        _countup_loop
       
   157 
       
   158 _countup_end:
       
   159         ; done
       
   160         ret
       
   161 
       
   162 ;; Blink between dot and empty
       
   163 Main_Blink:
       
   164 _blink_loop:
       
   165         ; dot
       
   166         ldi         r16, LED7_DOT
       
   167         ldi         r17, LED7_EMPTY
       
   168         rcall       LED7_Show
       
   169 
       
   170         ; wait...
       
   171         rcall       Sleep_ADC
       
   172         
       
   173         ; empty
       
   174         ldi         r16, LED7_EMPTY
       
   175         ldi         r17, LED7_DOT
       
   176         rcall       LED7_Show
       
   177         
       
   178         ; wait... 
       
   179         rcall       Sleep_ADC
       
   180         
       
   181         ; loop
       
   182         rjmp        _blink_loop
       
   183  
       
   184 ;; Chase segments
       
   185 Main_Spin:
       
   186 _spin_init:
       
   187         ; init from top
       
   188         ldi         r24, 0b00000001 
       
   189 
       
   190 _spin_next:
       
   191         ; display
       
   192         mov         r16, r24
       
   193         mov         r17, r24
       
   194         com         r17
       
   195         andi        r17, 0b00111111
       
   196         rcall       LED7_ShowRaw
       
   197 
       
   198         ; delay
       
   199         rcall       Sleep_ADC
       
   200         
       
   201         ; next segment
       
   202         lsl         r24
       
   203         
       
   204         ; go back to A if we hit G
       
   205         sbrc        r24, 6
       
   206         rjmp        _spin_init
       
   207 
       
   208         rjmp        _spin_next
       
   209 
       
   210 Main:
       
   211 init:
       
   212     ; Stack
       
   213         ldi         r16, high(RAMEND)
       
   214         ldi         r17, low(RAMEND)
       
   215         out         SPH, r16
       
   216         out         SPL, r17
       
   217 
       
   218     ; Enable interrupts
       
   219         sei
       
   220 
       
   221     ; ADC (slowest to start up)
       
   222         rcall       ADC_Init
       
   223     
       
   224     ; Timer
       
   225         rcall       Timer_Init
       
   226 
       
   227     ; SPI
       
   228         rcall       SPI_Init
       
   229     
       
   230     ; LCD (requires interrupts, blocks)
       
   231         rcall       LED7_Init    
       
   232     
       
   233     ; DEBUG
       
   234         sbi         DDRD, PORTD7
       
   235         cbi         PORTD, PORTD7
       
   236 
       
   237     ; Run
       
   238         rcall       Main_Countup
       
   239         ; rcall       Main_Countdown
       
   240         ; rcall       Main_ShowValue
       
   241         ; rcall       Main_Spin
       
   242         rcall       Main_Blink
       
   243 
       
   244 end:
       
   245         rjmp        end
       
   246