Saturday 26 April 2014

LCD 4 BIT INTERFACING WITH AVR MICROCONTROLLERS ATMEGA8,16,32,64

LCD 4 BIT INTERFACING WITH AVR MICROCONTROLLERS ATMEGA8,16,32,64

   This is the header file for LCD 4-bit mode interfacing with AVR Microcontroller series such as Atmega 16, 32, 64, 8, etc. It is coded in GCC & supports all AVR GCC compliers.

   This is designed for 16×2 LCD, but can be exteneded upto 20×4 LCD.
   Contains all functions with different cursor styles, string display from ram & flash rom, bcd, signed & unsigned  integer 8-bit and 16-bit numbers, shift left, shift right, etc.


The different LCD commands are as follows,
ELECDUDE: LCD-commands

To use it in your design, just replace the PORTs which you need for connection, then it is ready to go….

/* // // // // // // // // // // // // // // // // // // // // // // // // //
// Author: ElecDude
//         admin@elecdude.com
//
// Copyright – 2014 – ElecDude
//     www.elecdude.com    www.elecdude.info    www.forum.elecdude.info
//
//
DISCLAIMER:
//
// THIS SOURCE FILE MAY BE USED AND DISTRIBUTED WITHOUT
// RESTRICTION PROVIDED THAT THIS COPYRIGHT STATEMENT IS NOT
// REMOVED FROM THE FILE AND THAT ANY DERIVATIVE WORK CONTAINS
// THE ORIGINAL COPYRIGHT NOTICE AND THE ASSOCIATED DISCLAIMER.
//
// This is provided without any  express or implied warranties,
// including, but not limited  to, the implied warranties of merchantability
// and fitnessfor a particular purpose. FOR EDUCATIONAL PURPOSE ONLY.
//
// // // // // // // // // // // // // // // // // // // // // // // // // //
List of funtions used in this Header file
void LCD_init(uint8_t style);
LCD_OFF     display off
COFF           display on, cursor off
CBLINK         display on, cursor off,block blink
CULINE        display on, cursor on
LCD_clearall();
LCD_home();
LCD_movleft();
LCD_movright();
LCD_putuint8(i);    //3 digit UNSINGED
LCD_putuint16(i);    //5 digit  UNSINGED

LCD_gotoxy(uint8_t x,uint8_t y);
LCD_putsXY(x,y,msg);
LCD_puts2XY(x,y,msg);
LCD_putcXY(x,y,ch);
LCD_putvXY(x,y,ch);    //disp 1 digit BCD number
LCD_putuint8XY(x,y,i);     //3 digit SINGED
LCD_putuint61XY(x,y,i);  //5 digit SINGED

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <avr/io.h>
#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>

#ifndef _LCD4B_H
#define _LCD4B_H
/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
LCD CONNECTIONS
* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
//d0-d9 = 7-14
#define LCD_DATA_DDR     DDRC
#define LCD_DATA_PORT     PORTC
#define LCD_DATA_PIN    PINC
//rs=4
#define LCD_RS_PORT     PORTB
#define LCD_RS_DDR         DDRB
#define LCD_RS_POS         0
//rw=5
#define LCD_RW_PORT     PORTD
#define LCD_RW_DDR         DDRD
#define LCD_RW_POS         6
//e=6
#define LCD_E_PORT         PORTD
#define LCD_E_DDR         DDRD
#define LCD_E_POS         7

/* * ** ** ** ** ** ** ** ** ** ** *    DEFINITIONS   * ** ** ** ** **  */
#define LCD_OFF         0×08   /* display off                           */
#define COFF               0x0C   /* display on, cursor off                */
#define CBLINK             0x0D   /* display on, cursor off,block blink    */
#define CULINE            0x0E   /* display on, cursor on                 */

#define LM_4B2L_5x7      0×28   /* 5×7 dots, 4 bit 2 lines                  */
#define LM_4B2L_5x10      0x2C   /* 5×10 dots, 4 bit 2 lines                 */
#define LM_8B2L_5x7       0×38   /* 5×7 dots, 8 bit 2 lines                  */
#define LM_8B2L_5x10       0x3C   /* 5×10 dots, 8 bit 2 lines                 */

#define MOVE_CLEFT        0×10   /* move cursor left  (decrement)         */
#define MOVE_CRIGHT        0×14   /* move cursor right (increment)         */
#define CLEAR            0×01   /* Clear memory, display & home cursor    */
#define HOME                0×02   /* Clear display only & home cursor         */

/* * ** ** ** ** ** ** ** ** *    M A C R O S     * ** ** ** ** ** ** ** ** ** */
#ifndef BIT
#define BIT(x)    _BV(x)
#endif

#ifndef SETBIT
#define SETBIT(x,b)     x|=_BV(b);
#endif

#ifndef CLEARBIT
#define CLEARBIT(x,b)     x&=~_BV(b);
#endif

#define SET_E()  SETBIT(LCD_E_PORT,LCD_E_POS)
#define SET_RS() SETBIT(LCD_RS_PORT,LCD_RS_POS)
#define SET_RW() SETBIT(LCD_RW_PORT,LCD_RW_POS)

#define CLEAR_E()  CLEARBIT(LCD_E_PORT,LCD_E_POS)
#define CLEAR_RS() CLEARBIT(LCD_RS_PORT,LCD_RS_POS)
#define CLEAR_RW() CLEARBIT(LCD_RW_PORT,LCD_RW_POS)

/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
This function to put the data/command byte to LCD                              */
void LCD_put(uint8_t byt,uint8_t cmd_datb)
{
uint8_t hn; // high Nibble
uint8_t temp;

hn=byt>>4;
byt=(byt & 0x0F);

if(cmd_datb==0)
{ CLEAR_RS(); }
else
{ SET_RS(); }

_delay_us(1);
//Send high nibble
SET_E();
temp=(LCD_DATA_PORT & 0XF0)|(hn);
LCD_DATA_PORT=temp;
_delay_us(1);
CLEAR_E();
_delay_us(1);
//Send the lower nibble
SET_E();
temp=(LCD_DATA_PORT & 0XF0)|(byt);
LCD_DATA_PORT=temp;
_delay_us(1);
CLEAR_E();
//Wait till LCD process the data
_delay_us(80);
if(cmd_datb==0)    _delay_us(10);
}
/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
#define LCD_cmdbyt(c)         (LCD_put(c,0))
#define LCD_databyt(d)         (LCD_put(d,1))

#define LCD_setcur(cr) LCD_cmdbyt(cr)
/*#define LCD_setcur(cr) {\
LCD_cmdbyt(cr);\
_delay_us(20);\
}*/
/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
LCD INITIALISATION FUNCTION
This function Initializes the lcd module Must be called before calling lcd related functions
Arguments:
style => cursor off/underline/blink                                                         */
void LCD_init(unsigned char style)
{
//After power on Wait for LCD to Initialize
_delay_ms(30);
//Set IO Ports
LCD_DATA_DDR|=(0x0F);
SETBIT(LCD_E_DDR,LCD_E_POS)
SETBIT(LCD_RS_DDR,LCD_RS_POS)
SETBIT(LCD_RW_DDR,LCD_RW_POS)
//clear ports
LCD_DATA_PORT&=0xF0;
CLEAR_E();
CLEAR_RW();
CLEAR_RS();
_delay_us(1);
//start init
SET_E();
LCD_DATA_PORT|=(0×02);
_delay_us(1);
CLEAR_E();
_delay_us(1);
//Wait for LCD to execute the Functionset Command
_delay_us(130);
//Set Display mode & Cursor style
LCD_cmdbyt(LM_4B2L_5x7);
LCD_setcur(style);
}

/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
This function moves the cursor to the intended location
Arguments:
1) x    : 0-15 (16 characters)
2) y    : 0 or 1     (0= line1  &  1= line2)                                */
void LCD_gotoxy(uint8_t x,uint8_t y)
{
if(y) x=x+0xC0;
else x=x+0×80;  //1st line 0×80, 2nd line 0xC0
LCD_cmdbyt(x);
_delay_us(3);
}

/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
This function Writes a given string to lcd at the current cursor location.
Arguments:
msg: a null terminated string to print                                           */
void LCD_puts(const char *msg)
{
while(*msg !=’\0′) {
LCD_databyt(*msg);
msg++;
}
}

void LCD_putsXY(uint8_t x,uint8_t y,const char *msg)
{
LCD_gotoxy(x,y);
while(*msg !=’\0′) {
LCD_databyt(*msg);
msg++;
}
}

/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
This function to Clear the display                                                */
void LCD_clearall() {
LCD_cmdbyt(CLEAR);
_delay_ms(5);
}

/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
This function to move the cursor at 1st position (0xC0                            */
#define LCD_home() LCD_cmdbyt(HOME)

/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
This function to move the cursor at 1st position (0xC0                            */
#define LCD_clrhome() LCD_cmdbyt(0×08)

/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
This function to move the cursor left by 1                                        */
#define LCD_movleft()      LCD_cmdbyt(MOVE_CLEFT)

/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
This function to move the cursor right by 1                                        */
#define LCD_movright()     LCD_cmdbyt(MOVE_CRIGHT)

/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
This function displays a character at XY                                        */
#define LCD_putcXY(x,y,ch) {\
LCD_gotoxy(x,y);\
LCD_put(ch,1);\
}
/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
This function displays a single digit nnmber or BCD number at XY                */
#define LCD_putvXY(x,y,ch) {\
LCD_gotoxy(x,y);\
LCD_put((ch|0×30),1);\
}

// * * * * * *   This produces a smooth faded filling display effect  * * * * * * //
void LCD_puts2XY(uint8_t x,uint8_t y,const char *msg)
{
LCD_gotoxy(x,y);
while(*msg!=’\0′)    {
LCD_databyt(*msg);
msg++;
_delay_ms(80);
}
}

/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
THIS DISPLAY TEXT FROM FLASH (PROGRAM MEMORY)
Display string from program memory without auto linefeed
Input:     string from program memory be be displayed                             */

#include <avr/pgmspace.h>
#define LCD_putsPXY(x,y,str) {\
LCD_gotoxy(x,y);\
LCD_putsP(str);\
}
#define LCD_putsP(__s)         LCD_puts_pstr(PSTR(__s))
void LCD_puts_pstr(const char *progmem_s)
{
register char c;
while ( (c = pgm_read_byte(progmem_s++)) ) {
LCD_databyt(c);
}

}
/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
Displays the decimal 3digit value of 8bit unsigned number
Input:         The num to be diplayed                                        */

void LCD_putuint8(uint8_t i)
{
char s[4];
s[3]=’\0′;
s[2]= (i%10) | 0×30;//ones     s
i/=10;
s[1]= (i%10) | 0×30;//tens    s+1
s[0]= (i/10) | 0×30;//hund    s+2
LCD_puts(s);
}
void LCD_putint8XY(uint8_t x,uint8_t y,int8_t i)
{
LCD_gotoxy(x,y);
if(i<0)
{ LCD_databyt(‘-’); i=0-i; }
LCD_putuint8(i);
}
#define LCD_putintXY(x,y,i) {\
LCD_gotoxy(x,y);\
LCD_putuint8(i);\
}

/* * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
This function displays (5 digit) 16bit integer number.                                        */

void LCD_putuint16(uint16_t i)
{
char s[6]; //=”12345″;
s[5]=’\0′;
s[4]= (i%10) | 0×30;
i/=10;
s[3]= (i%10) | 0×30;
i/=10;
s[2]= (i%10) | 0×30;
i/=10;
s[1]= (i%10) | 0×30;
s[0]= (i/10) | 0×30;
LCD_puts(s);
}

void LCD_putint16XY(uint8_t x,uint8_t y,int16_t i)
{
LCD_gotoxy(x,y);
if(i<0)
{ LCD_databyt(‘-’); i=0-i; }
LCD_putuint16(i);
}

//_________________________________________________________________________________//
#endif



0 comments:

Post a Comment

Search Here...