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
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 ?
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);
}
}
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: