#include "../lcd_hw.h" #define V1_PHASE_A (PINE & (1<<6)) #define V1_PHASE_B (PINE & (1<<7)) signed int Wert_abs=10; volatile signed char Wert_rel=0; volatile signed char Stand=0; void interrupt() { signed char i=0; static unsigned char count=0; static char rtg_pos=0; //gibt Richtung des vorherigen Schrittes an: 1, falls pos, 0, falls neg //i=0; if (V1_PHASE_A) i=1; //graycode to binary if (V1_PHASE_B) i^=3; i-=stand; // bit 1,0: 01 falls neg, 11, falls pos if (i) { //Falls sich etwas verändert hat if ( (((i)&2)>>1)==rtg_pos) { // 1, falls pos, 0 falls neg, also falls sich der Encoder weiterdreht count++; //Zähler, nur für debugging if ( (V1_PHASE_A)&&(V1_PHASE_B) ) //Falls eingerastet Wert_rel+=(i & 2)-1; } rtg_pos=(((i)&2)>>1); Stand +=i; } SREG_I_bit=1; } void interrupt2() iv IVT_ADDR_INT7 { SREG_I_bit=0; interrupt(); } void interrupt3() iv IVT_ADDR_INT6{ SREG_I_BIT=0; interrupt(); } void main() { Lcd_Init(); // Initialize LCD Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off Lcd_Out(1,1,"Start"); DDRB|=(1<<0); PORTB|=(1<<0); //LED Display DDRE&=!((1<<6)|(1<<7)); PORTE|=(1<<7)|(1<<6); //V1/V2 Drehgeber SREG_I_bit=1; EICRB=0b01010000; //level change triggers E6,E7 EIMSK=0b11000000; //E6,E7: Interrupts while (1) { if (Wert_rel) { if (Wert_abs+Wert_rel > 255) Wert_abs=255; else if (Wert_abs + Wert_rel < 0) Wert_abs =0; else Wert_abs+=Wert_rel; WordToStr(Wert_abs,Wert_abs_str); Lcd_Out(1,15,Wert_abs_str); Wert_rel=0; } } }