1 | #define F_CPU 4000000L
|
2 | #define BAUD 9600L
|
3 |
|
4 | #include <avr/io.h>
|
5 | #include <util/delay.h>
|
6 | #include <stdint.h>
|
7 | #include <stdlib.h>
|
8 | #include <avr/interrupt.h>
|
9 |
|
10 | //überprüfen ob baudrate mit CPU Frequenz zusammenpasst
|
11 | #define UBRR_VAL ((F_CPU+BAUD * 8)/(BAUD*16)-1) //clever runde
|
12 | #define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) //reale Baudrate
|
13 |
|
14 | #define BAUD_ERROR ((BAUD_REAL*1000)/BAUD-1000) //Fehler in Promille
|
15 |
|
16 | #if ((BAUD_ERROR>10)||(BAUD_ERROR<-10))
|
17 | #error Systematischer Fehler in der Baudrate grösser 1% und damit zu hoch!
|
18 | #endif
|
19 |
|
20 | void initall();
|
21 | int uart_putc(unsigned char c);
|
22 | void uart_puts (char *s);
|
23 | uint16_t ADC_Reading(uint8_t Mux_channel);
|
24 |
|
25 | //globale Variablen definieren
|
26 | int ADIteiler=0;; int u=0; int v=0; int unwichtig;
|
27 | uint8_t result=0;char s[20];
|
28 |
|
29 |
|
30 | //Initialisierung
|
31 | void initall()
|
32 | {
|
33 | //Initialisierung UART-Schnittstelle
|
34 | UBRRH = UBRR_VAL >> 8;
|
35 | UBRRL = UBRR_VAL & 0xFF;
|
36 | UCSRB = (1<<RXEN)|(1<<TXEN); //UART TX einschalten
|
37 | UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0); //Asynchron 8N1
|
38 |
|
39 | //interrupt
|
40 | TCNT0 = 5; //Zählregister setzen
|
41 | TIMSK |= (1<<TOIE0); //damit interrupt auch ausläst wenn überlauf //erreicht wird
|
42 | TCCR0 |= (1<<CS01);
|
43 | TCCR0 |= (1<<CS00);
|
44 | TCCR0 &= ~(1<<CS01);
|
45 | TCCR0 &= ~(1<<CS02); //CPU Takt wird verwendet --> 12MHz
|
46 | TIFR |= (1<<TOV0); //startet interrupt bei jedem überlauf
|
47 | }
|
48 |
|
49 | uint16_t ADC_Reading(uint8_t Mux_channel)
|
50 | {
|
51 | uint8_t i;
|
52 | uint16_t result = 0;
|
53 |
|
54 | //Initialisierung ADC
|
55 | ADCSRA = (1<<ADEN); //Enables the ADC
|
56 | ADCSRA |= (1<<ADPS2) | (1<<ADPS0); //Determine the division factor
|
57 | ADMUX = Mux_channel; //Select pin ADC0 using MUX
|
58 | _delay_us(40);
|
59 | ADMUX |= (1<<REFS0) | (1<<REFS1);//Int. REF Voltage Reference Selection
|
60 | ADMUX |= (0<<ADLAR); //The result is right adjusted
|
61 | ADCSRA |= (1<<ADSC); //Start conversion
|
62 | while(ADCSRA & (1<<ADSC)); //wait until converstion completed
|
63 | result = ADCW; //ADCW must be read once
|
64 | result = 0; //Otherwise the result of the next transformation is not //taken over
|
65 |
|
66 | //Now reads 4 times the similar tension and selected channel channel
|
67 | //And then calculate the average value divided by 4. Because the ADC //measeres 0..1023, And the Port however can only 0..255 show
|
68 | for(i=0; i<4; i++)
|
69 | {
|
70 | ADCSRA |= (1<<ADSC); //A single conversion
|
71 | while(ADCSRA & (1<<ADSC)); //Waiting on conversion closing
|
72 | result += ADCW;
|
73 | }
|
74 | ADCSRA &= (0<<ADEN); // ADC reanables
|
75 | result /= 4;
|
76 | return result;
|
77 | }//End int ADC_Read()
|
78 |
|
79 | //Unterprogramme UART
|
80 | int uart_putc(unsigned char c)
|
81 | {
|
82 | while (!(UCSRA & (1<<UDRE))){} //warten bis Senden moeglich
|
83 | UDR = c; //sende Zeichen
|
84 | return 0;
|
85 | }
|
86 |
|
87 | void uart_puts (char *s)
|
88 | {
|
89 | while (*s)
|
90 | { // so lange *s != '\0' also ungleich dem "String-Endezeichen"
|
91 | uart_putc(*s); //schreibt die einzelne Ziffer auf die UART
|
92 | s++;//geht zur (falls vorhandenen) zweiten Ziffer des HEX-Wertes
|
93 | }
|
94 | }
|
95 |
|
96 | //Interrupt Service Routine für AD Wandlung
|
97 | ISR(TIMER0_OVF_vect)
|
98 | {
|
99 | ADIteiler++; //zählvariable um den interrupt auf jede ms zu takten
|
100 | if(ADIteiler >= 60) //Zählschleife für alle ms 60
|
101 | {
|
102 | ADIteiler=0; //Teiler wieder auf 0 setzen
|
103 | uint16_t result = ADC_Reading(0);//Reading the Analog voltage PinC 0
|
104 | uart_puts("\r\n");
|
105 | utoa( result, s, 2 ); //HEX-Zahlen in Ascii umwandeln
|
106 | uart_puts( s );
|
107 | uart_putc('$');
|
108 |
|
109 | DDRB = 0xFF; //define as output
|
110 | PORTB = (result / 10) + 1;
|
111 | }
|
112 | }
|
113 | int main(void)
|
114 | {
|
115 | initall();
|
116 |
|
117 | sei(); //Globale Interrupts freischalten
|
118 | while (1) {;}
|
119 | }
|