Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Sun Nov 11, 2018 2:54 am |
|
|
Seriously, don't use goto....
There are a few (very few) places where goto should/needs to be used in
C programming. It carries a risk, if a jump is done over a longer distance
than shown, of leaving the stack unbalanced. Consider goto as something
that should only ever be used when you fully understand it's implications.
So:
Code: |
#include "uyg_05_01.h"
#define led pin_d0
char i;
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
while(TRUE)
{
output_toggle(led);
i=0;
do
{
set_timer0(6);
while(get_timer0()>5)
; //
i++;
} while (i<50);
}
}
|
Now you made it hard for us to give a full answer by not telling us your
processor or clock rate. However some parts are explainable:
Timer0 is a hardware timer. Being fed in this case by your CPU clock/(4*8). So if your CPU clock was 1MHz, it'd advance by one count
every 1000000/(4*8) seconds.
Why 4*8?.
The answer to this is first that the peripherals are generally fed by the
instruction clock, which is Fosc/4. So this is the '4'. Then the '8', is the
divider set in your setup_timer_0 instruction RTCC_DIV_8.
So if your statement that the timer 'should wait for 2 ms to increase by 1', was true, your CPU would have to be running off a 16000Hz clock. Not very likely...
Now, the timer actually depends on what chip you are using. However
assuming a fairly basic PIC16, the odds are that it counts 0 to 255, then wraps back to 0, and starts again.
The code sets it to '6', so it'll count 6, 7,8....255, 0, 1 etc..
The loop test will wait 'while' the value read from the timer is greater than
5, so given it starts at 6, while it is 6, 7, 8.....255, then when it goes to
0, the test is no longer true. So the loop will wait for 250 counts of the
timer. If it is this 250 counts being 2mSec that actually applies, then the CPU clock would be 4MHz, which is rather more likely. |
|