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.

Why I am getting errors when Compiling this Code?

Status
Not open for further replies.
Advanced Member level 6
Joined
Dec 4, 2012
Messages
4,280
Helped
822
Reputation
1,654
Reaction score
791
Trophy points
1,393
Location
Bangalore, India
Activity points
0
Why I am getting errors when Compiling this Code?

Atmel Studio 6.1 Code. ATMega128, 8 MHz

Project files + Proteus file attached.


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
/*
 * lcd4bit.c
 *
 * Created: 5/7/2013 9:09:11 AM
 *  Author: Jayanth Devarayanadurga
 */ 
 
 
#include <avr/io.h>
#include <util/delay.h>
 
//Mention Clock frequency here
#define _XTAL_FREQ 8000000
 
//Define CPU Frequency
//This must be defined, if __delay_ms() or
//__delay_us() functions are used in the code
#define _XTAL_FREQ  8000000
 
#define LCD_RS PINA0
#define LCD_EN PINA5
#define LCD_D4 PINB4
#define LCD_D5 PINB6
#define LCD_D6 PINC7
#define LCD_D7 PIND4
 
#define _LCD_FIRST_ROW          0x80     //Move cursor to the 1st row
#define _LCD_SECOND_ROW         0xC0     //Move cursor to the 2nd row
#define _LCD_THIRD_ROW          0x94     //Move cursor to the 3rd row
#define _LCD_FOURTH_ROW         0xD4     //Move cursor to the 4th row
#define _LCD_CLEAR          0x01     //Clear display
#define _LCD_RETURN_HOME        0x02     //Return cursor to home position, returns a shifted display to
                                         //its original position. Display data RAM is unaffected.
#define _LCD_CURSOR_OFF         0x0C     //Turn off cursor
#define _LCD_UNDERLINE_ON       0x0E     //Underline cursor on
#define _LCD_BLINK_CURSOR_ON            0x0F     //Blink cursor on
#define _LCD_MOVE_CURSOR_LEFT           0x10     //Move cursor left without changing display data RAM
#define _LCD_MOVE_CURSOR_RIGHT          0x14     //Move cursor right without changing display data RAM
#define _LCD_TURN_ON            0x0C     //Turn Lcd display on
#define _LCD_TURN_OFF           0x08     //Turn Lcd display off
#define _LCD_SHIFT_LEFT         0x18     //Shift display left without changing display data RAM
#define _LCD_SHIFT_RIGHT        0x1E     //Shift display right without changing display data RAM
 
// Constants
#define EN_Delay 500
 
// Give a pulse on E pin
// so that LCD can latch the
// data from data bus
 
#define LCD_STROBE {LCD_EN = 1; _delay_us(EN_Delay); LCD_EN = 0; _delay_us(EN_Delay);};
 
 
//Function Prototypes
void LCD_Cmd(unsigned char Command);
void LCD_Ch(unsigned int row, unsigned int col, char LCDChar);
void LCD_Init();
void LCD_Out(unsigned int row, unsigned int col, const char *str);
 
void LCD_Cmd(unsigned char Command)
{
    LCD_RS = 0;           // It is a command
 
    LCD_D4 = (Command & 0x10)?1:0;
    LCD_D5 = (Command & 0x20)?1:0;
    LCD_D6 = (Command & 0x40)?1:0;
    LCD_D7 = (Command & 0x80)?1:0;
    LCD_STROBE;
    LCD_D4 = (Command & 0x01)?1:0;
    LCD_D5 = (Command & 0x02)?1:0;
    LCD_D6 = (Command & 0x04)?1:0;
    LCD_D7 = (Command & 0x08)?1:0;
    LCD_STROBE;
 
    if(Command == 0x01)
    _delay_ms(2);              // Delay for cursor to return at zero position
}
 
 
void LCD_Ch(unsigned int row, unsigned int col, char LCDChar)
{
    switch(row){
 
        case 1:
        LCD_Cmd(0x80 + col-1);
        break;
        case 2:
        LCD_Cmd(0xC0 + col-1);
        break;
        case 3:
        LCD_Cmd(0x94 + col-1);
        break;
        case 4:
        LCD_Cmd(0xD4 + col-1);
        break;
    }
 
    LCD_RS = 1;               // It is data
 
    LCD_D4 = (LCDChar & 0x10)?1:0;
    LCD_D5 = (LCDChar & 0x20)?1:0;
    LCD_D6 = (LCDChar & 0x40)?1:0;
    LCD_D7 = (LCDChar & 0x80)?1:0;
    LCD_STROBE;
 
    LCD_D4 = (LCDChar & 0x01)?1:0;
    LCD_D5 = (LCDChar & 0x02)?1:0;
    LCD_D6 = (LCDChar & 0x04)?1:0;
    LCD_D7 = (LCDChar & 0x08)?1:0;
    LCD_EN = 1;
    LCD_STROBE;
}
 
 
void LCD_Init()
{
    LCD_RS = 0;
    LCD_EN = 0;
    LCD_D4 = 0;
    LCD_D5 = 0;
    LCD_D6 = 0;
    LCD_D7 = 0;
     
    ///////////////// Reset process from datasheet //////////////
    _delay_ms(30);
 
    //Make Data pins zero
 
    LCD_D4 = 0;
    LCD_D5 = 0;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    //Write 0x3 value on data bus
    LCD_D4 = 1;
    LCD_D5 = 1;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    LCD_STROBE;
 
    _delay_ms(6);
 
    //Make Data pins zero
 
    LCD_D4 = 0;
    LCD_D5 = 0;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    //Write 0x3 value on data bus
    LCD_D4 = 1;
    LCD_D5 = 1;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    LCD_STROBE;
 
    _delay_ms(3);
 
    //Make Data pins zero
 
    LCD_D4 = 0;
    LCD_D5 = 0;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    //Write 0x3 value on data bus
    LCD_D4 = 1;
    LCD_D5 = 1;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    LCD_STROBE;
 
    _delay_ms(2);
 
    //Make Data pins zero
 
    LCD_D4 = 0;
    LCD_D5 = 0;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    //Write 0x2 value on data bus
    LCD_D4 = 0;
    LCD_D5 = 1;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    LCD_STROBE;
 
    _delay_ms(2);
    /////////////// Reset Process End ////////////////
    LCD_Cmd(0x28);    //function set
    LCD_Cmd(0x0C);    //display on,cursor off,blink off
    LCD_Cmd(0x06);    //entry mode, set increment
}
 
void LCD_Out(unsigned int row, unsigned int col, const char *str)
{
    while(*str)
    LCD_Ch(row,col++,*str++);   // print first character on LCD
    row = 1;
    col = 1;
}
 
int main(void){
 
    DDRA = 0xFF;
    DDRB = 0xFF;
    DDRC = 0xFF;
    DDRD = 0xFF;
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
     
    LCD_Init();
    LCD_Cmd(_LCD_CLEAR);
    LCD_Cmd(_LCD_CURSOR_OFF);
 
    LCD_Out(1,1,"PIC18F458");
    LCD_Out(2,1,"LCD 4-bit");
    LCD_Out(3,1,"20X4");
    LCD_Out(4,1,"Working?");
 
    while(1);
 
    return (0);
}



90706d1367922856-ats6.1lcd4bit.jpg
 

Attachments

  • ats6.1lcd4bit.jpg
    ats6.1lcd4bit.jpg
    275.9 KB · Views: 204
  • lcd4bitpinaddr.rar
    34 KB · Views: 156

If PINA0 is defined inside Atmel's libraries as a constant value (address) and not as pointer, then this is something I would expect. Check those definitions. Furthermore, all those lcd pins are outputs concerning AVR side. Why are you using definitions with the PIN register which is used to read a pin's value?
 
Working code for AVR Studio 4.19


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
/*
 * lcd4bit.c
 *
 * Created: 5/7/2013 9:09:11 AM
 *  Author: Jayanth Devarayanadurga
 */ 
 
 
#include <avr/io.h>
#include <util/delay.h>
 
//Mention Clock frequency here
#define _XTAL_FREQ 8000000
 
// structure to allow bit field operations, name conversions: PORTA.0 -> PORT_A.b0  PORTB.7 -> PORT_B.b7
typedef struct{ uint8_t b0:1;
                uint8_t b1:1;
                uint8_t b2:1;
                uint8_t b3:1;
                uint8_t b4:1;
                uint8_t b5:1;
                uint8_t b6:1;
                uint8_t b7:1; } bits;
 
// define all the ports of your microcontroller, add more ports depending on the available mcu ports
#define PORT_A (* (volatile bits *) &PORTA)
#define PIN_A (* (volatile bits *) &PINA)
#define DDR_A (* (volatile bits *) &DDRA)
 
#define PORT_B (* (volatile bits *) &PORTB)
#define PIN_B (* (volatile bits *) &PINB)
#define DDR_B (* (volatile bits *) &DDRB)
 
#define PORT_C (* (volatile bits *) &PORTC)
#define PIN_C (* (volatile bits *) &PINC)
#define DDR_C (* (volatile bits *) &DDRC)
 
#define PORT_D (* (volatile bits *) &PORTD)
#define PIN_D (* (volatile bits *) &PIND)
#define DDR_D (* (volatile bits *) &DDRD)
 
#define LCD_RS  PORT_A.b0
#define LCD_EN  PORT_A.b5
//#define rw  PORT_A.b2
#define LCD_D4  PORT_B.b0
#define LCD_D5  PORT_B.b5
#define LCD_D6  PORT_D.b2
#define LCD_D7  PORT_D.b7
 
#define _LCD_FIRST_ROW          0x80     //Move cursor to the 1st row
#define _LCD_SECOND_ROW         0xC0     //Move cursor to the 2nd row
#define _LCD_THIRD_ROW          0x94     //Move cursor to the 3rd row
#define _LCD_FOURTH_ROW         0xD4     //Move cursor to the 4th row
#define _LCD_CLEAR          0x01     //Clear display
#define _LCD_RETURN_HOME        0x02     //Return cursor to home position, returns a shifted display to
                                         //its original position. Display data RAM is unaffected.
#define _LCD_CURSOR_OFF         0x0C     //Turn off cursor
#define _LCD_UNDERLINE_ON       0x0E     //Underline cursor on
#define _LCD_BLINK_CURSOR_ON            0x0F     //Blink cursor on
#define _LCD_MOVE_CURSOR_LEFT           0x10     //Move cursor left without changing display data RAM
#define _LCD_MOVE_CURSOR_RIGHT          0x14     //Move cursor right without changing display data RAM
#define _LCD_TURN_ON            0x0C     //Turn Lcd display on
#define _LCD_TURN_OFF           0x08     //Turn Lcd display off
#define _LCD_SHIFT_LEFT         0x18     //Shift display left without changing display data RAM
#define _LCD_SHIFT_RIGHT        0x1E     //Shift display right without changing display data RAM
 
// Constants
#define EN_Delay 500
 
// Give a pulse on E pin
// so that LCD can latch the
// data from data bus
 
#define LCD_STROBE {LCD_EN = 1; _delay_us(EN_Delay); LCD_EN = 0; _delay_us(EN_Delay);};
 
 
//Function Prototypes
void LCD_Cmd(unsigned char Command);
void LCD_Ch(unsigned int row, unsigned int col, char LCDChar);
void LCD_Init();
void LCD_Out(unsigned int row, unsigned int col, const char *str);
 
void LCD_Cmd(unsigned char Command)
{
    LCD_RS = 0;           // It is a command
 
    LCD_D4 = (Command & 0x10)?1:0;
    LCD_D5 = (Command & 0x20)?1:0;
    LCD_D6 = (Command & 0x40)?1:0;
    LCD_D7 = (Command & 0x80)?1:0;
    LCD_STROBE;
    LCD_D4 = (Command & 0x01)?1:0;
    LCD_D5 = (Command & 0x02)?1:0;
    LCD_D6 = (Command & 0x04)?1:0;
    LCD_D7 = (Command & 0x08)?1:0;
    LCD_STROBE;
 
    if(Command == 0x01)
    _delay_ms(2);              // Delay for cursor to return at zero position
}
 
 
void LCD_Ch(unsigned int row, unsigned int col, char LCDChar)
{
    switch(row){
 
        case 1:
        LCD_Cmd(0x80 + col-1);
        break;
        case 2:
        LCD_Cmd(0xC0 + col-1);
        break;
        case 3:
        LCD_Cmd(0x94 + col-1);
        break;
        case 4:
        LCD_Cmd(0xD4 + col-1);
        break;
    }
 
    LCD_RS = 1;               // It is data
 
    LCD_D4 = (LCDChar & 0x10)?1:0;
    LCD_D5 = (LCDChar & 0x20)?1:0;
    LCD_D6 = (LCDChar & 0x40)?1:0;
    LCD_D7 = (LCDChar & 0x80)?1:0;
    LCD_STROBE;
 
    LCD_D4 = (LCDChar & 0x01)?1:0;
    LCD_D5 = (LCDChar & 0x02)?1:0;
    LCD_D6 = (LCDChar & 0x04)?1:0;
    LCD_D7 = (LCDChar & 0x08)?1:0;
    LCD_EN = 1;
    LCD_STROBE;
}
 
 
void LCD_Init()
{
    LCD_RS = 0;
    LCD_EN = 0;
    LCD_D4 = 0;
    LCD_D5 = 0;
    LCD_D6 = 0;
    LCD_D7 = 0;
     
    ///////////////// Reset process from datasheet //////////////
    _delay_ms(30);
 
    //Make Data pins zero
 
    LCD_D4 = 0;
    LCD_D5 = 0;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    //Write 0x3 value on data bus
    LCD_D4 = 1;
    LCD_D5 = 1;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    LCD_STROBE;
 
    _delay_ms(6);
 
    //Make Data pins zero
 
    LCD_D4 = 0;
    LCD_D5 = 0;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    //Write 0x3 value on data bus
    LCD_D4 = 1;
    LCD_D5 = 1;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    LCD_STROBE;
 
    _delay_ms(3);
 
    //Make Data pins zero
 
    LCD_D4 = 0;
    LCD_D5 = 0;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    //Write 0x3 value on data bus
    LCD_D4 = 1;
    LCD_D5 = 1;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    LCD_STROBE;
 
    _delay_ms(2);
 
    //Make Data pins zero
 
    LCD_D4 = 0;
    LCD_D5 = 0;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    //Write 0x2 value on data bus
    LCD_D4 = 0;
    LCD_D5 = 1;
    LCD_D6 = 0;
    LCD_D7 = 0;
 
    LCD_STROBE;
 
    _delay_ms(2);
    /////////////// Reset Process End ////////////////
    LCD_Cmd(0x28);    //function set
    LCD_Cmd(0x0C);    //display on,cursor off,blink off
    LCD_Cmd(0x06);    //entry mode, set increment
}
 
void LCD_Out(unsigned int row, unsigned int col, const char *str)
{
    while(*str)
    LCD_Ch(row,col++,*str++);   // print first character on LCD
    row = 1;
    col = 1;
}
 
int main(void){
 
    DDRA = 0xFF;
    DDRB = 0xFF;
    DDRC = 0xFF;
    DDRD = 0xFF;
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
     
    LCD_Init();
    LCD_Cmd(_LCD_CLEAR);
    LCD_Cmd(_LCD_CURSOR_OFF);
 
    LCD_Out(1,1,"ATMega128 AVR Studio");
    LCD_Out(2,1,"4.19 LCD 4-bit");
    LCD_Out(3,1,"20X4");
    LCD_Out(4,1,"Working?");
 
    while(1);
 
    return (0);
}




For Atmel Studio one can use the below defines with SBIT.H file. SBIT.H is attached.


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
#define LCD_RS  SBIT(PORTA, 0)
#define LCD_EN  SBIT(PORTA, 5)
#define LCD_D4  SBIT(PORTB, 0)
#define LCD_D5  SBIT(PORTB, 5)
#define LCD_D6  SBIT(PORTD, 2)
#define LCD_D7  SBIT(PORTD, 7)
 
//or still better
 
#define PORTA_0  SBIT(PORTA, 0)
#define PORTA_5  SBIT(PORTA, 5)
#define PORTB_0  SBIT(PORTB, 0)
#define PORTB_5  SBIT(PORTB, 5)
#define PORTD_2  SBIT(PORTD, 2)
#define PORTD_7  SBIT(PORTD, 7)
 
#define LCD_RS  PORTA_0
#define LCD_EN  PORTA_5
#define LCD_D4  PORTB_0
#define LCD_D5  PORTB_5
#define LCD_D6  PORTD_2
#define LCD_D7  PORTD_7

 

Attachments

  • avrs419lcd4bit.rar
    43.9 KB · Views: 164
  • SBIT.rar
    241 bytes · Views: 159
Last edited:

AVR Studio 4.19
Code:
#define LCD_RS  PORT_A.b0

For Atmel Studio one can use the below defines with SBIT.H file. SBIT.H is attached.
Code:
#define LCD_RS  SBIT(PORTA, 0)

According to those definitions, in AVR Studio 4.19 it would be

Code:
LCD_RS = 0;  //PORT_A.b0 = 0 which make sence


In ATMEL studio:

Code:
LCD_RS = 0;  //SBIT(PORTA, 0) = 0 which doesn't make sence

Obviously someone who uses ATMEL studio will give a quick answer since this problem is about IO which is usually a simple task inside libraries, but it is also obvious that from C point of view this is not correct. Can you please find and post the definition of SBIT(PORTA, 0) to investigate further? We need to see the whole "definitions tree" to the lowest level definition to have a clear picture. However the following definition is very strange:

Code:
#define LCD_RS  SBIT(PORTA, 0)

Are you sure it is an ATMEL library definition and not yours? Because how would you go if for instance you wish to clear this bit? If on the other hand you wish to set a bit you will just have to write:

Code:
LCD_RS;  //SBIT(PORTA, 0)
 
  • Like
Reactions: alexxx

    alexxx

    Points: 2
    Helpful Answer Positive Rating
OK sorry, I saw the file. I thought it was a set bit instruction. So it seems to be OK, as long as PORTA as a definition is a modifiable variable and not a defined value, or constant value etc. The poster in the reference you provide doesn't mention which compiler he is using, or if he made his own definitions because it was convenient for him.
So if you try using the ready libraries from ATMEL studio, it still fails?
 
It is working fine now. The SBIT.H file can be used with Atmel Studio 6.0 and 6.1. I didn't test it on AVR Studio 4.19 or Atmel Studio 5.x.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top