View previous topic :: View next topic |
Author |
Message |
irmanao
Joined: 08 Apr 2015 Posts: 77
|
External interrupt and lcd problem-FIXED |
Posted: Sun Jun 04, 2017 11:30 am |
|
|
Hey guys, i'm trying to make an lcd output something only when a pin is high, which is toggled by an external interrupt. (compiler:5.008)
Code: | #include <18F4550.h>
#fuses INTRC_IO, NOWDT, PUT ,NOBROWNOUT, NOLVP, CPUDIV1
#use delay(clock=8000000)
#include <flex_lcd.h>
void init_all (void);
void main()
{
init_all();
output_high(PIN_E1);
set_tris_d(0x00);
delay_ms(500);
lcd_init();
while(1)
{
if (input(PIN_E1) ){
lcd_putc("\f Pub \n");
}
else {
lcd_putc("\f");
}
}
}
void init_all (void) {
clear_interrupt(INT_EXT1);
ext_int_edge(1, H_TO_L);
enable_interrupts(GLOBAL);
enable_interrupts(INT_EXT1);
}
#INT_EXT1
void ext_int1 (void){
output_toggle(PIN_E1);
delay_ms(10);
} |
As long as i don't include the 'if' the led toggles but otherwise it toggles momentarily (10ms from the delay probably) and then goes low. Any ideas?
Last edited by irmanao on Mon Jun 05, 2017 11:17 am; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Sun Jun 04, 2017 12:02 pm |
|
|
rule number 1
Never EVER put a delay_ms() inside an ISR.
repeat aloud 1,000 times.
This also holds true for print()s as well.......
Set flags, set registers, inc/dec variables but never EVER put delays in an ISR.
Jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jun 04, 2017 3:15 pm |
|
|
1. You're treating Pin E1 as both an input pin and and output pin. That's
not normally done. Why are you doing this ?
2. Describe any external circuits that you have on Pin E1.
3. You have selected External Interrupt 1, which is on Pin B1. What
external circuits do you have on Pin B1 ? Describe them in detail. |
|
|
irmanao
Joined: 08 Apr 2015 Posts: 77
|
|
Posted: Mon Jun 05, 2017 3:25 am |
|
|
An led is connected to E1 and a push button, with a pullup resistor, on Pin B1.
I just wanted to "toggle" the lcd with the push of the button.
thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Jun 05, 2017 9:18 am |
|
|
Problem is you are both reading and writing E1.
Now the instruction to use if you want to know if E1 is high, is 'input_state', not 'input'.
input_state reads the _state_ that the pin currently is at, without treating it as an input. Treating the pin as an input, means the LED will go off...
Input is the correct instruction for B1 (where you do want the pin to be an input), but not for E1.
Other comments:
You need to delay. An LCD takes about 1/4 second to actually display a value. So your loop needs to slow down. Add perhaps a 1/2 second delay into the loop.
The problem with a delay in the interrupt is that this will upset all the delays in the LCD code (since you will be getting a warning 'interrupts disabled to prevent re-entrancy' - a search here will find all about this).
Filter your input. Just add a few pF of capacitance across the switch to get rid of the nasty spikes as it switches.
Hopefully you have got a suitable current limiting resistor on the LED?. |
|
|
irmanao
Joined: 08 Apr 2015 Posts: 77
|
|
Posted: Mon Jun 05, 2017 11:16 am |
|
|
Yup that's it Ttelmah.. the input_state fixes everything. Ok thanks guys for the extra information also. |
|
|
|