Hello,
I made a small disk counter with (wheel encoder with 2 part black and 2 part white) about 2 inches in diameter. I use a light to voltage sensor. It is working pretty well at low speed, but when I flick the disk, o Maybe a few turns per seconds the counter only counts one out of the 5 turns it made. My guess is that the clocking should be made higher.. any ideas?, The whole circuit is already soldered, I hope i do not need to get it in a 20Mhz crystal.
here is the code:
' Internal Oscillation program
' A/D testing -RA.1
' LCD
'/////////////////////////
'// Define section //
'/////////////////////////
INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
@ DEVICE pic16F88, INTRC_OSC, CCPMX_ON, MCLR_ON
OSCCON=$60 ' use internal 4MHZ osc
CMCON = 7 ' Turn OFF the comparators so pins can be used as digital I/O-pins
'/////////////////////////
'// LCD configuration //
'/////////////////////////
DEFINE LCD_DREG PORTB ' Set LCD Data port
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 1 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 0 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits) '4 therefore put wire at 4, 5, 6 and 7 of LCD
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2500
DEFINE LCD_DATAUS 250
DEFINE CHAR_PACING 2000
pause 1000
'/////////////////////////
'// PIN configuration //
'/////////////////////////
TRISA = %11100111 ' Set PORTA to all input
TRISB = %00001100 ' Set PORTB to all output
PortA = 0
'///////////////////////////////////////////////
'// Variable Declaration and initialization //
'///////////////////////////////////////////////
Output_Pot var byte
OldOutput_Pot var byte
Revolution var word
counter var byte
revolution = 0
Output_Pot = 0
OldOutput_Pot = 0
counter = 0
OldOutput_Pot = PortB.3
Mainloop:
'Calculate the distance
Output_Pot = PortB.3
if OldOutput_Pot == Output_Pot then
goto Mainloop
endif
if OldOutput_Pot != Output_Pot then
counter = counter +1 'If oldOutput_pot is different then Output_pot
OldOutput_Pot = Output_Pot
if counter =4 then
Revolution = Revolution + 1
counter =0
endif
endif
lcdout $FE,1, "Counter:",dec counter
lcdout $FE,$C0, "Revolution:",dec Revolution
pause 150
GOTO Mainloop
end
Bookmarks