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.

[PIC] ds33FJ16GS402 - lcd16x2

Status
Not open for further replies.

Ygon

Newbie level 3
Joined
Dec 22, 2014
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
91
Hello guys I have a problem in my code.

I'm not having control in writing, that is, is showing several characters on the screen.

Make a statement of 8bits.

What should is wrong?

Would anyone help me?



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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
BYTE LCDText[16*2+1];
 
static void LCDWrite(BYTE RS, BYTE Data)
{
    #if defined(LCD_DATA_TRIS)
        LCD_DATA_TRIS = 0x00;
    #else
        LCD_DATA0_TRIS = 0;
        LCD_DATA1_TRIS = 0;
        LCD_DATA2_TRIS = 0;
        LCD_DATA3_TRIS = 0;
        #if !defined(FOUR_BIT_MODE)
        LCD_DATA4_TRIS = 0;
        LCD_DATA5_TRIS = 0;
        LCD_DATA6_TRIS = 0;
        LCD_DATA7_TRIS = 0;
        #endif
    #endif
    LCD_RS_TRIS = 0;
    LCD_RD_WR_TRIS = 0;
    LCD_RD_WR_IO = 0;
    LCD_RS_IO = RS;
 
#if defined(FOUR_BIT_MODE)
    #if defined(LCD_DATA_IO)
        LCD_DATA_IO = Data>>4;
    #else
        LCD_DATA0_IO = ((Data & 0x10) == 0x10);
        LCD_DATA1_IO = ((Data & 0x20) == 0x20);
        LCD_DATA2_IO = ((Data & 0x40) == 0x40);
        LCD_DATA3_IO = ((Data & 0x80) == 0x80);
    #endif
    Nop();                  // Wait Data setup time (min 40ns)
    Nop();
    LCD_E_IO = 1;
    Nop();                  // Wait E Pulse width time (min 230ns)
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    LCD_E_IO = 0;
#endif
 
    #if defined(LCD_DATA_IO)
        LCD_DATA_IO = Data;
    #else
        LCD_DATA0_IO = ((Data & 0x01) == 0x01);
        LCD_DATA1_IO = ((Data & 0x02) == 0x02);
        LCD_DATA2_IO = ((Data & 0x04) == 0x04);
        LCD_DATA3_IO = ((Data & 0x08) == 0x08);
        #if !defined(FOUR_BIT_MODE)
        LCD_DATA4_IO = ((Data & 0x10) == 0x10);
        LCD_DATA5_IO = ((Data & 0x20) == 0x20);
        LCD_DATA6_IO = ((Data & 0x40) == 0x40);
        LCD_DATA7_IO = ((Data & 0x80) == 0x80);
        #endif
    #endif
    Nop();                  // Wait Data setup time (min 40ns)
    Nop();
    LCD_E_IO = 1;
    Nop();                  // Wait E Pulse width time (min 230ns)
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    LCD_E_IO = 0;
 
//  // Uncomment if you want the data bus to go High-Z when idle
//  // Note that this may make analog functions work poorly when using 
//  // Explorer 16 revision 5 boards with a 5V LCD on it.  The 5V LCDs have 
//  // internal weak pull ups to 5V on each of the I/O pins, which will 
//  // backfeed 5V weekly onto non-5V tolerant PIC I/O pins.
//  #if defined(LCD_DATA_TRIS)
//      LCD_DATA_TRIS = 0xFF;
//  #else
//      LCD_DATA0_TRIS = 1;
//      LCD_DATA1_TRIS = 1;
//      LCD_DATA2_TRIS = 1;
//      LCD_DATA3_TRIS = 1;
//      #if !defined(FOUR_BIT_MODE)
//      LCD_DATA4_TRIS = 1;
//      LCD_DATA5_TRIS = 1;
//      LCD_DATA6_TRIS = 1;
//      LCD_DATA7_TRIS = 1;
//      #endif
//  #endif
//  LCD_RS_TRIS = 1;
//  LCD_RD_WR_TRIS = 1;
}
 
 
/******************************************************************************
 * Function:        void LCDInit(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        LCDText[] is blanked, port I/O pin TRIS registers are 
 *                  configured, and the LCD is placed in the default state
 *
 * Note:            None
 *****************************************************************************/
void LCDInit(void)
{
  
    BYTE i;
 
    memset(LCDText, ' ', sizeof(LCDText)-1);
    LCDText[sizeof(LCDText)-1] = 0;
 
    // Setup the I/O pins
    LCD_E_IO = 0;
    LCD_RD_WR_IO = 0;
 
 
    #if defined(LCD_DATA_TRIS)
        LCD_DATA_TRIS = 0x00;
    #else
        LCD_DATA0_TRIS = 0;
        LCD_DATA1_TRIS = 0;
        LCD_DATA2_TRIS = 0;
        LCD_DATA3_TRIS = 0;
        #if !defined(FOUR_BIT_MODE)
        LCD_DATA4_TRIS = 0;
        LCD_DATA5_TRIS = 0;
        LCD_DATA6_TRIS = 0;
        LCD_DATA7_TRIS = 0;
        #endif
    #endif
    LCD_RD_WR_TRIS = 0;
    LCD_RS_TRIS = 0;
    LCD_E_TRIS = 0;
 
 
    // Wait the required time for the LCD to reset
    delay_ms(40);//DelayMs(40);
 
    // Set the default function
    // Go to 8-bit mode first to reset the instruction state machine
    // This is done in a loop 3 times to absolutely ensure that we get 
    // to 8-bit mode in case if the device was previously booted into 
    // 4-bit mode and our PIC got reset in the middle of the LCD 
    // receiving half (4-bits) of an 8-bit instruction
    LCD_RS_IO = 0;
    #if defined(LCD_DATA_IO)
        LCD_DATA_IO = 0x03;
    #else
        LCD_DATA0_IO = 1;
        LCD_DATA1_IO = 1;
        LCD_DATA2_IO = 0;
        LCD_DATA3_IO = 0;
        #if !defined(FOUR_BIT_MODE)
        LCD_DATA4_IO = 0;
        LCD_DATA5_IO = 0;
        LCD_DATA6_IO = 0;
        LCD_DATA7_IO = 0;
        #endif
    #endif
    Nop();                  // Wait Data setup time (min 40ns)
    Nop();
    for(i = 0; i < 3u; i++)
    {
        LCD_E_IO = 1;
        delay_us(10);//Delay10us(1);            // Wait E Pulse width time (min 230ns)
       
        LCD_E_IO = 0;
        delay_ms(2); //DelayMs(2);
       
    }
    
#if defined(FOUR_BIT_MODE)
    #if defined(SAMSUNG_S6A0032)
        // Enter 4-bit mode (requires only 4-bits on the S6A0032)
        #if defined(LCD_DATA_IO)
            LCD_DATA_IO = 0x02;
        #else
            LCD_DATA0_IO = 0;
            LCD_DATA1_IO = 1;
            LCD_DATA2_IO = 0;
            LCD_DATA3_IO = 0;
        #endif
        Nop();                  // Wait Data setup time (min 40ns)
        Nop();
        LCD_E_IO = 1;
        //Delay10us(1);         // Wait E Pulse width time (min 230ns)
          __delay_us(10);
        LCD_E_IO = 0;
    #else
        // Enter 4-bit mode with two lines (requires 8-bits on most LCD controllers)
        LCDWrite(0, 0x28);
    #endif
#else
    // Use 8-bit mode with two lines
    LCDWrite(0, 0x38);
#endif
    delay_us(50);//Delay10us(5);
    
    // Set the entry mode
    LCDWrite(0, 0x06);  // Increment after each write, do not shift
    delay_us(50);//Delay10us(5);
 
    // Set the display control
    LCDWrite(0, 0x0C);      // Turn display on, no cusor, no cursor blink
    delay_us(50);//Delay10us(5);
 
    // Clear the display
    LCDWrite(0, 0x01);  
    delay_ms(2);//DelayMs(2);
    
}
 
 
/******************************************************************************
 * Function:        void LCDUpdate(void)
 *
 * PreCondition:    LCDInit() must have been called once
 *
 * Input:           LCDText[]
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Copies the contents of the local LCDText[] array into the 
 *                  LCD's internal display buffer.  Null terminators in 
 *                  LCDText[] terminate the current line, so strings may be 
 *                  printed directly to LCDText[].
 *
 * Note:            None
 *****************************************************************************/
void LCDUpdate(void)
{
    BYTE i, j;
 
    // Go home
    LCDWrite(0, 0x02);
    delay_ms(2);//DelayMs(2);
 
    // Output first line
    for(i = 0; i < 16u; i++)
    {
        // Erase the rest of the line if a null char is 
        // encountered (good for printing strings directly)
        if(LCDText[i] == 0u)
        {
            for(j=i; j < 16u; j++)
            {
                LCDText[j] = ' ';
            }
        }
        LCDWrite(1, LCDText[i]);
        delay_us(50);//Delay10us(5);
    }
    
    // Set the address to the second line
    LCDWrite(0, 0xC0);
    delay_us(50);//Delay10us(5);
 
    // Output second line
    for(i = 16; i < 32u; i++)
    {
        // Erase the rest of the line if a null char is 
        // encountered (good for printing strings directly)
        if(LCDText[i] == 0u)
        {
            for(j=i; j < 32u; j++)
            {
                LCDText[j] = ' ';
            }
        }
        LCDWrite(1, LCDText[i]);
        delay_us(50);//Delay10us(5);
    }
}
 
/******************************************************************************
 * Function:        void LCDErase(void)
 *
 * PreCondition:    LCDInit() must have been called once
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Clears LCDText[] and the LCD's internal display buffer
 *
 * Note:            None
 *****************************************************************************/
void LCDErase(void)
{
    // Clear display
    LCDWrite(0, 0x01);
    delay_ms(2);//DelayMs(2);
 
    // Clear local copy
    memset(LCDText, ' ', 32);
}

 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top