CC5x, delay example, PIC16F690, Help!

Thread Starter

gargoor

Joined Jan 5, 2009
6
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:
void delay_ms( uns16 millisec) // 
// Delays a multiple of 1 milliseconds at 4 MHz
// using the TMR0 timer
{ OPTION = 2; // prescaler divide TMR0 rate by 8
    TMR0 = 2;  // deduct 2*8 fixed instruction cycles delay
    char next = 0;


    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  
            ;
    } while ( -- millisec != 0);
}


/*--------------------------------------------------
    subroutine: main
    parameters: none
    returns:    nothing
    task:    Main program function Light red leds then delay then light yello
---------------------------------------------------*/
void main(void)
{
    OPTION = 0;
    Init();
    while(1)
    {
    //Init();
        D0();
        Delay_ms(1000); 
        D1();
        Delay_ms(1000);
//it is too fast you got to change the time delay or add more functions to see disco light
        D2();
        Delay_ms(1000);
    }
}
Here are my questions :
1.
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?

2.
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 ?
}

3.
while(1)
{
//Init();
D0();
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?
D1();
Delay_ms(1000);

4. How much is standard F0sc in PIC16f690, is it 4MHz ?
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,415
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).

1.
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?
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.

2.
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 ?
}
"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.

3.
while(1)
{
//Init();
D0();
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?
D1();
Delay_ms(1000);
1000 is the delay beging requested. 1000 mS is one whole second.
Inside the routine the "millisec" variable is initialized to 1000.


4. How much is standard F0sc in PIC16f690, is it 4MHz ?
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.
 
Top