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.

How do i initialize LCD mounted on my microcontroler ???

Status
Not open for further replies.

215

Junior Member level 1
Junior Member level 1
Joined
Mar 22, 2014
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
264
I have this microcontroler with this LCD(DEM16216) mounted on it which i cannot initialize.


Only 4 of the pins are connected which thereby means it runs in a 4 bit mode.
I've looked the datasheet for it (DEM16216) , which gives an step by step guide on how to do it, but for reason it does not work for me...

Could someone tell me why this isn't working for me..

This is the datasheet for my LCD http://www.gaw.ru/pdf/lcd/lcm/Display/char/DEM16216SYH-LY.PDF

The one my teacher recomended to me was this one
**broken link removed**




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
#include"screen.h"
 
void nibble_value(INT8U binary_value)
{
    if((binary_value << 3) == 1)      //DB7
        GPIO_PORTB_DATA_R &= ~(0b00000001 << 3);
    else
        GPIO_PORTB_DATA_R |= (0b00000001 << 3);
 
        if((binary_value << 2) == 1)    //DB6
            GPIO_PORTB_DATA_R &= ~(0b00000001 << 2);
        else
            GPIO_PORTB_DATA_R |= (0b00000001 << 2);
 
            if((binary_value << 1) == 1)    //DB5
              GPIO_PORTB_DATA_R &= ~(0b00000001 << 1);
            else
              GPIO_PORTB_DATA_R |= (0b00000001 << 1);
 
                if((binary_value << 0) == 1)    // DB4
                   GPIO_PORTB_DATA_R &= ~(0b00000001 << 0);
                else
                   GPIO_PORTB_DATA_R |= (0b00000001 << 0);
 
}
 
void insert_value (INT8U binary_value)
{
    if((binary_value << 7) == 1)            // RS
    GPIO_PORTA_DATA_R &= ~(0b00000001 << 7);
    else
    GPIO_PORTA_DATA_R |= (0b00000001 << 7);
 
        if((binary_value << 6) == 1)            //RW
        GPIO_PORTA_DATA_R &= ~(0b00000001 << 6);
        else
        GPIO_PORTA_DATA_R |= (0b00000001 << 6);
 
 
        if((binary_value << 5) == 1)      //DB7
            GPIO_PORTB_DATA_R &= ~(0b00000001 << 5);
                else
                    GPIO_PORTB_DATA_R |= (0b00000001 << 5);
 
                    if((binary_value << 4) == 1)    //DB6
                        GPIO_PORTB_DATA_R &= ~(0b00000001 << 4);
                    else
                        GPIO_PORTB_DATA_R |= (0b00000001 << 4);
 
                        if((binary_value << 3) == 1)    //DB5
                            GPIO_PORTB_DATA_R &= ~(0b00000001 << 3);
                        else
                            GPIO_PORTB_DATA_R |= (0b00000001 << 3);
 
                            if((binary_value << 2) == 1)    // DB4
                                GPIO_PORTB_DATA_R &= ~(0b00000001 << 2);
                            else
                                GPIO_PORTB_DATA_R |= (0b00000001 << 2);
 
                                if((binary_value << 1) == 1)        // debug;
                                        GPIO_PORTB_DATA_R &= ~(0b00000001 << 1);
                                else
                                        GPIO_PORTB_DATA_R |= (0b00000001 << 1);
 
                                if((binary_value << 0) == 1)        // debug
                                    redLed(ON);
                                else
                                    redLed(OFF);
}
 
void enable_pin (int a)
{
    if (a)
    {
        GPIO_PORTB_DATA_R |= 0x40; // Enables  "enable" pin
        redLed(ON); //Debug
    }
 
    else{
        GPIO_PORTB_DATA_R &= 0xBF; //Disables "enable "pin
                redLed(OFF); //debug
    }
 
 
}
 
void screen_init( void )
{
     //POwer on
     MILLISEC(100);
 
     // Special case Function set
//----------------------------------------------------------------//
     enable_pin(ON);            // enable pin on
     MILLISEC(1);               //1 ms delay
     nibble_value(0x03);        // DB5 = 1, DB4 = 1
     enable_pin(OFF);           // enable pin off
     MILLISEC(15);              // 5 ms delaay
 
     enable_pin(ON);            //enable pin on
     MILLISEC(1);               // 1 ms delay
     nibble_value(0x03);        // DB5 = 1, DB4 = 1
     enable_pin(OFF);           // Enable pin off
     MILLISEC(15);              // 5 ms delay
 
     enable_pin(ON);            //enable pin on
     MILLISEC(1);               //1 ms delay
     nibble_value(0x03);        // DB5 = 1, DB4 = 1
     enable_pin(OFF);           // enable pin off
     MILLISEC(5);               // 5 ms delay
//-----------------------------------------------------------------//
     //initial Function set
//-----------------------------------------------------------------//
     //enable_pin(ON);          // enable pin on
     MILLISEC(1);               // 1 ms delay
     nibble_value(0x02);        // 0x02 = 0b00000010 => DB5 = 1
     enable_pin(OFF);           // enable pin = off
     MILLISEC(5);               // 5 ms delay
//----------------------------------------------------------------//
     //Function set
//----------------------------------------------------------------//
     enable_pin(ON);            // enable pin = on
     MILLISEC(1);               // 1 ms delay
     nibble_value(0x02);        // 0x02 = 0b00000010 => DB5 = 1
     enable_pin(OFF);           // enable  pin = off
     MILLISEC(5);               // 5 ms delay
 
     enable_pin(ON);            // enable pin on
     MILLISEC(1);               // 1 ms delay
     nibble_value(0x08);        // 0x08 = 0b1000 n = 1 and f = 0 => DB7 = 1
     enable_pin(OFF);           // enable pin = off
     MILLISEC(5);               // 5 ms delay
//----------------------------------------------------------------//
     //Display On/Off
//----------------------------------------------------------------//
     enable_pin(ON);            //enable pin = on
     MILLISEC(1);               // delay = 1 ms
     nibble_value(0x00);        // ZERO
     enable_pin(OFF);       //enable pin = off
     MILLISEC(5);           // delay 5 ms
 
     enable_pin(ON);            // enable pin = on
     MILLISEC(1);               // delay = 1 ms
     nibble_value(0x08);        // DB7  = 1 => display off
     enable_pin(OFF);           // enable pin = off
     MILLISEC(5);               //  delay = 5 ms
//---------------------------------------------------------------//
     //Clear Display
//---------------------------------------------------------------//
     enable_pin(ON);            // enable pin = on
     MILLISEC(1);               // delay = 1 ms
     nibble_value(0b00000000);  //  Zero
     enable_pin(OFF);       //  Enable pin = off
     MILLISEC(5);           //  Delay  = 5 ms
 
     enable_pin(ON);            // enable = on
     MILLISEC(1);               // delay = 1 ms
     nibble_value(0b00000001);  // No configurable bits, CLEAR Display
     enable_pin(OFF);           // enable = off
     MILLISEC(5);               // delay = 5 ms
//---------------------------------------------------------------//
     //Entry Mode set
//---------------------------------------------------------------//
     enable_pin(ON);            // enable pin = on
     MILLISEC(1);               // delay = 1 ms
     nibble_value(0b00000000);  //  Zero
     enable_pin(OFF);       //  Enable pin = off
     MILLISEC(5);           //  Delay  = 5 ms
 
     enable_pin(ON);            // enable = on
     MILLISEC(1);               // delay = 1 ms
     nibble_value(0b00000110);  // DB6 = 1, DB5 = 1 (increment by 1) , DB4 = 0 (No shift)
     enable_pin(OFF);           // enable = off
     MILLISEC(5);               // delay = 5 ms
//---------------------------------------------------------------//
 
 
 
 
}
 
void command (void)
{
    //Command
//---------------------------------------------------------------//
     enable_pin(ON);            //enable pin on
     MILLISEC(1);               //delay 1 ms
     nibble_value(0b00000000);  //  Zero
     MILLISEC(1);               //delay 1 ms
     enable_pin(OFF);           //Enable pin off
     MILLISEC(3);               //delay 3 ms
//--------------------------------------------------------------//
        //Command
//---------------------------------------------------------------//
     enable_pin(ON);            //enable pin on
     MILLISEC(1);               //delay 1 ms
     nibble_value(0b00001111);  // D  = 1, C = 1 , B = 1  => display on, cursor  on and blinking on..
     MILLISEC(1);               //delay 1 ms
     enable_pin(OFF);           //Enable pin off
     MILLISEC(3);               //delay 3 ms
//--------------------------------------------------------------//
}



init is performed outside of a superloop, command is performed inside a superloop.

Could explain why this isn't working??
 
Last edited:

Which microcontroller are you using? Provide its datasheet and also which compiler is needed to compile the code?

The method your instructor has provided is for HD44780 based LCDs. Your's use KS0070B driver. If KS0070B is compatible with HD44780 then you can use HD44780 compatible code. If it is not then you have to use different code. I am attaching two codes, one for hd44780 and another according to your datasheet. Try both. Change pin numbers for rs, rw, en pis and also for ldata (port used for D4-D7 (upper 4 bits of a port has to be connected to LCD D4-D7))

Here it is mentioned that KS0070B is HD44780 compatible, so you can use the hd44780 code.

https://forum.arduino.cc/index.php?topic=13796.0;wap2
https://www.electro-tech-online.com/threads/pic-16f628-and-ks0070b-lcd.106095/
https://www.mikroe.com/forum/viewtopic.php?f=13&t=18465&start=15
 

Attachments

  • LCD.rar
    1.4 KB · Views: 100
Last edited:

I see that you in your code use a masking method to only send 4 bit at the time, where i send 8 bit where, only 4 of them is used..

About the microcontroler it's a lm3s6965, can you use this information to pinpoint which method i shal use?

I use CooCox IDE , and crystal is 50 mhz.
 
Last edited:

Please zip and post all your project files and Circuit in .png format (MS Paint). Also answer the questions asked in my previous post regarding Compiler and crystal frequency used for lm3s6965.

Use code in HD44780 4bit.txt
 

I don't see why it is necessary... The code piece i've posted is everything..


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
RedLed(ON), REDLed(Off) it Works.
My main looks like this..
 
int Main
{
 screen_init();
 
while(1)
{
command();
}
}



regarding the circuit..

WpWTW.jpg

Regarding your code, could you tell me how it differs from mine, and those value you put on , where you got them..
 
Last edited by a moderator:

I think we aren't talking about the same thing..

Which datasheet should i USe?

Is the way i am doing it correct or incorrect? and if so, where am i going wrong?

please look at the updated code..
 

CooCox is an IDE and not Compiler. Which Compiler you use? Keil MDK ARM? I attached 2 codes, one is for HD44780 and another which I wrote is according to the datasheet you posted. If your instructor says that you can use HD44780 code then use any HD44780 code. It is widely used and available on net. Just google for HD44780 C Code.

You still didn't provide the circuit showing atleast LCD connections with MCU.

Try this .hex file. It prints "Stellaris" on line 1 and "LM3S6965" on line 2.

Main OSC used at 50 MHz (Not IOSC)

No PLL

LCD pin connections

RS PORTB.2
RW GND
EN PORTB.3

D4 PORTB.4
D5 PORTB.5
D6 PORTB.6
D7 PORTB.7


See attached file.
 

Attachments

  • lcd.rar
    2.2 KB · Views: 89
Last edited:

I don't have a picture of the connections to MCU, but i am certain of the way it is connected.
RS = PA7
RW = PA6
DB4 - DB7 = PB2 - PB5
E = PB6
 
Last edited:

Try this hex. It uses Main OSC 50 MHz PLL disabled and according to your LCD connections. Connect RW to GND.

Mention whether you are using PLL, INTOSC.
 

Attachments

  • lcd.rar
    2.2 KB · Views: 93

Try this hex. It uses Main OSC 50 MHz PLL disabled and according to your LCD connections. Connect RW to GND.

Mention whether you are using PLL, INTOSC.

I don't know how i should compile .hex code.. ?
If you are thinking about i2c then no.
 

gcc-arm-none-eabi-4_8-2013q4-20131204-win32.exe .
 

As your instructor has told to use HD44780 code use any HD44780 LCD code but make a proper delay function for your processor speed. Also see if you have to configure any other registers of your MCU.
 

I've configured everything that should be configured... But it still doesn't work..
My delay function is also working, i've debugged it with an LED and it toggle it's state for each second.
I will add an updated version in #1
 
Last edited:

Wait I will post a code soon.

Try this code. Change the LCD pin defines related code according to your Compiler. Change LCD pin connections in LCD defines according to your circuit. Before calling LCD_Init() function set GPIOA and GPIOB as output port and also clear the ports.

This code is for KS0070B driver based LCD. I read at a website that KS0070B is not completely compatible with HD44780. There is difference in timings and also initialization routine.


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
// LCD module connections
sbit LCD_RS at GPIO_PORTA_DATA.B3;
sbit LCD_RW at GPIO_PORTA_DATA.B4;
sbit LCD_EN at GPIO_PORTA_DATA.B5;
sbit LCD_D4 at GPIO_PORTB_DATA.B4;
sbit LCD_D5 at GPIO_PORTB_DATA.B5;
sbit LCD_D6 at GPIO_PORTB_DATA.B6;
sbit LCD_D7 at GPIO_PORTB_DATA.B7;
 
 
sbit LCD_RS_Direction at GPIO_PORTA_DIR.B3;
sbit LCD_RW_Direction at GPIO_PORTA_DIR.B4;
sbit LCD_EN_Direction at GPIO_PORTA_DIR.B5;
sbit LCD_D4_Direction at GPIO_PORTB_DIR.B4;
sbit LCD_D5_Direction at GPIO_PORTB_DIR.B5;
sbit LCD_D6_Direction at GPIO_PORTB_DIR.B6;
sbit LCD_D7_Direction at GPIO_PORTB_DIR.B7;
// End LCD module connections
 
unsigned long LCD_DATA at GPIO_PORTB_DATA;
 
typedef unsigned char uint8_t;
 
void LCD_Strobe_E() {
 
        LCD_EN = 1;
        //Delay of 850 ns here
        LCD_EN = 0;                        // Data must be valid on the fall of E
        //Delay of 1 us here                       // Don't raise E for > 950ns
}
 
void LCD_WriteByte(uint8_t Value) {
 
        uint8_t upperNibble = Value >> 4;
        uint8_t lowerNibble = Value & 0x0F;
 
        LCD_DATA = upperNibble;        // how long does this take?
        LCD_Strobe_E();
        LCD_DATA = lowerNibble;  // how long does this take?
        LCD_Strobe_E();
}
 
void LCD_Chr(uint8_t ch){
 
        LCD_EN = 0;
        LCD_RS = 1;
        LCD_RW = 0;
 
        // Need 60ns delay before LCD_EN = 1 - function call overhead sufficient
        //Delay of 100 ns here
 
        LCD_WriteByte(ch);
}
 
void LCD_Cmd(uint8_t cmd){
 
        LCD_EN = 0;
        LCD_RS = 0;
        LCD_RW = 0;
 
        // Need 60ns delay before LCD_E_HIGH_READ() - function call overhead sufficient
        //Delay of 100 ns here
 
        LCD_WriteByte(cmd);
}
 
void LCD_Out(uint8_t *Value) {
         while(*Value) {
               LCD_Chr(*Value++);
         }
}
 
void LCD_Init() {
 
        // Initialise LCD for 2 line, 5x7, 4 port mode
 
        GPIO_PORTA_DIR = 0X00;
        GPIO_PORTB_DIR = 0X00;
 
        LCD_EN = 0;
        LCD_RS = 0;
        LCD_RW = 0;
 
        // Wait for more than 30ms after Vdd rises to 4.5 v
        Delay_ms(50);
 
        LCD_DATA = 0b00100000;                        // Function set 1
        LCD_Strobe_E();
        Delay_ms(1);
        LCD_DATA = 0b00100000;                        // Function set 2
        LCD_Strobe_E();
        Delay_ms(1);
        LCD_DATA = 0b10110000;                        // Function set 3 - 5 x 7 dots, 2 line mode
        LCD_Strobe_E();
 
        // Wait for more than 39 ?s / 39000ns
        Delay_us(50);
 
        // Display On/Off Control
        LCD_DATA = 0b00000000;        // Display On/Off Control 1
        LCD_Strobe_E();
        LCD_DATA = 0b11110000;        // Display On/Off Control 2 - display on, cursor on, blink on
        LCD_Strobe_E();
 
        // Wait for more than 39 ?s / 39000ns
        Delay_us(50);
 
        LCD_DATA = 0b00000000;        // Clear the display 1
        LCD_Strobe_E();
        LCD_DATA = 0b00010000;        // Clear the display 2
        LCD_Strobe_E();
 
        // Wait for more than 1.53 ms
        Delay_ms(2);
 
        // Entry Mode Set
        LCD_DATA = 0b00000000;        // Entry Mode Set 1
        LCD_Strobe_E();
        LCD_DATA = 0b01100000;        // Entry Mode Set 2 - increment mode, entire shift off
        LCD_Strobe_E();
}
 
void main() {
 
        LCD_Init();
        LCD_Cmd(0x80);
        LCD_Out("Stellaris");
        LCD_Cmd(0xC0);
        LCD_Out("LM3S3965");
        
        while(1);
 
}

 
Last edited:

Which method does this one use?? because if is the same one as the one i've used so far, i wouldn't make sense to test it.. (alot things need to be changed to make it work. )
 

This is a different code and it is a working code for KS0070B driver based LCDs. Only thing is you have to change the LCD defines according to your compiler and LCD connections and add proper delays at the place mentioned by comments.
 

It complains about Sbit.. It does not recognize it..
 

It complains about Sbit.. It does not recognize it..

Yes, I know. That's what I said two time that you have to change the LCD pin defines as required by your compiler. My compiler supports sbit. You can use something like #define abc def

Changes needed are (replace below defines, see how you can define aliases for port bits and a port using your compiler)


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
sbit LCD_RS at GPIO_PORTA_DATA.B3;
sbit LCD_RW at GPIO_PORTA_DATA.B4;
sbit LCD_EN at GPIO_PORTA_DATA.B5;
sbit LCD_D4 at GPIO_PORTB_DATA.B4;
sbit LCD_D5 at GPIO_PORTB_DATA.B5;
sbit LCD_D6 at GPIO_PORTB_DATA.B6;
sbit LCD_D7 at GPIO_PORTB_DATA.B7;
 
 
sbit LCD_RS_Direction at GPIO_PORTA_DIR.B3;
sbit LCD_RW_Direction at GPIO_PORTA_DIR.B4;
sbit LCD_EN_Direction at GPIO_PORTA_DIR.B5;
sbit LCD_D4_Direction at GPIO_PORTB_DIR.B4;
sbit LCD_D5_Direction at GPIO_PORTB_DIR.B5;
sbit LCD_D6_Direction at GPIO_PORTB_DIR.B6;
sbit LCD_D7_Direction at GPIO_PORTB_DIR.B7;
// End LCD module connections
 
unsigned long LCD_DATA at GPIO_PORTB_DATA;

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top