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.

Dear all My first LCD program does not work

Status
Not open for further replies.

PA3040

Advanced Member level 3
Joined
Aug 1, 2011
Messages
883
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Activity points
6,936
Dear all My first LCD program does not work
please guide with wrong

my MCU is 16f877
LCD LMB162A

Code:
status		equ		0x03
trisb		equ		0x86
portb		equ		0x06
portd		equ		0x08
trisd		equ		0x88
count1		equ		0x20
count2		equ		0x21

#include <p16f877a.inc>
__CONFIG	_XT_OSC & _WDT_OFF& _BODEN_OFF


start		goto	main
main		call	sys_init
			call	lcd
			call	data1

lcd			bcf		portd,7		;E
			bcf		portd,5		;RS
			bcf		portd,6		;W
			call	delay_125
			movlw	0x38
			movwf	portb
			call	pulseE
			movlw	0x08
			movwf	portb
			call	pulseE
			movlw	0x0e
			movwf	portb
			call	pulseE
			movlw	0x06
			movwf	portb
			call	pulseE
			movlw	0x14
			movwf	portb
			call	pulseE
			movlw	0x01
			movwf	portb
			call	pulseE
			return

data1		bcf		portd,7
			bsf		portd,5
			call	delay_125
			
loop1		movlw	'D'
			movwf	portb
			call	delay_5ms
			movlw	'A'
			movwf	portb
			call	delay_5ms
			goto	loop1

pulseE		bsf		portd,7 	;E
			bcf		portd,7
			call	delay_125
			return


delay_125	movlw	D'42'
			movwf	count1
loop		decfsz	count1,f
			goto	loop
			return

delay_5ms	movlw	D'41'
			movwf	count2
delay		call	delay_125
			decfsz	count2,f
			goto	delay
			return



sys_init	bsf		status,5
			clrf	trisb
			clrf	trisd
			bcf		status,5
			clrf	portb
			clrf	portd
			return

end
 

Dear all
I need to send four letters to LCD and it should be display at once ex: "ABCD" I mean not Individualize letters
any one can advice me my LCD is HD44870 and MCU is 16f877
please advice
 

Do you specifically need to use assembly language? If c language is ok with you then I guess I can help. Let me know asap. Thanks
 

Hi,

Just asked you that question on the keypad routine.:smile:

The lcd is an even more complex routine and you really want to just use a ready made program for now - why make life difficult.

Nigels Win Pic tutorial also has a proven lcd routine or if you are stuck I can rake out my old 16F one.

What is important is the frequency of the oscillator you are using as it affects all the timings /delays used by the lcd program.
My routines use 4 mhz.
 

Dear All
Can we send letters to LCD up side down
see picture for more
LCD.JPG
 

It would make more sense to rotate your display in the direction you want.
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Thanks you guide that is one choice

can we do it in software?
 

Assuming that you are referring to a HD44780 display and not a graphic one then the characters are predefined and you cant rotate them.
There are 64 bytes CGRAM available for the user allowing up to eight 5*8 pixel characters or patterns, if you just want to show a few letters rotated then you can use it but not to show the complete alphabet rotated because there is not enough space.



Alex
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
The question was

You mean that there is a way in these tutorials to print characters upside down?

Hi,

No, sorry to mislead you, I just posted that link to show how he could code the CGRAM to create his own characters as you mentioned, not to invert the standard charater set.
 

Here is a sample program of book "Microcontroller Programming The Microchip PIC":


Code ASM - [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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
; File name: LCDTest1.asm
; Date: April 13, 2006
; Author: Julio Sanchez
; Processor: 16F84A
;
; Description:
; Program to exercises 8-bit PIC-to-LCD interface.
; Code assumes that LCD is driven by Hitachi HD44780
; controller and that the display supports two lines
; each one with 16 characters. The wiring and base
; address of each display line is stored in #define
; statements. These statements can be edited to
; accomodate a different set-up.
; Program uses delay loops for interface timing.
; WARNING:
; Code assumes 4Mhz clock. Delay routines must be
; edited for faster clock
 
; Displays: Minnesota State, Mankato
;
;===========================
;        switches
;===========================
; Switches used in __config directive:
;   _CP_ON              Code protection ON/OFF     
; * _CP_OFF      
; * _PWRTE_ON     Power-up timer ON/OFF
;   _PWRTE_OFF     
;   _WDT_ON       Watchdog timer ON/OFF  
; * _WDT_OFF       
;   _LP_OSC       Low power crystal occilator
; * _XT_OSC       External parallel resonator/crystal ocillator  
;   _HS_OSC       High speed crystal resonator (8 to 10 MHz)
;                 Resonator: Murate Erie CSA8.00MG = 8 MHz   
;   _RC_OSC       Resistor/capacitor ocillator (simplest, 20% error)
; |
; |_____ * indicates setup values
 
;=========================
; setup and configuration
;=========================
        processor 16f84A
        include   <p16f84A.inc>
        __config  _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF
 
;=====================================================
;                 constant definitions
;  for PIC-to-LCD pin wiring and LCD line addresses
;=====================================================
#define E_line 1        ;|
#define RS_line 2       ;| -- from wiring diagram 
#define RW_line 3       ;|
; LCD line addresses (from LCD data sheet)
#define LCD_1 0x80      ; First LCD line constant
#define LCD_2 0xc0      ; Second LCD line constant
; Note: The constant that define the LCD display line
;       addresses have the high-order bit set in
;       order to faciliate the controller command 
;
;=====================================================
;              variables in PIC RAM
;=====================================================
; Reserve 16 bytes for string buffer
        cblock  0x0c
        strData
        endc
; Leave 16 bytes and Continue with local variables
        cblock  0x1d            ; Start of block
        count1          ; Counter # 1
        count2          ; Counter # 2
        count3          ; Counter # 3
        pic_ad          ; Storage for start of text area
                                ; (labeled strData) in PIC RAM
        J                       ; counter J
        K                       ; counter K
        index           ; Index into text table (also used
                                ; for auxiliary storage)
        endc
 
;============================================================
;                           program
;============================================================
                org             0         ; start at address 
                goto    main
; Space for interrupt handlers
        org             0x08
 
main:
        movlw   b'00000000' ; All lines to output
        tris    PORTA           ; in port A
        tris    PORTB           ; and port B
        movlw   b'00000000' ; All outputs ports low
        movwf   PORTA
        movwf   PORTB
; Wait and initialize HD44780
        call    delay_5ms               ; Allow LCD time to initialize itself
        call    initLCD         ; Then do forced initialization
        call    delay_5ms               ; (Wait probably not necessary)
; Store base address of text buffer in PIC RAM
        movlw   0x0c            ; Start address of text buffer
        movwf   pic_ad          ; to local variable
;======================
;   first LCD line
;======================
; Store 16 blanks in PIC RAM, starting at address stored
; in variable pic_ad
        call    blank16
; Call procedure to store ASCII characters for message
; in text buffer
        movlw   d'3'            ; Offset into buffer
        call    storeMN
; Set DDRAM address to start of first line
        call    line1
; Call procedure to display 16 characters in LCD
        call    display16
;========================
;   second LCD line
;========================
        call    delay_125mcs    ; Wait for termination
        call    blank16         ; Blank buffer
; Call procedure to store ASCII characters for message
; in text buffer
        movlw   d'1'            ; Offset into buffer
        call    storeUniv
        call    line2           ; DDRAM address of LCD line 2
        call    display16
;=======================
;       done!
;=======================
loopHere:
        goto    loopHere  ;done
 
;************************************************************
;                  INITIALIZE LCD PROCEDURE 
;************************************************************
initLCD
; Initialization for Densitron LCD module as follows:
;       8-bit interface
;   2 display lines of 16 characters each
;   cursor on
;   left-to-right increment
;   cursor shift right
;   no display shift
;***********************|
;     COMMAND MODE      |
;***********************|
        bcf             PORTA,E_line    ; E line low
        bcf             PORTA,RS_line   ; RS line low for command
        bcf             PORTA,RW_line   ; Write mode
        call    delay_125mcs            ;delay 125 microseconds
;***********************|
;     FUNCTION SET      |
;***********************|
        movlw   0x38    ; 0 0 1 1 1 0 0 0 (FUNCTION SET)
                                        ;     | | | |__ font select:
                                        ;     | | |    1 = 5x10 in 1/8 or 1/11 dc
                                        ;     | | |    0 = 1/16 dc
                                        ;     | | |___ Duty cycle select
                                        ;     | |      0 = 1/8 or 1/11
                                        ;     | |      1 = 1/16 (multiple lines)
                                        ;     | |___ Interface width
                                        ;     |      0 = 4 bits
                                        ;     |      1 = 8 bits
                                        ;     |___ FUNCTION SET COMMAND 
        movwf   PORTB   ;0011 1000
        call    pulseE  ;pulseE and delay
 
;***********************|
;    DISPLAY OFF        |
;***********************|
        movlw   0x08    ; 0 0 0 0 1 0 0 0 (DISPLAY ON/OFF)
                                        ;         | | | |___ Blink character at cursor
                                        ;         | | |      1 = on, 0 = off
                                        ;         | | |___ Curson on/off
                                        ;         | |      1 = on, 0 = off
                                        ;         | |____ Display on/off
                                        ;         |       1 = on, 0 = off
                                        ;         |____ COMMAND BIT
 
        movwf   PORTB
        call    pulseE  ;pulseE and delay
 
;***********************|
; DISPLAY AND CURSOR ON |
;***********************|
        movlw   0x0e    ; 0 0 0 0 1 1 1 0 (DISPLAY ON/OFF)
                                        ;         | | | |___ Blink character at cursor
                                        ;         | | |      1 = on, 0 = off
                                        ;         | | |___ Curson on/off
                                        ;         | |      1 = on, 0 = off
                                        ;         | |____ Display on/off
                                        ;         |       1 = on, 0 = off
                                        ;         |____ COMMAND BIT
        movwf   PORTB
        call    pulseE  ;pulseE and delay
 
;***********************|
;    ENTRY MODE SET     |
;***********************|
        movlw   0x06    ; 0 0 0 0 0 1 1 0 (ENTRY MODE SET)
                                        ;           | | |___ display shift
                                        ;           | |      1 = shift
                                        ;           | |      0 = no shift
                                        ;           | |____ cursor increment mode
                                        ;           |       1 = left-to-right
                                        ;           |       0 = right-to-left
                                        ;           |___ COMMAND BIT
        movwf   PORTB   ;00000110
        call    pulseE
 
;***********************|
; CURSOR/DISPLAY SHIFT  |
;***********************|
        movlw   0x14    ; 0 0 0 1 0 1 0 0 (CURSOR/DISPLAY SHIFT)
                                        ;       | | | |_|___ don't care
                                        ;       | |_|__ cursor/display shift
                                        ;       |       00 = cursor shift left
                                        ;       |       01 = cursor shift right
                                        ;       |       10 = cursor and display
                                        ;       |            shifted left
                                        ;       |       11 = cursor and display
                                        ;       |            shifted right
                                        ;       |___ COMMAND BIT
        movwf   PORTB   ;0001 1111
        call    pulseE
 
;***********************|
;   CLEAR DISPLAY       |
;***********************|
        movlw   0x01    ; 0 0 0 0 0 0 0 1 (CLEAR DISPLAY)
                                        ;               |___ COMMAND BIT
        movwf   PORTB   ;0000 0001
;
        call    pulseE
        call    delay_5ms       ;delay 5 milliseconds after init
        return
;************************************************************
;               DELAY AND PULSE PROCEDURES 
;************************************************************
;=======================
;  Procedure to delay
;   42 microseconds
;=======================
delay_125mcs
        movlw   D'42'                   ; Repeat 42 machine cycles
        movwf   count1                  ; Store value in counter
repeat
        decfsz  count1,f                ; Decrement counter
        goto    repeat                  ; Continue if not 0
        return                                  ; End of delay  
;------------------------------------------------------------
;=======================
;  Procedure to delay
;   5 milliseconds
;=======================
delay_5ms
        movlw   D'41'                   ; Counter = 41
        movwf   count2                  ; Store in variable
delay
        call    delay_125mcs            ; Delay
        decfsz  count2,f        ; 40 times = 5 milliseconds
        goto    delay
        return                          ; End of delay
;========================
;     pulse E line 
;========================
pulseE
        bsf             PORTA,E_line    ;pulse E line
        bcf             PORTA,E_line
        call    delay_125mcs            ;delay 125 microseconds
        return
 
;=============================
;   long delay sub-routine
;     (for debugging)
;=============================
long_delay
                movlw   D'200'  ; w = 200 decimal
                movwf   J               ; J = w
jloop:  movwf   K               ; K = w
kloop:  decfsz  K,f             ; K = K-1, skip next if zero
                goto    kloop
                decfsz  J,f             ; J = J-1, skip next if zero
                goto    jloop
                return
;=============================
;   LCD display procedure
;=============================
; Sends 16 characters from PIC buffer with address stored
; in variable pic_ad to LCD line previously selected
display16:
; Set up for data
        bcf             PORTA,E_line    ; E line low
        bsf             PORTA,RS_line   ; RS line low for control
        call    delay_125mcs            ; Delay
; Set up counter for 16 characters
        movlw   D'16'                   ; Counter = 16
        movwf   count3  
; Get display address from local variable pic_ad
        movf    pic_ad,w                ; First display RAM address to W
        movwf   FSR                             ; W to FSR
getchar:
        movf    INDF,w                  ; get character from display RAM
                                                        ; location pointed to by file select
                                                        ; register
        movwf   PORTB
        call    pulseE  ;send data to display   
; Test for 16 characters displayed
        decfsz  count3,f                ; Decrement counter
        goto    nextchar                ; Skipped if done
        return
nextchar:
        incf    FSR,f                   ; Bump pointer
        goto    getchar
;========================
;     blank buffer
;========================
; Procedure to store 16 blank characters in PIC RAM
; buffer starting at address stored in the variable
; pic_ad
blank16:
        movlw   D'16'           ; Setup counter
        movwf   count1
        movf    pic_ad,w        ; First PIC RAM address
        movwf   FSR                     ; Indexed addressing
        movlw   0x20            ; ASCII space character
storeit:
        movwf   INDF            ; Store blank character in PIC RAM
                                                ; buffer using FSR register
        decfsz  count1,f        ; Done?
        goto    incfsr          ; no
        return                          ; yes
incfsr:
        incf    FSR,f           ; Bump FSR to next buffer space
        goto    storeit
 
;========================
; Set address register
;    to LCD line 1
;========================
; ON ENTRY:
;         Address of LCD line 1 in constant LCD_1 
line1:
        bcf             PORTA,E_line    ; E line low
        bcf             PORTA,RS_line   ; RS line low, set up for control
        call    delay_125mcs            ; delay 125 microseconds
; Set to second display line
        movlw   LCD_1                   ; Address and command bit
        movwf   PORTB
        call    pulseE                  ; Pulse and delay
; Set RS line for data
        bsf             PORTA,RS_line   ; Setup for data
        call    delay_125mcs            ; Delay
        return
;========================
; Set address register
;    to LCD line 2
;========================
; ON ENTRY:
;         Address of LCD line 2 in constant LCD_2 
line2:
        bcf             PORTA,E_line    ; E line low
        bcf             PORTA,RS_line   ; RS line low, setup for control
        call    delay_125mcs            ; delay
; Set to second display line
        movlw   LCD_2                   ; Address with high-bit set
        movwf   PORTB
        call    pulseE                  ; Pulse and delay
; Set RS line for data
        bsf             PORTA,RS_line   ; RS = 1 for data
        call    delay_125mcs            ; delay
        return
 
;===============================
;  first text string procedure
;===============================
storeMN:
; Procedure to store in PIC RAM buffer the message
; contained in the code area labeled msg1
; ON ENTRY:
;         variable pic_ad holds address of text buffer
;         in PIC RAM
;         w register hold offset into storage area
;         msg1 is routine that returns the string characters
;         an a zero terminator
;         index is local variable that hold offset into
;         text table. This variable is also used for
;         temporary storage of offset into buffer 
; ON EXIT:
;         Text message stored in buffer
;
; Store offset into text buffer (passed in the w register)
; in temporary variable 
        movwf   index           ; Store w in index
; Store base address of text buffer in FSR
        movf    pic_ad,w        ; first display RAM address to W
        addwf   index,w         ; Add offset to address
        movwf   FSR                     ; W to FSR
; Initialize index for text string access
        movlw   0                       ; Start at 0
        movwf   index           ; Store index in variable
; w still = 0
get_msg_char:
        call    msg1            ; Get character from table
; Test for zero terminator
        andlw   0x0ff
        btfsc   STATUS,Z        ; Test zero flag
        goto    endstr1         ; End of string
; ASSERT: valid string character in w
;         store character in text buffer (by FSR)
        movwf   INDF            ; store in buffer by FSR
        incf    FSR,f           ; increment buffer pointer
; Restore table character counter from variable
        movf    index,w         ; Get value into w
        addlw   1                       ; Bump to next character
        movwf   index           ; Store table index in variable
        goto    get_msg_char    ; Continue      
endstr1:
        return
 
; Routine for returning message stored in program area
msg1:
        addwf   PCL,f           ; Access table
        retlw   'M'
        retlw   'i'
        retlw   'n'
        retlw   'n'
        retlw   'e'
        retlw   's'
        retlw   'o'
        retlw   't'
        retlw   'a'
        retlw   0
 
;=================================
;   second text string procedure
;=================================
storeUniv:
; Processing identical to procedure StoreMSU
        movwf   index           ; Store w in index
; Store base address of text buffer in FSR
        movf    pic_ad,0        ; first display RAM address to W
        addwf   index,0         ; Add offset to address
        movwf   FSR                     ; W to FSR
; Initialize index for text string access
        movlw   0                       ; Start at 0
        movwf   index           ; Store index in variable
; w still = 0
get_msg_char2:
        call    msg2            ; Get character from table
; Test for zero terminator
        andlw   0x0ff
        btfsc   STATUS,Z        ; Test zero flag
        goto    endstr2         ; End of string
; ASSERT: valid string character in w
;         store character in text buffer (by FSR)
        movwf   INDF            ; Store in buffer by FSR
        incf    FSR,f           ; Increment buffer pointer
; Restore table character counter from variable
        movf    index,w         ; Get value into w
        addlw   1                       ; Bump to next character
        movwf   index           ; Store table index in variable
        goto    get_msg_char2   ; Continue      
endstr2:
        return
 
; Routine for returning message stored in program area
msg2:
        addwf   PCL,f           ; Access table
        retlw   'S'
        retlw   't'
        retlw   'a'
        retlw   't'
        retlw   'e'
        retlw   ','
        retlw   0x20
        retlw   'M'
        retlw   'a'
        retlw   'n'
        retlw   'k'
        retlw   'a'
        retlw   't'
        retlw   'o'
        retlw   0
 
        end



Hope that helps.
 

Dear All

#include "lcd.h" dose not support by MPLAB HITECH C compiler


Please help
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top