#include #include #include //#include #include #include //#include "register.c" #include "oldmacros.h" #include "adc_ex.h" #include typedef unsigned char u08; typedef unsigned short u16; typedef unsigned long u32; typedef short s16; //defines #define linksb 1 #define rechtsb 2 #define ExtRHR 0x00 #define ExtTHR 0x00 #define ExtIER 0x01 #define ExtFCR 0x02 #define ExtISR 0x02 #define ExtLCR 0x03 #define ExtMCR 0x04 #define ExtLSR 0x05 #define ExtMSR 0x06 #define ExtSPR 0x07 #define ExtDLL 0x00 #define ExtDLM 0x01 #define enter (!( PINB & ( 1< 0) data = (data & 0x1f) | 0x40 | 0x80; else data = (data & 0x1f) | 0x80; } Check_LCD_Busy_Flag(); // Hier alles auf Default outp(0x40, PORTC); // Hier wird der LCD_LE1&2 hochgezogen if (enlcdctl & 0x01) outp(0xC0, PORTC); if (enlcdctl & 0x02) outp(0x40, PORTD); outp(0xff, DDRA); outp(data, PORTA); // Hier wieder runter outp(0x40, PORTC); outp(0x00, PORTD); outp(0x00, DDRA); } void Write_LCD_Data (u08 data) { Check_LCD_Busy_Flag(); // Hier alles auf Default outp(0x50, PORTC); // Hier wird der LCD_LE1 oder 2 hochgezogen if (enlcdctl & 0x01) outp(0xD0, PORTC); if (enlcdctl & 0x02) outp(0x40, PORTD); outp(0xff, DDRA); outp(data, PORTA); // Hier wieder runter outp(0x50, PORTC); outp(0x00, PORTD); outp(0x00, DDRA); } u08 Read_LCD_Command (u08 nr) { // Wird nur für Busy Flag verwendet u08 data; // Hier alles auf Default outp(0x60, PORTC); // Hier wird der LCD_LE1 oder 2 hochgezogen if (!nr) outp(0xE0, PORTC); //LCDEN1 else outp(0x40, PORTD); //LCDEN2 outp(0x00, DDRA); data = inp(PINA); // Hier wieder runter outp(0x60, PORTC); outp(0x00, PORTD); return(data); } void Weiter(void) { Write_Text("Weiter", 18, 4, linksb, 6); Change_Cursor(24, 4, 0x03); } void Write_Leds(u08 data) { outp( 0x60, PORTC); //RW auf Read outp( 0xff, DDRA); //PORTA als Ausgang outp( 0x80, PORTD); if (data == 0) { outp (0x00, TCCR0); } else { outp (led, TCCR0); } outp( data, PORTA); //Daten anlegen outp( 0x00, PORTD); //LE runter //outp( 0x00, DDRA); //PORTA als Eingang } void Check_LCD_Busy_Flag (void) { volatile u08 flag = 0xff; if (enlcdctl & 0x01) { while ((flag & 0x80) != 0) { flag = Read_LCD_Command(0); // Wir checken Busy Flag (Controller 1) } } flag = 0xff; if (enlcdctl & 0x02) { while ((flag & 0x80) != 0) { flag = Read_LCD_Command(1); // Wir checken Busy Flag (controller 2) } } } void Wait (u16 delay) { //delay = 1 : entspricht ca. 750ns u16 i; for (i = 0; i < delay; i ++) i = i; } void Init_LCD (void) { Wait(65000); //Wait longer than 15ms Write_LCD_Init(0x39); Wait(6000); //Wait longer than 4.1ms Write_LCD_Init(0x39); Wait(200); //Wait longer than 100us Write_LCD_Init(0x39); Write_LCD_Command(0x39); //8bit Interface, 4 Lines, 24 Chars Write_LCD_Command(0x08); //Dispay off Write_LCD_Command(0x01); //Clear Display, Set DisplayRamAdr to 0 Write_LCD_Command(0x06); //EntryMode: off,inc Write_LCD_Command(0x0C); //Display on } void Init_Ports (void) { outp (0x00, DDRA); //PortA bidirektional für Datenleitungen outp (0xff, PORTB); //PortB 4-7 als Ausgang für Tasten outp (0xf0, DDRB); outp (0x40, PORTC); //PortC als Ausgang outp (0xFF, DDRC); outp (0x00, PORTD); //PortD7 als Ausgang outp (0xC0, DDRD); // jetzt zusätzlich 0x40 für LCDEN2 } void Change_Cursor(u08 x, u08 y, u08 cursor) { // Cursor ändern: 0x00 = off; 0x02 = on; 0x03 = flashing on Write_LCD_Command (0x80 | ( ( (y - 1) * 32 ) + (x - 1) ) ); //Position ans Diplay schicken cursor = cursor & 0x03; cursor = cursor | 0x0C; Write_LCD_Command(cursor); //ans Display schreiben } void Set_Int_Timer (void) { outp (0x21, TCCR1A); // 8bit PWM on oc1b outp (display, TCCR1B); // Run Timer1 with CK/1024 outp (0x00, OCR1BH); //highTeil auf Null setzen, da nur 8 bit //display outp (0xff, OCR1BL); outp (led, TCCR0); // Run Timer0 with Clk/8 -> OVL nach 277,7777us Leds outp (0x02, TIFR); // Clear Timer0 OVL-Flag outp (0x02, TIMSK); // Enable Timer0 OVL Interrupt } void Clear_Timer (u08 timernr) { u08 i; if (timernr > 4) { for (i = 0; i < 5; i++) { timer[i] = 0; } } timer[timernr] = 0; } void Write_Text(u08 *text, u08 x, u08 y, u08 ausricht, u08 anzahl) { u08 textlaenge = 0, i; u08 *hilfstext; if (!anzahl && !x && !y) { while (*text) Write_LCD_Data(*text++); return; } if (x || y) Write_LCD_Command (0x80 | ( ( (y-1) * 32 ) + (x - 1) ) ); //Position ans Diplay schicken if (!anzahl) anzahl = 24 - x + 1; if (ausricht == linksb) { for (i = 0; i < anzahl; i++) { if (*text) Write_LCD_Data(*text++); else Write_LCD_Data(0x20); } } else { hilfstext = text; while (*hilfstext++) textlaenge++; if (textlaenge > anzahl) textlaenge = anzahl; for (i = 0; i < (anzahl - textlaenge); i++) { Write_LCD_Data(0x20); } while (*text) Write_LCD_Data (*text++); } } void Write_Bar (u08 x, u08 y, u08 value) { //value = 0..255 u08 i, j; u16 spalten; spalten = ((24 - (x - 1)) * 5) + 1; spalten = spalten * value; spalten = spalten / 256; j = (spalten / 5); Write_LCD_Command (0x80 | ( ( (y - 1) * 32 ) + (x - 1) ) ); //Position ans Diplay schicken for (i = 0; (i < j); i++) { Write_LCD_Data (0xFF); } i = spalten % 5; if (i > 0) { Write_LCD_Data (4 - i); j++; } i = 24 - (x - 1) - j; for (j = 0; j < i; j++) { Write_LCD_Data (0x20); } } void itoa (u16 value, u08 *text, u08 radix) { u08 temp, hilfstext[24], i = 0; if (value == 0) *text++ = 0x30; else { while (value > 0) { temp = (value % radix); hilfstext[i] = (temp + 0x30); value = value / radix; i++; } for (temp = 0; temp < i; temp++) { *text++ = hilfstext[i - temp - 1]; } } *text = 0x00; } void ltoa (u32 value, u08 *text, u08 radix) { u08 temp, hilfstext[24], i = 0; if (value == 0) *text++ = 0x30; else { while (value > 0) { temp = (value % radix); hilfstext[i] = (temp + 0x30); value = value / radix; i++; } for (temp = 0; temp < i; temp++) { *text++ = hilfstext[i - temp - 1]; } } *text = 0x00; } u08 Check_Keys (void) { // gibt 0: für keine Änderung, 1: für LOBO Access, 3: für neue Taste zurück u08 i, temp, newkeys = 0, newdevice = 0, mux = 0xEF; for (i = 0; i < 4; i++) { // ersmal einlesen und zusammenwurschteln outp (mux, PORTB); mux = mux << 1; mux = mux | 0x01; temp = ~inp (PINB); if (i == 0) newkeys = temp & 0x07; if (i == 1) { temp = (temp << 3) & 0x38; newkeys = newkeys | temp; } if (i == 2) newdevice = temp & 0x07; if (i == 3) { temp = (temp << 3) & 0x40; newkeys = newkeys | temp; } } outp (0xFF, PORTB); if (newdevice != device) { dispofftime = 0; // Displayaus Zeit wieder auf null setzen if (newdevice == 0x07) { // LOBO Access device = 0; //newdevice; return (1); } if (!newdevice && devacc) { // Neues Device nur bei mehreren Devices über ext.Uart for (i = 0; i < 3; i++) { //devacces devicepresent[i] = 0; } temp = 0x01; for (i = 0; i < 3; i++) { if ((device & temp) > 0) { reqdevice = i; if (aktdevice == i) devicetype[0] = '\0'; device = newdevice; return (2); } temp = temp << 1; temp = temp & 0xFE; } } device = newdevice; } if (newkeys != keys) { // Neue Taste temp = 0x01; text[0] = '\3'; text[3] = '\r'; text[4] = '\0'; for (i = 0; i < 7; i++) { if ((newkeys & temp) != (keys & temp)) { if ((newkeys & temp) > 0) { text[2] = '1'; } else { text[2] = '0'; } text[1] = (i + 0x30); } temp = temp << 1; temp = temp & 0xFE; } keys = newkeys; dispofftime = 0; return (3); } return (0); } int main(void) { Init_Ports (); Write_Leds(0); Init_LCD (); Set_Int_Timer(); sei (); Write_Leds(leds); Clear_Timer(0xff); InitADCEx(); uint16_t result; DDRB = 0b00001111; //Taster Ausgänge PORTB = 0b00001111; while (1) { Clear_Display; result=ReadADCEx(0); Write_Text(result, 1, 2, linksb, 12); } }