I have been trying several days to write a simple program to supply power to a compressor for a specified time then turn the power off.
The chip is a 16f88 although I had the same problem with a 12f683. I have a momentary push button switch between 0v and VSS. I have the gate of an NPN transistor hooked to pin A2. An led is hooked to A3 only to show operation. The transistor is hooked up between 0V and VSS.
When the switch is closed the PIC starts up pin A1 is turned on and the transistor completes the circuit from VSS to 0v. The PIC will stay on for the specified time and then turn off. This works fine.
I would like to have a second momentary switch hooked to pin B0 to use the B0 Ext Int to turn the PIC off at any time.
At any mention of an interrupt the program quits working and will stay on only as long as I hold the switch on.
I have tried "On Interrupt PortbChange call ***" and other On Interrupt instructions.
The Help file states that interrupts can be used by naming a sub Interrupt. They procedure is included in the enclosed code.
Here is the code. If I uncomment any commented line of the code except the OPTION_REG set. The program will not operate. Can someone please tell me how to use interrups in GCBasic.
#chip 16f88, 8
'set OPTION_REG.7 ON 'Port B Pullup enabled
'set INTCON.7 on 'Global Interrupt enable
'set INTCON.6 on 'Peripheral Interrupt enable
'set INTCON.4 on 'RB 0 External Interrupt enable
'set INTCON.1 off 'RB 0 External Interrupt Flag
#define keepon porta.2
#define led porta.3
dir keepon out
dir led out
set keepon on
set led on
main:
wait 10 s
dir keepon in
goto main
sub interrupt
dir keepon in
end sub
end
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The reason I was setting/clearing bit in INTCON was to avoid the ON INTERRUPT command which also gave me the same results. I named the SUB INTERRUPT because I understood Help to say this was the second way to use interrupts.
I think maybe what is happening is that I am getting into a permanent interrupt by not putting a loop in the code to wait until the switch gets unpressed.
I have decided to go back to the very basics of interrupts and learn them from the bottom up through tutorials etc on the web.
Thanks again for the help.
Steve
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here was some code I used, to convince myself that the ExtInt0 interrupt would wake chip from sleep, do something, then go back to sleep. Just changing the chip and led port pin should do it for the 16f88.
Kent
#chip 16f690,8
#config MCLRE=On, INTRC_OSC_NOCLKOUT
#define Led PortC.0
dir LED out
On Interrupt ExtInt0 call Wakeup
OnWakeup = False
Main:
If OnWakeup = True then
notcount = 5
Else
notcount = 2
end if
For count = 1 to notcount
SET LED ON
wait 50 10ms
SET LED OFF
wait 50 10ms
Next
asm sleep
goto Main
sub Wakeup
OnWakeup = Not OnWakeup
end sub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Steve, I built the circuit you described and your program worked fine - both the posted version, and when I replaced the various set INTCON commands with "on interrupt ExtInt0 call powerOff" as Frank suggested.
Make sure that there's a pull up on PORTB.0, NOT_RBPU needs to be set to 0 to enable the PORTB pullups. Without anything pulling up the pin, PORTB.0 is probably picking up noise from the surrounding environment and toggling on and off at random.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I ran the code both ways (on interrupt and setting bits in intcon). Both worked but the bit setting seemed to be a little more responsive.
I think my problem was, besides not turning the pull ups on, I had the turn off switch hooked to the +5v side. I changed the switch to the 0v side and it worked.
I have one question that is bothering me. This led turns off on the release of the off switch not the press of the switch. Pressing the off switch when the pic is off will casuse the led to light as long as the switch is pressed. Grounding any i/o pin on the pic will cause the led to light. Is this normal? Whats happening here?
Steve
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The off switch is working on the release of the switch because the External Interrupt defaults to the rising edge. You can change that by clearing the INTEDG bit in the OPTION register - then the interrupt should occur on the falling edge when you press the switch.
The led turning on when you press the off switch is probably down to the protection diodes on each I/O pin - one connected to VSS and one connected to VDD. The protection diode connected to VSS provides a path to ground through the switch ( or any other grounded I/O pin) so the chip will be powered up. For the RB0 pin the led should turn off as soon as the switch is released as that will be the rising edge to trigger the interrupt. For the other pins I'd expect to see the led stay on for 10 seconds after you remove the ground. If you move the bottom end of your off switch to your switched ground point where the PIC's VSS pin, the transistor and the top of the on switch meet it should stop doing that I think if I'm understanding the circuit right.
You may find with your led connected to RA3 that there is some current flowing through that by the same route - if you've got a multimeter you could check and see if there are voltage drops across it (and the current limiting resistor if you are using one?) when the power is off.
Frank
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have been trying several days to write a simple program to supply power to a compressor for a specified time then turn the power off.
The chip is a 16f88 although I had the same problem with a 12f683. I have a momentary push button switch between 0v and VSS. I have the gate of an NPN transistor hooked to pin A2. An led is hooked to A3 only to show operation. The transistor is hooked up between 0V and VSS.
When the switch is closed the PIC starts up pin A1 is turned on and the transistor completes the circuit from VSS to 0v. The PIC will stay on for the specified time and then turn off. This works fine.
I would like to have a second momentary switch hooked to pin B0 to use the B0 Ext Int to turn the PIC off at any time.
At any mention of an interrupt the program quits working and will stay on only as long as I hold the switch on.
I have tried "On Interrupt PortbChange call ***" and other On Interrupt instructions.
The Help file states that interrupts can be used by naming a sub Interrupt. They procedure is included in the enclosed code.
Here is the code. If I uncomment any commented line of the code except the OPTION_REG set. The program will not operate. Can someone please tell me how to use interrups in GCBasic.
#chip 16f88, 8
'set OPTION_REG.7 ON 'Port B Pullup enabled
'set INTCON.7 on 'Global Interrupt enable
'set INTCON.6 on 'Peripheral Interrupt enable
'set INTCON.4 on 'RB 0 External Interrupt enable
'set INTCON.1 off 'RB 0 External Interrupt Flag
#define keepon porta.2
#define led porta.3
dir keepon out
dir led out
set keepon on
set led on
main:
wait 10 s
dir keepon in
goto main
sub interrupt
dir keepon in
end sub
end
Hi,
To enable Port B pullups you have to clear the option register bit NOT_RBPU so set it off instead of on.
You may also need to change you're interrupt code as you don't need to worry about setting the INTCON bits if you use -
on interrupt PORTBChange call powerOff
and rename you're interrupt sub to powerOff. GCBasic will then handle all the interrupt enable bits and flags itself.
I don't entirely understand you're circuit description but maybe these changes will help.
Frank
Maybe I should add that the on interrupt line could be placed just above the main: label.
Sorry the line should be probably be -
on interrupt ExtInt0 call powerOff
PORTBChange might also work but may be more troublesome and need some additional code.
Frank
Thanks Frank.
The reason I was setting/clearing bit in INTCON was to avoid the ON INTERRUPT command which also gave me the same results. I named the SUB INTERRUPT because I understood Help to say this was the second way to use interrupts.
I think maybe what is happening is that I am getting into a permanent interrupt by not putting a loop in the code to wait until the switch gets unpressed.
I have decided to go back to the very basics of interrupts and learn them from the bottom up through tutorials etc on the web.
Thanks again for the help.
Steve
Here was some code I used, to convince myself that the ExtInt0 interrupt would wake chip from sleep, do something, then go back to sleep. Just changing the chip and led port pin should do it for the 16f88.
Kent
Steve, I built the circuit you described and your program worked fine - both the posted version, and when I replaced the various set INTCON commands with "on interrupt ExtInt0 call powerOff" as Frank suggested.
Make sure that there's a pull up on PORTB.0, NOT_RBPU needs to be set to 0 to enable the PORTB pullups. Without anything pulling up the pin, PORTB.0 is probably picking up noise from the surrounding environment and toggling on and off at random.
I appreciate everyone's interest and response.
I will try again.
I will post my success/failure.
Steve
SUCCESS!!!!!
I ran the code both ways (on interrupt and setting bits in intcon). Both worked but the bit setting seemed to be a little more responsive.
I think my problem was, besides not turning the pull ups on, I had the turn off switch hooked to the +5v side. I changed the switch to the 0v side and it worked.
I have one question that is bothering me. This led turns off on the release of the off switch not the press of the switch. Pressing the off switch when the pic is off will casuse the led to light as long as the switch is pressed. Grounding any i/o pin on the pic will cause the led to light. Is this normal? Whats happening here?
Steve
Hi Steve,
The off switch is working on the release of the switch because the External Interrupt defaults to the rising edge. You can change that by clearing the INTEDG bit in the OPTION register - then the interrupt should occur on the falling edge when you press the switch.
The led turning on when you press the off switch is probably down to the protection diodes on each I/O pin - one connected to VSS and one connected to VDD. The protection diode connected to VSS provides a path to ground through the switch ( or any other grounded I/O pin) so the chip will be powered up. For the RB0 pin the led should turn off as soon as the switch is released as that will be the rising edge to trigger the interrupt. For the other pins I'd expect to see the led stay on for 10 seconds after you remove the ground. If you move the bottom end of your off switch to your switched ground point where the PIC's VSS pin, the transistor and the top of the on switch meet it should stop doing that I think if I'm understanding the circuit right.
You may find with your led connected to RA3 that there is some current flowing through that by the same route - if you've got a multimeter you could check and see if there are voltage drops across it (and the current limiting resistor if you are using one?) when the power is off.
Frank