Aplikasi LCD pada Sasakala-M32 Board

Postingan kali ini akan membahas bagaimana mengintegrasikan Sasakal Board dengan sebuah LCD 16x2.
Konfigurasi rangkaiannya dapat dilihat pada gambar berikut
Klik disini untuk memperbesar gambar
Rutin programnya adalah sebagai berikut:
/*
    == AVR-Sasakala - Port I/O Demo ==
************************************************

Hardware:
    - [Sasakala-M32 Board]
    - LED 8x
     - Resistor 470R 8x

************************************************
        Embedtronix (C)201009
*/

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

#define LDP  PORTA    //LCD Data Port
#define LCP  PORTB    //LCD Control pins
#define LDDR DDRA    //direction register for LCD data port
#define LCDR DDRB    //direction register for LCD control pins
#define LCD_RS    0     //pin LCD RS
#define LCD_RW    1     //pin LCD R/W
#define LCD_E    2    //pin LCD E
#define LCD_D0    0    //pin LCD D0
#define LCD_D1    1    //pin LCD D1
#define LCD_D2    2    //pin LCD D1
#define LCD_D3    3    //pin LCD D2
#define LCD_D4    4    //pin LCD D3
#define LCD_D5    5    //pin LCD D4
#define LCD_D6    6    //pin LCD D5
#define LCD_D7    7    //pin LCD D6

#define LCD_CLR       0    //DB0: clear display
#define LCD_HOME      1    //DB1: return to home position
#define LCD_CGRAM     6    //DB6: set CG RAM address
#define LCD_DDRAM     7    //DB7: set DD RAM address

//#define LCDClear() LCDsendCommand(0b00000001)
//#define LCDHome() LCDsendCommand(0b00000010);

// cursor position to DDRAM mapping
#define LCD_LINE0_DDRAMADDR        0x00
#define LCD_LINE1_DDRAMADDR        0x40
#define LCD_LINE2_DDRAMADDR        0x14
#define LCD_LINE3_DDRAMADDR        0x54


//=========================================================
void LCDinit(void)//Initializes LCD
{
_delay_ms(15);
LDP=0x00;
LCP=0x00;
LDDR|=1<<LCD_D7|1<<LCD_D6|1<<LCD_D5|1<<LCD_D4|1<<LCD_D3
|1<<LCD_D2|1<<LCD_D1|1<<LCD_D0;
LCDR|=1<<LCD_E|1<<LCD_RW|1<<LCD_RS;
//---------one------
LDP=0<<LCD_D7|0<<LCD_D6|1<<LCD_D5|1<<LCD_D4|0<<LCD_D3
|0<<LCD_D2|0<<LCD_D1|0<<LCD_D0; //8 it mode
LCP|=1<<LCD_E|0<<LCD_RW|0<<LCD_RS;        
_delay_ms(1);
LCP&=~(1<<LCD_E);
_delay_ms(1);
//-----------two-----------
LDP=0<<LCD_D7|0<<LCD_D6|1<<LCD_D5|1<<LCD_D4|0<<LCD_D3
|0<<LCD_D2|0<<LCD_D1|0<<LCD_D0; //8 it mode
LCP|=1<<LCD_E|0<<LCD_RW|0<<LCD_RS;        
_delay_ms(1);
LCP&=~(1<<LCD_E);
_delay_ms(1);
//-------three-------------
LDP=0<<LCD_D7|0<<LCD_D6|1<<LCD_D5|1<<LCD_D4|0<<LCD_D3
|0<<LCD_D2|0<<LCD_D1|0<<LCD_D0; //8 it mode
LCP|=1<<LCD_E|0<<LCD_RW|0<<LCD_RS;        
_delay_ms(1);
LCP&=~(1<<LCD_E);
_delay_ms(1);
//--------8 bit dual line----------
LDP=0<<LCD_D7|0<<LCD_D6|1<<LCD_D5|1<<LCD_D4|1<<LCD_D3
|0<<LCD_D2|0<<LCD_D1|0<<LCD_D0; //8 it mode
LCP|=1<<LCD_E|0<<LCD_RW|0<<LCD_RS;        
_delay_ms(1);
LCP&=~(1<<LCD_E);
_delay_ms(1);
//-----increment address, cursor shift------
LDP=0<<LCD_D7|0<<LCD_D6|0<<LCD_D5|0<<LCD_D4|1<<LCD_D3
|1<<LCD_D2|1<<LCD_D1|0<<LCD_D0; //8 it mode
LCP|=1<<LCD_E|0<<LCD_RW|0<<LCD_RS;        
_delay_ms(1);
LCP&=~(1<<LCD_E);
_delay_ms(5);
}

//=========================================================
void LCDsendCommand(uint8_t cmd)    //Sends Command to LCD
{
LDP=cmd;
LCP|=1<<LCD_E;        
_delay_ms(1);
LCP&=~(1<<LCD_E);
_delay_ms(1);    
}

//=========================================================
void LCDsendChar(uint8_t ch)        //Sends Char to LCD
{
LDP=ch;
LCP|=1<<LCD_RS;
LCP|=1<<LCD_E;        
_delay_ms(1);
LCP&=~(1<<LCD_E);    
LCP&=~(1<<LCD_RS);
_delay_ms(1);
}

//=========================================================
void LCDclr(void)                    //Clears LCD
{
LCDsendCommand(1<<LCD_CLR);
}

//=========================================================
void LCDhome(void)            //LCD cursor home
{
LCDsendCommand(1<<LCD_HOME);
}

//=========================================================
void LCDWriteString(const char *msg)
{
while(*msg!='\0')
{
LCDsendChar(*msg);
msg++;
}
}

//===================================================================
void LCDWriteInt(int val,unsigned int field_length)
{
char str[5]={0,0,0,0,0};
int i=4,j=0;
while(val)
{
str[i]=val%10;
val=val/10;
i--;
}
if(field_length==-1)
while(str[j]==0) j++;
else
j=5-field_length;

if(val<0) LCDsendChar('-');
for(i=j;i<5;i++)
{
LCDsendChar(48+str[i]);
}
}

//=========================================================
void LCDGotoXY(uint8_t x, uint8_t y)    //Cursor to X Y position
{
register uint8_t DDRAMAddr;
// remap lines into proper order
switch(y)
{
case 0: DDRAMAddr = LCD_LINE0_DDRAMADDR+x; break;
case 1: DDRAMAddr = LCD_LINE1_DDRAMADDR+x; break;
case 2: DDRAMAddr = LCD_LINE2_DDRAMADDR+x; break;
case 3: DDRAMAddr = LCD_LINE3_DDRAMADDR+x; break;
default: DDRAMAddr = LCD_LINE0_DDRAMADDR+x;
}
// set data address
LCDsendCommand(1<<LCD_DDRAM | DDRAMAddr);

}

//=========================================================
void WritetoLCD(x,y,msg) 
{
LCDGotoXY(x,y-1);
LCDWriteString(msg);
}

//=========================================================
void LCDWriteIntXY(x,y,value,FieldLength) 
{
LCDGotoXY(x,y-1);
LCDWriteInt(value,FieldLength);
}

//=========================================================
void delay1s(void)
{
uint8_t i;
for(i=0;i<100;i++)
{
_delay_ms(10);
}
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void main()
{
unsigned char i;

LCDinit();                    //Initialize LCD module

while(1)
{

LCDclr();                    //Clear the screen

WritetoLCD(0,1,"[Sasakala Board]");
WritetoLCD(0,2,"Embedtronix.com");
delay1s();                    //Wait

LCDclr();                            //Clear the screen
WritetoLCD(0,1,"Waiting... ");        //String on line 1
WritetoLCD(0,2,"Loading ");            //String on line 2

//Print some numbers
for (i=0;i<99;i+=1)
{
LCDWriteIntXY(9,2,i,2);
WritetoLCD(11,2,"%");
_delay_loop_2(0);    
_delay_loop_2(0);    
//_delay_loop_2(0);    
//_delay_loop_2(0);    
}

LCDclr();                            //Clear the screen
WritetoLCD(0,1,"SasakalaLCD Demo");
WritetoLCD(3,2,"Mode 8-bit");
delay1s();                    //Wait
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



Sebelum mencobanya langsung pada Sasakala Board, kita coba dulu membuat simulasinya di Proteus. Berikut rangkaian dan screenshot pada saat simulasi di Proteus





Setelah sukses baru kita coba langsung ke Sasakala board


Selamat mencoba !


Download Sasakala-LCD-8bit dalam project AVR Studio lengkap dengan simulasi Proteus

Tidak ada komentar:

Posting Komentar

author
Embedtronix
Build Your Embedded System. Belajar bareng ngoprek Microcontroller, Arduino, TPLink, GLiNet dan ilmu Microelectronic lainnya.