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;
}
}
}
|
|
|