16x2 LCD Issue

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello. I have an issue with a 16x2 LCD. With the below code, all the LCD does is show black boxes on both the lines. Maybe I am missing something in the code, but my connections are correct and not loose. Your help shall be appreciated.

 

Connections are as follows:

Port C: 8-bit data for the LCD

Port A: {

            Pin 5 - EN -  Enable

            Pin 6 - RW - Read Write

            Pin 7 - RS - Register Select

           }

 

Thanks.

 

.INCLUDE "M32DEF.INC"

.EQU EN = 5
.EQU RW = 6
.EQU RS = 7

.ORG 0x00

LDI R16, LOW(RAMEND)
OUT SPL, R16
LDI R16, HIGH(RAMEND)
OUT SPH, R16

LDI R16, 0xFF
OUT DDRC, R16		; Make Port C an output port
LDI R16, (1 << EN) | (1 << RW) | (1 << RS)
OUT DDRA, R16		; Make A5, A6, A7 as output pins

LDI R16, 0x00
OUT PORTC, R16		; Clear Port C
CALL DELAY

;================================================

CBI PORTA, RW		; Write always
CBI PORTA, RS		; Select command register
CBI PORTA, EN		; Clear enable line

LDI R20, 0x0F		; Display ON without cursor
OUT PORTC, R20
CALL SETENABLE

LDI R20, 0x01		; Clear the display
OUT PORTC, R20
CALL SETENABLE

LDI R20, 0x80		; Set DDRAM address to 00
OUT PORTC, R20
CALL SETENABLE

SBI PORTA, RS		; Select data register

LDI ZL, LOW(MYDATA<<1)
LDI ZH, HIGH(MYDATA<<1)

L1:
	LPM R20, Z+
	TST R20
	BREQ EXIT
	OUT PORTC, R20
	CALL SETENABLE
	RJMP L1

EXIT:
	RJMP EXIT

;================================================

SETENABLE:
	SBI PORTA, EN
	RCALL DELAY
	CBI PORTA, EN
	RET

DELAY:
	LDI R21, 0xFF
L2:	LDI R22, 0xFF
L3:	DEC R22
	BRNE L3
	DEC R21
	BRNE L2
	RET

;================================================
.ORG 0x200
MYDATA:
	.DB "Hello, World!", 0
This topic has a solution.
Last Edited: Sat. Apr 3, 2021 - 03:14 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Mega32 has JTAG on PORTC. Disable at runtime with JTD.
One row of Black boxes means the lcd initialisation has failed i.e. wrong code.
Two rows of Black boxes means initialisation has succeeded. you should just adjust the contrast i.e. variable resistor on pin #3 of LCD.

Last Edited: Sat. Apr 3, 2021 - 11:15 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
Mega32 has JTAG on PORTC. Disable at runtime with JTD.

Or keep them as JTAG - so that you can use the debugger.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Go on.   The OP has already written code for PORTC.

 

You either disable JTAGEN fuse at programming time.

Or use JTD at runtime.

 

Re-writing code for a different PORT is a lot more work.   Especially since everything looks ok.   i.e. TWO rows of black boxes is good.

This implies that JTAG has already been disabled.   And the OP only has to adjust the contrast pot.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
The OP has already written code for PORTC.

Always seems a fundamental mistake to me - why would you deliberately lock yourself out of such a valuable resource as the JTAG port?

 

It's like deliberately tying your hands behind your back!

 

Sure, get this one working as-is to start with - but get it off PORTC ASAP.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some displays take a few dozen ms to get ready after power up.   Put in a 100 ms second delay before sending to the display (or  loop back & keep repeating the write sequence.

any clear display command can also take a few ms...allow a long delay after giving the clear command before sending data  (or you can read the LCD ready pin to know when it is ok to proceed)

Make sure your 8 data lines are not somehow swapped...any wire swap will cause the wrong command to be seen, resulting in nothing or garbage & you will spend weeks banging your head through the nearest wall.

 

BRNE L2

note this could simply say BRNE L3

 

edit changed times for future visitors

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sat. Apr 3, 2021 - 05:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Some tips and sample code for HD44780 displays:

http://nerdralph.blogspot.com/20...

 

I have no special talents.  I am only passionately curious. - Albert Einstein

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There are several timing issues.   We don't know what F_CPU value is.

1.   power-up delay.   unlikely to be a problem for default 8-bit mode.

2.   execution time for lcd_command(0x01) i.e. Clear the Display.

 

The DELAY function is going to take ~ 193600 cycles.  i.e. 194ms @ F_CPU = 1MHz or 12ms @ F_CPU = 16MHz.

 

(1) is seldom a problem for default 8-bit mode.   programming or reset button ensures adequate power-up time.

 

(2) lcd_command(0x01) needs about 2ms.   Whatever the F_CPU value is,   the delays are much greater than 2ms.

 

No,   I have not run the OP's code on real hardware.    It is a pain to wire up an 8-bit data bus.    But it looks like a simple "adjust pot" or even just "connect 270R between GND #1 and VO #3"

 

If the OP is using the default 1MHz RC the text will be easy to see.    "Hello, World!" will take 2.5 seconds to print.

It should even be "watchable" @ 16MHz.

 

David.

 

@awneil,

 

JTAG is only any good if you possess a JTAG debugger.

If you have JTAG hardware you will avoid using the JTAG pins (if possible).

 

@avrcandies,

 

yes,  power-up delay is important.  some power supplies take 100s of ms to get to 5V.    If you want to change from default 8-bit mode to 4-bit mode you must follow some timing constraints.

 

lcd_command(0x01) is typical 1.37ms.   2ms is a safer value.    It is never "hundreds of ms".

Last Edited: Sat. Apr 3, 2021 - 01:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

david.prentice wrote:
JTAG is only any good if you possess a JTAG debugger.

 

But why lock yourself out of the possibility of ever using it ?

 

It's not like JTAG debuggers are hard to come by these days...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Apr 3, 2021 - 01:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It works with PORTD instead of PORTC. It's just a simple find and replace.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please read your fuses.   And report back.    Preferably with the hex values of the fuses.

I suspect that you are running with the default fuses i.e. 1MHz RC clock and JTAGEN.

 

If you post a link to the development board that you are using,   we can help you with your projects.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The fuses are:

LF: 0xE1

HF: 0x99

 

The development board that I'm using:

https://www.silicontechnolabs.in...

One small issue is that the download links on their page won't work.

 

EDIT:

I have connected the LCD to the dev board using a breadboard along with a 10k pot for the contrast.

Last Edited: Sat. Apr 3, 2021 - 04:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

  It is never "hundreds of ms".

Yes, I should have said wait 100us or may be 200us.  The clear is dirt slow compared to these, but 2ms , not 200!!   That would indeed be quite a slow refresh when you rurn the scroll wheel!

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes,  you were running on 1MHz RC with JTAGEN fuse.

 

This means that PORTC pins would not work as regular GPIO pins.   Hence the LCD did not work.

The TWO rows of black boxes showed that the HD44780 was ok.   But since PORTC was not working,  it received no proper commands.

 

Thanks for the link.   It shows that there is a XTAL on the dev board.   I can't read the value.   I am guessing that it is 11.059MHz.

You also have an RS232 socket.   So you can send text to an RS232 Serial Terminal with a RS232 cable.   Or to the PC with a USB-RS232 cable.

 

I suggest that you write some "better" delay functions.   e.g. 1ms that you can call with different register values.  to implement different delays like 50ms or 2ms

The SETENABLE function only needs 1 us pulse.   193600 us is overkill !!

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The XTAL is 16MHz, but I'm not sure if 16MHz can be used for serial communication.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

epyczen wrote:
The XTAL is 16MHz, but I'm not sure if 16MHz can be used for serial communication.

What do the tables in the datasheet tell you?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."