Skip to content

Instantly share code, notes, and snippets.

@malustewart
Created March 31, 2019 21:52
Show Gist options
  • Save malustewart/ade6e55a2b6d5c76067ef9e913e63cfb to your computer and use it in GitHub Desktop.
Save malustewart/ade6e55a2b6d5c76067ef9e913e63cfb to your computer and use it in GitHub Desktop.
7 segment countdown. Atmega328p assembly.
;
; asmtest.asm
;
; Created: 1/20/2019 5:31:12 PM
; Author : mlste
;
; Cuenta de 0 a 9 en un 7 segmentos
; a,b,c,d,e,f,g = PD2,PD3,PD4,PD5,PD6,PD7,PB0,PB1
; = PIN2 to PIN9 (Arduino uno)
.equ CERO = 0x3f
.equ UNO = 0x06
.equ DOS = 0x5b
.equ TRES = 0x4f
.equ CUATRO = 0x66
.equ CINCO = 0x6d
.equ SEIS = 0x7d
.equ SIETE = 0x07
.equ OCHO = 0x7f
.equ NUEVE = 0x6f
.equ MAX_NUM = 9 ; limite del contador (entre 1 y 9)
;::::INICIALIZACION::::::::::
;Puertos D y B son de output
ldi R16,0xFF
out DDRD,R16
out DDRB,R16
;Stack al final de la memoria
ldi r16, LOW(RAMEND)
out SPL, r16
ldi r16, HIGH(RAMEND)
out SPH, r16
;:::::CUENTA REGRESIVA::::::::
start_counter:
ldi r16, MAX_NUM
next_num:
call dis_num
call delay_250
call delay_250
call delay_250
call delay_250
dec r16
brmi start_counter ; Si es menor que cero, reiniciar
jmp next_num ; Si no es menor que cero, lo muestro
; :::::::DIS_NUM:::::::
; Recibe un numero en r16 y lo muestra en el 7 segmentos
dis_num:
push r16
push r17
push r18
; Cargo el codigo correspondiente al numero en r17
; todo: hacer con macro parametrizada quizas?
tst r16
brne not_0_dis_num
ldi r17, CERO
jmp got_num_dis_num
not_0_dis_num:
dec r16
brne not_1_dis_num
ldi r17, UNO
jmp got_num_dis_num
not_1_dis_num:
dec r16
brne not_2_dis_num
ldi r17, DOS
jmp got_num_dis_num
not_2_dis_num:
dec r16
brne not_3_dis_num
ldi r17, TRES
jmp got_num_dis_num
not_3_dis_num:
dec r16
brne not_4_dis_num
ldi r17, CUATRO
jmp got_num_dis_num
not_4_dis_num:
dec r16
brne not_5_dis_num
ldi r17, CINCO
jmp got_num_dis_num
not_5_dis_num:
dec r16
brne not_6_dis_num
ldi r17, SEIS
jmp got_num_dis_num
not_6_dis_num:
dec r16
brne not_7_dis_num
ldi r17, SIETE
jmp got_num_dis_num
not_7_dis_num:
dec r16
brne not_8_dis_num
ldi r17, OCHO
jmp got_num_dis_num
not_8_dis_num:
dec r16
brne end_dis_num
ldi r17, NUEVE
;Una vez que tengo el numero, lo acomodo para mandar a los dos puertos
;Voy rotando y paso el bit mas significativo de r17 al menos
;significativo de r18 a traves del carry.
got_num_dis_num:
clr r18
lsl r17
rol r18
lsl r17
rol r18
;Mando a los pines
out PORTD,r17
out PORTB,r18
end_dis_num:
pop r18
pop r17
pop r16
ret
; Delay about 250ms.
; One millisecond is about 16000 cycles at 16MHz.
; The basic loop takes about 5 cycles, so we need about 3000 loops.
delay_250:
push r31
push r30
push r20
ldi r20, 250
one_ms_delay_250:
ldi r31, 3000>>8 ; high(3000)
ldi r30, 3000&255 ; low(3000)
five_cycles_delay_250:
sbiw r30, 1 ; aca r30 y r31 funcionan como un solo registro de 16 bits
brne five_cycles_delay_250
subi r20, 1
brne one_ms_delay_250
pop r20
pop r31
pop r30
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment