|
|
View previous topic :: View next topic |
Author |
Message |
daalex
Joined: 10 Jan 2014 Posts: 10
|
get time between two events,need a little help |
Posted: Sat May 17, 2014 4:16 pm |
|
|
Hello at all,
i try to get the time between two events like this:
button1 is pressed -> start counting
button2 is pressed -> stop counting and print out the time in miliseconds.
i use a PIC18F27J53
so
this is my attempt so far:
is this the right way?
because it should be as fast as possible and accurate as possible.
and how can i calculate the value of timer1 into milliseconds -> the timer things confuses me a little bit
Code: |
// uC-Einstellungen --------------------------------------------------------------------------
#include <18F27J53.h>
#device ADC=12
#fuses HSPLL,PLLEN,PLL5, NOCPUDIV, NOWDT,NOPROTECT
#fuses ADC12, RTCOSC_INT, DSWDTOSC_T1
#use delay(clock=48000000)
// Bootloader ------------------------------------------------
#build (reset=0x1000, interrupt=0x1008)
#org 0x0,0xfff {}
// -------------------------------------------------------------------------------------------
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
// USB ------------------------------------------------------------------------------------------
// USB-Funktions von CCS
#include <usb_cdc.h>
// schreibe Char in USB-Transmitt-Buffer
void usb_putc(char cc) // printf(usb_putc,...)
{
while(!usb_cdc_putready());
//delay_ms(1);
usb_cdc_putc(cc);
}
// USB -------------------------------------------------------------------------------------------
//Diverse Variablen
int1 newCharRec=0;
char newChar;
int16 zeit=0;
int16 start=0;
int16 stop=0;
int32 overflow=0;
//Zeichen empfangen?
#INT_RDA
void rda_handler () {
newChar=getc();
newCharRec=1;
}
//Timer interrupt
#INT_TIMER1
void timer()
{
overflow++;
}
// Init der Hardware
void HW_Init()
{
//timer interrupts usw.
//Timer konfigurieren...
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
}
// Ausgabe über USB -> Hyperterminal
void doTransfer()
{
if (newCharRec==1)
{
printf(usb_putc,"%c",newChar);
newCharRec=0;
}
}
//Hauptprogramm
void main() {
HW_Init(); // Init Hardware
usb_init(); // USB Init
while(!usb_cdc_connected());
while (TRUE) {
doTransfer();
if(input(PIN_B3)){
// timer auf 0 und starten....
set_timer1(0);
start=get_timer1();
}
if(input(PIN_A5)){
output_high(PIN_B6);
// timer wert abfragen und ausgeben
stop = get_timer1();
zeit =(stop-start);
//Ausgabe
printf(usb_putc,"Start: %lu \r\n "start);
printf(usb_putc,"Stop: %lu \r\n "stop);
printf(usb_putc,"Reaktionszeit: %lu \r\n "zeit);
output_low(PIN_B6);
output_low(PIN_B7);
}
}
}
|
would be very nice if someone can help me
thanks |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sat May 17, 2014 5:29 pm |
|
|
CCS supplies a 'stopwatch' type program in the 'examples' folder. It might be called 'ex_swtc.c'....not on PC with CCS ....
It should help you .....
Be 'simple' to implement the 2 button feature...
hth
jay |
|
|
daalex
Joined: 10 Jan 2014 Posts: 10
|
|
Posted: Sun May 18, 2014 5:20 am |
|
|
Hello,
thanks for help,
i used this example to get the code i have posted, but i think my code is not working correct.
i think i am a little bit confused about the timer thing,
i want the time in milliseconds..
so...:
pressing the first button -> set timer 0
pressing the second button:
get the timer value and add the overflow
time= (stop+(overflow*65535)/625); // 625= 20000000/4*8*1000
so im on the right way with this or totally wrong?
greetz
Alex |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sun May 18, 2014 7:55 am |
|
|
first problem is that the 'code' is based on a 20MHz clock...and you're using a 48MHz clock(needed for USB).
You'll have to modify the 'math' to get the correct values.
Also....you can 'search' this forum and probably locate several code examples of what you need as you're not the first to ask about this type of project.
hth
jay |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Mon May 19, 2014 1:03 am |
|
|
I think very appropriate would be to use tick_timer.
See the manual page 152 - #USE TIMER
Code: |
#USE TIMER(TIMER=1,TICK=1ms,BITS=16,NOISR)
#define butt1_pressed (!input(pin_A5))
#define butt2_pressed (!input(pin_B3))
unsigned int16 ticks; //The time between events in [ms]
short int flag = 0;
while(true){
while((butt1_pressed)&&(flag==1)){
ticks = get_ticks();
flag = 0; //to prevent re-entrance
//print whatever you print here . . .
output_low(PIN_B6);
output_low(PIN_B7);
}
if((butt2_pressed)){
//some debounce time here. . .
flag = 1;
set_ticks(0);
}
}
|
The advantage here is that you dont need to calculate the time. You shouldn't worry about the clock freq, either. It is done by the compiler with a minimum deviation you can see as warning. _________________ A person who never made a mistake never tried anything new. |
|
|
daalex
Joined: 10 Jan 2014 Posts: 10
|
|
Posted: Sat May 24, 2014 7:45 pm |
|
|
Thanks @ all for help,
i did some calculation mistakes, now with some improvement the code works really fine.
rikotech8, i have seen your post too late,
but for the next time i will Keep that in mind with the tick timer, i think that would be the better way to do that,
thanks.
greetz
Alex |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|