3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

PIC micro Timer Code

PIC micro Timer Code
Here's the minimal code to make timer 1 (a 16 bit timer) work using PICBasic.

General descriptions of PIC timer variables used to control the timer: (you should be able to use these no matter what programming language you use):

T1CON.0 is the first bit of the timer configuration byte, this bit is used to start and stop the timer.
--so--
T1CON.0=1, starts the timer
T1CON.0=0, stops the timer

TMR1H is the timer value's high byte (bits 8-15)
TMR1L is the timer value's low byte (bits 0-7)
--so--
TMR1H = 0 'resets the timer value's high byte
TMR1L = 0 'resets the timer value's low byte
--and--
MyTime.Lowbyte = TMR1L 'puts the timer's low byte in MyTime's lower 8 bits
MyTime.Highbyte = TMR1H 'puts the timer's high byte in MyTime's upper 8 bits

NOTE:
the MyTime should be declared as a word, not a byte since it has to be 16 bits long to hold the whole 16 bit timer1 value

NOTE:
When writing to or reading from the timer, it is very important in which order it is done. When reading the timer values you have to read first the LOW then the HIGH byte. When writing the timer values write first the HIGH then the LOW, this is due to complications in how the timer works.

NOTE:
when I say 'timer value' I mean a number representing the amount of time since the timer was started. To convert between real time and timer1 value units, there seems to be a 5:1 ration between timer1 units and microseconds (5000 timer1 units = 1000 microsecond = 1 millisecond)
 
Remove these adsRemove these ads by Signing Up
 

Step 1The code

the code
'this code is an infinite loop.
'It will run through MamaLoop, then it will stop and
'wait until a specified amount of time (SomeNumber)
'has passed since the start of MamaLoop before continuing
'to FinishLoop.

'####################################################
SomeNumber var word
SomeNumber = 4500

MyTime var word

'====================================
MamaLoop:

T1CON.0=0 'stop the timer
TMR1H = 0 'Set the high part of the timer value to 0
TMR1L = 0 'Set the low part of the timer value to 0
T1CON.0=1 'start the timer

'do whatever you want in the loop
'....blah if, then, blahblah
'....blah if, then, blahblah
'....blah

'-----
'the EndLoopDelay will wait until a specified amount of time has passed
'since the start of the MamaLoop (when the timer value was set to 0)
EndLoopDelay:

CLEARWDT 'clear the watchdog timer (so PIC doesn't reset)

MyTime.Highbyte=TMR1H 'get part of the timer value
MyTime.Lowbyte=TMR1L 'get the rest of the timer value

IF MyTime > SomeNumber THEN 'if the timer value > than your number
GOTO FinishLoop
else
GOTO EndLoopDelay
endif
'-----
'====================================

FinishLoop:
'do something at the end of the loop
'then go back to restart the MamaLoop
GOTO MamaLoop

'####################################################
« Previous StepDownload PDFView All StepsNext Step »
8 comments
Mar 7, 2012. 1:34 AMcbr_umt says:
Slam
hello every one
hope u fine
dear all


the above code is good one , i understand it . thats much simple
actualy
i want to calculate 1 second in pic 19f877a
can any one give an hint ............
nd when timer1 is overflow wht indiction we get.......


@ autther: plz place some more code for understanding
i work a lot on basic bt nt on timer

nd frequency measurement using pic is my part of project


thx
Jun 10, 2011. 6:07 AMrobot1398 says:
do i just copy paste the code to the programming software
Aug 9, 2009. 11:29 PMinstructable.com says:
HI, would like to add one button to start/stop timer for the loop(toggle led + some delay) instead of fix "SomeNumber = 4500"
Need help...
Thanks...
Jan 26, 2007. 6:46 PMstienman says:
This would be more informative if you included information such as: - What the relationship between the clock speed and "somenumber" is - ie, which somenumber will delay for one second at a given clock speed. - Pointers for further reading and possible problems (such as the race condition right after clearing the watchdog timer) - More explanation (the comments are fine for people who don't need this information - but beginners really need more than what you've explained) - This code assumes a lot about what state timer 1 is in when it starts. Does your compiler do some setup for that, or are you relying on a particular chip's reset state? - Which chips does this code work for? (Don't need an exhaustive list - but your title indicates that every PIC will support this code - which is untrue. Give a few part numbers for PICs you know it works on) You might give an example of a situation or problem that this could be used for. Of course you may be trying to keep it as simple as possible, in which case you really should include references to other material for further reading. -Adam
Jan 26, 2007. 11:44 AMsam says:
Does anyone know how to do this in C?

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
81
Followers
40
Author:leevonk
www.leevonk.com