The main loop of code begins next. The value of
Timer0 is tested to see if it has changed from zero. This
indicates whether the cart is moving. If the value of TMR0
is not zero, then the cart is moving so the value of TMR0
is used to calculate the total inches traveled and is stored
in the variable inches.
if TMR0 = 0 then loop ‘Test for no movement
inches = (30 TMR0) + inches ‘Total new dist
The value of TMR0 is again cleared since we used its
value and it can start counting again as we continue
calculating. The value of yards is calculated with simple
math and stored in the variable yards. The decimal
value for yards is calculated by getting the remainder
calculation, multiplying by 10, and then dividing by the
value of inches for a yard. The trick is the double slash
that gives us only the remainder.
TMR0 = 0
‘Reset Timer 0
yards = inches / 36 ‘Convert Distance to yards
decimal = (inches//36)*10 / 36
Now that we have the calculations, we can display
them on the LCD with the same command lines explained
earlier. We then pause for one second and loop back to
do it again. During that one second delay, Timer 0 is still
collecting pulses as we walk.
‘****Display Distance on LCD****
SEROUT lcd,6,[$FE,$1,”Total Yards: “,
SEROUT lcd,6,[$FE,$C0,”Total Inches: “,#inches]
‘Delay a second between updates\
The program used 22 lines of code, so it is well within
the 31 command line limit of the sample version. You can
download the sample version from www.melabs.com.
Figure 7 shows the display after taking a short walk.
The software doesn’t include a mechanism to reset
the display count back to zero, but the development
board has a switch tied to the reset line of the PIC16F690.
Therefore, I just pressed the switch to reset everything
back to the beginning. An improvement would clearly be
to reconfigure the MCLR pin to a digital input and reset
the values displayed when the switch is pressed. (I actually
showed how to do this in a previous article.) That could
be a great next-step project if you decide to build one of
these counters for yourself.
This project was a lot of fun. I hope you now
understand how easy it is to use Timer0 as a counter. The
limit of this program is the distance. The inches variable
stops at 65,535, because it is a word variable. That
equates to 1,820 yards. If you walked a typical, complete
golf course, you would travel 7,200 yards or more.
GETTING STARTED WITH PICs
Some modification will be required to create a larger
variable size by using two variables — one upper-word
size and one lower-word size. Linking these together by
creating an overflow from the lower word to the upper
word creates a larger variable. I may cover this in a future
column, but I’ll have to play around with it first. The
PICBASIC PRO compiler offers a 32-bit “LONG” variable
type, but this is only available on PIC18F applications. You
can read more about this in the PICBASIC PRO compiler
manual. I also need to improve the sensor bracket and
create some kind of cover for the electronics on the golf
pull-cart, in case it rains. There are a lot of improvements
that can be made, but this is a great starting point.
If you have any questions, comments, or suggestions
for project ideas, pass them on to me at chuck@elprod
ucts.com. I’m also working on improving my website with
more information for my readers. Look for those improvements soon at www.elproducts.com. I’m moving to a new
server to give me more space and more options. See you
next time in the July edition of Nuts & Volts. NV
Chuck Hellebuyck has worked in the electronics industry for over 25 years,
holds three US Patents, and has published four books on embedded
electronics. He is a Field Applications Engineer with Microchip Technology.
NOTE: The Microchip name, logo, and PIC are registered trademarks of
Microchip Technology, Inc., in the USA and other countries. PICkit is a trademark
of Microchip Technology, Inc., in the USA and other countries. All other
trademarks mentioned herein are property of their respective companies.
May 2009 31