I tried this but nothing happens - the display changes erratically with encoder movement and only really goes up or down by 1:
Code:
#DEFINE USE_CUI_ROTARY_ENCODER ; comment out if using Grayhill rotary encoder
; which triggers an interrupt only once per detent
' Enable interrupts on RPM control now that motors are fully spun up
INTCON = %10001000 ' Global int enabled, IOCI enabled,
' INTF flag bit 0 clr, IOCI flag bit 0 clr
#IFDEF USE_CUI_ROTARY_ENCODER
IOCAP.1 = 1 ' Enable positive (rising edge) change, PORTA.1
' The CUI encoder goes thru a full quad cycle with 1 detent turn,
' so only need to capture 1 edge (A channel, rising)
' CH A
' ====
' ------ ------
' | | | |
' | | | |
' --- ------ ------
' CH B
' ====
' ------ ------
' | | | |
' | | | |
' ------ ------ ---
' Detent markers
' ==============
' | | |
' | | |
' If interrupt is triggered, it means A had a rising edge:
' If B = 0 Then CW
' If B = 1 Then CCW
#ELSE
' For Grayhill encoder, 1 detent triggers only 1 edge
IOCAP.0 = 1 ' Enable positive (rising edge) change, PORTA.0
IOCAP.1 = 1 ' Enable positive (rising edge) change, PORTA.1
IOCAN.0 = 1 ' Enable negative (falling edge) change, POARTA.0
IOCAN.1 = 1 ' Enable negative (falling edge) change, PORTA.1
IOCAF.0 = 0 ' Clear interupt-on-change flag
IOCAF.1 = 0 ' Clear interupt-on-change flag
#ENDIF
@ INT_ENABLE IOC_INT ; Interrupt-on-Change interrupt
Main:
' Check if motor RPM has changed
IF MotorRPM <> Old_RPM Then
Old_RPM = MotorRPM
GOSUB ChngMotorHPWM
EndIF
If ValueDirty = 1 THEN
TimeToSave = TimeToSave - 1
If TimeToSave = 0 THEN ' Time to save ?
WRITE EE_MotorRPM, MotorRPM
ValueDirty = 0 ' Clear the flag
#IFDEF USE_LCD_FOR_DEBUG
HSEROUT [LCD_INST, LCD_CLR]
pause 5
HSEROUT ["Saved RPM=", DEC MotorRPM, " ", 13, 10] ' Send text followed by carriage return and linefeed
#ENDIF
ENDIF
ENDIF
GOTO Main
' ***************************************************************
' [IOC - interrupt handler]
' ***************************************************************
RotEncAdjust:
#IFDEF USE_CUI_ROTARY_ENCODER
IF PORTA.0 = 0 THEN
RotEncDir = 1 ; CW rotation if B channel is 0
ELSE
RotEncDir = 0 ; CCW rotation
ENDIF
#ELSE
New_Bits = PORTA & (%00000011)
IF New_Bits = Old_Bits Then DoneRotEnc
RotEncDir = New_Bits.1 ^ Old_Bits.0
#ENDIF
IF RotEncDir = 1 Then
; CW rotation - increase speed but only to a max of 'MaxDuty'
IF MotorRPM < MaxDuty then MotorRPM = MotorRPM + 1
Else
' CCW rotation - decrease speed to a min of 0
IF MotorRPM > 0 Then MotorRPM = MotorRPM - 1
EndIF
ValueDirty = 1 ; Set dirty flag
TimeToSave = 50000 ; Set value to begin countdown to EEPROM save
DoneRotEnc:
Old_Bits = New_Bits
IOCAF.0 = 0 ' Clear interrupt flags
IOCAF.1 = 0
@ INT_RETURN
(I left out the rest of the code)
Bookmarks