|
#include <STC12C5A60S2.H>
#include <math.h>
#define uint unsigned int
#define uchar unsigned char
#define BIT0 0x01
#define BIT1 0x02
#define BIT2 0x04
#define BIT3 0x08
#define BIT4 0x10
#define BIT5 0x20
#define BIT6 0x40
#define BIT7 0x80
#define P0OUT P0
#define P1OUT P1
#define P2OUT P2
#define P3OUT P3
#define P4OUT P4
sbit LCD_EN=P2^7; //使能
sbit LCD_RW=P2^6; //讀寫選擇
sbit LCD_RS=P2^5; //數(shù)據(jù)命令選擇
uchar bdata change;
uchar ch;
uchar code num[]={"0123456789ABCDEF"};
uchar key_t,key,t;
sbit change_0 = change^0;
sbit change_1 = change^1;
sbit change_2 = change^2;
sbit change_3 = change^3;
sbit change_4 = change^4;
sbit change_5 = change^5;
sbit change_6 = change^6;
sbit change_7 = change^7;
sbit P00 = P0^0;
sbit P01 = P0^1;
sbit P02 = P0^2;
sbit P03 = P0^3;
sbit P04 = P0^4;
sbit P05 = P0^5;
sbit P06 = P0^6;
sbit P07 = P0^7;
void changeIO(uchar data1)
{
change=data1;
P00 = change_7;
P01 = change_6;
P02 = change_5;
P03 = change_4;
P04 = change_3;
P05 = change_2;
P06 = change_1;
P07 = change_0;
}
uchar inv(uchar data1)
{
uchar t;
change=data1;
data1=0;
data1 |= (BIT0&change_7);
t=BIT0&change_6;data1 |= t<<1;
t=BIT0&change_5;data1 |= t<<2;
t=BIT0&change_4;data1 |= t<<3;
t=BIT0&change_3;data1 |= t<<4;
t=BIT0&change_2;data1 |= t<<5;
t=BIT0&change_1;data1 |= t<<6;
t=BIT0&change_0;data1 |= t<<7;
return data1;
}
void delayms(uint xms) //1MS延時
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void LCD_write_com(uchar comm) //寫命令
{
LCD_RS=0;
LCD_RW=0;
changeIO(comm);
delayms(5);
LCD_EN=0;
LCD_EN=0;
LCD_EN=1;
delayms(5);
}
void LCD_write_data(uchar lcd_data) //寫數(shù)據(jù)
{
LCD_RS=1;
LCD_RW=0;
changeIO(lcd_data);
delayms(5);
LCD_EN=0;
LCD_EN=0;
LCD_EN=1;
delayms(5);
}
uchar LCD_read_data() //讀數(shù)據(jù)
{
uchar t;
P0OUT =0xff;
LCD_RS=1;
LCD_RW=1;
delayms(5);
LCD_EN=0;
LCD_EN=0;
LCD_EN=1;
delayms(5);
t=P0OUT;
return inv(t);
}
void LCD_set_addr(uchar x,uchar y) //x是橫坐標(biāo)(列),y是縱坐標(biāo)(行), 行從1開始,列從1開始
{
x =(x+1)/2;
switch(y){
case 1:
LCD_write_com(0x7F+x);
break;
case 2:
LCD_write_com(0x8F+x);
break;
case 3:
LCD_write_com(0x87+x);
break;
case 4:
LCD_write_com(0x97+x);
break;
default:
break;
}
}
void LCD_write_char(uchar x,uchar y,char lcd_data)
{
uchar t;
LCD_set_addr(x, y);
if((x%2)==0)
{
t=LCD_read_data();
LCD_set_addr(x, y);
LCD_write_data(t);
}
LCD_write_data(lcd_data);
}
void LCD_write_string(uchar x,uchar y,char* str)
{
uchar LCD_temp;
LCD_set_addr(x,y);
if((x%2)==0)
{
LCD_temp=LCD_read_data();
LCD_set_addr(x, y);
LCD_write_data(LCD_temp);
}
LCD_temp=*str;
while(LCD_temp!=0x00){
LCD_write_data(LCD_temp);
LCD_temp=*(++str);
}
}
void LCD_init()
{
delayms(500);
LCD_write_com(0x30);
LCD_write_com(0x0C);
LCD_write_com(0x01);
LCD_write_com(0x02);
LCD_write_com(0x80);
}
void LCD_clear()
{
LCD_write_com(0x01);
delayms(15);
}
void main()
{
LCD_init();
LCD_clear();
LCD_write_char(1,1,'a');
LCD_write_string(1,2,"12345789");
LCD_write_char(4,3,'b');
LCD_write_string(1,4,"12345789");
LCD_write_char(2,4,'b');
while(1);
}
|
|