Hello Everyone,

I'm new to this forum and PIC BASIC PRO. I do although have some background in PICs and other forms of programming for them.

I have a question about a PIC12F683 program I'm writing. I believe the internal clock is 4MHz, typically you set it a different way, but the guy before me said what's in the "Define PIC Internals" will do it. What I'm trying to do is count the number of pulses coming into an input on the PIC from a hall effect sensor. The frequency should be nothing faster than 2kHz. Basically the hall effect will be like giving a position based off of counts, and I will be outputting a 2kHz PWM wave with the number of counts determining the duty cycle.

I would do a COUNT function but the only problem is I need to be looking at the pulses coming in all the time while changing the duty cycle based on the counts I have. I don't want to miss any of the counts and I'm worried if I make the processor sit and just count pulses I will have to update the duty cycle and while I'm doing so miss a couple counts. I might be thinking way to hard about this and missing a very easy way to complete it. The PWM signal does not take many cycles to update so I might be able to use the COUNT function but I'm unsure.

Below is the code I have. All this is suppose to do is count the pulses and store them into EEPROM. I only add 1 to position every 10 pulses because of overflow of variable. When I run this I get 00 00 stored into EEPROM. I have gpio.2 tied directly to +5V and a function generator on gpio.3 at 500 Hz. I usually run the system for 10 seconds as well.

Any help on this would be appreciated, or just any feedback and ideas. Thank guys, and hopefully I can be a good addition to this forum.

Anthony Smith

************************************************** ***
' Define PIC Internals

@ DEVICE PIC12F683, INTRC_OSC_NOCLKOUT
' System Clock options
@ DEVICE PIC12F683, WDT_ON
' Watchdog timer
@ DEVICE PIC12F683, PWRT_ON
' Power-On Timer
@ DEVICE PIC12F683, MCLR_OFF
' Master Clear Options
@ DEVICE PIC12F683, BOD_ON
' Brown-Out Detect
@ DEVICE PIC12F683, CPD_OFF
' Data Memory Code Protect
@ DEVICE PIC12F683, PROTECT_OFF
' Program Code Protection

' Define Hardware Variables


' Define Software Variables
Position VAR WORD

Shrink VAR BYTE

WasLow VAR BIT

' Initialize PIC Configuration
CMCON0 = 7

TRISIO = %00011110
'WPU = %00011111


Position = 0
WasLow = 0
Shrink = 0
PAUSE 50

MainLoop:
IF gpio.3 = 1 AND WasLow = 0 THEN
IF gpio.2 = 1 THEN
Shrink = Shrink + 1
ELSE
Shrink = Shrink - 1
ENDIF
WasLow = 1
ENDIF

IF Shrink = 10 THEN
Position = Position + 1
Shrink = 0
ENDIF

IF gpio.3 = 0 THEN
WasLow = 0
ENDIF

IF Shrink = -10 THEN
Position = Position - 1
Shrink = 0
ENDIF

WRITE 0, Position.Byte1
WRITE 1, Position.Byte0


GOTO MainLoop

************************************************** ****