CC5x, delay example, PIC16F690, Help!

Discussion in 'Embedded Systems and Microcontrollers' started by gargoor, May 23, 2015.

  1. gargoor

    Thread Starter New Member

    Jan 5, 2009
    Ladies & Gents,

    I have been going through timer0 and its studeis. Everytime I get close to understand the delay function given in CC5x compiler, I end up confused. I want to delay 1s the leds I got connected to chip. But I can't figure how to calculate exactly a delay of one second. I understand the following formula
    Next * Presc * 1/Fosc/4 = delay
    and the functionality of Timer0 as I read from some tutorials and the datasheet

    But I'm confused .. Please see the code and the questions below the following code

    Code (Text):
    1. void delay_ms( uns16 millisec) //
    2. // Delays a multiple of 1 milliseconds at 4 MHz
    3. // using the TMR0 timer
    4. { OPTION = 2; // prescaler divide TMR0 rate by 8
    5.     TMR0 = 2;  // deduct 2*8 fixed instruction cycles delay
    6.     char next = 0;
    9.     do  {
    10.         next += 125;   // Does it start from 125 to 256 or 0 to 125 ?
    11.         clrwdt();  // needed only if watchdog is enabled
    12.         while (TMR0 != next)   // 125 * 8 * (1/4MHz/4)= 1 ms  
    13.             ;
    14.     } while ( -- millisec != 0);
    15. }
    18. /*--------------------------------------------------
    19.     subroutine: main
    20.     parameters: none
    21.     returns:    nothing
    22.     task:    Main program function Light red leds then delay then light yello
    23. ---------------------------------------------------*/
    24. void main(void)
    25. {
    26.     OPTION = 0;
    27.     Init();
    28.     while(1)
    29.     {
    30.     //Init();
    31.         D0();
    32.         Delay_ms(1000);
    33.         D1();
    34.         Delay_ms(1000);
    35. //it is too fast you got to change the time delay or add more functions to see disco light
    36.         D2();
    37.         Delay_ms(1000);
    38.     }
    39. }
    Here are my questions :
    void delay_ms( uns16 millisec) // what is uns16 ? why not just int or signed ? I guess it isn't signed because PIC16f690 doesn't use 2's complement?

    do {
    next += 125; // Does it start from 125 to 256 or 0 to 125 ?
    clrwdt(); // needed only if watchdog is enabled
    while (TMR0 != next) // 125 * 8 * (1/4MHz/4)= 1 ms
    How much is TMR0? . Is 1ms the time it takes to count next till 256 (till overflow)?
    } while ( -- millisec != 0);
    Is --millisec equals 1000 and it reduces to 999, 998 ..... so on till 0 ? how much time it takes to do so ?

    Delay_ms(1000); // What is 1000 for ? It subtitues millisec variable . so then it reduces 1000 to 999 to 998 and so on ? or how?

    4. How much is standard F0sc in PIC16f690, is it 4MHz ?
    Last edited: May 23, 2015
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    I see two things in your questions: you need a better understanding of C, and you also need to know your target PIC device better.

    You would be well served to find some tutorials written for this compiler to follow. I know of none, and if you do not find any I suggest changing to a free the Microchip compiler where you can get such tutorials (along with a hardware board that allows you to run all the tutorials).

    Why would you need to guess? You chose the CC5X compiler, one I have never heard of before, yet I was able to absolutely find the definition for this symbol in a couple of minutes by simply Reading The Fine Manual (RTFM) for your compiler.

    You should have found it faster as that manual would have been in your download package.

    It means "millisec" is an unsigned 16 bit integer.

    And the PIC16f690 absolutely does indeed use 2's complement.

    In this case there is no such thing as a negative time delay so a negative number makes no sense so it is not allowed. "millisec" will hold the number of milliseconds the routine will wait until it returns.

    "next" is defined as an (unsigned) char with an initial value of zero. (CCS makes the char type unsigned by default (RTFM) )"+=" means add the value on the right to the variable on the left.

    "TMR0" is the Timer1 register in the PIC that holds the current timer time; again RTFM for the PIC you are using.

    The (very short) while loop waits on that line for 125 counts on Timer1 to occure. If Timer1 is set up corrently it takes 1 full milliseccond for the count to advance by 125 counts.

    Then the "millisec" variable is decremented and tested to see if the total delay has passed. If not, the loop repeats.

    1000 is the delay beging requested. 1000 mS is one whole second.
    Inside the routine the "millisec" variable is initialized to 1000.

    There are 8 different internal clock rates and an infinite number of external rates so it entirely depends on how the device is wired and/or configured.