AtMega328P interface with LCD for 4bit mode

Go To Last Post
74 posts / 0 new

Pages

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

floresta1212 wrote:

 

 

This last code (the working  one) is not from this forum. It's a code I found somewhere that I managed to get working with my hardware!

 

You got that code by following the second recommendation in reply #36.  A simple Google search for LCD programming examples should get you there as well.  It pops up first when searching from the US, I don't know about other countries.

 

Don

 

Hum, ok... Anyway, when I started this thread I already had tested that code before! So, that's why I said I didn't got the code from this forum.

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

jgmdesign wrote:

MAke sure you confirm your wiring as the connections in my post are different than in Johans.

 

JIm

 

@jgmdesign, do you know how do I compile your #19 post project using avr-gcc from Atmel Toolchain??? I only know how to compile a single .c file!

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

I already did it for you.  If you look in the debug folder there is a hex file already for you to program into the part.

 

I did the program based on a 16 megahertz cpu clock as thats what you said you were running.  By the way, are you doing all this on an Arduino, or on something else?

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

jgmdesign wrote:

I already did it for you.  If you look in the debug folder there is a hex file already for you to program into the part.

 

I did the program based on a 16 megahertz cpu clock as thats what you said you were running.  By the way, are you doing all this on an Arduino, or on something else?

 

JIm

 

Ah ok...

 

I was already trying to build my makefile but I was getting errors... Weird errors. It was complaining of multiple definitions of function main, but only one is defined!

 

No, I'm using the AtMega328P-PU on the breadboard with the 16MHz crystal!

 

Ok, going to try to flash the .hex file!

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

PsySc0rpi0n wrote:
No, I'm using the AtMega328P-PU on the breadboard with the 16MHz crystal!

Hopefully you have the fuses set properly, and you have 18pf caps on the crystal, and decoupling caps on the avr.  Vcc and AVcc connected to +5vdc.

 

Also, Why don't you use studio and make your life easier?

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

jgmdesign wrote:

PsySc0rpi0n,

Take a look at teh attached project.  It was done in Studio 6_2 but will work in version 7 as well.  There is an ELF and HEX file you can load into your Mega328 using AVRdude if need be.  It has been configured for a 16megahertz clock, and the LCD is connected to PORTD.

 

LCD Pinouts are:

RS = 0

R/W = 1

E = 2

Data0 = 3

Data1 = 4

Data2 = 5

Data3 = 6

 

PORTD-7 is not used.

 

I tested the code on a Mega328 in my STK600 and it does work.

 

You can see in LCD.h how the port, the number of lines and characters, and XTAL for the library is set up.

 

Jim

 

I do not understand what are those pins.

RS -> PD0

RW -> PD1

.

.

.

D7 -> Data3 -> PD6???

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

The defines cannot be correct! I think they need to be adjusted!

 

In lcd.h we have

 

#define LCD_PORT         PORTD        /**< port for the LCD lines   */
#define LCD_DATA0_PORT   LCD_PORT     /**< port for 4bit data bit 0 */
#define LCD_DATA1_PORT   LCD_PORT     /**< port for 4bit data bit 1 */
#define LCD_DATA2_PORT   LCD_PORT     /**< port for 4bit data bit 2 */
#define LCD_DATA3_PORT   LCD_PORT     /**< port for 4bit data bit 3 */
#define LCD_DATA0_PIN    3            /**< pin for 4bit data bit 0  */
#define LCD_DATA1_PIN    4            /**< pin for 4bit data bit 1  */
#define LCD_DATA2_PIN    5            /**< pin for 4bit data bit 2  */
#define LCD_DATA3_PIN    6            /**< pin for 4bit data bit 3  */
#define LCD_RS_PORT      LCD_PORT     /**< port for RS line         */
#define LCD_RS_PIN       0           /**< pin  for RS line         */
#define LCD_RW_PORT      LCD_PORT     /**< port for RW line         */
#define LCD_RW_PIN       1            /**< pin  for RW line         */
#define LCD_E_PORT       LCD_PORT     /**< port for Enable line     */
#define LCD_E_PIN        2            /**< pin  for Enable line     */

 

LCD_RS_PIN will turn into PORTD0. But this pin belongs to RxD.

LCD_RW_PIN will turn into PORTD1. But this pin belongs to TxD.

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

 

PsySc0rpi0n wrote:
I do not understand what are those pins. RS -> PD0 RW -> PD1 . . . D7 -> Data3 -> PD6???

 

Are you SERIOUS?  Have you even read a datasheet?

 

I did make one error so I am going to rewrite the connections:

 

LCD                                   MEGA328

RS                                      PORTD,0                           Register select

R/W                                    PORTD,1                           Read/Write

E                                        PORTD,2                           Enable

D4                                      PORTD,3                           Data bit

D5                                      PORTD,4                           Data bit

D6                                      PORTD,5                           Data bit

D7                                      PORTD,6                           Data bit

 

You connect the LCD lines to AVR PORTD port.

 

Jim

 

 

EDIT:

Just follow the pinout and trust me on this.

 

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

Last Edited: Fri. Oct 28, 2016 - 06:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

 

PsySc0rpi0n wrote:
I do not understand what are those pins. RS -> PD0 RW -> PD1 . . . D7 -> Data3 -> PD6???

 

Are you SERIOUS?  Have you even read a datasheet?

 

I did make one error so I am going to rewrite the connections:

 

LCD                                   MEGA328

RS                                      PORTD,0                           Register select

R/W                                    PORTD,1                           Read/Write

E                                        PORTD,2                           Enable

D4                                      PORTD,3                           Data bit

D5                                      PORTD,4                           Data bit

D6                                      PORTD,5                           Data bit

D7                                      PORTD,6                           Data bit

 

You connect the LCD lines to AVR PORTD port.

 

Jim

 

 

EDIT:

Just follow the pinout and trust me on this.

 

 

Of course I read the datasheet.

 

PD0 and PD1 are used to upload the firmware to the uC. But thye are using the same pins for LCD control pins!

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

Lets back up.  MAke a list of what you are using to upload to the AVR, and I am guessing you are using some sort of bootloader if you are using the TX and RX lines.  Which I was not aware of.

 

Post what you are using for an IDE and programming.  I am not wasting anymore time on this.  50+ posts and it's like the chip just came out of the tube.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

jgmdesign wrote:

Lets back up.  MAke a list of what you are using to upload to the AVR, and I am guessing you are using some sort of bootloader if you are using the TX and RX lines.  Which I was not aware of.

 

Post what you are using for an IDE and programming.  I am not wasting anymore time on this.  50+ posts and it's like the chip just came out of the tube.

 

Jim

 

I'm sorry if I have not provided all the info

 

I'm using AtMega328P-PU on a breadboard, using a 16MHz XTAL, 2 22pF caps, a reset button with a 10k ohm resistor. As interface between PC and uC I'm using an USB-to-serial converter and yes, I'm using TxD and Rxd to upload the firmware to the board!

I'm using Atmel Toolchain to compile and upload the firmware to the uC.

 

 

Edited;

I'm closer now to get it working, but still not perfect! I can see a lot of characters being printed on the LCD, and somehow, the messages you typed in the code are being printed in the display but all messed up with the other characters that are also being printed! I'm uploading a video!  

Last Edited: Fri. Oct 28, 2016 - 07:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PsySc0rpi0n wrote:
As interface between PC and uC I'm using an USB-to-serial converter

I hope it's a USB to TTL serial converter and not RS232

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

jgmdesign wrote:

PsySc0rpi0n wrote:
As interface between PC and uC I'm using an USB-to-serial converter

I hope it's a USB to TTL serial converter and not RS232

 

JIm

 

Yes, it's TTL. 

Here it is the small video:

https://drive.google.com/open?id...

 

Youtube kink

https://youtu.be/vEwrnfaczH8

Last Edited: Fri. Oct 28, 2016 - 07:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

THat is a bloody disaster of a wiring rats nest...putting it politely.

 

I'll modify the library to use a separate port for the three control lines.  MAy be a little while as I am in the middle of something.

 

In the meantime clean that mess up.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, here is a modified, compiled, and tested project that has the three control lines moved to PORTB, lines 0, 1, and 2.

 

PORTB,0 = RS

PORTB,1 = R/W

PORTB,2 = E

 

Use the data lines on PORTD as they were

 

LCD                                    AVR

D4                                      PORTD,3                           Data bit

D5                                      PORTD,4                           Data bit

D6                                      PORTD,5                           Data bit

D7                                      PORTD,6                           Data bit

 

Code has been tested

Jim

Attachment(s): 

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

jgmdesign wrote:

OK, here is a modified, compiled, and tested project that has the three control lines moved to PORTB, lines 0, 1, and 2.

 

PORTB,0 = RS

PORTB,1 = R/W

PORTB,2 = E

 

Use the data lines on PORTD as they were

 

LCD                                    AVR

D4                                      PORTD,3                           Data bit

D5                                      PORTD,4                           Data bit

D6                                      PORTD,5                           Data bit

D7                                      PORTD,6                           Data bit

 

Code has been tested

Jim

 

Ok... Going to give it a try!

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

Unfortunately, I can't make it work! I tried to upload directly the .hex file from Debug folder. It was uploaded successfully but nothing shows up on the display!

 

I'm going to try to compile the code by myself as a last attempt!

 

I tried to catch signals with the scope on data lines and control lines but I captured none using "Single" function of my Rigol that catches a signal as soon as it appears on the scope!

Last Edited: Fri. Oct 28, 2016 - 08:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, after recompiling the files, finally it is working...

 

I would like to dig the source files to know and understand the mechanisms, but honestly, I'm not very motivated to do it!

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

So you have the four lines of text just like in my picture?

The screen should blank and then print the four lines again then repeat

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

jgmdesign wrote:
So you have the four lines of text just like in my picture? The screen should blank and then print the four lines again then repeat Jim

 

Yes sir... Just like your example!

 

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

PsySc0rpi0n wrote:
would like to dig the source files to know and understand the mechanisms, but honestly, I'm not very motivated to do it!

The only file you need to really understand is the LCD.h file as thats where you configure the library with the CPU clock, number of lines the display has, how many characters wide it is, and the port (in your case ports) are used and the pin configurations.  The LCD.c file does all the heavy lifting.  My TEST_LCD.c file is nothing more than a crude loop.

 

In LCD.h you will find all the commands the library supports as well.  The lcd.h file is VERY well commented so you should have very few issues understanding what the code is doing.  If yo have any questions please post them here and one of us will do our best to answer them.

 

Regards,

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Yes, but if I want to see the actual delays, I need to be switching between files and there are code there that I do not understand. Chunks of 'asm' code, a lot of defines and macros and I get easily lost between files!

 

Anyway, I'm now  trying to make another code work.

 

It's "danni"'s code:

https://www.avrfreaks.net/forum/t...

 

I have downloaded his files and it's way more simple to understand and keep track of what is going on! The problem is that i have an #include of a file that do not exist in the .zip file! It's "mydefs.h". What file is this and where is it?

 

Edited;

I found the file... But the code still not working!

 

Anyway, jgmdesign, the project you worked out for me it's working but it's hard to dissect the code. For instance, I would like to know about the delays on the init sequence so that I could apply them to other codes!

Last Edited: Sat. Oct 29, 2016 - 11:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The delays are simple _delay_ms() macros that count off clock cycles to create the delays.  THe number of clock cycles counted is dependent on the F_CPU frequency.

 

You can read about the delay_ms(), and _delay_us() here:

 

http://www.nongnu.org/avr-libc/u...

 

Much easier than trying to dissect the code. 

 

The library I used for the project I made up for you is very well commented so I do not understand what is so hard. 

 

as far as Danni's coed is concerned...  I made up another project for you based on the pin connections I used in my original project and it worked right away.  As I would have expected.  I did see though that in main.h he has F_CPU defined there.  I had already set F_CPU in my project properties so I UN-commented it out.  Since you are not using Studio I would suggest you remove the two // in front of the line if you plan of making any changes and re-compiling.

 

I have attached the project and you can find the HEX file in the debug folder, just like the last project.

 

THis statement of yours:

PsySc0rpi0n wrote:
I have downloaded his files and it's way more simple to understand and keep track of what is going on!

also baffles me as there are 5 files to make the LCD work.  My project uses 3.  MAybe you think Danni's code is easier to understand because the library is spread out over multiple files?  OK, thats fine.

 

One thing I noticed is that Danni's code used up almost 2k of flash space and 30 bytes of RAM, where Peter Fleury's library used 778 bytes of flash space and 54 bytes of RAM.  Big difference on flash by almost 3:1.

 

To each his own.

 

 

Attachment(s): 

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

Pages