PDA

View Full Version : newbie: 16F688 with COUNT & SEROUT



ShortBus
- 14th June 2007, 01:20
New guy here with both microcontrollers and PBP...

All I want to do is count the number of times a switch cycles over the timespan of a second and output it to a serial port. The serial output is working fine, but I'm getting only lines that read "Hertz:". I beleive that it's a problem with the registers, but I've tried various combinations without luck.

The switch is connected between pin 12 (DIP package) on the PIC and +5VDC, and my code looks like this:



INCLUDE "Modedefs.bas"
' Have tried various registers. Here's what I currently have:
ANSEL = %00000000 ' no analog inputs
CMCON0 = %00000111 ' comparators off

W1 Var word

Loop:
COUNT 1,1000,W1
SEROUT 0,N2400,["Hertz: ",W1,13,10]
PAUSE 1000
GOTO Loop

End


Any suggestions? Thanks!

Archangel
- 14th June 2007, 01:33
Hi Shortbus,
You might try using a timer to count for you.
Look at this thread: http://www.picbasic.co.uk/forum/showthread.php?t=5519&highlight=timer
JS

ShortBus
- 14th June 2007, 02:58
Thanks for the reply.

Can you recommend a particular timer? Also, why can't COUNT be directly utilized for this application?

mister_e
- 14th June 2007, 03:15
in your previous code, add a # sign before W1 and post your results.

SEROUT 0,N2400,["Hertz: ",#W1,13,10]

Count is not bad, but using a timer allow much flexibility, accuracy, and use less code space.

skimask
- 14th June 2007, 04:49
Thanks for the reply.

Can you recommend a particular timer? Also, why can't COUNT be directly utilized for this application?

You'll also be limited to about 50Khz at the input to the PIC if your PIC is running at 8Mhz.

ShortBus
- 14th June 2007, 13:07
Thanks for the replies!

Even though it may not be the best solution this should work, right? (I'm just experimenting/learning)

Are my registers set correctly? Is logical Pin 1 --> physical Pin 12 on the 16F688?

mister_e
- 14th June 2007, 13:29
I'm not a PIN number fan/ser, i'll prefer to use the Pin Name instead

SEROUT PORTB.0,N2400,["Hertz: ",#W1,13,10]

OR using a alias


SerialOutput VAR PORTB.0
'
'
'
'
'
SEROUT SerialOutput,N2400,["Hertz: ",#W1,13,10]

You can find an explanation on pin number in section 4.11 of the manual.

HTH