J
JJ
Hello,
I have problem with counting ticks of an encoder.
I can achieve about 5 kHz without errors.
Above this frequency, I loose ticks.
I use second AVR processor as the generator of
two signals -- I just test my device, without real encoder.
What to do to improve frequency ?
Below, I present my program.
Processor: AVR ATmega16
You should connect an Encoder in the way:
Canal A : PD2 i PD3
Canal B: PD4
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include "lcd.h"
long int licz = 0;
int flagaA = 0, flagaB = 0;
SIGNAL (SIG_INTERRUPT0)
{
if (!(inb(PIND) & 16)) /* if 0 on canal B */
flagaA = 1;
else /* otherwise, (1 on canal B) */
flagaB = 1;
}
SIGNAL (SIG_INTERRUPT1)
{
if (inb(PIND) & 16) { /* if 1 on canal B */
if (flagaA) /* if flagaA is set */
licz++, flagaA = 0;
} else if (flagaB) /* if flagaB is set */
licz--, flagaB = 0;
}
int main(void)
{
char s[16];
outp(0, DDRD);
/* Setting interrupts INT 0 i INT1 -- rising, falling edge
*/
outp((1<<INT0)|(1<<INT1), GIMSK);
outp((1<<ISC01)|(1<<ISC00) | (1<<ISC11), MCUCR);
initLCD();
sei(); /* star interrupts */
for (; {
cursorHome();
sprintf(s, "%ld ", licz);
showStringOnLCD(s);
}
return 0;
}
I have problem with counting ticks of an encoder.
I can achieve about 5 kHz without errors.
Above this frequency, I loose ticks.
I use second AVR processor as the generator of
two signals -- I just test my device, without real encoder.
What to do to improve frequency ?
Below, I present my program.
Processor: AVR ATmega16
You should connect an Encoder in the way:
Canal A : PD2 i PD3
Canal B: PD4
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include "lcd.h"
long int licz = 0;
int flagaA = 0, flagaB = 0;
SIGNAL (SIG_INTERRUPT0)
{
if (!(inb(PIND) & 16)) /* if 0 on canal B */
flagaA = 1;
else /* otherwise, (1 on canal B) */
flagaB = 1;
}
SIGNAL (SIG_INTERRUPT1)
{
if (inb(PIND) & 16) { /* if 1 on canal B */
if (flagaA) /* if flagaA is set */
licz++, flagaA = 0;
} else if (flagaB) /* if flagaB is set */
licz--, flagaB = 0;
}
int main(void)
{
char s[16];
outp(0, DDRD);
/* Setting interrupts INT 0 i INT1 -- rising, falling edge
*/
outp((1<<INT0)|(1<<INT1), GIMSK);
outp((1<<ISC01)|(1<<ISC00) | (1<<ISC11), MCUCR);
initLCD();
sei(); /* star interrupts */
for (; {
cursorHome();
sprintf(s, "%ld ", licz);
showStringOnLCD(s);
}
return 0;
}