Saturday, 6 December 2014

Embedded C program for Graphical LCD(GLCD) in 8051

#define glcd P2
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;

sbit RST=P2^3;
sbit CS1=P2^4;
sbit CS2=P2^5;
#define PAGE_0 0xb8		//first page address
#define PAGE_7 0xbf		//last page address
#define COL_ADDR 0x40	//base address of 64 columns

#define NONE 0
#define CHIP1 1
#define CHIP2 2
#define BOTH 3

//primary drives proto types
void InitGLCD(void);
void CmdGLCD(unsigned char,unsigned char);
void DispGLCD(unsigned char);
void WriteGLCD(unsigned char);

void ChipSel(unsigned char);

//secondary driver proto types
void ClrScreen();
void ClrPage(unsigned char,unsigned char);
void ShowDigit(unsigned char);
void ShowAlphabet(unsigned char);

//api proto types
void Puts_GLCD(char *);
void Puti_GLCD(unsigned int);
void Putf_GLCD(float f);
void ShowImage(void);

/////////////////////////////////////////
void InitGLCD(void)
{
RST=0;
RST=1;		//high clock for reset lcd controllers
CmdGLCD(0x3e,BOTH);		//to turn it OFF
CmdGLCD(0x3f,BOTH);		//to display ON
CmdGLCD(0xb8,BOTH);		//page '0' base address
CmdGLCD(0x40,BOTH);		//column base address
CmdGLCD(0xc0,BOTH);		//take cursor to top line of refresh RAM
}
///////////////////////////////////////
void CmdGLCD(unsigned char cmd,unsigned char chip)
{
ChipSel(chip);
RS=0;
WriteGLCD(ch);
}
////////////////////////////////////////
void DispGLCD(unsigned char ch)
{
RS=1;
WriteGLCD(ch);
}
//////////////////////////////////////
void WriteGLCD(unsigned char val)
{
RW=0;
GLCD=val;
EN=1;
EN=0;
}
////////////////////////////////////////
void ChipSel(unsigned char cs)
{
switch(cs)
{
case 0:CS1=0;CS2=0; return;
case 1:CS1=1;CS2=0; return;
case 2:CS1=0;CS2=1; return;
case 3:CS1=1;CS2=1; return;
}
}
////////////////////////////////////////
void ClrScreen()
{
unsigned char i,y;
for(i=PAGE_0;i<=PAGE_7;i++)
{
CmdGLCD(i,BOTH); //select pages
CmdGLCD(COL_ADDR,BOTH);
for(y=0;y<64;y++)
DispGLCD(0x00);		//clear data in GLCD
}
}
///////////////////////////////////////
void ClrPage(unsigned char page,unsigned char chip)
{
unsigned char y;
CmdGLCD(PAGE_0+page,chip);
CmdGLCD(COL_ADDR,chip);
for(y=0;y<64;y++)
DispGLCD(0x00);		//clear data in GLCD
}
///////////////////////////////////////
code unsigned char digit_arr[10][4]=
{
	0xff,0x81,0x81,0xff,		//0
	0xff,0x00,0x00,0x00,		//1
	0xf9,0x89,0x89,0x8f,		//2
	0x89,0x89,0x89,0xff,		//3
	0x0f,0x08,0x08,0xff,		//4
	0x8f,0x89,0x89,0xf9,		//5
	0xff,0x89,0x89,0xf9,		//6
	0x01,0x01,0x01,0xff,		//7
	0xff,0x89,0x89,0xff,		//8
	0x9f,0x91,0x91,0xff,		//9
};
//////////////////////////////////////
void ShowDigit(unsigned char dig)
{
unsigned char i;
for(i=0;i<4;i++)
{
DispGLCD(digit_arr[dig][i]);
}
DispGLCD(0x00);		//space between each digit
}
//////////////////////////////////////
void Puti_GLCD(unsigned int n)
{
char a[5]={0},i=0;
if(n==0)
{
ShowDigit(0);
return;
}
while(n)
{
a[i++]=(n%10);
n=n/10;
}
for(--i;i>=0;i--)
{
ShowDigit(a[i]);
}
}
///////////////////////////////////

code unsigned char alpha_arr[26][4]=
{
	0xfe,0x09,0x09,0xfe,	//A
	0xff,0x89,0x89,0xf6,	//B
	0xff,0x81,0x81,0x81		//C
};
///////////////////////////////////
void ShowAlphabet(char c)
{
unsigned char i;
for(i=0;i<4;i++)
{
DispGLCD(alpha_arr[c-'A'][i]);
}
DispGLCD(0x00);		//to provide spacing
}
////////////////////////////////////
void Puts_GLCD(char *s)
{
while(*s)
ShowAlphabet(*s++);
}
///////////////////////////////////
void Putf_GLCD(float f)
{
int i;
unsigned char j;
i=f;
Puti_GLCD(i);
DispGLCD(0x80);
for(j=0;j<3;j++)
{
f=(f-i)*10;
i=f;
Puti_GLCD(i);
}
}
///////////////////////////////

/*void ShowImage()
{
unsigned char i,j;
for(i=0;i<8;i++)
{
CmdGLCD(0xb8+i,BOTH);
CmdGLCD(0x40,BOTH);
ChipSel(1);
for(j=0;j<128;j++)
{
if(j==64)
{
ChipSel(2);
}
DispGLCD(arr[i][j]);
}
}
}*/

Glcdmain.c
#include<reg51.h>
//#include"glcd.h"
//#include"delay.h"
void main(void)
{
unsigned char i;
InitGLCD();
CmdGLCD(PAGE_0,BOTH);
CmdGLCD(COL_ADDR,BOTH);
Puti_GLCD(1234);
ShowAlphabet('A');
ShowDigit('5');
Puts_GLCD("HAI");
while(1);
}