Microcontroller › 8051 › Matrix display › #include
#include<reg51.h>
sbit rs = P3^0;
sbit rw = P3^1;
sbit en = P3^2;
sbit busy = P2^7;
#define TRUE 1
#define FALSE 0
void lcdready();
void clearscreen();
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void lcdinitialize(void);
void lcddisplay(char str[], unsigned int k);
unsigned int keydetect (void);
char numdetect(char i);
void operate(char i);
void numprint(long float i);
sbit col0 = P1^0;
sbit col1 = P1^1;
sbit col2 = P1^2;
sbit col3 = P1^3;
unsigned char num1;
unsigned int j=16;
unsigned char str[]=”enter 2nd matrix”;
int c=0;
int d=0;
char sign=0;
char co,lo=0;
void main()
{
char i;
int p=0;
unsigned int j=16;
unsigned char str[] = “enter 1st matrix”;
unsigned char a[2][2];
int k,m;
lcdinitialize();
lcddisplay(str, j);
while(1)
{
i=keydetect ();
p++;
if(i!=0){
numdetect(i);
a[0][0]=num1;
if(p==1){
k=a[0][0];
lcddata(k);}
}
}
return ;
}
void lcdready()
{
busy = 1;
rs = 0;
rw = 1;
en = 0;
en = 1;
while(busy==1)
{
en = 0;
en = 1;
}
return;
}
void lcdcmd(unsigned char value)
{
lcdready();
P2 = value;
rs = 0;
rw=0;
en=1;
en=0;
return;
}
void lcddata(unsigned char value)
{
lcdready();
P2 = value;
rs = 1;
rw=0;
en=1;
en=0;
return;
}
void lcdinitialize(void)
{
lcdcmd(56);
lcdcmd(14); // display on, cursor blinking
lcdcmd(1);
lcdcmd(6);
lcdcmd(0x80); //line 1, position 0
}
void lcddisplay(char str[], unsigned int k)
{
unsigned int i;
for(i=0; i<k; i++)
lcddata(str);
}
unsigned int keydetect (void){
unsigned char key=0;
char i;
for (i=0; i<4; i++){
P1 &= ~(0x80>>i);
if(col0 == 0){
key = P1;
while(col0 == 0);
return key;
}
if(col1 == 0){
key = P1;
while(col1 == 0);
return key;
}
if(col2 == 0){
key = P1;
while(col2 == 0);
return key;
}
if(col3 == 0){
key = P1;
while(col3 == 0);
return key;
}
P1 |= (0x80>>i);
}
return 0;
}
char numdetect(char var)
{
switch (var)
{
case 0xEE:
lcddata(‘7’);
num1 =7;
lcdcmd(0x14);
c++;
if(c==2){
lcdcmd(0xC0);
c=0; }
break;
case 0xED:
lcddata(‘8’);
num1=8;
lcdcmd(0x14);
c++;
if(c==2){
lcdcmd(0xC0);
c=0;}
break;
case 0xEB:
lcddata(‘9’);
num1=9;
lcdcmd(0x14);
c++;
if(c==2){
lcdcmd(0xC0);
c=0;}
break;
case 0xDE:
lcddata(‘4’);
num1=4;
lcdcmd(0x14);
c++;
if(c==2){
lcdcmd(0xC0);
c=0;}
break;
case 0xDD:
lcddata(‘5’);
num1=5;
lcdcmd(0x14);
c++;
if(c==2){
lcdcmd(0xC0);
c=0;}
break;
case 0xDB:
lcddata(‘6’);
num1=6;
lcdcmd(0x14);
c++;
if(c==2){
lcdcmd(0xC0);
c=0;}
break;
case 0xBE:
lcddata(‘1’);
num1=1;
lcdcmd(0x14);
c++;
if(c==2){
lcdcmd(0xC0);
c=0;}
break;
case 0xBD:
lcddata(‘2’);
num1=2;
lcdcmd(0x14);
c++;
if(c==2){
lcdcmd(0xC0);
c=0;}
break;
case 0xBB:
lcddata(‘3’);
num1=3;
lcdcmd(0x14);
c++;
if(c==2){
lcdcmd(0xC0);
c=0;}
break;
case 0x7D:
lcddata(‘0’);
num1=0;
lcdcmd(0x14);
c++;
if(c==2){
lcdcmd(0xC0);
c=0;
break;
case 0x7E:
clearscreen();
num1=-1;
break;
case 0x7B:
lcddisplay(str,j);
num1=-1;
break;
}
return num1;
}
}
void clearscreen( void )
{
unsigned int i;
lcdcmd(0x01);
return;
}