CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Question about Timer

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
marten363



Joined: 11 Nov 2018
Posts: 1

View user's profile Send private message

Question about Timer
PostPosted: Sun Nov 11, 2018 1:07 am     Reply with quote

Hi

I'm new to CCS and i'm learning about timer lastly.
I have a question. How is the lower loop working at this code ?
Like the timer should wait for 2 ms to increase by 1, but how is this working in code ?
Code:

#include "uyg_05_01.h"
#define led pin_d0
char i;
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
while(1)
  {
   output_toggle(led);
   i=0;
   bekle:                                     //???
   set_timer0(6);
   while(get_timer0()>5);               //  ??????????
   i++;
   if(i<50)
      goto bekle;
  }

}
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Sun Nov 11, 2018 2:54 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group