PDA

View Full Version : How can i use Interrupt in my program??



alabbadi
- 17th November 2007, 11:26
Im a new user of PBP and im using PIC16F877A microcontroller chip...
the program i need to have is that a pulses generated with specific frequency and it should generate this frequency pulses untill an interrupt occures from the user, but the problem is that i dont know how to use the interrupt code (i cudnt know the concept of ON INTERRUPT, DISABLE INTERRUPT, ENABLE INTERRUPT and how they are working through the code)

==> the interrupt should check if the user presses any other key to change the frequency, if not it should cont looping and generating the previous frequency

following is my code:
=====================================
Define LOADER_USED 1

' Define LCD connections
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1


' Define program variables
col Var Byte ' Keypad column
row Var Byte ' Keypad row
key Var Byte ' Key value
fre Var byte ' half pulse time
LED0 var PORTD.0 ' Alias PORTD.0 to LED

OPTION_REG = $7f ' Enable PORTB pullups

ADCON1 = 7 ' Make PORTA and PORTE digital
Low PORTE.2 ' LCD R/W low (write)

Pause 100 ' Wait for LCD to start

Lcdout $fe, 1, "Choose Your Frequency" ' Display sign on message

loop1: Gosub getkey ' Get a key from the keypad

loop3: if key = 1 then
Lcdout $fe, 1, "1 Hz"
fre = 500
gosub loop2
endif

loop4: if key = 2 then
Lcdout $fe, 1, "10 Hz"
fre = 50
gosub loop2
endif

loop5: if key = 3 then
Lcdout $fe, 1, "50 Hz"
fre = 80
gosub loop2
endif

loop6: if key = 4 then
Lcdout $fe, 1, "100 Hz"
fre = 40
gosub loop2
endif


loop2: High LED0 ' Turn on LED connected to PORTD.0
Pause fre ' Delay for "fre" seconds

lOW LED0
Pause fre ' Delay for "fre" seconds

goto loop2

Return ' Go back to loop and blink LED forever


' Subroutine to get a key from keypad
getkey:
Pause 500 ' Debounce

getkeyu:
' Wait for all keys up
PORTB = 0 ' All output pins low
TRISB = $f0 ' Bottom 4 pins out, top 4 pins in
If ((PORTB >> 4) != $f) Then getkeyu ' If any keys down, loop

Pause 50 ' Debounce

getkeyp:
' Wait for keypress
For col = 0 To 3 ' 4 columns in keypad
PORTB = 0 ' All output pins low
TRISB = (dcd col) ^ $ff ' Set one column pin to output
row = PORTB >> 4 ' Read row
If row != $f Then gotkey ' If any keydown, exit
Next col

Goto getkeyp ' No keys down, go look again

gotkey: ' Change row and column to key number 1 - 16

key = (col * 4) + (ncd (row ^ $f))
Return ' Subroutine over

End
===========================================

please.. i need help ASAP... thnx

RodSTAR
- 17th November 2007, 13:28
Use search at the top of the forum, it's full of discussions and cool examples.