time problem with pic 16f877a

Thread Starter

Mostafa Abu Elkhair

Joined Apr 2, 2016
4
i'm quite beginner in using pic .. when i was making ir remote and receiver circuit i noticed some problems with time .. the pic doesn't do things at accurate timing it's sometimes late and some times works well ... the delay time sometimes reach 1 or 2 seconds and i think this is too much
my cod has delays in 10us and i'm using 8mhz oscillator
is that can be an oscillator normal problem or i did some thing wrong in programming ?
is there some thing that i can do for better accuracy ?
 

GopherT

Joined Nov 23, 2012
8,012
i'm quite beginner in using pic .. when i was making ir remote and receiver circuit i noticed some problems with time .. the pic doesn't do things at accurate timing it's sometimes late and some times works well ... the delay time sometimes reach 1 or 2 seconds and i think this is too much
my cod has delays in 10us and i'm using 8mhz oscillator
is that can be an oscillator normal problem or i did some thing wrong in programming ?
is there some thing that i can do for better accuracy ?
How are you setting timing? Are you using some delay() library command, an interrupt, or counting clock cycles, other? Are you using the internal RC oscillator or do you have an external crystal? If using internal oscillator, are you using the factory trimmed correction?
 

ErnieM

Joined Apr 24, 2011
8,053
The oscillator may run a bit fast or slow but it will run at a constant rate, baring changes in supply voltage or extreme temperature changes. So look at your code if it is running in a "jumpy" mannor.
 

Thread Starter

Mostafa Abu Elkhair

Joined Apr 2, 2016
4
thank you for replies
i'm using external crystal and this is the code

int x=0;
int m=0;
int y=0;
void main() {
trisd.f4=1;
trisd.f1=0;
trisd.f3=0;
portd.f3=0;
portd.f1=1;
while(1) {
if (portd.f4==0){
y=0;
for(m=0;m<1000;m++) {
delay_us(10);
if (portd.f4==1){
y=1;

}
}
if (y==0) {
if (x==0) {
portd.f3=1 ;
x = 1 ;
delay_ms(1000) ;
}
else {
portd.f3=0 ;
delay_ms(1000) ;
x=0 ;
}
}

}
}
}
i had some problem with the receiver so i put this ugly for() function
 
Top