PCF8574模塊使實(shí)現(xiàn)IIC驅(qū)動LCD1602成為現(xiàn)實(shí)。
下圖是PCF8574模塊與1602的連接電路圖: 實(shí)物接法如下: 想要使用PCF8574模塊來實(shí)現(xiàn)IIC驅(qū)動LCD1602,首先非常必要弄清楚IIC的時序、PCF8574設(shè)備地址、四線控制LCD1602方法。 下面是兩個重要的h文件。通過修改這兩個h文件中的部分代碼(接線有所不同)即可移植到你的程序中去。
單片機(jī)源程序如下:
- void delay1(uchar x)
- {
- uchar a,b;
- for(a=x;a>0;a--)
- for(b=200;b>0;b--);
- }
- void write_com(uchar com) //寫命令函數(shù)
- { 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) //寫數(shù)據(jù)函數(shù)
- {
- 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() //初始化函數(shù)
- {
- write_com(0x33); //顯示模式設(shè)置
- delayms(6);
- write_com(0x32); //顯示模式設(shè)置
- delayms(6);
- write_com(0x28); //4位總線,雙行顯示,顯示5×7的點(diǎn)陣字符
- delayms(6);
- write_com(0x01); //清屏
- delayms(6);
- write_com(0x06); //字符進(jìn)入模式:屏幕不動,字符后移
- delayms(6);
- write_com(0x0c); //顯示開,關(guān)光標(biāo)
- //write_LCD_Command(0x0f); //顯示開,開光標(biāo),光標(biāo)閃爍
- delayms(6);
- }
- //顯示字符串:第x行第y列顯示什么內(nèi)容
- 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++;
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
PCF8574 LCD1602_4.rar
(685 Bytes, 下載次數(shù): 658)
2019-3-4 18:50 上傳
點(diǎn)擊文件名下載附件
PCF8574.rar
(536 Bytes, 下載次數(shù): 444)
2019-3-4 18:50 上傳
點(diǎn)擊文件名下載附件
|