View previous topic :: View next topic |
Author |
Message |
ganesh
Joined: 29 Feb 2008 Posts: 1
|
require a code snippet for use of the interrupt on change. |
Posted: Mon Mar 03, 2008 10:19 am |
|
|
Hi ppl,
i am a newbie for this forum.. i am a b.tech student... i need the code snippet for using the interrupt on change feature in pic 16f877a... please do help me... |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
|
Posted: Mon Mar 03, 2008 10:55 am |
|
|
Here is an extract from a shared interrupt handler. The handler is used for interrupt driven software UART. A Timer 3 is used for receive bit timing. IOC is used for start bit detection (INT0 to INT3 would have been beter for this task but were not available on the target hardware platform).
IOC is enabled when the serial interface is waiting for the next character. When an IOC condition occurs, the interrupt source is check to determine it is an IOC event (required because this is a shared handler) and the Rx pin is sampled to see if it is a valid start bit condition. It then enables the serial receive bit timer and disabled the IOC interrrupt.
Code: |
// perform start bit detection
// Is this an interrupt on change interrupt?
if ((INTCON & 0x09) == 0x09)
{
// Handle Interrupt on change
// Only gets here if waiting for a start bit
if (!bit_test(SWU_Rx))
{
// start bit condition
// load the bit timer and initialise state
// bit_clear(T3CON, TMR3ON); // turn off the rx bit timer (already off)
IH_data_mask = ~(SWU_TmrCnt + (SWU_TmrCnt >> 1) - 87);
TMR3H = IH_mask;
TMR3L = IH_data;
bit_set(T3CON, TMR3ON); // turn on the bit timer
clear_interrupt(INT_TIMER3);
enable_interrupts(INT_TIMER3);
SWU_RxBCnt = SWU_BITCOUNT;
disable_interrupts(INT_RB);
}
clear_interrupt(INT_RB);
} |
_________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Mon Mar 03, 2008 12:04 pm |
|
|
Some PIC's specifically here a 12F683 have interrupt on change but allow a specific pin to be chosen via masking of the other pins.
Here the pins are RA and a specific pin RA3 is chosen.
There are difference between PIC'swith respect to interrupt on change.
Some use portB (#int_RB) some allow specific pins to be selected.
Never forget to look up the specifications of you PIC before coding.
Now interrupts occur while your code in main is executing. The CCS compilers ISR handler behind the scenes saves your main code status then gives your isr code a chance to execute and then again behind the scenes restores all that is needed to get your main code back to executing as if the interrupt had not occurred. Communication from the your isr has to be done via global variables no calling parameters can be passed to an isr . It must always have the form
my_isr(); and always be preceded with #INT_XX where XX is the specific interrupt you want your isr to react to.
Code: |
///////////////////////////////////////ISR /////////////////////////////////
#int_RA
onchange_isr()
{
/// your code goes here
}
main()
{
enable_interrupts(INT_RA3);
enable_interrupts(GLOBAL);
/// more of your code goes here
while(1);
} |
|
|
|
|