PCF8574模块使实现IIC驱动LCD1602成为现实。
下图是PCF8574模块与1602的连接电路图: 实物接法如下: 想要使用PCF8574模块来实现IIC驱动LCD1602,首先非常必要弄清楚IIC的时序、PCF8574设备地址、四线控制LCD1602方法。 下面是两个重要的h文件。通过修改这两个h文件中的部分代码(接线有所不同)即可移植到你的程序中去。
单片机源程序如下:
- void delay1(uchar x)
- {
- uchar a,b;
- for(a=x;a>0;a--)
- for(b=200;b>0;b--);
- }
- void write_com(uchar com) //写命令函数
- { uchar com1,com2;
- com1=com|0x0f;
- write_add(com1&0xfc);
- delay1(2);
- write_add(com1&0xf8);
- com2=com<<4;
- com2=com2|0x0f;
- write_add(com2&0xfc);
- delay1(2);
- write_add(com2&0xf8);
-
- }
- void write_date(uchar date) //写数据函数
- {
- uchar date1,date2;
- date1=date|0x0f;
- write_add(date1&0xfd);
- delay1(2);
- write_add(date1&0xf9);
- date2=date<<4;
- date2=date2|0x0f;
- write_add(date2&0xfd);
- delay1(2);
- write_add(date2&0xf9);
-
- }
- void init_lcd() //初始化函数
- {
- write_com(0x33); //显示模式设置
- delayms(6);
- write_com(0x32); //显示模式设置
- delayms(6);
- write_com(0x28); //4位总线,双行显示,显示5×7的点阵字符
- delayms(6);
- write_com(0x01); //清屏
- delayms(6);
- write_com(0x06); //字符进入模式:屏幕不动,字符后移
- delayms(6);
- write_com(0x0c); //显示开,关光标
- //write_LCD_Command(0x0f); //显示开,开光标,光标闪烁
- delayms(6);
- }
- //显示字符串:第x行第y列显示什么内容
- void ShowString(unsigned char x,unsigned char y,unsigned char *str)
- {
-
- if(x == 1)
- {
- write_com(0x80 | y-1);
- }
- if(x == 2)
- {
- write_com(0xc0 | y-1);
- }
- //输出字符串
-
- while(*str!='\0')
- {
- write_date(*str);
- str++;
- }
- }
复制代码
所有资料51hei提供下载:
PCF8574 LCD1602_4.rar
(685 Bytes, 下载次数: 658)
PCF8574.rar
(536 Bytes, 下载次数: 444)
|