|
|
View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
Reading Timer problem |
Posted: Thu Jan 12, 2017 12:06 am |
|
|
Hi,
I'm trying to read time between pulse. I've connect a signal below to pin B6.
Code: |
IGNORE 1 0 0 0 0 1 0 1 0 1 0 0 0 1 0
_______ ________ ____ ____ ____ ____ ________ ____ ________ ____ ________ ____ ____ ____ ________ ____ __________ HIGH
I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I
I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I
I__I I____I I____I I____I I____I I____I I____I I____I I____I I____I I____I I____I I____I I____I I____I I____I LOW
350us 1750us 700us
|
I get a result :
" 46 -85 -84 65 15 -83 46 -79 47 67 47 -79 2 14 -49 -82 "
The given result is not stable and I would like to know why it give -value ?
Here is my code
Code: |
#include <18F4550.h>
//#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#fuses none
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
#include <usb_bootloader.h>
void main()
{
char c;
int8 i ,x;
int r2;
unsigned char d;
unsigned char key;
usb_init_cs();
while (TRUE)
{
usb_task();
if (usb_cdc_kbhit())
{
c=usb_cdc_getc();
if (c=='\n') {putc('\r'); putc('\n');}
if (c=='\r') {putc('\r'); putc('\n');}
while(true)
{
ART:
d = usb_cdc_getc();
if(d=='R') // push R for RECEIVER
{
while (key!=32) // push SPACE BAR to stop
{
if(usb_cdc_kbhit())
{key=usb_cdc_getc();}
int a1;
setup_timer_0(RTCC_DIV_2|RTCC_INTERNAL); // presacaler=2, Use INTERNAL clock
//then every tick approximately = 0.167 Usecond
while(1)
{
printf(usb_cdc_putc,"\r");
while (input(PIN_B6));// wait for B6 to go low
r2=1;
for(i=0; i<16; i++)
{
while (!input(PIN_B6)); // wait for B6 to go high
set_timer0(0); // init timer0 to 0
while (input(PIN_B6)); // wait for B6 to go low
a1=get_timer0();
printf(usb_cdc_putc,"%d ", a1 );
}
delay_ms(200);
}
} key=0; // to initialize 'key' not as SPACE BAR
}
} // For USB format
} // For USB format
} // For USB format
} // For USB format
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19524
|
|
Posted: Thu Jan 12, 2017 1:51 am |
|
|
Several things, and one 'big one'....
First, the chip may be doing other things. USB interrupts will occur at intervals, and the chip has to go off and service these. If one occurs while it is waiting to 'see' an edge, the code to handle this will then be delayed while the interrupt is serviced....
There is also a 'quantisation error'. Several instruction times round the loop, and the edge could be occurring at any point in this loop (however see below...)
This is why the CCP is such a powerful tool. It can record the value in the associated timer, when an edge occurs, and save it.
The big one though is you are not waiting for the edges.....
You arrive round the loop. Signal is sitting high. The first test then jumps straight through, though you may well be half way through the pulse.....
Also remember the timer is a 16bit value. You are only looking at the low byte.
Your timer is clocking off 6MHz.
350uSec, will be about 2100 cycles.
You are also confusing things by displaying the number as 'signed'. %u is the correct format to display an unsigned value.
Code: |
#define WAIT_RISING() while(input(PIN_B6)); while(!input(PIN_B6))
//wait for signal to have been low and go high.
//parts of code only
int16 a1; //not int8
//Then wait for a rising edge
WAIT_RISING();
set_timer0(0);
while(input(PIN_B6))
;
a1=get_timer(0);
printf(usb_cdc_putc,"%lu ", a1 );
|
|
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Thu Jan 12, 2017 10:48 pm |
|
|
Dear Ttelmah,
I've tried your code, but it seem still cannot get a stable result. It give a positive number but with letter "l" behind. Why? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19524
|
|
Posted: Fri Jan 13, 2017 2:35 am |
|
|
I suspect it doesn't like not having a field width. I've always specified one, so never seen this. So "%4Lu".
Unless it interprets the 'l' it won't correctly pull the high byte. Also if you have case significance enabled, then the 'l' must be upper case.
You will still get variation (because of the other reasons mentioned), but hopefully a much smaller amount relative to the numbers involved. |
|
|
|
|
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
|