Interrupt on 8051V6
Posted: 09 Apr 2010 16:29
Hi everyone;
I have to do a project that consist in this, it starts counting down numbers like; 9, 8, 7, 6 ,5..... and if i press a button, inverts the counting, for exemple;
....0,9,8,7,6
Button pressed
7,8,9,0,1,2....
The Digits are presented in the LED Ports.
IIn my code, i can do this, however, i think its not the perfect way, because, of th timing that i need to show the digits, if i press the button in that time, the counting its not reversed. I think i need to do an interrupt. Can somebody, help me with this problem?
Here is my code;
program Trabalho2
' ****************************************************************************
'
' Trabalho 2 - Mostrar numeros e inverte após carregar botão
' ======================
'
' Author: Hugo Oliveira
' Date: Abril, 2010
' ****************************************************************************
dim num as byte
dim i as byte
dim Button_Pin as sbit at P2_5_bit
dim flag as bit
dim oldstate as bit
' SUB-ROTINAS E FUNÇÕES
' ====================================================================
sub procedure Display_Digito(dim num as byte)
select case num
case 0
P0 = 0x00
P1 = 0x7E
P2 = 0x7E
P3 = 0x00
case 1
P0 = 0xFF
P1 = 0xFF
P2 = 0xFF
P3 = 0x00
case 2
P0 = 0x06
P1 = 0x76
P2 = 0x76
P3 = 0x70
case 3
P0 = 0x76
P1 = 0x76
P2 = 0x76
P3 = 0x00
case 4
P0 = 0xF0
P1 = 0xF7
P2 = 0xF7
P3 = 0x00
case 5
P0 = 0x70
P1 = 0x76
P2 = 0x76
P3 = 0x06
case 6
P0 = 0x00
P1 = 0x6F
P2 = 0x6F
P3 = 0x0F
case 7
P0 = 0xFE
P1 = 0xFE
P2 = 0xFE
P3 = 0x00
case 8
P0 = 0x00
P1 = 0x76
P2 = 0x76
P3 = 0x00
case else
P0 = 0xF0
P1 = 0xF6
P2 = 0xF6
P3 = 0x00
end select
end sub
' INICIO PROGRAMA
' ====================================================================
main:
P0 = 0xFF ' Leds PORT0 Desligados
P1 = 0xFF ' Leds PORT1 Desligados
P2 = 0xFF ' Leds PORT2 Desligados
P3 = 0xFF ' Leds PORT3 Desligados
flag = 0
num = 9
P2.5=1
oldstate = 0
while TRUE
if (Button(1, 0) <> 0) then
oldstate = 1
end if
if (oldstate and Button(1, 1)) then
flag = not flag
oldstate = 0
end if
if flag = 1 then
if num=9 then
Display_Digito(num)
num=0
delay_ms(500)
else
Display_Digito(num)
inc(num)
delay_ms(500)
end if
else
if num=0 then
Display_Digito(num)
num=9
delay_ms(500)
else
Display_Digito(num)
dec(num)
delay_ms(500)
end if
end if
wend
end.
I have to do a project that consist in this, it starts counting down numbers like; 9, 8, 7, 6 ,5..... and if i press a button, inverts the counting, for exemple;
....0,9,8,7,6
Button pressed
7,8,9,0,1,2....
The Digits are presented in the LED Ports.
IIn my code, i can do this, however, i think its not the perfect way, because, of th timing that i need to show the digits, if i press the button in that time, the counting its not reversed. I think i need to do an interrupt. Can somebody, help me with this problem?
Here is my code;
program Trabalho2
' ****************************************************************************
'
' Trabalho 2 - Mostrar numeros e inverte após carregar botão
' ======================
'
' Author: Hugo Oliveira
' Date: Abril, 2010
' ****************************************************************************
dim num as byte
dim i as byte
dim Button_Pin as sbit at P2_5_bit
dim flag as bit
dim oldstate as bit
' SUB-ROTINAS E FUNÇÕES
' ====================================================================
sub procedure Display_Digito(dim num as byte)
select case num
case 0
P0 = 0x00
P1 = 0x7E
P2 = 0x7E
P3 = 0x00
case 1
P0 = 0xFF
P1 = 0xFF
P2 = 0xFF
P3 = 0x00
case 2
P0 = 0x06
P1 = 0x76
P2 = 0x76
P3 = 0x70
case 3
P0 = 0x76
P1 = 0x76
P2 = 0x76
P3 = 0x00
case 4
P0 = 0xF0
P1 = 0xF7
P2 = 0xF7
P3 = 0x00
case 5
P0 = 0x70
P1 = 0x76
P2 = 0x76
P3 = 0x06
case 6
P0 = 0x00
P1 = 0x6F
P2 = 0x6F
P3 = 0x0F
case 7
P0 = 0xFE
P1 = 0xFE
P2 = 0xFE
P3 = 0x00
case 8
P0 = 0x00
P1 = 0x76
P2 = 0x76
P3 = 0x00
case else
P0 = 0xF0
P1 = 0xF6
P2 = 0xF6
P3 = 0x00
end select
end sub
' INICIO PROGRAMA
' ====================================================================
main:
P0 = 0xFF ' Leds PORT0 Desligados
P1 = 0xFF ' Leds PORT1 Desligados
P2 = 0xFF ' Leds PORT2 Desligados
P3 = 0xFF ' Leds PORT3 Desligados
flag = 0
num = 9
P2.5=1
oldstate = 0
while TRUE
if (Button(1, 0) <> 0) then
oldstate = 1
end if
if (oldstate and Button(1, 1)) then
flag = not flag
oldstate = 0
end if
if flag = 1 then
if num=9 then
Display_Digito(num)
num=0
delay_ms(500)
else
Display_Digito(num)
inc(num)
delay_ms(500)
end if
else
if num=0 then
Display_Digito(num)
num=9
delay_ms(500)
else
Display_Digito(num)
dec(num)
delay_ms(500)
end if
end if
wend
end.