L'ingresso e' alle porte RB0-RB1 che prenderanno in carico i 2 segnali in quadratura in uscita dall'encoder.
Un led sulla porta C discrimina la direzione.
Il codice e' in Mikrobasic.
Collegando un economico encoder meccanico (tipo volume autoradio) ha dei problemi anche col trigger col CD4093.
Con un encoder ottico ha funzionato tutto alla prima senza trigger.
- Codice: Seleziona tutto
program encoder_A
' MCU: PIC16F877A
' Dev.Board: Myboard
' Oscillator: HS, 20.000 MHz
' Ext. Modules: trigger with CD4093, Lcd_2x16
'PROGRAM TO DETERMINE DIRECTION OF QUADRATURE ENCODER
DIM PORTB_OLD AS BYTE
DIM PORTB_NEW AS BYTE
dim count as byte
dim txt as string[3]
'USING A 2 BIT (QUADRATURE SYSTEM) THIS IS THE RULE:
'LOW BIT OF OLD VALUE IS ALWAYS EQUAL TO HIGH BIT OF NEW VALUE
'IF MOVING CLOCKWISE
'LOW BIT OF OLD VALUE DOES NOT EQUAL HIGH BIT OF NEW VALUE
'IF MOVING COUNTERCLOCKWISE
main:
'TRISA = 0 'RA0 LED WILL INDICATE CW; RA1 LED WILL INDICATE CCW
TRISB = %00000011 'RB0 and RB1 Inputs
TRISC = 0
PORTA = 0
PORTB = 0
LCD_init(PORTD) 'LCD su portD
LCD_out(1,1,"Numero impulsi:")
LCD_out(2,1,"Dir:")
PORTB_OLD = PORTB
my_direction:
PORTB_NEW = PORTB
IF PORTB_NEW XOR PORTB_OLD <> 0 THEN 'test for encoder change
IF (PORTB_NEW >> 1) = (PORTB_OLD AND 1) THEN
PORTC = 1 'CW DIRECTION
LCD_out(2,6,"I")
count = count-1
ELSE
PORTC = 0 'CCW DIRECTION
count = count+1
LCD_out(2,6,"A")
END IF
END IF
bytetostr(count,txt)
LCD_out(1,18,txt)
PORTB_OLD = PORTB_NEW
GOTO my_direction
end.
Ciao a tutti

Elettrotecnica e non solo (admin)
Un gatto tra gli elettroni (IsidoroKZ)
Esperienza e simulazioni (g.schgor)
Moleskine di un idraulico (RenzoDF)
Il Blog di ElectroYou (webmaster)
Idee microcontrollate (TardoFreak)
PICcoli grandi PICMicro (Paolino)
Il blog elettrico di carloc (carloc)
DirtEYblooog (dirtydeeds)
Di tutto... un po' (jordan20)
AK47 (lillo)
Esperienze elettroniche (marco438)
Telecomunicazioni musicali (clavicordo)
Automazione ed Elettronica (gustavo)
Direttive per la sicurezza (ErnestoCappelletti)
EYnfo dall'Alaska (mir)
Apriamo il quadro! (attilio)
H7-25 (asdf)
Passione Elettrica (massimob)
Elettroni a spasso (guidob)
Bloguerra (guerra)

