View previous topic :: View next topic |
Author |
Message |
dhpl
Joined: 21 Nov 2017 Posts: 2
|
Trouble with EXT INT |
Posted: Tue Nov 21, 2017 11:50 am |
|
|
I'm new to PIC.
I need 4 ext int for a project. A quick example as is follow, however I'm unable to make the ext int to work. The input pins are working as normal input (under main) however none of them is responding from interrupt.
Any help will be appreciated.
Code: |
#include <16f877a.h>
#fuses HS,NOWDT,NOPROTECT,BROWNOUT,NOLVP,NOCPD
#use delay(clock = 20MHz)
//#use fast_io(b)
// LCD 2x16
#define LCD_DB7 PIN_D7
#define LCD_DB6 PIN_D6
#define LCD_DB5 PIN_D5
#define LCD_DB4 PIN_D4
#define LCD_RS PIN_D3
#define LCD_RW PIN_D2
#define LCD_E PIN_D1
#include <flex_LCD.c>
#define BUTTON_A PIN_B4
#define BUTTON_B PIN_B5
#define BUTTON_C PIN_B6
#define BUTTON_D PIN_B7
int state = 0;
// ############################################################################
#INT_EXT
void btn_int() {
if(input(BUTTON_A)) {
state = 1;
}
if(input(BUTTON_B)) {
state = 2;
}
if(input(BUTTON_C)) {
state = 3;
}
if(input(BUTTON_D)) {
state = 4;
}
}
// ############################################################################
void main ()
{
set_tris_b(0x1E);
lcd_init(); // LCD start
delay_ms(20);
lcd_putc("\f"); // Clear the LCD
delay_ms(100);
ext_int_edge( L_TO_H ); // Sets up EXT
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
while(TRUE){
lcd_gotoxy(1,2);
switch(state) {
case 1:
printf(lcd_putc, "Action 1");
// ...
break;
case 2:
printf(lcd_putc, "Action 2");
// ...
break;
case 3:
printf(lcd_putc, "Action 3");
// ...
break;
case 4:
printf(lcd_putc, "Action 4");
// ...
break;
}
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19554
|
|
Posted: Tue Nov 21, 2017 12:21 pm |
|
|
INT_EXT, only works on pin B0.
The interrupt you are trying to use is the interrupt on change. INT_RB.
Interrupt on change triggers always on both edges (L_TO_H only applies to INT_EXT).
There is another problem in the approach.
If (for instance) B7 goes high, 'state' will equal 4. However another then triggers without this releasing, the interrupt will be called again, and it'll still return '4', ignoring the others that have triggered.... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9249 Location: Greensville,Ontario
|
|
Posted: Tue Nov 21, 2017 2:29 pm |
|
|
I seem to recall that CCS has an 'how-to' in the FAQ section of the manual...
not on the eng pc so I can't look it up... |
|
|
dhpl
Joined: 21 Nov 2017 Posts: 2
|
|
Posted: Tue Nov 21, 2017 8:53 pm |
|
|
Many thanks for your reply.
I changed the interrupt to int_rb. Not much success.
Read a lot of how to, but couldn't figure out the rb approach. Shading more lights may help. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9249 Location: Greensville,Ontario
|
|
Posted: Tue Nov 21, 2017 9:09 pm |
|
|
CCS has an example 'ex_pbutt.c' that may work for you...
there is another , I thought was in the FAQ section of the manual.. I KNOW it's 'somewhere' as I used it on an 877 years ago... it showed how in the ISR you read the port, decide which pin was active (RB4,RB5,RB6 or RB7). |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 21, 2017 9:11 pm |
|
|
Look at this CCS examples file:
It's in the Examples folder on your PC for CCS. |
|
|
|