;This program counts up from 0 to 99 and then rolls over to zero. ;The numbers are displayed on the seven segments. ;Connect the 7-segment module to the upper pins of JA and JB. ;The code is in AVR assembly. Use AVR Studio IDE to make the hex file. ;For more information see www.microdigitaled.com and www.digilentinc.com .INCLUDE "M64DEF.INC" ;ATmega64 is used by Cerebot2 board ;initialize SP (Stack Pointer) LDI R20, HIGH(RAMEND) OUT SPH, R20 LDI R20, LOW(RAMEND) OUT SPL, R20 ;JA (PORTA) and JB(PORTC) as outputs LDI R20, 0xFF OUT DDRA, R20 OUT DDRC, R20 L1: LDI R22, 0 ;R22 = 0 (The left digit) L2: LDI R21, 0 ;R21 = 0 (The right digit) L3: LDI R18,50 ;scan the 7-segments 50 times and then count up L4: ;================================================================= ;Right 7-segment ;================================================================= LDI ZH, HIGH(SEVEN_SEG_LOOKUP<<1) LDI ZL, LOW(SEVEN_SEG_LOOKUP<<1) ;Z points to SEVEN_SEG_LOOKUP ADD ZL, R21 LPM R20,Z ;R20 = SEVEN_SEG_LOOKUP[R21] ;put the high nibble of R20 on JB (PORTC) MOV R16, R20 ;R16 = R20 ANDI R16, 0x70 SWAP R16 OUT PORTC, R16 ;put the low nibble of R20 on JA (PORTA) MOV R16, R20 ;R16 = R20 ANDI R16, 0x0F ;R16 = R16 & 0x0F OUT PORTA, R16 ;PORTA = R16 CALL DELAY_10ms ;wait for a while ;R20 = 0x3F (to display '0' on 7-segment we should put 0x07 on the port) LDI R20, 0x3F ;================================================================= ;Left 7-segment ;================================================================= LDI ZH, HIGH(SEVEN_SEG_LOOKUP<<1) LDI ZL, LOW(SEVEN_SEG_LOOKUP<<1) ;Z points to SEVEN_SEG_LOOKUP ADD ZL, R22 LPM R20,Z ;R20 = SEVEN_SEG_LOOKUP[R22] ;put the high nibble of R20 on JB (PORTC) MOV R16, R20 ;R16 = R20 ANDI R16, 0x70 ORI R16, 0x80 ;enable the catode for the Left 7-segment SWAP R16 OUT PORTC, R16 ;put the low nibble of R20 on JA (PORTA) MOV R16, R20 ANDI R16, 0x0F OUT PORTA, R16 CALL DELAY_10ms ;wait for a while DEC R18 BRNE L4 INC R21 ;increase the right digit CPI R21,10 BRNE L3 ;If the right digit is less than 10 then go to L3 LDI R21,0 ;otherwise, load R21 with 0 and INC R22 ;increase the left digit. CPI R22,10 ;If the left digit is less than 10 BRNE L2 ;then go to L2. RJMP L1 ;otherwise, goto L1 and start from beginning ;======================================================================= ;The Delay subroutine creates a delay which is 10 ms for XTAL = 8MHz. ;======================================================================= DELAY_10ms: PUSH R21 PUSH R22 LDI R21, 64 ;R21 = 64 DL1:LDI R22, 250 ;R22 = 250 DL2:NOP NOP DEC R22 ;R22 = R22 - 1 BRNE DL2 ;if R22 is not equal to zero then go to DL2 DEC R21 ;R21 = R21 - 1 BRNE DL1 ;if R21 is not equal to zero then go to DL1 POP R22 POP R21 RET .ORG $100 ;To display 0 on 7-segment we should put 0x3F on it, to display 1 we should put 0x06, and so on. ;The lookup array contains the number that we should put on 7-segment, when we want to display each number. ; '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' SEVEN_SEG_LOOKUP: .DB 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F