Hi all,
I did the usual and trolled though the internet and this forum, on how debounce the Bourns type encoders.
I tried the 555 pulse circuits, the 7414/7474 combinations and a lot of the programs by other parties, None of which worked
to my satisfaction. Also I wanted some extra things like
- Multiple clicks of the encoder before the counter is increased or decreased.
- When the number is changed, I wanted to be able to increment by a given amount.
So I decide to do my own, and here it is. I think it should be easily understood by all, It's commented in the interesting bits
Dean
[code:1:94e9b75eff]
$regfile = "8535DEF.dat"
$crystal = 8000000
$baud = 9600 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 10 ' default use 10 for the SW stack
$framesize = 40 ' default use 40 for the frame space
$lib "glcdKS108.lib"
Dim Encoderval As Byte
Dim Addnumber As Byte ' 100, 10, 1 This can be a word or integer if you want
Dim Dividerup As Byte ' This can be a word or integer if you want
Dim Dividerdwn As Byte
Dim Divvalue As Byte
Config Porta = Output
Config Portb = Output
Config Portc = Output
Config Portd = Output
Config Portd.2 = Input
Config Portd.3 = Input
Portd.2 = 1 'Do Pullups
Portd.3 = 1
Led Alias Portd.5
ChannelA Alias Pind.2
Channelb Alias Pind.3
On Int0 Encfound
Config Int0 = Falling
Const Waittime = 1 'How long to wait before looking at the inputs after the first falling edge, Gets rid of 99% debounce.
' ===[ Setup LCD Display ]======================================================
Config Graphlcd = 128 * 64sed , Dataport = Portc , Controlport = Portb , Ce1 = 0 , Ce2 = 1 , Cd = 2 , Rd = 3 , Reset = 4 , Enable = 5 , Mode = 6
Dividerup = 0 'Init the int divider counters
Dividerdwn = 0
Encoderval = 0 'This is the actual encoder click counter
Cls
Setfont Font8x8
Lcdat 1 , 1 , "Encoder test"
'These Values Can Be Changed From Within The Program.
Divvalue = 0 'Divider for encoder 0= every click, 1 = 2 clicks, 2 = 3 clicks to inc or dec counter By the Addnumber Value
Addnumber = 1 'Value to add in to the counter
'The two above values are used in the encoder interrupt.
'Work like this.
'The DivValue is the number of encoder clicks required to get to increment or decrement the counter ( Which is EncoderVal )
'When the programgets to inc or dec the EncoderVal variable, it can be incremented or decremented by any value (Addnumber )
'This is good for doing things like incrementing in 10s or 100s.
Enable Interrupts
Enable Int0
Do
Set Led
Lcdat 5 , 2 , "Enc: " ; Encoderval ; " "
Reset Led
Waitms 10
Loop
Encfound:
Toggle Led 'in here for testing
Waitms Waittime ' This is the mS wait for debounce
If ChannelA = 0 Then 'Channel A has a falling edge see if it's low here
If Channelb = 0 Then ' If so, is channel B high or low. If it's low,
If Dividerup < Divvalue Then 'Check the Click count divider value, is it < the required value.
Incr Dividerup ' Yes, so increment it for next time round
Else
EncoderVal = EncoderVal + Addnumber 'The Click count divider was equal to or above the required count so Add the incremental number.
Dividerup = 0 'Zero the click counter.
End If
Else
If Dividerdwn < Divvalue Then 'Same as the increment section, but decrements the number
Incr Dividerdwn
Else
EncoderVal = EncoderVal - Addnumber
Dividerdwn = 0
End If
End If
End If
Gifr = 64
Return
End
[/code:1:94e9b75eff]
↧