Calculating the time a button is held
Posted: Mon Oct 26, 2015 4:03 pm
Hello again,
I am creating a program that will blink an LED light at the speed of which you hold and release a button. I therefore want a timer to count the time the button is held so that I can pass it as an argument through the __delay_ms function. I am using XC16 compiler and have found the time.h in the 16bit language tools library (XC.h).
Here are my encountered problems:
- The functions that I have found only count in seconds. I would like it to be able to count milliseconds
.
- Both clock_t and time_t don't appear to give me the actual time in seconds. I've read that the time function returns clock cycles by default for the 16 bit compiler and that you must do something within the pic30 libs to customize this. However, I haven't been able to find out how to do this. I found this information on this link: http://ww1.microchip.com/downloads/en/d ... 51456b.pdf (Page 366) (It is explained how to rebuild on this page, but it is for the C30 and also I didn't find the file)
Here is the code I am writing. The code runs, but the Elapsed time is random numbers. Sometimes I get numbers in the tens of thousands, sometimes in the hundreds and sometimes negative numbers. I am new to MPLAB and somewhat new to programming. I am sure there are unoptimized lines of code. Any advice is very welcome.
Thank you very much
Sebastien
I am creating a program that will blink an LED light at the speed of which you hold and release a button. I therefore want a timer to count the time the button is held so that I can pass it as an argument through the __delay_ms function. I am using XC16 compiler and have found the time.h in the 16bit language tools library (XC.h).
Here are my encountered problems:
- The functions that I have found only count in seconds. I would like it to be able to count milliseconds
.
- Both clock_t and time_t don't appear to give me the actual time in seconds. I've read that the time function returns clock cycles by default for the 16 bit compiler and that you must do something within the pic30 libs to customize this. However, I haven't been able to find out how to do this. I found this information on this link: http://ww1.microchip.com/downloads/en/d ... 51456b.pdf (Page 366) (It is explained how to rebuild on this page, but it is for the C30 and also I didn't find the file)
Here is the code I am writing. The code runs, but the Elapsed time is random numbers. Sometimes I get numbers in the tens of thousands, sometimes in the hundreds and sometimes negative numbers. I am new to MPLAB and somewhat new to programming. I am sure there are unoptimized lines of code. Any advice is very welcome.
Thank you very much
Sebastien
- Code: Select all
#include "xc.h"
#include <time.h>
#include "LCD.h"
#include <stdio.h> /* for printf */
#define FOSC (7370000ULL) // Defining Oscillator Speed
#define FCY (FOSC/2) // Defining Clock
#include <libpic30.h> // Includes delay function
int main(void) {
LCD_Initialize();
TRISDbits.TRISD6 = 1; /* made this pin as input */
TRISAbits.TRISA7 = 0;
TRISAbits.TRISA6 = 0;
LATAbits.LATA7 = 1;
time_t start, stop;
int Elapsed;
int count;
char Time[33];
stop = NULL;
while(1){
if(!PORTDbits.RD6){
start = NULL;
time(&start);
LATAbits.LATA6 = 1;
stop = NULL;
while(!PORTDbits.RD6);
}
while(PORTDbits.RD6){
if(!LATAbits.LATA6){ // For Startup
count++; // Only here so that there is something in the if statement.
}
else{
if(stop == NULL)
time(&stop);
Elapsed = difftime(stop, start);
LATAbits.LATA7 = 0;
LATAbits.LATA6 = 0;
sprintf(Time, "%d", Elapsed);
LCD_PutString(Time, 10);
__delay_ms(1000);
LATAbits.LATA7 = 1;
__delay_ms(1000)
LATAbits.LATA6 = 1;
LCD_ClearScreen();
}
}
}
return 0;
}