View previous topic :: View next topic |
Author |
Message |
jemly
Joined: 13 May 2007 Posts: 34
|
18f452 External Interrupts Problem |
Posted: Mon May 21, 2007 7:00 am |
|
|
I've written what I thought was some simple code to get an external interrupt working on a pic18f452 but my interrupt routine never ever gets hit and I don't know why although I'm guessing I haven't configures this properly. Here's the code:
Code: |
#include "J:\My Documents\CCS Projects\GNSMotors02\main.h"
#define LED0 PIN_B1
boolean pressed = false;
void led_on()
{
output_high(LED0);
}
void led_off()
{
output_low(LED0);
}
#int_EXT
void EXT_isr(void)
{
led_on();
delay_ms(100); //debounce switch
pressed = true;
}
void Do_States()
{
if(pressed)
{
//SetMotorPositions();
}
}
void main()
{
//port_b_pullups(TRUE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
//timer 2 set up for maximum frequency of 4MHz clock
//4000000 / 1024 3906.25Hz
setup_timer_2(T2_DIV_BY_1,255,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
//set up pwm outputs
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
//initialise at 0% duty cycle
set_pwm1_duty (0);
set_pwm2_duty (0);
//set up interrupt on B0
clear_interrupt(INT_EXT);
enable_interrupts(INT_EXT);
ext_int_edge(0,H_TO_L);
enable_interrupts(GLOBAL);
set_tris_b(11111101);
port_b_pullups(00000001);
while(1)
{
led_off();
Do_States();
}
}
|
I've got a non latching switch connected to port b pin 0 and it is normally high, going low when pressed (although reading the voltage at pin b0 when the switch is not pressed only gives me 1.7V - is this right?!); It does go to 0V when pressed. The EXT_isr routine never gets hit so the interrupt doesn't seem to be working at all. Any ideas why this is? |
|
![](templates/subSilver/images/spacer.gif) |
drh
Joined: 12 Jul 2004 Posts: 192 Location: Hemet, California USA
|
|
Posted: Mon May 21, 2007 8:16 am |
|
|
Change this:
Quote: | set_tris_b(11111101);
port_b_pullups(00000001);
|
to this:
set_tris_b(0b11111101);
port_b_pullups(0b00000001); _________________ David |
|
![](templates/subSilver/images/spacer.gif) |
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Mon May 21, 2007 9:03 am |
|
|
Quote: |
I've got a non latching switch connected to port b pin 0 and it is normally high, going low when pressed (although reading the voltage at pin b0 when the switch is not pressed only gives me 1.7V - is this right?!);
|
No. The voltage should be close to +5V.
Something wrong:
1) in your hardware
2) in the pin direction configuration
3) in the pull up resistor
It is not a good practice to use delays inside an interrupt handler !!!
To 'see' the RB0 interrupt action, the following code should be enough.
Code: |
#int_ext
void EXT_ISR()
{
output_low(LED0);
pressed = true;
}
void main()
{
enable_interrupts(INT_EXT);
ext_int_edge(0,H_TO_L);
enable_interrupts(GLOBAL);
while(1)
{
.......
your stuff
.......
if(pressed)
{
delay_ms(200);
clear_interrupt(INT_EXT);
output_high(LED0);
delay_ms(200);
pressed = FALSE;
}
}
}
|
Humberto |
|
![](templates/subSilver/images/spacer.gif) |
jemly
Joined: 13 May 2007 Posts: 34
|
|
Posted: Wed May 23, 2007 4:25 am |
|
|
Thank you Humberto that's really useful! |
|
![](templates/subSilver/images/spacer.gif) |
|