kgavionics
Full Member level 3
- Joined
- Jun 12, 2012
- Messages
- 167
- Helped
- 7
- Reputation
- 14
- Reaction score
- 11
- Trophy points
- 1,298
- Location
- Alberta.Canada
- Activity points
- 2,479
;Rotary encoder hoocked up to an Arduino Uno.Signal A is going to pin INt0 and signal B is going to pin pd3.If the Rotary encoder is turned clock wise, Led 1 ( PC0) is turned ON, if turned CCW then led 2 ( PC1) is turn on.
.device Atmega328P
.org 0x0
jmp main
.org 0x002
jmp int0_isr
main:
sbi ddrc,0
sbi ddrc,1
sbi portd,2 ; pull up resistor on Pd2
sbi portd,3; pull up resistor on Pd3 ; B signal
cbi ddrd,3
cbi ddrd,2
ldi r16,0x01 ;Int0 enabled
out eimsk,r16
ldi r16,0x02 ; falling edge
sts eicra,r16
sei
loop:
rjmp loop
.org 0x100
int0_isr:
sbis portd,3
rjmp ccw
sbi portc,1
cbi portc,0
rjmp exit
ccw:
sbi portc,0
cbi portc,1
exit:
reti
Thank you KlausST for your inputHi,
ports have input anpd output registers.
Like PIND and PORTD.
in your ISR you (try to) read PORTD, but it should be PIND.
Klaus
Hi,
maybe it´s a mechanic switch, which may bounce. Thus you need to use a debouncing techniquie on the input.
This could be done with software, but maybe an external capacitor could do the job.
I don´t know your pullup resistor value: Try a capacitor with 0.00001/R. No exact value needed.
Klaus
The RE I use already has a low pass filter included, so I don't think it's a debounce problem, but I'm not sure, because I don't have an oscilloscope to check it!Hi,
maybe it´s a mechanic switch, which may bounce. Thus you need to use a debouncing techniquie on the input.
This could be done with software, but maybe an external capacitor could do the job.
I don´t know your pullup resistor value: Try a capacitor with 0.00001/R. No exact value needed.
Klaus
Can you refer to a datasheet specifying the filter feature?The RE is use already has a low pass filter included, so I don't think it's a debounce problem
You guys are right! My RE has a lot of noise, because even with this Arduino sketch(not mine), I still get some noise!so now I'm sure that the problem is coming for my RE!Can you refer to a datasheet specifying the filter feature?
Your decoder logic isn't good because it acts only on 1 out of 4 encoder output transitions. Better use state-of-the-art qudrature logic as sketched in post #3. Debouncing might be also achieved by scanning the inputs at a fixed rate controlled by a timer.
/*
Rotary Encoder Demo
rot-encode-demo.ino
Demonstrates operation of Rotary Encoder
Displays results on Serial Monitor
DroneBot Workshop 2019
https://dronebotworkshop.com
*/
// Rotary Encoder Inputs
#define inputCLK 4
#define inputDT 5
// LED Outputs
#define ledCW 8
#define ledCCW 9
int counter = 0;
int currentStateCLK;
int previousStateCLK;
String encdir ="";
void setup() {
// Set encoder pins as inputs
pinMode (inputCLK,INPUT);
pinMode (inputDT,INPUT);
// Set LED pins as outputs
pinMode (ledCW,OUTPUT);
pinMode (ledCCW,OUTPUT);
// Setup Serial Monitor
Serial.begin (9600);
// Read the initial state of inputCLK
// Assign to previousStateCLK variable
previousStateCLK = digitalRead(inputCLK);
}
void loop() {
// Read the current state of inputCLK
currentStateCLK = digitalRead(inputCLK);
// If the previous and the current state of the inputCLK are different then a pulse has occured
if (currentStateCLK != previousStateCLK){
// If the inputDT state is different than the inputCLK state then
// the encoder is rotating counterclockwise
if (digitalRead(inputDT) != currentStateCLK) {
counter --;
encdir ="CCW";
digitalWrite(ledCW, LOW);
digitalWrite(ledCCW, HIGH);
} else {
// Encoder is rotating clockwise
counter ++;
encdir ="CW";
digitalWrite(ledCW, HIGH);
digitalWrite(ledCCW, LOW);
}
Serial.print("Direction: ");
Serial.print(encdir);
Serial.print(" -- Value: ");
Serial.println(counter);
}
// Update previousStateCLK with the current state
previousStateCLK = currentStateCLK;
}
sbi portd,2 ; pull up resistor on Pd2
sbi portd,3; pull up resistor on Pd3 ; B signal
cbi ddrd,3
cbi ddrd,2
ldi r16,0x03 ;Int0 and Int1 enabled
out eimsk,r16
ldi r16,0x0b ;int0 rising edge /int1 falling edge
sts eicra,r16
sei
loop:
rjmp loop
.org 0x100
int0_isr:
sbic pind,3
rjmp ccw
sbi portc,1
cbi portc,0
rjmp exit
ccw:
sbi portc,0
cbi portc,1
exit:
reti
.org 0x150
int1_isr:
sbis pind,2
rjmp cw2
sbi portc,0
cbi portc,1
rjmp exit2
cw2:
sbi portc,1
cbi portc,0
exit2:
reti
;Rotary encoder hooked up to an Arduino Mega. Signal A is going to pin INt0 (PD0) and signal B is going to pin Int1(PD1). If the Rotary encoder is turned clockwise, Led 1 (PC0) is turned ON, if turned CCW then led 2 (PC1) is turn on.
.device Atmega2560
.org 0x0
jmp main
.org 0x0002
jmp int0_isr
.org 0x0004
jmp int1_isr
main:
sbi ddrc,0 ;pc0 as output
sbi ddrc,1 ;pc1 as output
cbi ddrd,1 ;pd1 as input
cbi ddrd,0 ;pd0 as input
sbi portd,0 ; pull up resistor on Pd0 int0
sbi portd,1; pull up resistor on Pd1 ; B signal
ldi r16,0x03 ;Int0 and Int1 enabled
out eimsk,r16
ldi r16,0x0b ;int0 rising edge /int1 falling edge
sts eicra,r16
sei
loop:
rjmp loop
.org 0x100
int0_isr:
sbic pind,1
rjmp ccw
sbi portc,1
cbi portc,0
rjmp exit
ccw:
sbi portc,0
cbi portc,1
exit:
reti
.org 0x150
int1_isr:
sbis pind,0
rjmp cw2
sbi portc,0
cbi portc,1
rjmp exit2
cw2:
sbi portc,1
cbi portc,0
exit2:
reti
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?