#include<stc12c5a60s2.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//這三個引腳參考資料
sbit LCD1602_EN=P3^3; //1602使能引腳
sbit LCD1602_RW=P3^4; //1602讀寫引腳
sbit LCD1602_RS=P3^5; //1602數據/命令選擇引腳
#define LCDPORT P0 //液晶的數據口
unsigned char code ucForum[]="123456789"; //在CODE區定義一個用于顯示的常量字符串
unsigned char ab;
void Delay(unsigned int uiCount); //延時函數
void LCD1602_CheckBusy(void); //液晶忙檢測
void LCD1602_WriteInformation(unsigned char ucData,bit bComOrData); //在液晶上寫數據或者寫命令,0為命令,1為數據
void LCD1602_Init(void); //液晶初始化
void LCD1602_MoveToPosition(unsigned char x,unsigned char y); //液晶的坐標移動到指定位置
void LCD1602_DisplayOneCharOnAddr(unsigned char x,unsigned char y,unsigned char ucData); //在液晶指定位置顯示字符
void LCD1602_DisplayString(unsigned char *ucStr); //在液晶上顯示字符串
void re()
{ab=100;
}
/******************************************************************************
函數名稱:main
函數功能:程序主函數
入口參數:無
返回值:無
備注:無
*******************************************************************************/
void main(void)
{
LCD1602_Init(); //液晶初始化
re();
while(1) //程序循環
{
LCD1602_DisplayOneCharOnAddr(0,0,'A'); //在液晶的第1個位置顯示1
LCD1602_DisplayOneCharOnAddr(0,1,'B'); //在液晶的第2個位置顯示A
LCD1602_DisplayOneCharOnAddr(0,2,'C'); //在液晶的第3個位置顯示2
LCD1602_DisplayOneCharOnAddr(0,3,'D'); //在液晶的第4個位置顯示B
LCD1602_DisplayOneCharOnAddr(0,4,'E'); //在液晶的第5個位置顯示3
LCD1602_DisplayOneCharOnAddr(0,5,'F'); //在液晶的第6個位置顯示C
LCD1602_DisplayOneCharOnAddr(0,7,ab/100+0x30);
LCD1602_DisplayOneCharOnAddr(0,8,ab/10+0x30);
LCD1602_DisplayOneCharOnAddr(0,9,ab%10+0x30);
LCD1602_MoveToPosition(1,0); //顯示位置移動到指定位置
LCD1602_DisplayString(ucForum); //顯示的內容
while(1);
}
}
void LCD1602_CheckBusy(void)
{
unsigned char i = 255;
LCDPORT = 0xFF; //讀之前先置位,準備讀取IO口數據
LCD1602_RS = 0;
LCD1602_RW = 1; //使液晶處于讀數據狀態
LCD1602_EN = 1; //使能液晶,高電平有效
while((i--) && (LCDPORT & 0x80)); //忙檢測
LCD1602_EN = 0;
}
void LCD1602_WriteInformation(unsigned char ucData,bit bComOrData)
{
LCD1602_CheckBusy(); //在寫入數據或者命令前先進行忙檢測
LCDPORT = ucData; //先將數據或者命令送至IO
LCD1602_RS = bComOrData; //確定是寫入數據還是寫命令
LCD1602_RW = 0; //使液晶處于寫入信息狀態
LCD1602_EN = 1; //使能液晶,高電平有效
LCD1602_EN = 0;
}
void LCD1602_Init(void)
{
LCD1602_WriteInformation(0x38,0);
Delay(300);
LCD1602_WriteInformation(0x38,0);
Delay(100);
LCD1602_WriteInformation(0x38,0);
Delay(100);
LCD1602_WriteInformation(0x38,0); //寫入命令,5x7點陣工作方式,8位數據接口
Delay(100);
LCD1602_WriteInformation(0x0c,0); //顯示設置,開顯示,光標不顯示,不閃爍
Delay(20);
LCD1602_WriteInformation(0x01,0); //清屏指令
Delay(20);
}
void LCD1602_MoveToPosition(unsigned char x,unsigned char y)
{
if(0 == x)
LCD1602_WriteInformation((0x80 | y),0); //光標定位到第一行的y列
if(1 == x)
LCD1602_WriteInformation((0xC0 | y),0); //光標定義到第二行的y列
}
void LCD1602_DisplayOneCharOnAddr(unsigned char x,unsigned char y,unsigned char ucData)
{
LCD1602_MoveToPosition(x,y); //光標位置
LCD1602_WriteInformation(ucData,1); //寫入數據
}
void LCD1602_DisplayString(unsigned char *ucStr)
{
while(*ucStr != '\0') //字符串結束之前,循環顯示
{
LCD1602_WriteInformation(*ucStr,1); //依次寫入每一個字符
ucStr++; //指針增加
}
}
void Delay(unsigned int uiCount)
{
unsigned char j = 244;
for(;uiCount > 0;uiCount--) while(--j);
}
|