Jump to content
Sign in to follow this  

Timer0 (tmr0) Problem

Recommended Posts

I'm having some issues with timer0 (using 16F677). I WAS trying to increase the frequency of timer0, because it wasn't Fosc/4 (internal frequency = 8MHz, no prescalar) as advertised. When I tested the output, each bit of timer0 seemed to have it's own agenda, operating at approximately these frequencies:


tmr0.0: 1.8kHz

tmr0.1: 1.5kHz

tmr0.2: 40kHz

tmr0.3: 20kHz

tmr0.4: 27kHz

tmr0.5: 1.8kHz

tmr0.6: 1.8kHz

tmr0.7: 1.8kHz


I set up the code below and simulated it in SourceBoost with the HEX output set to port C, but it didn't count in any conventional manner. Anyone have any idea what's going on? I tried switching out the MCU, but that didn't help.


#include <system.h>
#pragma CLOCK_FREQ 8000000

void main()
option_reg = 0b00001000;
intcon.7 = 0;
trisc = 0b00000000;
anselh = 0b00000000;
ansel = 0b00000011;
portc = 0b00000000;
tmr0 = 0;

portc.0 = tmr0.0;
portc.1 = tmr0.1;
portc.2 = tmr0.2;
portc.3 = tmr0.3;
portc.4 = tmr0.4;
portc.5 = tmr0.5;
portc.6 = tmr0.6;
portc.7 = tmr0.7;

Share this post

Link to post
Share on other sites

I believe your problem is because of the way Timer0 works. The tmr0 register increments after every instruction cycle (not the same as an instruction in C). Since it takes 7 instruction cycles to change each of the outputs of portc and about 2 cycles for the jump, portc will not look like a binary counter, which is the behavior I'm assuming that you are expecting. Instead, it increments by 7 for each step.


Using the debugger in the SourceBoost IDE, I added a watch to tmr0. tmr0 is 0 when it enters the while loop, and is 0x3A (58) by the time it reaches "portc.0 = tmr0.0" again.


If you are looking to create a counter, would suggest increasing portc via an interrupt when the timer overflows.


I hope this helps,

- Bill

Share this post

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...