Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 20
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
One birthday today, congrats!
Steve Conner (46)


Next birthdays
04/28 Steve Conner (46)
04/29 GODSFUSION (37)
04/29 Zajcek (37)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

HD44780 LCD Initialization Issues

1 2 
Move Thread LAN_403
The Zander
Sat Apr 08 2006, 04:10AM Print
The Zander Registered Member #295 Joined: Tue Mar 07 2006, 06:59PM
Location:
Posts: 23
I recently aquired a 4X20 character LCD and i have been pulling my hair out trying to get the thing to work. i have plenty of experience with low level programming, and PICs, but i just can't seem to get the LCD initialized. perhapes someone more experienced in matters of this type may be able to help. i'll breifly outline exactly what i have done upto this point.

- connected LCD module and backlight to power, both turn on
- programmed a pic with the following simple program intended to simply display a flashing cursor

#include <P16F84A.INC>

DELAYCOUNT EQU 0CH

RW EQU 1
RS EQU 2
ENABLE EQU 3

START
	BSF STATUS, RP0
	MOVLW B'00000000'
	MOVWF TRISB
	MOVLW B'00000'
	MOVWF TRISA
	BCF STATUS, RP0

LOOPY
	MOVLW B'00011'
	MOVWF PORTA
	MOVLW D'250'
	MOVWF DELAYCOUNT
	CALL X_MS_DELAY
	CLRF PORTA
	MOVLW D'250'
	MOVWF DELAYCOUNT
	CALL X_MS_DELAY
	GOTO LOOPY

	MOVLW B'00000000'
	MOVWF PORTB
	MOVWF PORTA

	BCF PORTA, RW
	BCF PORTA, RS

	MOVLW B'00000110'
	MOVWF PORTB
	
	BSF PORTA, ENABLE
	CALL MS_DELAY
	BCF PORTA, ENABLE


LOOP
	NOP
	GOTO LOOP

MS_DELAY
	MOVLW D'250'
DELAYLOOP
	NOP
	NOP
	DECFSZ W
	GOTO DELAYLOOP
	RETURN

X_MS_DELAY
	DECFSZ DELAYCOUNT
	GOTO REPEAT
	RETURN
REPEAT
	CALL MS_DELAY
	GOTO X_MS_DELAY

END
- when powered up the backlight turns on, and i can adjust the constrast of the LCD display from black to zero, but there is no flashing cursor to be seen.
- PortB of the pic is connected to the data bus of the LCD, and bits on PortA corresponding the resigter select, and read/write and connected correctly as well.
- the PIC is of course powered, and it MCLR line held high.

any help remedying this problem is greatly appreciated

Thanx
-TheZander

[Edit: Made the loops easier to see.]
Back to top
Wolfram
Sat Apr 08 2006, 10:18AM
Wolfram Registered Member #33 Joined: Sat Feb 04 2006, 01:31PM
Location: Norway
Posts: 971
LOOPY
MOVLW B'00011'
MOVWF PORTA
MOVLW D'250'
MOVWF DELAYCOUNT
CALL X_MS_DELAY
CLRF PORTA
MOVLW D'250'
MOVWF DELAYCOUNT
CALL X_MS_DELAY
GOTO LOOPY


I don't see how it gets past this part, it looks like an endless loop to me.

Also, an other common mistake:
RW EQU 1
RS EQU 2
ENABLE EQU 3
Are you sure you didn't mean 0, 1 and 2?

Here are some good documents on driving character LCDs, if you haven't seen them already: Link2 Link2
Back to top
Avalanche
Sat Apr 08 2006, 12:25PM
Avalanche Registered Member #103 Joined: Thu Feb 09 2006, 08:16PM
Location: Derby, UK
Posts: 845
I would say you probably haven't got long enough delays in. Those character modules are quite slow to respond I find.

A problem I had once was caused by the oscillator not starting up quickly enough. On power up, the PIC would skip through the LCD initialization routine way too quick for the module, before the oscillator settled down, and the PIC began running properly.
Back to top
rupidust
Sat Apr 08 2006, 08:18PM
rupidust Banned
Registered Member #110 Joined: Fri Feb 10 2006, 12:23AM
Location: Banned City
Posts: 85
[size]
I admire the effort. As for pulling your hair out, even the veterans run through a hundred compiles before it works.

Yes, LOOPY does not end. Calls to X_MS_DELAY is 320 milliseconds rather than 250, which can cause problems later. Too much going on with the X_MS_DELAY sub. If you can, keep it in one spot.

There is no need to load PORTA AND PORTB with 0's then in next line clear these bits:
BCF PORTA, RW
BCF PORTA, RS

Use B'xxxxxxxx' with all 8 bits, otherwise any bits not included are defaulted to 0.
MOVLW B'00000' = MOVLW B'00000000'


The delay sub MS_DELAY always generates a 1280 microsecond delay because decrementing W decrements 255 times. W is initially set to 0x00 because it is defined as W EQU H'0000' in the include file. The first loop lowers that H'00' to H'FF' forcing the DELAYLOOP to be repeated 254 more times regardless what MOVLW D'xxx' is set to. DECFSZ W does not decrement the PIC's working register unless you do this DECFSZ W, 0 which would repeatedly dump H'00' in to the true working register for eternity. W is meant to be used as a Destination flag in compliment with F.
MS_DELAY
    MOVLW D'250'      'EITHER MOVLW D'2' OR MOVLW D'250'
DELAYLOOP             'CAUSES 1280 MICROSECOND DELAY
    NOP
    NOP
    DECFSZ W
    GOTO DELAYLOOP
    RETURN

No harm in generating a full 1 millisecond delay to toggle the ENABLE line. However, since the ENABLE lines does not need that much time, needs only 10 micro seconds during pulse and after, this excess delay will cause timing issues later or at least make LCD writing extremelly slow.
BSF PORTA, ENABLE
    CALL MS_DELAY
    BCF PORTA, ENABLE
There is not point to reading from the LCD at the moment, therefore just tie the R/W LCD line to ground for write mode. That frees up a PIC i/o line and coding complications. If you do not have a scope, do all you can to get one, a digital one. [/size]
Back to top
The Zander
Wed Apr 12 2006, 01:40AM
The Zander Registered Member #295 Joined: Tue Mar 07 2006, 06:59PM
Location:
Posts: 23
<rupidist>

in regards to the MS_DELAY function causing a 1280ms delay

MS_DELAY
MOVLW D'250' 'EITHER MOVLW D'2' OR MOVLW D'250'
DELAYLOOP 'CAUSES 1280 MICROSECOND DELAY
NOP
NOP
DECFSZ W
GOTO DELAYLOOP
RETURN

it is my understanding the "nop" and "decfsz" execute in a single cycle (assuming the location w is != 0) and "goto delay loop" executes in 2 cycles. adding all of these up it would seem that the "delayloop" part of the above function should execute for 255 * 1uS, which yields 1020 microseconds (pretty close to 1mS). i'm not questioning your response, but i would like to know where my logic is flawed for future reference.

Thanx
-TheZander
Back to top
Bjørn
Wed Apr 12 2006, 02:02AM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
What he really is trying to say is that your delay function does something completely different than you imagine.

"DECFSZ W" Will decrement address 0 because W is a special variable defined by the include file and is defined to be 0. Look in the datasheet for a little surprise to see what you really are decrementing.

To get your milisecond delay (at 4 MHz) you need to:
MS_DELAY
    MOVLW D'249'      ;Subtract one because the call and return takes two cycles each
    MOVWF DELAYCOUNT  ;You must move the count into a file, DECFSZ does not work on the W register.
DELAYLOOP
    NOP
    DECFSZ DELAYCOUNT
    GOTO DELAYLOOP    ;This takes two clock cycles so remove one nop
    RETURN

That should amount to a total of 1001 µs unless I counted wrong.
Back to top
rupidust
Wed Apr 12 2006, 09:32AM
rupidust Banned
Registered Member #110 Joined: Fri Feb 10 2006, 12:23AM
Location: Banned City
Posts: 85
[size]My scope measures the time I calculated. I used same code to flash an LED and noticed time was constant.
MS_DELAY
    MOVLW D'250'                ; 1 usec
DELAYLOOP                       ; 5 usec * 255
    NOP                               
    NOP                               
    DECFSZ W                       
    GOTO DELAYLOOP          
    RETURN                      ;2 usec

(5 * 255) - (1)   + 2 + 1 = 1277 usec + (external calls)
[/size]
Back to top
Bjørn
Wed Apr 12 2006, 10:28AM
Bjørn Registered Member #27 Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
There are two problems. Since W is defined to be 0 it points to the INDF register. It in turn points to an address contained in the FSR register. The FSR register has an undefined content at startup. So it may by luck be pointing to free RAM. Since it is undefined it may change at power up and/or behave differently in different models.

The other detail is that after the last DECFSZ W the content of the file will be 0 so it will loop 256 times and not 255.

If FSR=0 the Microchip simulator says that INDF will always return 0. It has been known to return the wrong result in some odd cases so it might possibly behave different in hardware.
Back to top
Steve Conner
Wed Apr 12 2006, 10:31AM
Steve Conner Registered Member #30 Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
Bjorn is right. The decfsz instruction is only meant to be used on file registers. If you use it on the W register, it won't work. Look at the instruction set summary for a description of how it works.

Bjorn's reply made me finally realise why "W" is defined as zero in the include file. I guess it's for when you use it as an option bit with the decfsz, decf, incf, comf, etc. instructions. incf register,W gets preprocessed by the assembler into incf <address>,0

PICs have lots of fun quirks like this. But we put up with them because they're cheap and easy to buy wink
Back to top
The Zander
Wed Apr 12 2006, 09:19PM
The Zander Registered Member #295 Joined: Tue Mar 07 2006, 06:59PM
Location:
Posts: 23
EDIT:
well, my progress has halted once again, i have been able to partially initialize the LCD (as far as i can tell) i can get it to display the flashing cursor by simply sending the "turn LCD on, turn cursor on, blink cursor" instruction. but if i send the "8-bit interface instruction" or the "cursor increment direction" instrucion the cursor is not displayed. in addition i am completely unable to display characters on the LCD.

here is my new code:
#include <P16F84A.INC>

MS_DELAYCOUNT EQU 0DH
X_MS_DELAYCOUNT EQU 0CH
CHAR_TEMP EQU 0EH
INS_TEMP EQU 0FH

RW EQU 1
RS EQU 2
ENABLE EQU 3

START
BSF STATUS, RP0
MOVLW B'00000000'
MOVWF TRISB
MOVLW B'00000001'
MOVWF TRISA
BCF STATUS, RP0

MOVLW D'250'
MOVWF X_MS_DELAYCOUNT
CALL X_MS_DELAY

MOVLW B'00000000'
MOVWF PORTB
MOVWF PORTA

SETUP_INTERFACE
MOVLW B'00111000'
MOVWF INS_TEMP
CALL INS_WRITE

CALL MS_DELAY

TURN_DISPLAY_ON
MOVLW B'00001111'
MOVWF INS_TEMP
CALL INS_WRITE

CALL MS_DELAY

SET_MOVE_DIRECTION
MOVLW B'00000110'
MOVWF INS_TEMP
CALL INS_WRITE

CALL MS_DELAY

DISPLAY_A
; MOVLW B'01000001'
; MOVWF CHAR_TEMP
; CALL CHAR_WRITE


INS_WRITE

MOVF INS_TEMP, 0
MOVWF PORTB

BCF PORTA, RW
BCF PORTA, RS

BSF PORTA, ENABLE
CALL MS_DELAY
BCF PORTA, ENABLE
RETURN


CHAR_WRITE

MOVF CHAR_TEMP, 0
MOVWF PORTB

BCF PORTA, RW
BSF PORTA, RS

BSF PORTA, ENABLE
CALL MS_DELAY
BCF PORTA, ENABLE
RETURN


LOOP
NOP
GOTO LOOP

MS_DELAY
MOVLW D'250'
MOVWF MS_DELAYCOUNT
DELAYLOOP
NOP
NOP
DECFSZ MS_DELAYCOUNT, 1
GOTO DELAYLOOP
RETURN

X_MS_DELAY
DECFSZ X_MS_DELAYCOUNT
GOTO REPEAT
RETURN
REPEAT
CALL MS_DELAY
GOTO X_MS_DELAY

END

Once again, i appologize for using you guys like spellcheckers, but honestly i have been compiling/fiddling with this for hours on end and am getting no where on my own.

-TheZander
Back to top
1 2 

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.