|
|
View previous topic :: View next topic |
Author |
Message |
Guard
Joined: 20 Jan 2005 Posts: 43
|
Delay bug |
Posted: Mon Feb 21, 2005 11:26 am |
|
|
PCWH 2.218
Hi,
on pin C4 I read 47KHz signal!
The steps are about 128x2uS (Toff) and 128x2uS (ton)!
#include <16f676.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,INTRC, NOPROTECT,MCLR, NOCPD,PUT,BROWNOUT
#use fast_IO(c)
#byte portc=0x07
#byte porta=0x05
static int secondi;
static int A;
static long timers;
static short int start;
static long t;
static long Ton;
static long Toff;
void MsgErr(int Index );
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);
setup_timer_1(T1_DISABLED);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
disaBLE_INTERRUPTS(GLOBAL);
set_tris_c(15);
set_tris_a(12);
portc=0;
porta=0;
output_Low(PIN_C4);
delay_ms(500);
MsgErr(12);
}
void MsgErr(int Index )
{
do {
output_low(PIN_C4);
do {
t++;
delay_us(2);
} while (t<128);
output_High(PIN_C4);
do {
t++;
delay_us(2);
} while (t<128);
} while (1);
} |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Feb 21, 2005 11:49 am |
|
|
Your clock is running at 4MHz, resulting in 1 million instruction clock cycles per second --> 1 instruction clock cycle == 1 us.
Most instructions take 1 clock cycle, but some like 'goto' need 2 cycles.
Look at the list file below and you see the delay(2us) was coded with 2 NOP intructions, which is exactly 2 microseconds. The compiler is correct...
Code: | .................... do {
.................... t++;
0020: INCF 2A,F
0021: BTFSC 03.2
0022: INCF 2B,F
.................... delay_us(2);
0023: NOP
0024: NOP
.................... } while (t<128);
0025: MOVF 2B,F
0026: BTFSS 03.2
0027: GOTO 02C
0028: MOVF 2A,W
0029: SUBLW 7F
002A: BTFSC 03.0
002B: GOTO 020
....................
....................
.................... output_High(PIN_C4);
002C: BSF 07.4 |
Your loop takes about 13 instruction cycles total, or 13 microseconds. That's where your timing error is comming from. |
|
|
|
|
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
|