Greetings fellow GCBasic users. I am new to the GC compiler and I am having trouble with the Interrupt on Change functionality of the PIC 16F684. I have written code that works but it only works on port RA2 (which happens to be the dedicated interrupt pin for this device.) When I try to configure the interrupt for a different port (RA4) my code does not work. I have searched the help files and the forum and I am completely baffled as to why it doesn't work. Here is the code that works:
;Chip Settings
#chip 16F684,10
#config MCLRE = OFF, OSC = INT
;Defines (Constants)
#define P1_LED PORTC.0
#define P2_LED PORTC.1
#define P3_LED PORTC.2
#define P4_LED PORTC.3
;Interrupt Handlers
On Interrupt PORTACHANGE Call CheckPortA
;Set RA2 port to interrupt
[b]Dir PORTA.2 In
Set IOCA2 On[/b]
;initialize port RC0 to RC3 as input pins These are P1 to P4 LEDs Setting as input puts them in HiZ or off
Dir P1_LED In
Dir P2_LED In
Dir P3_LED In
Dir P4_LED In
Main:
Do Forever
Wait 1 ms
Loop
Sub CheckPortA
IntOff
[b]If PORTA.2 = On Then[/b]
FOR FLASHES = 1 TO 3
GREEN_LED 1
GREEN_LED 2
Wait 30 ms
ALL_LED_OFF
NEXT
;Interrupt has occurred so set PORTA change interrupt flag to 0
RAIF = 0
End If
IntOn
End Sub
Sub GREEN_LED (In LED_NUM)
SELECT CASE LED_NUM
CASE 1
DIR P1_LED OUT
Set P1_LED OFF
CASE 2
DIR P2_LED OUT
Set P2_LED OFF
CASE 3
DIR P3_LED OUT
Set P3_LED OFF
CASE 4
DIR P4_LED OUT
Set P4_LED OFF
END SELECT
End Sub
Sub RED_LED (In LED_NUM)
SELECT CASE LED_NUM
CASE 1
DIR P1_LED OUT
Set P1_LED ON
CASE 2
DIR P2_LED OUT
Set P2_LED ON
CASE 3
DIR P3_LED OUT
Set P3_LED ON
CASE 4
DIR P4_LED OUT
Set P4_LED ON
End Select
End Sub
Sub ALL_LED_OFF
Set P1_LED OFF
DIR P1_LED IN
Set P2_LED OFF
DIR P2_LED IN
Set P3_LED OFF
DIR P3_LED IN
Set P4_LED OFF
DIR P4_LED IN
Wait 60 ms
End Sub
When I change the bolded lines above to reference port 4 it doesn't work. Any help would be appreciated.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I solved my own problem. Hopefully the following helps others when setting up the On Interrupt.
The first thing you need to do is set the Configuration Word for the PIC device to allow the pin to function as an IO pin and not another function. In the case of the 16F684 the RA4 pin can function as a Clock Output or IO. I had originally selected the internal Oscillator function for the chip which I assumed would allow all PORT A pins to funtion as IOs.
Silly me. I should really have read the datasheet. If just INTOSC is selected RA4 acts as a Clock Output. What I should have selected was the INTOSCIO. This allows RA4 to operate as an IO. As soon as I did this it worked perfectly.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Greetings fellow GCBasic users. I am new to the GC compiler and I am having trouble with the Interrupt on Change functionality of the PIC 16F684. I have written code that works but it only works on port RA2 (which happens to be the dedicated interrupt pin for this device.) When I try to configure the interrupt for a different port (RA4) my code does not work. I have searched the help files and the forum and I am completely baffled as to why it doesn't work. Here is the code that works:
When I change the bolded lines above to reference port 4 it doesn't work. Any help would be appreciated.
It looks like the bold font is a bit light. The lines above that I have changed to port 4 are:
;Set RA2 port to interrupt
Dir PORTA.2 In
Set IOCA2 On
AND
(within the CheckPortA subroutine)
If PORTA.2 = On Then
I solved my own problem. Hopefully the following helps others when setting up the On Interrupt.
The first thing you need to do is set the Configuration Word for the PIC device to allow the pin to function as an IO pin and not another function. In the case of the 16F684 the RA4 pin can function as a Clock Output or IO. I had originally selected the internal Oscillator function for the chip which I assumed would allow all PORT A pins to funtion as IOs.
Silly me. I should really have read the datasheet. If just INTOSC is selected RA4 acts as a Clock Output. What I should have selected was the INTOSCIO. This allows RA4 to operate as an IO. As soon as I did this it worked perfectly.