0
\$\begingroup\$

To get min 5ms square wave. I chosen prescalar as 1:32 (The internal clock given 4Mhz), what will be the TMR0 register value? Is it 0x64 or 0x63 !!

\$\endgroup\$
8
  • \$\begingroup\$ This will depend on your oscillator frequency. More details needed. \$\endgroup\$
    – David
    Nov 19, 2014 at 4:42
  • \$\begingroup\$ Added main frequency.(4 Mhz) \$\endgroup\$
    – Jithin
    Nov 19, 2014 at 4:44
  • \$\begingroup\$ I have edited my answer to your question with a 4Mhz frequency. \$\endgroup\$ Nov 19, 2014 at 6:42
  • \$\begingroup\$ Check your answer again. \$\endgroup\$
    – Jithin
    Nov 19, 2014 at 7:48
  • \$\begingroup\$ At 4MHz, you can get either 496 or 504us, not 500 exactly. \$\endgroup\$
    – Matt Young
    Nov 19, 2014 at 14:00

2 Answers 2

1
\$\begingroup\$

By my calcs, with a 4MHz system clock, and a 1MHz timer clock, and a prescaler of 32, that's a tick frequency of 31250 Hz. Your tick time is 32 microseconds. To count to 0.005 seconds, your need 156.25 clock ticks. Round up to 0x9D.

So, you can set the timer to 0, wait until it reaches 0x9D, toggle your bit, then reset to 0.

Another way to do this would be to preset the timer to 0xFF-0x9D, and enable the interrupt. The interrupt, triggered when the timer rolls over from 0xFF to 0x00 would toggle your bit, and reset the timer to 0xFF-0x9D. (or more accurately 0xFF-0x9D+the current value of the timer, which hasn't stopped during the interrupt)

\$\endgroup\$
2
  • \$\begingroup\$ Ok my case is 5ms so 0x63 or 0x64 better. \$\endgroup\$
    – Jithin
    Nov 19, 2014 at 15:57
  • \$\begingroup\$ 0x9D by my calc. \$\endgroup\$ Nov 19, 2014 at 17:15
0
\$\begingroup\$

What is your main clock frequency? If it is 20MHz, to get .5ms with a prescale of 32, it needs to count to 313. I would suggest a prescale of 64 and look for the TMR0 value to exceed 0x9C, so you do not need to count overflow.

This is how to do the calculation, it is simple:

Timer Clock (Hz) = Clock Frequency / Prescale

Timer Tick (Seconds) = 1 / Timer Clock

Timer Ticks Needed (Integer) = .5ms / Timer Tick

--- EDIT after frequency provided

Look for the register value to exceed 0x3F if you want .504ms, and 0x3E if you want .496ms. That is as accurate as it gets with your configuration.

\$\endgroup\$
8
  • \$\begingroup\$ Frequency is 4Mhz \$\endgroup\$
    – Jithin
    Nov 19, 2014 at 4:40
  • \$\begingroup\$ I'm not getting .504ms when I multiply 0x3F with 1:32 prescalar. \$\endgroup\$
    – Jithin
    Nov 19, 2014 at 7:48
  • \$\begingroup\$ You are not supposed to multiply it with the prescalar, you are supposed to multiply it by the time of one timer tick. Look at the equations I posted. \$\endgroup\$ Nov 19, 2014 at 15:03
  • 1
    \$\begingroup\$ Note that the TIMER clock is the SYSCLOCK/4 -- i.e., if your system clock is 4 MHz, your timer clock is 1 MHz \$\endgroup\$ Nov 19, 2014 at 15:35
  • 2
    \$\begingroup\$ @VijithaStephy Helping you would be easier if you told us what you DID see along with what you DIDN'T \$\endgroup\$ Nov 19, 2014 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.