View previous topic :: View next topic |
Author |
Message |
Ranfo
Joined: 08 Sep 2008 Posts: 38
|
Ext Interrupt only triggered on port B pin 0 |
Posted: Mon Sep 08, 2008 11:50 am |
|
|
I need an external interrupt that is triggered by a change on ANY pins of port b. Here is the code that works with pin 0 and no others. I have tried everything I can think of. Am I missing something simple?
Code: |
#include <16F887.h> // PIC Device Header
#device ADC=8 PASS_STRINGS=IN_RAM // HIGH_INTS=TRUE
#include <stdlib.h>
// --- FUSES ------------------------------------------------
#FUSES LP // Use low power osc
#FUSES INTRC_IO // Internal RC Osc
#FUSES NOWDT // No Watch Dog Timer
#fuses NODEBUG // No Debug mode for ICD
#fuses PROTECT // Code protected from reading
#fuses NOWRT // Program memory not write protected
#fuses NOLVP // No low voltage prgming
#fuses NOCPD // No EE protection
#fuses NODEBUG // No Debug mode for ICD
#fuses NOBROWNOUT // No brownout reset
#use delay( clock=31000 )
// === Global Variables ===
int8 gSleepcounter = 0;
int1 gIsSleeping = 0;
// === TIMER 2 ===================================
#INT_TIMER2 // Service Timer 2
void isr_timer2(void) {
gSleepcounter++;
if (gSleepcounter > 10) {
gIsSleeping = 1;
}
}
// === external interrupt =============
#INT_EXT
void ext_isr() {
gIsSleeping = 0;
gSleepcounter = 0;
}
// === Initialize =============================
void initialize() {
setup_oscillator(OSC_31KHZ);
setup_timer_2(T2_DIV_BY_16, 255, 1); // Timer 2 (DIV_BY, Period, Post)
enable_interrupts(INT_TIMER2); // Enable Timer 2 Interrupt
ext_int_edge(H_TO_L); // init interrupt triggering for button press
enable_interrupts(INT_EXT); // turn on interrupts1
enable_interrupts(GLOBAL); // Enable Interrupts Global
port_b_pullups(0x1F); // Pullups on Keyboard Input Port
}
// === main ============================
void main()
{
initialize();
output_high(PIN_A0);
output_e(0x00);
output_c(0x00);
while(TRUE)
{
if (!gIsSleeping)
{ // Awake
output_high(PIN_A0); // turn on radio
}
else
{ // Asleep
output_low(PIN_A0); // turn off radio
}
output_d(input_b()); // Send Data
delay_ms(100);
}
}
|
|
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: Ext Interrupt only triggered on port B pin 0 |
Posted: Mon Sep 08, 2008 12:51 pm |
|
|
What you want is "Interrupt on Change". That is a different interrupt from INT_EXT. Also interrupt on change causes an interrupt when an enabled Port B pin changes in either direction. The external interrupt on RB0 only works in one direction. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
Ranfo
Joined: 08 Sep 2008 Posts: 38
|
|
Posted: Mon Sep 08, 2008 12:58 pm |
|
|
Thank you. Can you show me where I can find information on this? |
|
|
meereck
Joined: 09 Nov 2006 Posts: 173
|
|
Posted: Mon Sep 08, 2008 1:05 pm |
|
|
simply:
Code: |
#int_RB
void RB_isr()
{
int8 current;
static int8 last=0;
current=input_b();
if ((!bit_test(current,PIN_B0))&&(bit_test(last,PIN_B0))) {
// we have falling edge on B0
}
last=current;
}
|
and enable global and RB interrupts:
Code: |
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
|
|
|
|
Ranfo
Joined: 08 Sep 2008 Posts: 38
|
|
Posted: Mon Sep 08, 2008 1:17 pm |
|
|
If I replace INT_EXT with INT_RB then the interrupt never triggers even on pin 0. I am still missing something. |
|
|
Ranfo
Joined: 08 Sep 2008 Posts: 38
|
|
Posted: Mon Sep 08, 2008 1:28 pm |
|
|
OK, seems to be working now. Thank you all. Posting changes so others can see:
Code: |
// === external interrupt =============
// #INT_EXT
#INT_RB
void ext_isr() {
int8 n;
n = input_b();
gIsSleeping = 0;
gSleepcounter = 0;
}
// === Initialize =====================
void initialize() {
setup_oscillator(OSC_31KHZ);
setup_timer_2(T2_DIV_BY_16, 255, 1); // Timer 2 (DIV_BY, Period, Post)
enable_interrupts(INT_TIMER2); // Enable Timer 2 Interrupt
enable_interrupts(INT_RB0);
enable_interrupts(INT_RB1);
enable_interrupts(INT_RB2);
enable_interrupts(INT_RB3);
enable_interrupts(INT_RB4);
enable_interrupts(GLOBAL);
port_b_pullups(0x1F); // Pullups on Keyuboard Input Port
}
|
|
|
|
|