/*################################################################################################# Für : Vorlage Version : 1.1 autor : Martin Junghans jtronics@gmx.de www.jtronics.de License : GNU General Public License Hardware: AT90CAN128, 16MHz, //################################################################################################*/ #include #include #include #include #include "lcd.h" //#################################### check CPU Takt #ifndef F_CPU #define F_CPU 16000000UL #endif //#################################### Touch #include #define SK_A0 (1<<5) #define SK_B0 (1<<4) #define SK_A1 (1<<2) #define SK_B1 (1<<3) #define SK_A2 (1<<0) #define SK_B2 (1<<1) #define SK_A012 (SK_A0 | SK_A1 | SK_A2) #define SK_B012 (SK_B0 | SK_B1 | SK_B2) #define MAX_CYCLE 2000 uint16_t keys[3]; //#################################### Macros #define uniq(LOW,HEIGHT) ((HEIGHT << 8)|LOW) // 2x 8Bit --> 16Bit #define LOW_BYTE(x) (x & 0xff) // 16Bit --> 8Bit #define HIGH_BYTE(x) ((x >> 8) & 0xff) // 16Bit --> 8Bit #define sbi(ADDRESS,BIT) ((ADDRESS) |= (1<<(BIT))) // set Bit #define cbi(ADDRESS,BIT) ((ADDRESS) &= ~(1<<(BIT))) // clear Bit //########################################################################################## Variablen volatile int timer_Taster =0, // timer für echtzeit ausführung timer_blink =1000; char buffer[7]; // Buffer für Text Displays oder UART Funktionen int LED =0; // sonstige Variablen //########################################################################################## initialisierung void initialisierung(void) { cli(); //####################################### Ports DDRC=0b11111111; PORTC=0b00000000; //####################################### Zeit in ms-- 8 Bit Timer0 TCCR0A |= _BV(CS00)| _BV(CS01); // 8bit - Prescaller 64 - zähler 131-->125 schritte --> Millisecunden TCNT0 = 5; // Timer 131 setzen zählt durch, overflow nach 1 ms TIMSK0 |= _BV(TOIE0); // interrupt aktivieren //####################################### Text LCD lcd_init(LCD_DISP_ON); //initialize display, cursor off sei(); // now enable interrupt, since UART library is interrupt controlled } //########################################################################################## touch read_senskey void read_senskey( void ) { uint16_t i = MAX_CYCLE; uint8_t a, b, x, y; ATOMIC_BLOCK(ATOMIC_FORCEON) { a = DDRE & ~(SK_A012 | SK_B012); b = PORTE & ~(SK_A012 | SK_B012); y = SK_B012; // input mask do { DDRF = a; // tristate PORTF = b | SK_B012; DDRF = a | SK_B012; // Bx = strong high DDRF = a; // tristate PORTF = b; DDRF = a | SK_A012; // Ax = strong low if( --i == 0 ) break; // timeout x = y & PINF; // not immediately after set DDRA !!! if( x ) { if( x & SK_B0 ) keys[2] = i; if( x & SK_B1 ) keys[1] = i; if( x & SK_B2 ) keys[0] = i; } y ^= x; // clear processed input }while( y ); // all inputs done DDRF = a | SK_A012 | SK_B012; // discharge } } //########################################################################################## uint8_t read_senskey_0( void ) {if( keys[0] >= 1565 ) return 1; else return 0;} //########################################################################################## uint8_t read_senskey_1( void ) {if( keys[1] >= 1354 ) return 1; else return 0;} //########################################################################################## uint8_t read_senskey_2( void ) {if( keys[2] >= 1497 ) return 1; else return 0;} //########################################################################################## /*#include #include "mydefs.h" #define MAX_CYCLE 1000 #define SENSKEY_A0 SBIT( PORTB, 0 ) // connect to senspad #define SENSKEY_A0_DDR SBIT( DDRB, 0 ) #define SENSKEY_B0 SBIT( PORTB, 1 ) // connect to cap to A0 #define SENSKEY_B0_DDR SBIT( DDRB, 1 ) #define SENSKEY_B0_PIN SBIT( PINB, 1 ) // A0 B0 B0_PIN // 0 0 full discharge // loop: // Z Z gap // Z 1 partial charge over pin A0 and senspad // Z Z gab // 0 Z partial charge over pin B0 only // 0 Z 1 end uint16_t read_senskey( void ) { uint16_t i = MAX_CYCLE + 1; SENSKEY_A0 = 0; SENSKEY_A0_DDR = 1; SENSKEY_B0 = 0; SENSKEY_B0_DDR = 1; // discharge cap ATOMIC_BLOCK(ATOMIC_FORCEON){ do{ SENSKEY_A0_DDR = 0; // A0 = tristate SENSKEY_B0 = 1; // B0 = weak high SENSKEY_B0_DDR = 1; // B0 = strong high SENSKEY_B0_DDR = 0; // B0 = weak high SENSKEY_B0 = 0; // B0 = tristate SENSKEY_A0_DDR = 1; // A0 = strong low if( --i == 0 ) break; // timeout }while( SENSKEY_B0_PIN == 0 ); // until charged } SENSKEY_B0_DDR = 1; // discharge cap return MAX_CYCLE - i; // big value = key touched or timeout } */ //########################################################################################## Timer in Millisekunden ISR(SIG_OVERFLOW0) { TCNT0 = 5; // Zählregister auf 5 setzen --> 200 schritte bis Überlauf // routine wird aller 1msec ausgeführt und zählt variablen runter //#################### timer für Echtzeitablauf timer_Taster--; if (timer_Taster <0) {timer_Taster =0;} // timer } //########################################################################################## Hauptprogramm //########################################################################################## int main(void) { initialisierung(); while(1) { //#################### while if (timer_Taster <=0) { timer_Taster=200; //### sense taster messen cli(); read_senskey(); sei(); //### sense taster auswerten LED -= read_senskey_0(); if(LED<0) LED =0; LED += read_senskey_2(); if(LED>8) LED =8; if( read_senskey_1()==1) { if (LED >4) LED = 0; else if (LED <4) LED = 8; } //### update LEDs for (int i=0;i