time problem with pic 16f877a

Discussion in 'Embedded Systems and Microcontrollers' started by Mostafa Abu Elkhair, Apr 4, 2016.

  1. Mostafa Abu Elkhair

    Thread Starter New Member

    Apr 2, 2016
    4
    0
    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 ?
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    You did something wrong in programming.

    That's about the best we can do with the information given.
     
    Mostafa Abu Elkhair likes this.
  3. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,043
    3,806
    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?
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    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.
     
  5. odm4286

    Active Member

    Sep 20, 2009
    155
    5
    Can you post your code?
     
  6. Mostafa Abu Elkhair

    Thread Starter New Member

    Apr 2, 2016
    4
    0
    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
     
  7. Mostafa Abu Elkhair

    Thread Starter New Member

    Apr 2, 2016
    4
    0
    mr Gophert
    i'm not sure what is the factory trimmed correction
     
  8. Mostafa Abu Elkhair

    Thread Starter New Member

    Apr 2, 2016
    4
    0
    thank you all
    problem solved
    i made some changes in the code and winpic option and it works much better
     
Loading...