Measure total button press time using timer

Enhanced mid-range devices. PIC12F1xxx and PIC16F1xxx

Measure total button press time using timer

Postby abt » Thu Mar 26, 2020 2:19 pm

I am new to pic. I am using a 16f887 controller. I am using a hi-tech compiler. I want to measure button press time. I want to use that total button press time for delay and using that led should be on for that much of delay(button press time). I am not able to store that button press value count that value and make led on for that much of time. Button press timer is more or less than 5 seconds.

Code: Select all
#include <htc.h>
#define _XTAL_FREQ 8000000
#define SWITCH RD0
#define LED RD2

unsigned long int total_count = 0,count = 0,count1 = 0;
unsigned char flag =0,flag1 =0;
unsigned char i=0,k=0;

void Init_Timer1() // Timer Initialization at 0.6ms
{
    TMR1ON    = 0x01;
    TMR1IF   = 0;
    TMR1H    = 0xE0;
    TMR1L    = 0xC0;
    TMR1IE    = 1;
    INTCON   |= 0xC0;
}
void Extern()
{
    TRISB |= 0X01;
    INTCON |= 0XD0;
    INTEDG = 1; // Rising Edge Detect
}

void interrupt ISR(void)
{       

   if(TMR1IF) // Timer1 Interrupt flag
   {
       TMR1IF = 0;
       TMR1H  = 0xE0;  //0xFB
       TMR1L  = 0xC0;  //0x4F
       TMR1ON = 0x01;
       
       if(flag==1)
      {           
          count++;
          if(count==250)
          {  LED=~LED;           
           count = 0;
           flag=1;
         }
          }

   }
}

void main()
{   
    Init_Timer1();
    TRISD2 = 0;
    TRISD0 = 1;
    LED=0;
    while(1)
    {
       if((RD0 == 0))//&&(flag==0))
      {
             flag=1;     
        }         
     }   
}


abt
 
Posts: 1
Joined: Thu Mar 26, 2020 2:01 pm

Re: Measure total button press time using timer

Postby jtemples » Sat Mar 28, 2020 9:57 pm

One obvious problem is "flag" isn't volatile. What is the purpose of polling RD0 in a loop and setting a flag to be tested in the ISR? Why not just test RD0 in the ISR?
jtemples
Verified identity
 
Posts: 191
Joined: Sun May 25, 2014 2:23 am
Location: The 805
PIC experience: Professional 5+ years with MCHP products

Re: Measure total button press time using timer

Postby dan1138 » Fri Apr 24, 2020 10:02 pm

I answered this question on Stackoverflow here:
https://stackoverflow.com/questions/60751429/how-to-calculate-total-press-time-of-button-using-timer

I am not sure why he asked here 2 days later.
dan1138
 
Posts: 3
Joined: Sat May 30, 2015 6:59 pm
PIC experience: Professional 5+ years with MCHP products


Return to 14-Bit Core (enhanced)

Who is online

Users browsing this forum: Majestic-12 [Bot] and 1 guest

cron