Problem mit UART Ausgabe

Gast #1301919
Lesenswert?
• ▲
▼
Hallo zusammen,

wir haben ein Problem mit der Aussgabe über die UART. Das Problem ist 
unten genauer Beschrieben hier erstmal der komplette Quellcode, darunter 
dann der Ausschnitt der den Fehler verursacht mit Abmerkungen was schieß 
geht.

Wir verwenden Codevision v2.04.2c und einen ATmega168.

Danke schonmal für die Hilfe und Gruß ;-)
1
#include <mega168.h>
2
#include <delay.h>
3
#include <stdlib.h>
4
#include <string.h>
5

6

7
//Globale Variablen und Funktionsprototypen
8
int FileIndex=1000;
9
int CounterT0=0;
10
int CounterT2=0;
11
int iZeitT0=0;
12
char cIST[5], cSOLL[5], cZeitT0[4];
13
void USBinit();
14
void NewFile(int Index, int Typ, int Handle);
15
void ADConvert();
16
void USBWrite(int Handle);
17

18

19
// Alphanumeric LCD Module functions
20
#asm
21
   .equ __lcd_port=0x05 ;PORTB
22
#endasm
23
#include <lcd.h>
24

25
// Standard Input/Output functions
26
#include <stdio.h>
27

28
// Timer 0 overflow interrupt service routine
29
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
30
{
31
// Place your code here
32
CounterT0++;
33
if(CounterT0==60)
34
{
35
iZeitT0++;
36
ADConvert();
37
USBWrite(0);
38
printf("\r\n");
39
CounterT0=0;
40
};
41
}
42

43
// Timer2 overflow interrupt service routine
44
interrupt [TIM2_OVF] void timer2_ovf_isr(void)
45
{
46
// Place your code here
47

48

49
}
50

51
#define ADC_VREF_TYPE 0x00
52

53
// Read the AD conversion result
54
unsigned int read_adc(unsigned char adc_input)
55
{
56
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
57
// Delay needed for the stabilization of the ADC input voltage
58
delay_us(10);
59
// Start the AD conversion
60
ADCSRA|=0x40;
61
// Wait for the AD conversion to complete
62
while ((ADCSRA & 0x10)==0);
63
ADCSRA|=0x10;
64
return ADCW;
65
}
66

67
// Declare your global variables here
68

69
void main(void)
70
{
71
// Declare your local variables here
72

73
// Crystal Oscillator division factor: 1
74
#pragma optsize-
75
CLKPR=0x80;
76
CLKPR=0x00;
77
#ifdef _OPTIMIZE_SIZE_
78
#pragma optsize+
79
#endif
80

81
// Input/Output Ports initialization
82
// Port B initialization
83
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
84
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
85
PORTB=0x00;
86
DDRB=0x00;
87

88
// Port C initialization
89
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
90
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
91
PORTC=0x00;
92
DDRC=0x00;
93

94
// Port D initialization
95
// Func7=Out Func6=Out Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
96
// State7=0 State6=0 State5=T State4=T State3=T State2=T State1=T State0=T 
97
PORTD=0x00;
98
DDRD=0xC0;
99

100
// Timer/Counter 0 initialization
101
// Clock source: System Clock
102
// Clock value: Timer 0 Stopped
103
// Mode: Normal top=FFh
104
// OC0A output: Disconnected
105
// OC0B output: Disconnected
106
TCCR0A=0x00;
107
TCCR0B=0x00;
108
TCNT0=0x00;
109
OCR0A=0x00;
110
OCR0B=0x00;
111

112
// Timer/Counter 1 initialization
113
// Clock source: System Clock
114
// Clock value: Timer1 Stopped
115
// Mode: Normal top=FFFFh
116
// OC1A output: Discon.
117
// OC1B output: Discon.
118
// Noise Canceler: Off
119
// Input Capture on Falling Edge
120
// Timer1 Overflow Interrupt: Off
121
// Input Capture Interrupt: Off
122
// Compare A Match Interrupt: Off
123
// Compare B Match Interrupt: Off
124
TCCR1A=0x00;
125
TCCR1B=0x00;
126
TCNT1H=0x00;
127
TCNT1L=0x00;
128
ICR1H=0x00;
129
ICR1L=0x00;
130
OCR1AH=0x00;
131
OCR1AL=0x00;
132
OCR1BH=0x00;
133
OCR1BL=0x00;
134

135
// Timer/Counter 2 initialization
136
// Clock source: System Clock
137
// Clock value: 31,250 kHz
138
// Mode: Normal top=FFh
139
// OC2A output: Disconnected
140
// OC2B output: Disconnected
141
ASSR=0x00;
142
TCCR2A=0x00;
143
TCCR2B=0x06;
144
TCNT2=0x00;
145
OCR2A=0x00;
146
OCR2B=0x00;
147

148
// External Interrupt(s) initialization
149
// INT0: Off
150
// INT1: Off
151
// Interrupt on any change on pins PCINT0-7: Off
152
// Interrupt on any change on pins PCINT8-14: Off
153
// Interrupt on any change on pins PCINT16-23: Off
154
EICRA=0x00;
155
EIMSK=0x00;
156
PCICR=0x00;
157

158
// Timer/Counter 0 Interrupt(s) initialization
159
TIMSK0=0x01;
160
// Timer/Counter 1 Interrupt(s) initialization
161
TIMSK1=0x00;
162
// Timer/Counter 2 Interrupt(s) initialization
163
TIMSK2=0x01;
164

165
// USART initialization
166
// Communication Parameters: 8 Data, 1 Stop, No Parity
167
// USART Receiver: Off
168
// USART Transmitter: On
169
// USART0 Mode: Asynchronous
170
// USART Baud Rate: 9600
171
UCSR0A=0x00;
172
UCSR0B=0x08;
173
UCSR0C=0x06;
174
UBRR0H=0x00;
175
UBRR0L=0x17;
176

177
// Analog Comparator initialization
178
// Analog Comparator: Off
179
// Analog Comparator Input Capture by Timer/Counter 1: Off
180
ACSR=0x80;
181
ADCSRB=0x00;
182

183
// ADC initialization
184
// ADC Clock frequency: 1000,000 kHz
185
// ADC Voltage Reference: AREF pin
186
// ADC Auto Trigger Source: None
187
// Digital input buffers on ADC0: On, ADC1: On, ADC2: On, ADC3: On
188
// ADC4: On, ADC5: On
189
DIDR0=0x00;
190
ADMUX=ADC_VREF_TYPE & 0xff;
191
ADCSRA=0x83;
192

193
// LCD module initialization
194
lcd_init(20);
195

196
// Global enable interrupts
197
#asm("sei")
198

199

200
TCCR0B=0x03;
201

202
while (1)
203

204
      {
205
      if(PORTD.4==1)
206
      {
207
      delay_ms(100);
208
      USBinit();
209
      };
210
      
211
      if(PORTC.2==1)
212
      {
213
      delay_ms(100);
214
      NewFile(FileIndex,0,0);
215
      NewFile(FileIndex,1,1);
216
      ADConvert();
217
      USBWrite(1);
218
      TCCR0B=0x03;
219
      };
220
      
221
      
222
      
223
      };
224
}
225

226
void USBinit()
227
{
228
printf("U\r");
229
}
230

231
void NewFile(int Index, int Typ, int Handle)         // Funktion zum Anlegen einer neuen TXT-Datei
232
{    
233
if(Typ==0)
234
  {
235
  printf("O %iW>MESS%i.TXT\r",Handle,Index);          // neue TXT-Datei erzeugen -> Tracking
236
  };
237
if(Typ==1)
238
    {
239
  printf("O %iW>ZUSA%i.TXT\r",Handle,Index);          // neue TXT-Datei erzeugen -> Zusammenfassung
240
    };
241
}
242

243
void ADConvert()
244
{
245
int iIST, iSOLL;
246
float fIST, fSOLL;
247
// AD Werte einlesen
248
iIST=read_adc(0);
249
iSOLL=read_adc(1);
250
// Winkel berechnen
251
fIST=((float)iIST)/1023*90;
252
fSOLL=((float)iSOLL)/1023*90;
253
// Winkel in char wandeln
254
ftoa(fIST,2,cIST);
255
ftoa(fSOLL,2,cSOLL);
256
}
257

258
void USBWrite(int Handle)
259
{
260
int Laenge;
261
itoa (iZeitT0, cZeitT0);
262
//Laenge = strlen(cZeitT0) + strlen(cSOLL) + strlen(cIST) + 10;
263
//printf("W%i>%s\r",Handle, Laenge);
264
printf("%s;\r",cZeitT0);
265
printf("\n");
266
printf("%s;\r",cIST);
267
printf("\n");
268
printf("%s\r\n",cSOLL);
269
//printf("C%i\r",Handle);
270
}

die Ausgabe erfolgt in diesem Codeabschnitt
1
void USBWrite(int Handle)
2
{
3
int Laenge;
4
itoa (iZeitT0, cZeitT0);
5
//Laenge = strlen(cZeitT0) + strlen(cSOLL) + strlen(cIST) + 10;
6
//printf("W%i>%s\r",Handle, Laenge);
7
printf("%s;\r",cZeitT0);
8
printf("\n");
9
printf("%s;\r",cIST);
10
printf("\n");
11
printf("%s\r\n",cSOLL);
12
//printf("C%i\r",Handle);
13
}

Erwartet haben wir eine Ausgabe in der Form: (jeweils die Werte der 
Variablen)
cZeitT0;
cIST;
cSOLL

erhalten tun wir allerdings eine wilde Mixture aus allem mit allem 
Kombiniert die dann wie folgt aussieht: (jeweils die Werte der 
Variablen)
cZeitT0;
cISTcISTcZeitT0;
cSOLLcZeitT0
Gast #1301966
Lesenswert?
• ▲
▼
Danke für die schnelle Antwort.
Deine Vermutung hat sich als wahr heraus gestellt wir haben die Array um 
jeweils eine Stelle vergrößert und schon war das Problem gelöst

Vielen herzlichen danke euch beiden und Gruß
APG Team
#1301982
Lesenswert?
• ▲
▼
APG Team schrieb:
> Danke für die schnelle Antwort.
> Deine Vermutung hat sich als wahr heraus gestellt wir haben die Array um
> jeweils eine Stelle vergrößert und schon war das Problem gelöst

Legt alle Arrays zusammen und wandelt erst bei der Ausgabe die 
numerischen Werte in die jeweiligen Texte. Kurz vor der Ausgabe.

float fIST, fSOLL;

müssen dann ebenfalls globale Variablen werden, so wie ihr das bei 
iZeitT0 auch gemacht habt.

* dadurch dass der Textbuffer größer wird, läufst du weniger Gefahr bei 
Programmänderungen in dasselbe Problem noch einmal hineinzulaufen.

* in Summe verbrauchst du weniger Speicher

* mit den Winkeln in numerischer Form kann man dann auch rechnen. Mit 
den Texten kannst du ausser ausgeben nicht viel anfangen.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren