Interrupt on 8051V6

General discussion on mikroBasic PRO for 8051.
Post Reply
Author
Message
gadelhas
Posts: 77
Joined: 09 Apr 2010 01:09

Interrupt on 8051V6

#1 Post by gadelhas » 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.
Thanks and Regards
Gadelhas

gadelhas
Posts: 77
Joined: 09 Apr 2010 01:09

Re: Interrupt on 8051V6

#2 Post by gadelhas » 14 Apr 2010 13:19

Hi, can anyone help me??

Regards
Thanks and Regards
Gadelhas

octal
Posts: 534
Joined: 09 Feb 2005 14:15
Location: France
Contact:

Re: Interrupt on 8051V6

#3 Post by octal » 14 Apr 2010 13:48

download the Easy8051 board examples from here
http://www.mikroe.com/zip/easy8051_v6/e ... s_v100.zip
and check the Seven Segment Display example !!!
http://www.pocketmt.com

foravr
Posts: 130
Joined: 11 May 2009 19:34

Re: Interrupt on 8051V6

#4 Post by foravr » 14 Apr 2010 19:18

Hi,
your problem doesn't need an interrupt. You are on the right way. Experiment with timing values and different loops.
If you want to read a button your loop time should not exceed 100 ms otherwise your code doesn't recognize if button is pressed.
foravr

octal
Posts: 534
Joined: 09 Feb 2005 14:15
Location: France
Contact:

Re: Interrupt on 8051V6

#5 Post by octal » 14 Apr 2010 20:07

foravr wrote:Hi,
your problem doesn't need an interrupt. You are on the right way. Experiment with timing values and different loops.
If you want to read a button your loop time should not exceed 100 ms otherwise your code doesn't recognize if button is pressed.
foravr
What if he needs to do actions uppon button press (like incrementing time, or setting some other thing)?
Sorry but using Hardware Timers (interrupts) if available is the best solution for this kind of problems !!!
http://www.pocketmt.com

foravr
Posts: 130
Joined: 11 May 2009 19:34

Re: Interrupt on 8051V6

#6 Post by foravr » 14 Apr 2010 20:55

Hi,
What if he needs to do actions uppon button press (like incrementing time, or setting some other thing)?
Sorry but using Hardware Timers (interrupts) if available is the best solution for this kind of problems !!!
[/quote]
The described problem doesn't need interupts.
foravr

gadelhas
Posts: 77
Joined: 09 Apr 2010 01:09

Re: Interrupt on 8051V6

#7 Post by gadelhas » 18 Apr 2010 22:13

Hi;

I did it like this and it work.

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
for i = 1 to 10
if (Button(1, 0) <> 0) then
oldstate = 1
end if

if (oldstate and Button(1, 1)) then
flag = not flag
oldstate = 0
end if
delay_ms(100)
next i

if flag = 1 then
if num=9 then
Display_Digito(num)
num=0
else
Display_Digito(num)
inc(num)
end if
else
if num=0 then
Display_Digito(num)
num=9
else
Display_Digito(num)
dec(num)
end if
end if
wend
end.


However, can anybody teach me hoe to use Interrupts? For exeample, on a Pin of a Port??

Regards
Thanks and Regards
Gadelhas

gadelhas
Posts: 77
Joined: 09 Apr 2010 01:09

Re: Interrupt on 8051V6

#8 Post by gadelhas » 21 Apr 2010 19:31

Hi;

Can anyone help me how to do an interrupt in the 8051, on a port pin? Please!
Thanks and Regards
Gadelhas

foravr
Posts: 130
Joined: 11 May 2009 19:34

Re: Interrupt on 8051V6

#9 Post by foravr » 21 Apr 2010 21:35

Hi,

Code: Select all

'first:
'write a subroutine that does the work orged with the adress of the desired interrupt
'intadress is the interrupt vector see datasheet  (0x003 for ext. int0)
'ilevel is the prioity value of the interrupt (0 the lowest / 3 the highest)

sub procedure yourinterrupt  org intadress ilevel 1 (0...3)
      perhaps you have to switch off IE bit 7 read 8253 datasheet
      your instructions put here
      ....
      ....
      perhaps you have to switch on IE bit 7 read 8253 datasheet
end sub

main:
'second:
'switch on the corresponding bit (corresponding with your interrupt see datasheet) of IE register:
IE.0 = 1 ' enable interrupt on external input int0
'switch on global interrupt :
IE.7 = 1 ' enable global interrupts
'for other interrupts (timers, uart, spi) you have to set the corresponding bits in the special registers like
'TCON register for the timers
that's all.
perhaps you want to choose if int0 is level triggered or triggered by falling edge then set the IT0 bit in
TCON register
and once again: read the data sheet

foravr

gadelhas
Posts: 77
Joined: 09 Apr 2010 01:09

Re: Interrupt on 8051V6

#10 Post by gadelhas » 23 Apr 2010 00:19

Hi;

After following the instructions of foravr, a made this simple program just to teste the interrupt on INT0, and it worked fine.

program Trabalho2A
' ****************************************************************************
'
' Trabalho 2A - Interrupt
' ======================
'
' Author: Hugo Oliveira
' Date: Abril, 2010
' ****************************************************************************
dim flag as bit

' SUB-ROTINAS E FUNÇÕES
' ====================================================================
sub procedure Interrupt() org 0x003
EA_bit = 0 'desactiva interrupts
EX0_bit = 0 'desactiva int0
flag = not flag
delay_ms(500)
EA_bit = 1 'activa interrupts
EX0_bit = 1 'activa int0
end sub

' INICIO PROGRAMA
' ====================================================================
main:
EA_bit = 1 'activa interrupts
EX0_bit = 1 'activa int0
flag=0
P0 = 0xFF ' Leds PORT0 Desligados

while TRUE

if flag=1 then
p0.7=1
p0.0=1
delay_ms(2000)
p0.0=0
delay_ms(2000)
else
p0.0=1
p0.7=1
delay_ms(2000)
p0.7=0
delay_ms(2000)
end if

wend
end.

Thanks foravr for everything.

Regards
Thanks and Regards
Gadelhas

Post Reply

Return to “mikroBasic PRO for 8051 General”