Znaleziono 1 wynik

autor: Yogi_
19 wrz 2011, 17:18
Forum: Automatyka przemysłowa
Temat: Enkoder kwadraturowy - szybki "algorytm odczytu"
Odpowiedzi: 18
Odsłony: 6359

Witam :-)
Hmm... może się wtrącę - choć prawdopodobnie nie na temat
Używając procesorów z rodziny PIC i programujac w języku jal v.2
wyglądałoby to tak:

Kod: Zaznacz cały

;Quadrature decoder for Rotary encoder
;------------------------------------------------------
-- Initialize the interrupt control register
INTCON_GIE = 1 -- Enable global interrupt
INTCON_RBIE = 1 -- Enable interrupt on port(B4-B7) change

var bit UV_stat = false
var volatile bit button
var word mouse ; two BCD bytes 99:59
var byte mouse_b[2] at mouse ; aliasing
var byte last_quad = 0
var volatile bit click ; mouse changed

procedure quadrature() is
pragma interrupt
if pin_b6 == high then
_usec_delay(10) 
button = pin_b6
else
button = low
end if
var sbyte n = 0
; always process the mouse
if INTCON_RBIF == high then
_usec_delay(10) -- debounce
last_quad = last_quad | (PORTB & 0b_0011_0000) 
case last_quad of
--0b_0010_0000,
--0b_1011_0000,
--0b_1101_0000,
0b_0100_0000: 
n = 1
--0b_0001_0000,
--0b_0111_0000,
--0b_1110_0000,
0b_1000_0000: 
n = -1
end case
last_quad = last_quad << 2
INTCON_RBIF = 0
end if
; but change the actual variable only when UV is off
if UV_stat == false then
; do not allow mouse to overflow (and wrap around) 
if (n == 1) then
if (mouse != (99 << 8)+99) then
bcd_inc(mouse_b) 
click = true
end if
elsif (n == -1) then
if (mouse != 0) then
bcd_dec(mouse_b) 
click = true
end if
end if
end if
end procedure

Wróć do „Enkoder kwadraturowy - szybki "algorytm odczytu"”