Aansluiten staat boven de sourcecode (port b en c):
Kan uiteraard (na aanpassing) ook met een rotary encoder.
(ikzelf vindt druktoetsen fijner...)
Versie voor encoder (aansluiten zelfde, code is nog veel simpeler):
'======================================================================================
Program Test
Dim REncA As sbit at PORTA.B0
REncB As sbit at PORTA.B1
Dim Cnt As Word
KeyCnt As Byte
KeyRepCnt As Byte
SaveCnt As Word
Flags As Byte
fSave As sbit at Flags.0
fOldREncA As sbit at Flags.1
Const MINCNT = 3300
MAXCNT = 5300
STARTCNT = 3300
REPTIME = 8
SAVETIME = 1000
EEADDRESS = 0x20
'========================================================================================
Sub procedure Interrupt() iv 0x0004 ics ICS_AUTO
'========================================================================================
If TMR2IF_bit = 1 Then
If SaveCnt < SAVETIME Then
Inc(SaveCnt)
If SaveCnt = SAVETIME Then fSave = 1 End If
End If
TMR2IF_bit = 0
End If
End Sub
'========================================================================================
Sub Procedure Init()
'========================================================================================
TRISA = 0x03 ANSELA = 0
TRISB = 0x00 ANSELB = 0
TRISC = 0x00
Flags = 0
PR2 = 124
T2CON = %01001100
TMR2IE_bit = 1
GIE_bit = 1
PEIE_bit = 1
Hi(Cnt) = EEProm_Read(EEADDRESS)
Lo(Cnt) = EEProm_Read(EEADDRESS+1)
If (Cnt < MINCNT) or (Cnt > MAXCNT) Then Cnt = STARTCNT End If
End Sub
'========================================================================================
main:
'========================================================================================
Init()
while true
If REncA <> fOldREncA Then
Delay_ms(5)
If REncA <> fOldREncA Then
If (REncA = 1) And (REncB = 1) Then
Inc(Cnt)
If (Cnt > MAXCNT) Then Cnt = MINCNT End If
End If
If (REncA = 1) And (REncB = 0) Then
Dec(Cnt)
If (Cnt < MINCNT) Then Cnt = MAXCNT End If
End If
fOldREncA = REncA
SaveCnt = 0
LATB = Lo(Dec2BCD16(Cnt))
LATC = Hi(Dec2BCD16(Cnt))
End If
If fSave Then
EEProm_Write(EEADDRESS, Hi(Cnt))
EEProm_Write(EEADDRESS+1, Lo(Cnt))
fSave = 0
End If
End If
wend
End.
[Bericht gewijzigd door Arco op woensdag 19 oktober 2022 15:05:01 (88%)