Gast
#1383240
Hallo,
ich habe mir einen drehgeber bei reichelt bestellt. Nachdem ich ja noch
immer recht wenig plan von dem Ganzen habe habe ich bissl geschaut und
folgenden quelltext gefunden
/***********************************************************************
*/
/*
*/
/* Reading rotary encoder */
/* one, two and four step encoders supported */
/*
*/
/* Author: Peter Dannegger
*/
/*
*/
/***********************************************************************
*/
#include <avr/io.h>
#include <avr/interrupt.h>
// target: ATmega16
//----------------------------------------------------------------------
--
#define XTAL 8e6 // 8MHz
#define PHASE_A (PINA & 1<<PA1)
#define PHASE_B (PINA & 1<<PA3)
#define LEDS_DDR DDRC
#define LEDS PORTC // LEDs against VCC
volatile int8_t enc_delta; // -128 ... 127
static int8_t last;
void encode_init( void )
{
int8_t new;
new = 0;
if( PHASE_A )
new = 3;
if( PHASE_B )
new ^= 1; // convert gray to binary
last = new; // power on state
enc_delta = 0;
TCCR0 = 1<<WGM01^1<<CS01^1<<CS00; // CTC, XTAL / 64
OCR0 = (uint8_t)(XTAL / 64.0 * 1e-3 - 0.5); // 1ms
TIMSK |= 1<<OCIE0;
}
ISR( TIMER0_COMP_vect ) // 1ms for manual move-ment
{
int8_t new, diff;
new = 0;
if( PHASE_A )
new = 3;
if( PHASE_B )
new ^= 1; // convert gray to binary
diff = last - new; // difference last - new
if( diff & 1 ){ // bit 0 = value (1)
last = new; // store new as next last
enc_delta += (diff & 2) - 1; // bit 1 = direction (+/-)
}
}
int8_t encode_read1( void ) // read single step encoders
{
int8_t val;
cli();
val = enc_delta;
enc_delta = 0;
sei();
return val; // counts since last call
}
int8_t encode_read2( void ) // read two step encoders
{
int8_t val;
cli();
val = enc_delta;
enc_delta = val & 1;
sei();
return val >> 1;
}
int8_t encode_read4( void ) // read four step encoders
{
int8_t val;
cli();
val = enc_delta;
enc_delta = val & 3;
sei();
return val >> 2;
}
int main( void )
{
int32_t val = 0;
LEDS_DDR = 0xFF;
encode_init();
sei();
for(;;){
val += encode_read1(); // read a single step encoder
LEDS = val;
}
}
Dieser code ist für den mega16. Ich habe aber nur nen mega 8515 und nen
mega32 zur hand, aber das dürfte doch auch nicht anders sein, oder? Ich
müsste dann doch einfach im AVR stu-dio beim gcc als target eben den
mega32 auswählen, oder?
Mein Ziel wäre echt einfach nur n Drehgeber, bei dem die LED einfach
immer einen schritt wei-tergeht (damit ich diesen code dann für eine
displayansteuerung/menüauswahl verwenden kann).
Danke schon mal
Grüße