基于51單片機的簡易計算器1602顯示
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define CHECK_BUSY
sbit RS = P2^4;
sbit RW = P2^5;
sbit EN = P2^6;
void DelayMs(int z)
{
int x,y;
for (x=z;x>0;x--)
for(y=110;y>0;y--);
}
/***********判忙函數***********/
bit LCD_Check_Busy()
{
#ifdef CHECK_BUSY
P0= 0xFF;
RS=0;
RW=1;
EN=0;
_nop_();
EN=1;
return (bit)(P0 & 0x80);
#else
return 0;
#endif
}
/***********寫入命令函數***********/
void write_com(uchar com)
{
while(LCD_Check_Busy()); //忙則等待
RS=0;
RW=0;
EN=1;
P0= com;
_nop_();
EN=0;
}
/**********寫入數據函數**********/
void write_dat(uchar dat)
{
while(LCD_Check_Busy()); //忙則等待
RS=1;
RW=0;
EN=1;
P0= dat;
_nop_();
EN=0;
}
/*******寫入字符函數***********/
void LCD_Write_Char(uchar x,uchar y,uchar dat)
{
if (y == 0)
{
write_com(0x80 + x);
}
else
{
write_com(0xC0 + x);
}
write_dat( dat);
}
/******寫入字符串函數***********/
void Write_String(uchar x,uchar y,uchar *s)
{
while (*s)
{
LCD_Write_Char(x,y,*s);
s++;
x++;
}
}
/*****初始化函數******/
void LCD_Init()
{
write_com(0x38); /*顯示模式設置*/
DelayMs(5);
write_com(0x06);/*顯示光標移動設置*/
DelayMs(5);
write_com(0x0C); /*顯示開及光標設置*/
write_com(0x01); /*顯示清屏*/
}


評論