單片機(jī)I2C通信及LCD1602顯示C程序
/*
*功能: 利用iic發(fā)送數(shù)據(jù),在數(shù)碼管上顯示,并通過(guò)串口發(fā)生到上位機(jī),
*且在lcd1602上顯示"Han Zhu Ying Xue"
*"1754410821@qq.com";
*作者: 徐冉
*日期: 2013-05-29-22:21 2013-5-2919:15 - 22:17
*備注: 程序已經(jīng)順利通過(guò)調(diào)試,耗時(shí)3小時(shí)。
*/
/*********51hei開(kāi)發(fā)板***********/
/********AT89C52-RC MCU***********/
#include
#define write_add 0xa0
#define read_add 0xa1
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7;
sbit dula = P2^6;
//i2c
sbit SCL = P2^1;
sbit SDA = P2^0;
//LCD1602
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P2^5;
sbit BUSY = P0^7;
uchar num,flag;
uchar code string_1[] = "Han Zhu Ying Xue";
uchar code string_2[] = "1754410821@qq.com";
uchar code table[] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F //"9"
};
/********************************************************************/
/**********************LCD1602底層驅(qū)動(dòng)程序***************************/
/********************************************************************/
//delay: 延時(shí)xms
void delay(uint xms)
{
uchar i, j;
for(i = 0; i < xms; i++)
for(j = 0; j < 110; j++);
}
//wait:判忙等待
void wait()
{
P0 = 0xff;
do
{
RS = 0;
RW = 1;
EN = 0;
EN = 1;
}while(BUSY == 1);
EN = 0;
}
//write:cmd寫入命令
void write_cmd(uchar cmd)
{
RS = 0;
RW = 0;
EN = 0;
P0 = cmd;
EN = 1;
EN = 0;
}
//write:data 寫入數(shù)據(jù)
void write_data(uchar datas)
{
RS = 1;
RW = 0;
EN = 0;
P0 = datas;
EN = 1;
EN = 0;
}
//send:string 發(fā)送一串字符
void send_string(uchar* str)
{
while(*str !=