Hallo, zuerst danke an alle die für diese Web Seite verantwörtlich sind. ich bin neu im Gebiet der uC. Ich habe schon einige Tutorials durchgearbeitet und "spiele" selber noch mit dem MyAVR-Board mit einem Mega8 ein bischen herum. Meine Frage. Ich will z.B. ein 0-2,3V Spannungssignal auf einem PinF0 von atmega128 einlesen und in ein digital 10-stellige Wert umwandeln. Der Wert muss dann über UART ausgegeben werden. Ich benutze dazu fast 1:1 das Tutorial. weil mein RefSpannung:3,2v ist habe ich mein analog eingang direkt ohne Transtor an PinF0 angeschlossen. Was mich da wundert, Für den gleichen analog Werte sind meine Rückgabewerten unterschiedliche. Ich denke brauche also, dass mein Fehler bei der Ermittlung des Mittelwert liegt. Wie koregiere ich das? Danke im Voraus.
1 | #include <avr/io.h> |
2 | #include <inttypes.h> |
3 | #include <stdlib.h> |
4 | #include <avr/interrupt.h> |
5 | #include <string.h> |
6 | |
7 | #include "uart.h" |
8 | #include "adc.h" |
9 | |
10 | // define global variables
|
11 | int ADIteiler=0; |
12 | char s[20]; |
13 | |
14 | void Adc_portinit(void) //Initialsierung |
15 | {
|
16 | DDRF = 0x00; // define as Input |
17 | PORTF = 0x00; //enables Pull-Up on PC0 |
18 | }//end Adc_portinit() |
19 | |
20 | unsigned int ADC_Read(unsigned char Mux_channel) |
21 | {
|
22 | uint8_t i; |
23 | uint16_t result = 0; |
24 | |
25 | ADCSRA = (1<<ADEN); //enables the ADC |
26 | ADCSRA |= (1<<ADPS2) | (1<<ADPS0); //determine the division factor between the XTAL frequency and the input clock to the ADC. |
27 | //ADC division factor auf 8 setzen Tmin = 20 , Tmax = 80 systemt clock F_CPU = 4000000
|
28 | |
29 | ADMUX = Mux_channel; // Analog Channel Selection |
30 | ADMUX |= (1<<REFS0) | (1<<REFS1);//int. REF Voltage Reference Selection |
31 | ADMUX |= (0<<ADLAR); //the result is right adjusted |
32 | |
33 | |
34 | |
35 | ADCSRA |= (1<<ADSC);// The ADC initialization and made a dummy readout |
36 | while(ADCSRA & (1<<ADSC)); |
37 | result = ADCW; // ADCW must be read once |
38 | result = 0;// otherwise the result of the next transformation is not taken over |
39 | |
40 | |
41 | //Now reads 4 times the similar tension and selected channel channel
|
42 | //and then calculate the average value divided by 4. Because the ADC measeres 0..1023,
|
43 | //and the Port however can only 0..255 show
|
44 | for(i=0; i<4; i++) |
45 | {
|
46 | ADCSRA |= (1<<ADSC); //a single conversion |
47 | |
48 | while(ADCSRA & (1<<ADSC));//waiting on conversion closing |
49 | |
50 | result += ADCW; |
51 | }
|
52 | |
53 | ADCSRA &= (0<<ADEN); // ADC reanables |
54 | |
55 | result /= 4; |
56 | |
57 | return result; |
58 | }//end int ADC_Read() |
59 | |
60 | void Send_adcValue(char* ADC_Value, int Length) |
61 | {
|
62 | Adc_portinit(); |
63 | |
64 | if(strcmp(ADC_Value, "adc") == 0) |
65 | {
|
66 | uart0_puts ("\r\n"); |
67 | uart0_puts ("Messwert: "); |
68 | unsigned int result = ADC_Read(0);// Reading the Analog on PinF 0, |
69 | // Thus ADF0. In result steht das Ergebnis.
|
70 | utoa( result, s, 10 ); // convert HEX-Zahlen in Ascii |
71 | uart0_puts( s ); |
72 | }
|
73 | else
|
74 | uart0_puts("error"); |
75 | }//Send_adcValue() |