View previous topic :: View next topic |
Author |
Message |
Mana
Joined: 05 Feb 2019 Posts: 5
|
Help needed PIC16 interrupts |
Posted: Tue Feb 05, 2019 4:35 pm |
|
|
Hi, I've just started using mplab with the ccs complier to program a pic but am very confused. I want to write a code that would interrupt ten times a second to check if one of the inputs on port B have changed and if so identify which one (e.g. if one of 3 switches is pressed). If there is a change in one of the inputs i then want to assign a value of 20 to a variable which would then decrement with every following interrupt. Can someone please help me with the code required? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Tue Feb 05, 2019 7:28 pm |
|
|
The code depends on which PIC you're using !
PORT B usually has interrupt capability (4 bits) and CCS supplies code in the FAQ/Q&A area of the manual.
Switches of course have 'bounce', so you need to eliminate that, either in hardware using filtering like a .68 cap across the points and/or software using delays or validation counters. |
|
|
Mana
Joined: 05 Feb 2019 Posts: 5
|
|
Posted: Wed Feb 06, 2019 2:09 am |
|
|
Hi, I'm using a PIC16F819. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Wed Feb 06, 2019 9:59 am |
|
|
I downloaded the datasheet, that PIC has 4 'Interrupt-on-change' pins, RB4-7.
There should be some posts/maybe code here that you could easily modify for your purpose.
Just use the 'search' button above the post. |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Wed Feb 06, 2019 11:06 am |
|
|
If you want to interrupt and sample the port at a set rate you can use one of the timers. It will depend on your oscillator frequency and how accurate you want your sample rate to be. Timer2 is usually a good choice for this type of application with its period register. If you want to sample 10 times a second and your timer will not set that slow you can interrupt 20 times a second and sample every other interrupt or 50 times a second and sample every 5th interrupt. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Wed Feb 06, 2019 11:48 am |
|
|
With interrupt on change on PIC16, at least in the one I used (16F1939), you need to specify the edge to interrupt on. For example:
Code: | enable_interrupts(INT_RB4_L2H); |
Check the datasheet and your header file for a full listing.
And then for the actual interrupt, you use only INT_RB like this, and not whatever you put in the enable interrupt:
Code: | #INT_RB
void rb_isr()
{
// isr stuff
} |
It's just something that caught me when I first tried to do interrupt on change.
In "isr stuff" you need to figure out which one of the port B interrupts was the one that triggered.
I personally think that using the interrupt on change will be better than polling a pin repeatedly. |
|
|
|