Hi zusammen,
Ich sitze seit längerem daran, von einem Poti eine Spannung einzulesen,
die Spannung soll dann umgerechnet werden zum Verdrehwinkel des Potis
und über eine 7Segmentanziege an PortA ausgegeben werden.
Um den Nullwinkel ändern zu können, kann dieser über einen Taster in den
EEPROM speicher gespeichert werden.
Das Problem was ich habe ist, dass der ADC nicht arbeitet
Ich verwede den ADC8 (PinB5)als Eingang und AVCC als Referenzspannung
(5V über LC-Netzwerk (s.Datenblatt Seite106)
1 | #define F_CPU 1000000UL
|
2 |
|
3 | // Pin def
|
4 | #define LED PINB2
|
5 | #define caliper PINB6
|
6 | #define decade PINB1
|
7 | #define unitPosition PINB0
|
8 |
|
9 | //7-Segment Anzeige def
|
10 | #define one 0b11101011
|
11 | #define two 0b00001100
|
12 | #define three 0b10001010
|
13 | #define four 0b00101011
|
14 | #define five 0b00011010
|
15 | #define six 0b00011000
|
16 | #define seven 0b11001011
|
17 | #define eight 0b0001000
|
18 | #define nine 0b0001010
|
19 | #define zero 0b0101000
|
20 |
|
21 | #include <avr/io.h>
|
22 | #include <avr/eeprom.h>
|
23 | #include <util/delay.h>
|
24 | #include <stdint.h>
|
25 |
|
26 | uint8_t eeOffset EEMEM = 253;
|
27 |
|
28 | void int_ADC ()
|
29 | {
|
30 | ADMUX |=(1<<MUX2);// ADC 8
|
31 |
|
32 | ADCSR = (1<<ADPS1) | (1<<ADPS2);
|
33 | ADCSR |= (1<<ADEN);
|
34 |
|
35 | ADCSR |= (1<<ADSC);
|
36 | while (ADCSR & (1<<ADSC) ) { }
|
37 |
|
38 | (void) ADC;
|
39 | }
|
40 |
|
41 | uint16_t ADC_Read()
|
42 | {
|
43 | ADCSR |= (1<<ADSC);
|
44 | while (ADCSR & (1<<ADSC) ) { }
|
45 | return ADC;
|
46 | }
|
47 |
|
48 | uint16_t readADC ()
|
49 | { uint32_t x = 0;
|
50 | uint32_t arrange=0;
|
51 | for(int i=0; i<=5;i++)
|
52 | {
|
53 | x+=ADC_Read();
|
54 | }
|
55 | arrange = x/5;
|
56 | return arrange;
|
57 | }
|
58 | void Offset () //Offsetmessung für Verdrehwinkel
|
59 | { uint16_t offsetread;
|
60 | uint8_t offset; //muss globale Variable werden
|
61 | if (!(PINB & (1<<caliper)))
|
62 | {
|
63 | _delay_ms(3000); //3sec min. gedrückt
|
64 | if (!(PINB & (1<<caliper)))
|
65 | {
|
66 | offsetread=readADC();
|
67 | offset = offsetread/4; // damit offset 1byt
|
68 | eeprom_write_byte(&eeOffset, offset);
|
69 | for(int i=0;i<=3;i++) //LED blinken
|
70 | {
|
71 | PORTB |= (1 << LED); // Hiermit wird ein Bit in x gesetzt
|
72 | _delay_ms(250);
|
73 | PORTB &= ~(1 << LED); // Hiermit wird ein Bit in x geloescht
|
74 | _delay_ms(250);
|
75 | }
|
76 | }
|
77 | }
|
78 | }
|
79 | void display (uint8_t number, uint8_t position)
|
80 | {
|
81 | switch(number)
|
82 | {
|
83 | case 1:
|
84 | PORTA = one;
|
85 | break;
|
86 | case 2:
|
87 | PORTA = two;
|
88 | break;
|
89 | case 3:
|
90 | PORTA = three;
|
91 | break;
|
92 | case 4:
|
93 | PORTA = four;
|
94 | break;
|
95 | case 5:
|
96 | PORTA = five;
|
97 | break;
|
98 | case 6:
|
99 | PORTA = six;
|
100 | break;
|
101 | case 7:
|
102 | PORTA= seven;
|
103 | break;
|
104 | case 8:
|
105 | PORTA = eight;
|
106 | break;
|
107 | case 9:
|
108 | PORTA = nine;
|
109 | break;
|
110 | case 0:
|
111 | PORTA = zero;
|
112 | break;
|
113 | default:
|
114 | PORTA = 0b01000000;
|
115 | break;
|
116 | }
|
117 | if (position==10)
|
118 | {
|
119 | PORTB |= (1 << decade);
|
120 | _delay_ms(10);
|
121 | PORTB &= ~(1 << decade);
|
122 | }
|
123 | if (position==1)
|
124 | {
|
125 | PORTB |= (1 << unitPosition);
|
126 | _delay_ms(10);
|
127 | PORTB &= ~(1 << unitPosition);
|
128 | }
|
129 | }
|
130 |
|
131 | int main(void)
|
132 | {
|
133 | uint16_t offset = 0;
|
134 | uint16_t torsionAngle=0;
|
135 | const float scaling=270/1024;
|
136 |
|
137 | int_ADC ();
|
138 |
|
139 | //Config PORTS
|
140 | DDRA=0xFF;
|
141 | DDRB = (1<<LED) | (1<<decade) | (1<<unitPosition); //Pins als Ausgang
|
142 | DDRB &= ~(1 << caliper);
|
143 | PORTB |= (1<<caliper);
|
144 |
|
145 | offset = eeprom_read_byte(&eeOffset); //Offset aus EEPROM lesen
|
146 | offset = 4*offset;
|
147 |
|
148 | while(1)
|
149 | {
|
150 | if (!(PINB & (1<<caliper)))
|
151 | {
|
152 | Offset();
|
153 | offset = eeprom_read_byte (&eeOffset);
|
154 | offset = 4*offset;
|
155 | }
|
156 |
|
157 | torsionAngle=ADC_Read();
|
158 | if (torsionAngle<offset) //minus Anzeige über LED
|
159 | PORTB = (1<<LED);
|
160 | //7Segment Zahlen ermitteln
|
161 | torsionAngle=abs((torsionAngle-offset));
|
162 |
|
163 |
|
164 | uint16_t angle = torsionAngle * scaling;//Umrechnung auf den Winkel
|
165 |
|
166 | uint16_t unitPositionNumber = angle % 10;
|
167 | uint16_t decadeNumber = (angle - unitPositionNumber)/10;
|
168 |
|
169 | //7Segment schalten/multiplexen
|
170 | for (int i=0; i<=5; i++)
|
171 | {
|
172 | display(unitPositionNumber,1);
|
173 | display(decadeNumber,10);
|
174 |
|
175 | }
|
176 | }
|
177 | }
|
Mein Problem liegt darin, dass es zu keiner Änderung der 7Segmentanzeige
kommt, auch nach Internet suche, konnte ich leider nichts finden, woran
es liegen könnte
Vielen Dank im Voraus
MfG Adrian