Saturday, January 12, 2013

EMBEDDED SYSTEMS TUTORIAL 9- INTERFACING 16X2 LCD WITH AVR PART-2




Hi…. friends this time I am here with another article ,we are going to discuss some interesting functions and projects of LCD. In this tutorial we will learn about rotating a text , displaying integer and integers operations, displaying floating point numbers and their operations .

ROTATING A TEXT STRING
 
Rotating a text on LCD is a bit tricky thing, to do so we will change the position of text every time with some delay after clearing the screen so it will look like text is moving the sample code is given below just use this source code in your LCD project and text will look like moving-

/*
 * MOVINGTEXT.c
 *
 * Created: 1/12/2013 8:36:31 PM
 *  Author: ABHILASH DIXIT
 */


#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lcd.h"
#include "lcd.c"

int main(void)
{
       while(1)
       {
             lcd_init(LCD_DISP_ON);
             for(int i=-10;i<16;i++)     // setting an integer i to 0 and incrementing its value till 16
             {
                    lcd_gotoxy(i,0);       // setting cursor to (i,0)
                    lcd_puts("LCD"); // to display text
                    if(i==13)
                    i=0;
                    _delay_ms(100);              // some delay
                    lcd_clrscr();            //  clear display
                   
             }
       }
}


So this was the  trick to rotate the text string on the LCD.

USING INTEGERS ON LCD

You cannot use a integer on LCD directly  for this you have to use a function itoa(), it converts an integer in string form. So if we want to use integers in LCD we will first create a buffer of some size as

char buf[8];

now lets suppose the number to be displayed is i , so we will use itoa() function for i to convert it in string form as

itoa(i,buf,10);

here 10 is for decimal, for hexadecimal we will use 16 and for binary we will use 2.
Now finally i can be displayed using lcd_puts() command as-
lcd_puts(buf);


 so lets try a source code – in this program we will display a number i=0 and will increase its value till 10.

/*
 * DISPLAY INTEGER.c
 *
 * Created: 1/12/2013 9:16:31 PM
 *  Author: ABHILASH DIXIT
 */

#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "lcd.h"
#include "lcd.c"
#include <util/delay.h>
int main()
{
       while(1)
       {
             lcd_init(LCD_DISP_ON);   // initializing display
             int i=0;                  // setting value of integer i to 0
             char buf[8];              // initializing buffer
             lcd_gotoxy(0,0);           // setting cursor position to (0,0)
             lcd_puts("i=");            // displaying text
             for(i=0;i<=9;i++)          // increasing value of i till 9
             {
                    itoa(i,buf,10);            // using itoa() function
                    lcd_gotoxy(2,0);             // setting cursor position to (2,0)
                    lcd_puts(buf);             // displaying value of i every time
                    _delay_ms(100);            // some delay
                   
             }
       }
}

USING A FLOATING POINT NUMBER

Like an integer a floating point number cannot be used directly too , so to use an floating point no on LCD we have to use some function called  dtostrf() it converts an floating point number into string . let us suppose we have to display a floating point number say i on LCD.  So first of all we have to create a buffer of some size

char buf[8];

then we will use dtostrf () function with i to convert i in string form as

dtostrf(i,10,6,buf);

here 10 is for decimal and 6 is denoting numbers after decimal we can change it accordingly.

Now the no i can be displayed using lcd_puts() command 

Here is the source code for simply displaying a floating point number i=1.12345


/*
 * DISPLAY FLOAT.c
 *
 * Created: 1/12/2013 9:26:31 PM
 *  Author: ABHILASH DIXIT
 */

#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "lcd.h"
#include "lcd.c"
int main()
{
       lcd_init(LCD_DISP_ON);
       float i=1.12345;
       char buf[8];
       dtostrf(i,10,6,buf);
       lcd_puts(buf);
}


NOTE:-  to use itoa() and dtostrf() we must include stdlib.h header file in beginning of the program .


I hope it was a useful article for you all……in case of any query, doubt or critical view just leave a comment……..in next tutorial we well discuss about some other embedded systems topics till then ………..bye..

THNX…..:)

No comments:

Post a Comment