groberts
Joined: 08 Feb 2009 Posts: 4
|
16F886 Port B interrupts |
Posted: Thu Apr 22, 2010 10:54 pm |
|
|
I have been scanning the forum for something which matched my question but haven't seen anything. So here goes...
I have a momentary switch connected to the B0 pin. Using #INT_EXT I have had no problems servicing this interrupt. But the CCS manual is not clear about how to handle using interrupts on the other portB pins. I want to use B1 as another interrupt for a ack from another Pic. I understand that the int should be both edges and will manage that. But with V4.086, the management of interrupts on bits B1-3 is not clear. Must I clump them all within #INT_RB? How will that affect the #INT_EXT since this is using B0?
Based on the variables in the 16F88.h file I thought I would be able to use something specific like #INT_B1 or #INT_RB1. Those result in compile errors.
Is the only way to work this, through a single interrupt for the port B which has to determine which pin caused the interrupt?
Looking forward to your thoughts/help
GregR |
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 23, 2010 2:54 pm |
|
|
Here's a demo program that is a modification of the CCS Ex_pbutt.c
program. It demonstrates how to do PortB Interrupt-on-Change
interrupts and External interrupts in the same program.
It has Port B Interrupt-on-change interrupts on Pins B1 and B2,
and External interrupts on Pin B0. When you press a push-button
on any of those pins, you will get output on the Terminal window of
your PC, showing which button was pressed.
Code: |
#include <16F886.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
short int dbutton0, dbutton1, dbutton2;
//-------------------------
// This function reads Port B without changing the TRIS.
int8 input_state_b(void)
{
#byte PortB = getenv("SFR:PORTB")
return(PortB);
}
//-------------------------
#int_ext
void int_ext_isr(void)
{
dbutton0 = TRUE;
delay_ms(1); // Debounce delay for push-button switch
}
//-------------------------
#define HIGHTOLOW FALSE
#define LOWTOHIGH TRUE
int8 current, last;
#int_rb
void int_rb_isr(void)
{
current = input_state_b();
#if LOWTOHIGH
if ((!bit_test(last,1))&&(bit_test(current,1)))
dbutton1 = TRUE;
if ((!bit_test(last,2))&&(bit_test(current,2)))
dbutton2 = TRUE;
#elif HIGHTOLOW
if((!bit_test(current,1))&&(bit_test(last,1)))
dbutton1 = TRUE;
if((!bit_test(current,2))&&(bit_test(last,2)))
dbutton2 = TRUE;
#endif
last = current;
delay_ms(1);
}
//------------------------------
void init()
{
dbutton0=0;
dbutton1=0;
dbutton2=0;
current = input_state_b();
last = current;
}
//=======================================
void main()
{
init();
port_b_pullups(0b00000111); // Enable pullups on PortB bits 0,1,2
delay_us(10); // Allow time for pull-ups to initialize
enable_interrupts(INT_EXT_L2H);
enable_interrupts(INT_RB1);
enable_interrupts(INT_RB2);
input_state_b(); // Clear mismatch condition
clear_interrupt(INT_RB);
clear_interrupt(INT_EXT);
enable_interrupts(GLOBAL);
while(1)
{
if(dbutton0)
{
putc('0');
dbutton0 = FALSE;
}
if(dbutton1)
{
putc('1');
dbutton1 = FALSE;
}
if(dbutton2)
{
putc('2');
dbutton2 = FALSE;
}
}
}
|
|
|