Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <avr/interrupt.h>
- //****************Beginn Deklarationen und Definitionen f?r LCD*******************
- //Signale f?r das LCD an PortC
- #define RS 6 // 0= Kommando, 1= Datenwert
- #define RW 5 // 0= Schreiben ins Display, 1= Lesen vom Display
- #define E 4 // enable, active high
- //Funktionen f?r LCD
- void LCDInit();
- void LCDClearDisplay();
- void LCDSetCursor(uint8_t, uint8_t);
- void LCDStringOut(char *);
- //Hilfsfunktionen, nicht zur eigenen Verwendung vorgesehen
- uint8_t LCDGetCom();
- void LCDDataOut(char);
- void e_active();
- void LCDComOut(uint8_t);
- void delay_cycles(unsigned int zyklen);
- void delay_ms(unsigned int ms);
- //****************Ende Deklarationen und Definitionen f?r LCD*********************
- unsigned int getAnalogValue(unsigned char channel)
- {
- //ADMUX ADCMultiplex Selection Register -- Kanalauswahl
- //Bit 0 1 2 legen den Kanal fest. bsp. Kanal.5 = Bit 101
- //ADCRSA ADC Control and Status Register
- //Steuerung und Konfiguration des ADC
- ADMUX=channel & 0x07;
- ADCSRA |= (1 << ADEN) //ADC Enable -> Bit 7 - An - Aus Schalter vom ADC (wenn noch eine Bearbeitung läuft wird sie beendet und gelöscht)
- | (1 << ADSC) //ADC Conversion -> Bit 6 -Starte Umwandung
- | (1 << ADPS2) //ADC Prescaler Select Bits -> Bit 2 - (Vorteiler) Prescaler einstellen nach Taktfrequenz -> Register
- | (1 << ADPS1) //ADC Prescaler Select Bits -> Bit 1 -(Vorteiler) Prescaler einstellen nach Taktfrequenz -> Register
- | (1 << ADPS0);
- //ADPS0 - ADPS1 legen Fest welcher Vorteiler für die Taktfrequenz verwendet wird
- //In unserem Falls Bit2=1 Bit1=1 Bit0=1 der Vorteiler ist hier 128
- //Siehe Seite 222
- //Bei einer µc-Taktfrequenz von 16 MHz -> 16MHz / 64 = 125 KHz
- while((ADCSRA & (1 << ADSC)) != 0); //Wartet bis ADSC auf 0 gesetzt ist. Wartet bis Umwandlung fertig ist
- return ADC; //ADC Data Register enthält den Umgewandelten Dual Wert als 10 Bit Zahl.
- }
- int main(void)
- {
- DDRD = 0x00;
- DDRC = 0xFF;
- //Timer1 init.
- OCR1A = 62500;
- TIMSK|= (1 << OCIE1A);
- TCCR1B|= (1 << WGM12);
- TCCR1B|= (1<<CS12);
- sei();
- while(1);
- }
- ISR(TIMER1_COMPA_vect)
- {
- PORTC = ~PORTC;
- }
- //*****************Beginn Implementierung der LCD-Funktionen**************
- //Initialisierung des LCDs im 2 Zeilen-Modus, blinkender Cursor, schreiben nach rechts
- void LCDInit()
- {
- delay_cycles(2);
- DDRC=0x7f;
- delay_cycles(17400);
- PORTC=0x03;
- e_active();
- delay_cycles(4700);
- e_active();
- delay_cycles(150);
- e_active();
- PORTC=0x02;
- e_active();
- LCDComOut(0x28);
- LCDComOut(0x0F);
- LCDComOut(0x01);
- LCDComOut(0x06);
- //delay_cycles(40000);
- }
- //L?scht alle Zeichen im Display
- void LCDClearDisplay()
- {
- LCDComOut(0x01);
- }
- //Setzt den Cursor: 2 Zeilen mit jeweils 16 Stellen sind verf?gbar
- void LCDSetCursor(uint8_t zeile, uint8_t spalte)
- {
- uint8_t wert = 0;
- if((zeile < 2) && (zeile >=0) && (spalte >=0) && (spalte < 16))
- wert=zeile*64+spalte;
- wert=wert | 0x80;
- LCDComOut(wert);
- }
- //Gibt einen String an der Position des Cursors aus.
- void LCDStringOut(char *pbuf)
- {
- while( *pbuf != '\0')
- {
- LCDDataOut(*pbuf);
- pbuf++;
- }
- }
- //Hilfsfunktion: Befehl zum LCD ?bertragen
- void LCDComOut(uint8_t data)
- {
- uint8_t wert;
- do
- {
- wert=LCDGetCom();
- }
- while((wert & 0x80) != 0);
- DDRC=0x7F;
- wert=data & 0xF0;
- wert=wert >> 4;
- PORTC=wert;
- e_active();
- wert=data & 0x0F;
- PORTC=wert;
- e_active();
- }
- //Hilfsfunktion: Daten vom LCD lesen im 4-Bit-Modus
- uint8_t LCDGetCom()
- {
- uint8_t wert,wert2,oldddrc;
- oldddrc=DDRC;
- DDRC=0x70;
- PORTC=0x20;
- delay_cycles(2);
- PORTC |= (1 << E);
- delay_cycles(2);
- wert=PINC;
- wert = wert & 0x0F;
- PORTC &= ~(1 << E);
- delay_cycles(2);
- PORTC |= (1 << E);
- delay_cycles(2);
- wert2=PINC;
- wert2=wert2 & 0x0F;
- wert = wert << 4;
- wert = wert | wert2;
- PORTC &= ~(1 << E);
- DDRC=oldddrc;
- return wert;
- }
- //Hilfsfunktion: (Anzeige-)Daten zum LCD ?bertragen im 4-Bit-Modus.
- void LCDDataOut(char data)
- {
- uint8_t wert;
- do
- {
- wert=LCDGetCom();
- }
- while((wert & 0x80) != 0);
- DDRC=0x7F;
- wert=data >> 4;
- wert=wert | (1 << RS) ;
- PORTC=wert;
- e_active();
- wert=data & 0x0F;
- wert=wert | (1 << RS);
- PORTC=wert;
- e_active();
- }
- //Hilfsfunktion: Realisiert Timing der Steuerleitung E f?r Daten?bertragung
- void e_active()
- {
- delay_cycles(2);
- PORTC |= (1<<E);
- delay_cycles(2);
- PORTC &= ~(1 << E);
- delay_cycles(2);
- }
- //Hilfsfunktion: Zeitschleife - Wartet bestimmte Anzahl von Zyklen.
- void delay_cycles(unsigned int zyklen)
- {
- asm volatile("L_1:" "\tSBIW R24,0x00\n"
- "\tNOP\n"
- "\tBREQ L_2\n"
- "\tSBIW R24,0x01\n"
- "\tRJMP L_1\n"
- "L_2:\t\n"
- ::);
- }
- //Hilfsfunktion: Zeitschleife - Wartet bestimmte Anzahl an Millisekunden
- void delay_ms(unsigned int ms)
- {
- unsigned char f_cpu_MHz = 16;
- //f_cpu in MHz -> f_cpu:MHz*1000 Zyklen pro ms
- //Laufzeitkorrektor
- ms = ms/8;
- //Ausf?hrungszeit = 1ms * ms
- for(int j = 0; j < ms; j++)
- {
- //Ausf?hrungszeit = 1 ms
- delay_cycles(f_cpu_MHz*1000);
- }
- }
- //*****************Ende Implementierung der LCD-Funktionen**************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement