dip-switches-or-microcontroller-based-lcd-character-displays-my_comssats

LCD interface with Microcontroller using C Language (Part 2)

In this tutorial LCD interface with Microcontroller using C Language (Part 2) we will display characters on LCD that is pressed by keypad. Before sending any command or data to LCD, we need to check for the busy flag of LCD to know if the LCD is ready to take command or data or not. In this tutorial, we will not check the busy flag; instead we know that maximum time a LCD can take to be ready is 1 millisecond, so we just wait for 1 millisecond before sending any command or data.We will create two separate functions for sending command or data. Whenever you need to send command or data, you just call these functions with the required command or data

Delay Function

Delay function is used to set delay at any point in your program you can also can skip it if you don’t want delay.

Void delay (void){   for(int i = 0; i<1000; i++)  {     ; } }

Command Function

As I said before we will use separate function for command so we can give any command easily with this function and can use it as many times as we want.

void command(char a){  PORTC=a;  PORTD=0b10000000;  delay();  PORTD=0b00000000;}

Display Function

Again we will use a function to display a character on LCD with a specific PORT.

void display(char a) {  PORTC=a;  PORTD=0b11000000;  delay();  PORTD=0b01000000; }

Main Function

Okay, Here is the main function of our program it seems a lot of code but its very simple and easy. interiores . Its just a code of keypad as we did in 7 segment display (Not the exact same code but logic is almost same). You can find it in my post Interfacing a 12 Button Keypad with Microcontroller.So here is the code and description of code is given below

void main() { DDRA=0xff;       DDRB=0b11111000; DDRC=0xff; PORTA=0x07; PORTB=0x00; PORTC=0x00; DDRD=0xff; command(0x38); command(0x0e); while(1) { switch(PORTA) { case 0b00000111: PORTA=0b00001011; break; case 0b00001011: PORTA=0b00001101; break; case 0b00001101: PORTA=0b00001110; break; case 0b00001110: PORTA=0b00000111; break; } if(PINB!=7) { switch(PINB) { case 0b00000011: if(PORTA==0b00000111) {display('1'); break;} if(PORTA==0b00001011) {display('3'); break;} if(PORTA==0b00001101) {display('7'); break;} if(PORTA==0b00001110) {display('A'); break;} case 0b00000101: if(PORTA==0b00000111) {display('2'); break;} if(PORTA==0b00001011) {display('5'); break;} if(PORTA==0b00001101) {display('8'); break;} if(PORTA==0b00001110) {display('0'); break;} case 0b00000110: if(PORTA==0b00000111) {display('3'); break;} if(PORTA==0b00001011) {display('6'); break;} if(PORTA==0b00001101) {display('9'); break;} if(PORTA==0b00001110) {display('B'); break;} } while(PINB!=7){} } } }
  • Command 0×38 means that we are using both lines of LCD. Almost all useful command are given in Part1 of this post which is : Introduction to LCD (Interfacing a LCD with Microcontroller) Part 1
  • if(PINB!=7) is for turn on LCD press each button from every column. For example to turn on LCD first you have to press “1″ , “2″ and “3″.

Let’s Do Some More

We have following tasks:

  • LCD just display characters on 1st line and does not move to next line. Write a microcontroller code for same circuit which can switch to next line if first line is full of character

  • Now modify the above task so that you can use # character to clear whole LCD again and * character to produce a space (space bar) between characters.

So we can do it simply as for next line

if(count==16){command(0xc0);  //0xco command is used to move on next line}

and in each case statement we will use.

count +=1;

and for second task we will used following commands

command(0x14); //For spacecommand(0x01); //Clear Whole Screen

Hope you will find this post useful for any help or problem please let me know by posting you comments. If there is any mistake please point it as there is no one perfect in this world except Allah.

VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
LCD interface with Microcontroller using C Language (Part 2), 5.0 out of 5 based on 1 rating
If you have any question please post here