嵌入式 > 嵌入式开发 > 详情

单片机I2C通信及LCD1602显示C程序

发布时间:2024-06-19 发布时间:
|
本程序所用的原理图下载:点这里,单片机芯片使用的stc89c52;找到数码管的原理图即可.这是一整个单片机开发板的电路图其他的忽略.本程序keil工程文件下载:/zixunimg/eepwimg/www.51hei.com/f/1602iic.rar

/*
*功能: 利用iic发送数据,在数码管上显示,并通过串口发生到上位机,
*且在lcd1602上显示"Han Zhu Ying Xue"
*"1754410821@qq.com";
*作者: 徐冉
*日期: 2013-05-29-22:21 2013-5-2919:15 - 22:17
*备注: 程序已经顺利通过调试,耗时3小时。
*/
/*********51hei开发板***********/
/********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底层驱动程序***************************/
/********************************************************************/
//delay: 延时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 写入数据
void write_data(uchar datas)
{
RS = 1;
RW = 0;
EN = 0;
P0 = datas;
EN = 1;
EN = 0;
}
//send:string 发送一串字符
void send_string(uchar* str)
{
while(*str != )
{
write_data(*str++);
}
}
//lcd1602:initLCD1602 显示初始化
voidlcd_init()
{
delay(15);
write_cmd(0x38);
delay(5);
write_cmd(0x38);
delay(5);
write_cmd(0x38);
wait();
write_cmd(0x38); //设置16 * 2,5 * 7点阵
wait();
write_cmd(0x08);
wait();
write_cmd(0x01);//清屏
wait();
write_cmd(0x06);
wait();
write_cmd(0x0c); //开显示
}



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
485总线升级成CAN总线