Here the code for a rotary optical encoder, using quadrature.
Just connect the Channel A to portB.6 and channel B to portB.7, ground portB.5 and portB.4
Connection for LCD can be found to page 96 of PBP user manual.
EnjoyCode:include "ALLDIGITAL.pbp" INCLUDE "DT_INTS-14.bas" ; Base Interrupt System 'INCLUDE "ReEnterPBP.bas" ; Include if using PBP interrupts DEFINE OSC 20 ' PIC 16F628 ' PicBasic program to demonstrate operation of an LCD in 4-bit mode ' and reading an optical encoder in quadrature ' ' LCD should be connected as follows: ' LCD PIC ' DB4 PortA.0 ' DB5 PortA.1 ' DB6 PortA.2 ' DB7 PortA.3 ' RS PortA.4 (add 4.7K pullup resistor to 5 volts) ' E PortB.0 ' RW Ground ' Vdd 5 volts ' Vss Ground ' Vo 20K potentiometer (or ground) ' DB0-3 No connect TrisA = 000000 TrisB = 110000 PortB=0 Define LCD_DREG PORTA Define LCD_DBIT 0 Define LCD_RSREG PORTA define LCD_RSBIT 4 define LCD_EREG PORTB define LCD_EBIT 0 define LCD_BITS 4 define LCD_LINES 2 define LCD_COMMANDUS 2000 define LCD_DATAUS 50 Flag var bit wsave VAR BYTE $70 SYSTEM ' alternate save location for W Q_New var Byte Q_Old var byte Q_Count var word ' Set variable value @ startup Flag = 0 Q_New = 0 Q_Old = 0 Q_Count = 0 ASM INT_LIST macro ; IntSource, Label, Type, Resetflag? INT_Handler RBC_INT, _Encoder, ASM, yes endm INT_CREATE ; Creates the interrupt processor ENDASM @ INT_ENABLE RBC_INT ; enable external (INT) interrupts Lcdout $fe, 1 ' Clear LCD screen Lcdout "System Ready" ' Display message Pause 500 ' Wait .5 second Goto Main_Loop Encoder: Q_New = PortB.7 + PortB.7 + PortB.6 if Q_Old = 0 then if Q_New = 2 then Minus_Count if Q_New = 1 then Plus_Count endif if Q_Old = 1 then if Q_New = 0 then Minus_Count if Q_New = 3 then Plus_Count endif if Q_Old = 3 then if Q_New = 1 then Minus_Count if Q_New = 2 then Plus_Count endif if Q_Old = 2 then if Q_New = 3 then Minus_Count if Q_New = 0 then Plus_Count endif goto Q_Skip Minus_Count: Q_Count = Q_Count - 1 Flag = 1 goto Q_Skip Plus_Count: Q_Count = Q_Count + 1 Flag = 1 Q_Skip: Q_Old = Q_New @ INT_RETURN Main_Loop: if Flag = 1 then Lcdout $fe, 1 Lcdout Dec Q_Count Flag = 0 endif goto Main_Loop end
Cheers
Al.




Bookmarks