LCD DATA LINES INTERCHANGE

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

I question came to my mind when i was designing a pcb for a home project which includes a lcd (16*2 lcd connected in 4 - bit mode) and an atmega32 i had connected that in this manner:

RS -  PB4, E - PB5, RW - GND  and D0 to D7 connected to PB0 to PB3 respectively. 

Now let's come to the problem i want to interchange the data lines connected to PORTB i.e. I want to connect them in reverse order like this:-

D0 to D7 should now connect to PB7 to PB4 and RS to PB1 , and E to PB2. I can change the position of RS and E but i don't know how can i change the code to send data in this manner to the lcd. Here is the code that i am using for lcd interfacing.

#define F_CPU 8000000UL			/* Define CPU Frequency e.g. here 8MHz */
#include <avr/io.h>			/* Include AVR std. library file */
#include <util/delay.h>			/* Include Delay header file */

#define LCD_Dir  DDRB			/* Define LCD data port direction */
#define LCD_Port PORTB			/* Define LCD data port */
#define RS PB0				/* Define Register Select pin */
#define EN PB1 				/* Define Enable signal pin */


void LCD_Command( unsigned char cmnd )
{
	LCD_Port = (LCD_Port & 0x0F) | (cmnd & 0xF0); /* sending upper nibble */
	LCD_Port &= ~ (1<<RS);		/* RS=0, command reg. */
	LCD_Port |= (1<<EN);		/* Enable pulse */
	_delay_us(1);
	LCD_Port &= ~ (1<<EN);

	_delay_us(200);

	LCD_Port = (LCD_Port & 0x0F) | (cmnd << 4);  /* sending lower nibble */
	LCD_Port |= (1<<EN);
	_delay_us(1);
	LCD_Port &= ~ (1<<EN);
	_delay_ms(2);
}


void LCD_Char( unsigned char data )
{
	LCD_Port = (LCD_Port & 0x0F) | (data & 0xF0); /* sending upper nibble */
	LCD_Port |= (1<<RS);		/* RS=1, data reg. */
	LCD_Port|= (1<<EN);
	_delay_us(1);
	LCD_Port &= ~ (1<<EN);

	_delay_us(200);

	LCD_Port = (LCD_Port & 0x0F) | (data << 4); /* sending lower nibble */
	LCD_Port |= (1<<EN);
	_delay_us(1);
	LCD_Port &= ~ (1<<EN);
	_delay_ms(2);
}

void LCD_Init (void)			/* LCD Initialize function */
{
	LCD_Dir = 0xFF;			/* Make LCD port direction as o/p */
	_delay_ms(20);			/* LCD Power ON delay always >15ms */
	
	LCD_Command(0x02);		/* send for 4 bit initialization of LCD  */
	LCD_Command(0x28);              /* 2 line, 5*7 matrix in 4-bit mode */
	LCD_Command(0x0c);              /* Display on cursor off*/
	LCD_Command(0x06);              /* Increment cursor (shift cursor to right)*/
	LCD_Command(0x01);              /* Clear display screen*/
	_delay_ms(2);
}


void LCD_String (char *str)		/* Send string to LCD function */
{
	int i;
	for(i=0;str[i]!=0;i++)		/* Send each char of string till the NULL */
	{
		LCD_Char (str[i]);
	}
}

void LCD_String_xy (char row, char pos, char *str)	/* Send string to LCD with xy position */
{
	if (row == 0 && pos<16)
	LCD_Command((pos & 0x0F)|0x80);	/* Command of first row and required position<16 */
	else if (row == 1 && pos<16)
	LCD_Command((pos & 0x0F)|0xC0);	/* Command of first row and required position<16 */
	LCD_String(str);		/* Call LCD string function */
}


void LCD_Clear()
{
	LCD_Command (0x01);		/* Clear display */
	_delay_ms(2);
	LCD_Command (0x80);		/* Cursor at home position */
}

int main()
{

	LCD_Init();			/* Initialization of LCD*/
	LCD_String_xy(0, 10, "Hello World");
	
	while(1);
}

This topic has a solution.

anshumaan kumar

Last Edited: Sat. Nov 30, 2019 - 06:09 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

D0 to D7 should now connect to PB7 to PB4   ...what do you mean?!!?..... that is 8 wires reversed to only 4 wires???

 

it appears your code is already set for 4  bit mode init.

 

Why not just include some code to reverse all the bits before sending to the LCD port?

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

Last Edited: Fri. Nov 29, 2019 - 08:27 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry my bad it was supposed to be D4 to D7 connected to PB0 to PB3 and they should be connected in reverse order for example PB4 to PB7. Sorry for the misprint i was little bit confused. Can this be done ??Beacuse I don't this and unfortunately didn't find anything about this on the internet.

anshumaan kumar

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

4 data bits could be flipped using a quick lookup table---only 16 entries

Would prob be faster than shifting , rotating bits around

 

0001 looks up as 1000

0111 looks up as 1110

1011 looks up as 1101

etc

 

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

I recall posting something probably 15 years ago. Any port, any pin any order.

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

Just use Peter Fluery's LCD code:

 

http://www.peterfleury.epizy.com/doxygen/avr-gcc-libraries/group__pfleury__lcd.html

http://www.peterfleury.epizy.com...

 

Within that you have:

#ifndef LCD_PORT
#define LCD_PORT         PORTA        /**< port for the LCD lines   */
#endif
#ifndef LCD_DATA0_PORT
#define LCD_DATA0_PORT   LCD_PORT     /**< port for 4bit data bit 0 */
#endif
#ifndef LCD_DATA1_PORT
#define LCD_DATA1_PORT   LCD_PORT     /**< port for 4bit data bit 1 */
#endif
#ifndef LCD_DATA2_PORT
#define LCD_DATA2_PORT   LCD_PORT     /**< port for 4bit data bit 2 */
#endif
#ifndef LCD_DATA3_PORT
#define LCD_DATA3_PORT   LCD_PORT     /**< port for 4bit data bit 3 */
#endif
#ifndef LCD_DATA0_PIN
#define LCD_DATA0_PIN    0            /**< pin for 4bit data bit 0  */
#endif
#ifndef LCD_DATA1_PIN
#define LCD_DATA1_PIN    1            /**< pin for 4bit data bit 1  */
#endif
#ifndef LCD_DATA2_PIN
#define LCD_DATA2_PIN    2            /**< pin for 4bit data bit 2  */
#endif
#ifndef LCD_DATA3_PIN
#define LCD_DATA3_PIN    3            /**< pin for 4bit data bit 3  */
#endif

While that currently sets PA0..PA3 nothing stops you doing this:

#ifndef LCD_DATA0_PORT
#define LCD_DATA0_PORT   PORTC     /**< port for 4bit data bit 0 */
#endif
#ifndef LCD_DATA1_PORT
#define LCD_DATA1_PORT   PORTF     /**< port for 4bit data bit 1 */
#endif
#ifndef LCD_DATA2_PORT
#define LCD_DATA2_PORT   PORTA     /**< port for 4bit data bit 2 */
#endif
#ifndef LCD_DATA3_PORT
#define LCD_DATA3_PORT   PORTB     /**< port for 4bit data bit 3 */
#endif
#ifndef LCD_DATA0_PIN
#define LCD_DATA0_PIN    5            /**< pin for 4bit data bit 0  */
#endif
#ifndef LCD_DATA1_PIN
#define LCD_DATA1_PIN    2            /**< pin for 4bit data bit 1  */
#endif
#ifndef LCD_DATA2_PIN
#define LCD_DATA2_PIN    7            /**< pin for 4bit data bit 2  */
#endif
#ifndef LCD_DATA3_PIN
#define LCD_DATA3_PIN    4            /**< pin for 4bit data bit 3  */
#endif

which puts the data lines on PC5, PF2, PA7, PB4 or whatever combination you like. Just putting 7..4 instead of 0..3 on the same port would be trivial.

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

After you figure out how to deal with your data lines you really should think about correcting your LCD initialization procedure.

 

Don

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

avrcandies wrote:

4 data bits could be flipped using a quick lookup table---only 16 entries

Would prob be faster than shifting , rotating bits around

 

0001 looks up as 1000

0111 looks up as 1110

1011 looks up as 1101

etc

 

Well this is quite advanced for me to be honest with me but i will try to see this too.

 

Kartman wrote:
I recall posting something probably 15 years ago. Any port, any pin any order.

You must be having the link for it somewhere I find your tutorials to be very easy to understand you can post it here for the help of everyone(like me smiley).

clawson wrote:
Just use Peter Fluery's LCD code:

 

Thanks for the links these libraries are quite good and I was able to solve my problem.

 

Thanks to all of you for helping me there is not a single problem of me that was not solved with the help of you people.Thanks once again.

And sorry for late replies I was busy somewhere testing something on the hardware.

 

floresta1212 wrote:
After you figure out how to deal with your data lines you really should think about correcting your LCD initialization procedure.

 

Well what I am using here is from the internet and 99% of the sites use this and you know I have been learning everything from the internet there is no one near me to teach me Embedded electronics(In which i am highly intrested as you can see). So I think you can help me in this too.smiley

 

anshumaan kumar

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

Luxury! I didn’t have the benefit of the interwebs when i was learning.

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

That is why you have earned experience by yourself i have to rely on others for my problems but it doesn't matter until people like you are helping me hence I complain less and ask more and learn more.

Thanks,

Anshumaan.

anshumaan kumar

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

clawson wrote:
Just use Peter Fluery's LCD code:
Clawson can you please explain me anything that I try to make with this library doesn't work with AVR clocked higher than 2MHz , at first i thought it was something else but even simple codes can't run with this library. On hardware it shows nothing but I have tried testing it on a software it shows this error everytime I try to clock the MCU

 

 

 

LCD controller received command when busy

higher than 2MHz. Also as this problem is related to this library I hadn't created a new discussion is there a need to create one?

anshumaan kumar

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

Have you defined the macro for the actual CPU speed.

 

The library code sets the length of the E pulse with

#define lcd_e_delay()   _delay_us(LCD_DELAY_ENABLE_PULSE)

which is using _delay_us() form <util/delay.h>. That in turn is dependent on F_CPU being defined. So, when lcd.c is built it MUST be built with a -D that sets F_CPU.

Last Edited: Mon. Dec 2, 2019 - 09:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, I always define the cpu frequency , I use this everytime:

 

 #define F_CPU 8000000UL

 

well as I know this tells the compiler that how fast the microcontroller will run.

 

clawson wrote:
So, when lcd.c is built it MUST be built with a -D that sets F_CPU.

With the D you are saying that I should add this define cpu frequency in the lcd.c file? please explain?? Until you answer let me test this.

 

anshumaan kumar

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

Anshumaan wrote:
I use this everytime:
Which is almost certainly wrong. How is lcd.c going to "see" this #define?

 

Are you saying you made the define in some "settings.h" or something and then added a #include of this as the very first thing in lcd.c above all other #include's ?

 

Or are you saying you modified lcd.c and put that #define as the very first line in the file ? (if you did it this way you are going to end up with multiple #define in lots of different .c files).

 

The "better" way to do this will depend on how you build the software but in many IDEs and build systems there is a way to make "common defines". (in AS7 for example this is called "Symbols"). Anything you define there is a passed as a -D on the command line for each .c file in the project that is built.

Last Edited: Mon. Dec 2, 2019 - 11:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The "-D" is the command-line option for specifying a preprocessor define

 

https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html

 

As far as the compiler is concerned, it really makes no difference where it is defined - just so long as it is defined!

 

From a practical Project maintenance perspective, having it in the Project settings (from where it will be applied as a command-line option) makes life a whole lot easier - because it ensures that it is defined without you having to worry about maintaining it in the source files.

 

EDIT

 

clawson beat me to it.

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: Mon. Dec 2, 2019 - 11:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

clawson wrote:
in AS7 for example this is called "Symbols"

Here:

(although the AVR32 Compiler is shown, it's the same for others)

 

#ProjectDefines

 

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: Mon. Dec 2, 2019 - 11:45 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you guys some wizards or something?? Sorry I am just amazed to know that you hit the very nerve of the problem everytime and if the person is able to understand you completely then 'Voila!', the problem gets solved and never comes back.Thanks for the help.

 

clawson wrote:
(in AS7 for example this is called "Symbols"). Anything you define there is a passed as a -D on the command line for each .c file in the project that is built.

 

Yes I used this and it worked flawlessly.

Thanks to awneil too because of the picture you posted I was able to find it easily.Thanks again.

All problems solved for the day.wink

Anshumaan.

anshumaan kumar