CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Help needed PIC16 interrupts

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Mana



Joined: 05 Feb 2019
Posts: 5

View user's profile Send private message

Help needed PIC16 interrupts
PostPosted: Tue Feb 05, 2019 4:35 pm     Reply with quote

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? Very Happy
temtronic



Joined: 01 Jul 2010
Posts: 9106
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 05, 2019 7:28 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 06, 2019 2:09 am     Reply with quote

Hi, I'm using a PIC16F819.
temtronic



Joined: 01 Jul 2010
Posts: 9106
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Feb 06, 2019 9:59 am     Reply with quote

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: 288

View user's profile Send private message

PostPosted: Wed Feb 06, 2019 11:06 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Wed Feb 06, 2019 11:48 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group