#INT_timer1
void isr()
{
if(TMR1IF == 1)
{
timer_flag = 0;
TMR1IF = 0;
TMR1IE = 1;
TMR1H = 0xFE; // Values calculated for 100 millisecond delay with 4MHz crystal
TMR1L = 0x79;
}
}
while(1)
{
if(timer_flag==0)
{
if(input(Button)==0)
{
i = i + 0.1;
lcd_cmd(0x80);
printf(lcd_data,"%6.3f",i);
}
timer_flag = 1;
}
}
#INT_timer1
void isr()
{
if(TMR1IF == 1)
{
if(input(button)==0) // if button pressed
{
change_uom (0); // function to change the units in the display
}
TMR1IF = 0;
TMR1IE = 1;
TMR1H = 0xD8; // 10ms
TMR1L = 0xEF;
}
}
if(TMR1IF == 1)
TMR1IF = 0;
TMR1IE = 1;
TMR1H = 0xD8; // 10ms
TMR1L = 0xEF;
I have changed the isr like this.
in the isr if you see button is pressed, you can set a flag - "button_pressed = TRUE". and clear it when it is processed in your main loop.
you can also use this idea to provide software debounce. i think it will work better then
#INT_timer1
void isr()
{
if(TMR1IF == 1)
{
if(input(Button)==0)
{
timer_flag = 0;
}
TMR1IF = 0;
TMR1IE = 1;
TMR1H = 0xD8; // 10ms
TMR1L = 0xEF;
}
}
while(1)
{
if(timer_flag==0)
{
i = i + 0.1;
lcd_cmd(0x80);
printf(lcd_data,"%6.3f",i);
timer_flag = 1;
}
}
I couldn't change the pin because i have designed the pcb and ordered more pcb. All interrupt pins in uC are used for some other purpose.
Even i can't change the crystal frequency, because my device is battery operated.
my problem is not about debounce. read my 1st post Please.
Due to polling the button, and the program is pretty big (say about 1500 lines) , button press is not detected sometimes. for eg, say about im pressing the button with interval of 500ms, sometimes units has been changed in the display corresponding to button press and sometimes it doesn't changes. Thats the real problem. Read from the 1st post. Also, I am aware of debounce.
So i have wrote a small program, when i press the button, increments a counter in the display. It works perfectly and never misses a button press whatever the interval may be. So i have concluded that the program is big, will it cause the problem while polling the button press. Please help if someone knows alternate and perfect solution.
#include <18F2520.h>
#device adc=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES XT //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(clock=4000000)
#define button PIN_C0
#define LED PIN_B0
int1 var_1 = false;
#int_TIMER1
void TIMER1_isr(void)
{
var_1 = true;
set_timer1(65535);
}
void main()
{
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
setup_timer_3(T3_DISABLED | T3_DIV_BY_1);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
set_timer1(65535);
while(TRUE)
{
if(var_1)
{
output_toggle(LED);
var_1 = false;
}
}
}
thats what hemnath has already saidI think that the delay your program is presenting now, is due to the large number of lines that it have.
If what the program have to do once you press the button is not very extensive, you can put that code directly within the interruption routine and it will be faster.
supposedly what has to be done is to display on LCD. this process can take tens of milliseconds to complete. however OP may have only used this only for testing purpose, and actual processing is less, then of course this makes sense.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?