Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

getchar function on codevision AVR

Status
Not open for further replies.

hasmibaih

Junior Member level 2
Junior Member level 2
Joined
Dec 7, 2013
Messages
23
Helped
1
Reputation
2
Reaction score
0
Trophy points
8
Visit site
Activity points
286
Hi,
i confuse about getchar() function
i had a case that detect a sentence not a character which we type in hyperterminal and the show it on lcd of mikrokontroller on CodeVision AVR?
this is my program, but it's false, make it correct sir, thanks


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void main(void)
{
 
int i;
char temp;
lcd_init(16);
 
while (1)
      {
      // Place your code here
           while(getchar()!='\n')
            {temp[i]=getchar();
            lcd_putsf(temp[i]);  
            i++;}  
                      
      }
}

 
Last edited by a moderator:

Re: getchar function on code vision AVR

The first call of getchar() in the while test will get the next character but it is then lost since it is not stored. Then the next character is input. This loop will discard every other character until a new line is found. Is this what you see? Also, if the new line is input and stored in temp[], it will be skipped since it is not tested. Finally, temp is not an array. Remove the index from temp. Try something like:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void main(void)
{
 int i;
char temp;
lcd_init(16);
 
while (1)
      {
      // Place your code here
i = 0;
           while((temp = getchar()) != '\n')
            {lcd_init[i]=temp;
i++;}  
 lcd_putsf(lcd_init);  
 
      }
}

 
Last edited by a moderator:

Re: getchar function on code vision AVR

i got a warning sir, what must i do?

warning.PNG
 

this is sir,

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.3 Standard
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
[url]http://www.hpinfotech.com[/url]
 
Project : 
Version : 
Date    : 1/10/2014
Author  : hasmi baih
Company : zzz
Comments: 
 
 
Chip type               : ATmega162
Program type            : Application
AVR Core Clock frequency: 11.059200 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/
 
#include <mega162.h>
#include <delay.h>
 
// Alphanumeric LCD functions
#include <alcd.h>
 
#ifndef RXB8
#define RXB8 1
#endif
 
#ifndef TXB8
#define TXB8 0
#endif
 
#ifndef UPE
#define UPE 2
#endif
 
#ifndef DOR
#define DOR 3
#endif
 
#ifndef FE
#define FE 4
#endif
 
#ifndef UDRE
#define UDRE 5
#endif
 
#ifndef RXC
#define RXC 7
#endif
 
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
 
// Get a character from the USART1 Receiver
#pragma used+
char getchar1(void)
{
char status,data;
while (1)
      {
      while (((status=UCSR1A) & RX_COMPLETE)==0);
      data=UDR1;
      if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
         return data;
      }
}
#pragma used-
 
// Write a character to the USART1 Transmitter
#pragma used+
void putchar1(char c)
{
while ((UCSR1A & DATA_REGISTER_EMPTY)==0);
UDR1=c;
}
#pragma used-
 
// Standard Input/Output functions
#include <stdio.h>
 
// Declare your global variables here
 
int i;
char temp;
 
 
               
void main(void)
{
// Declare your local variables here
 
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
 
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTA=0x00;
DDRA=0x00;
 
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;
 
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;
 
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;
 
// Port E initialization
// Func2=In Func1=In Func0=In 
// State2=T State1=T State0=T 
PORTE=0x00;
DDRE=0x00;
 
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
 
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
 
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
 
// Timer/Counter 3 initialization
// Clock value: Timer3 Stopped
// Mode: Normal top=0xFFFF
// OC3A output: Discon.
// OC3B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR3A=0x00;
TCCR3B=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
 
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
// Interrupt on any change on pins PCINT0-7: Off
// Interrupt on any change on pins PCINT8-15: Off
MCUCR=0x00;
EMCUCR=0x00;
 
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
 
ETIMSK=0x00;
 
// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: On
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud Rate: 115200
UCSR0A=0x00;
UCSR0B=0x18;
UCSR0C=0x86;
UBRR0H=0x00;
UBRR0L=0x05;
 
// USART1 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART1 Receiver: On
// USART1 Transmitter: On
// USART1 Mode: Asynchronous
// USART1 Baud Rate: 115200
UCSR1A=0x00;
UCSR1B=0x18;
UCSR1C=0x86;
UBRR1H=0x00;
UBRR1L=0x05;
 
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
 
// SPI initialization
// SPI disabled
SPCR=0x00;
 
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTC Bit 0
// RD - PORTC Bit 1
// EN - PORTC Bit 2
// D4 - PORTC Bit 4
// D5 - PORTC Bit 5
// D6 - PORTC Bit 6
// D7 - PORTC Bit 7
// Characters/line: 16
 
lcd_init(16);
 
 
 
while (1)
      {
      // Place your code here
          i = 0;
           while((temp = getchar()) != '\n')
           {lcd_init[i]=temp;
            i++;}           
            lcd_putsf(lcd_init);    
                      
      }
}



where is my wrong, sir ?
 
Last edited by a moderator:

As mentioned above, seems as was not closed the structure...


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
while (1)
      {
      // Place your code here
          i = 0;
           while((temp = getchar()) != '\n')
           {lcd_init[i]=temp;
            i++;}                                   //         what´s the purpose of this "}" here ?
            lcd_putsf(lcd_init);    
                      
      }




+++
 

i'm confused sir
correct my code and give explanation please
 

As first attempt, simply remove "}" symbol highlighted at code shown above.


Now...

Code C - [expand]
1
i++;}


...after

Code C - [expand]
1
i++;



+++
 

Re: getchar function on code vision AVR

My previous example is a mess. Try something like this to get started:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void main(void)
{
 int i;
char temp[16];
 
lcd_init(16);
 
while (1)
      {
      // Place your code here
i = 0;
while((temp[i] = getchar()) != '\n')
{
i++;
};
i++;
temp[i] = '\0';  
lcd_putsf(temp);  
}

 

i got same mistake sir

=> function argument #1 of type 'unsigned char [16] is incompatible with required parameter of type 'flash unsigned char*'

help me please
 

Change "unsigned char" to "flash unsigned char". This should solve the type incompatibility.

I think you should investigate why the "flash" type is required and what it means. I have no AVR experience and do not know what this means. If it means to put the variable in flash memory, this could cause a problem and is probably not what you want.

Addition: Looks like you want to use lcd_puts() to write an "unsigned char temp[]" variable to the LCD. The function lcd_putsf() is for writing constants stored in flash memory like "flash unsigned char xyz["Enter: "]". Many applications would use both since displays are often combinations of constant strings (stored in flash memory) and variable strings (stored in RAM). BTW adding the "\0" onto the end of temp[] is to make a correctly terminated string. lcd_puts() will need this to know when to stop writing. Creating a string in flash will automagically add "\0" on the end.
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top