找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3535|回復(fù): 8
打印 上一主題 下一主題
收起左側(cè)

共享IIC液晶程序,已經(jīng)調(diào)試成功!!

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:50469 發(fā)表于 2013-8-9 09:59 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#include<reg52.h>
#include<intrins.h>
#define uint  unsigned int
#define uchar unsigned char
sbit SCL=P2^1;
sbit SDA=P2^0;
uchar code table[]="study up";
//延時(shí)函數(shù)
void delayus()
{
;;;
}
void delayms(uchar x)
{
uchar j;
while(x--)
  for(j=110;j>0;j--);
}
//開始信號(hào)
void iic_start(void)
{
SDA = 1;
delayus();
    _nop_();_nop_();_nop_();_nop_();_nop_();
SCL = 1;
delayus();
SDA=0;
delayus();
}
//停止信號(hào)
void iic_stop(void)
{
SDA = 0;
delayus();
SCL = 1;
delayus();
SDA = 1;
delayus();
}
//初始化
void iicinit(void)
{
SDA = 1;
delayus();
SCL=1;
delayus();
}
//應(yīng)答
void respons(void)
{
uchar i=0;
SCL = 1;
delayus();
while((SDA=1)&&(i<255))
  i++;
SCL = 0;
delayus();
}
//發(fā)送一個(gè)字節(jié)
void write_byte(uchar date)
{
uchar i,temp;
temp=date;      
for(i=0;i<8;i++)
{
  temp=temp<<1;        
  SCL = 0;
  delayus();
  SDA=CY;
  delayus();
  SCL = 1;
  delayus();
}
SCL=0;
delayus();
SDA=1;
delayus();
}
//寫地址,數(shù)據(jù)
void write_lcd(uchar contr)
{
iic_start();
write_byte(0x4e);      
respons();   
write_byte(contr);   //延時(shí)時(shí)間太長,背光特別閃
respons();  
iic_stop();
}
void write_com(uchar temp)
{
uchar temp1;
temp1=temp<<4;
write_lcd(temp&0xf0|0x08);
write_lcd(temp&0xf0|0x0c);  //先寫地址
delayms(5);
write_lcd(temp&0xf0&0xfb);
write_lcd(temp1&0xf0|0x08);
write_lcd(temp1&0xf0|0x0c);
delayms(5);
write_lcd(temp1&0xf0&0xfb);
//iic_stop();
}
void write_data(uchar temp)
{
uchar temp1;
temp1=temp<<4;
write_lcd(temp&0xf0|0x09);
write_lcd(temp&0xf0|0x0d);
delayms(5);
write_lcd(temp&0xf0&0xfb);

write_lcd(temp1&0xf0|0x09);  
write_lcd(temp1&0xf0|0x0d);
delayms(5);
write_lcd(temp&0x00&0xfb);
}
void lcd_init()
{
  write_com(0x28);  //四位數(shù)據(jù)控制
  write_com(0x0c);
  write_com(0x06);
  write_com(0x01);   
}
void main()
{
iicinit();
lcd_init();
while(1)
{
  uchar i=0;
  write_com(0x80);
  write_data('1');
  write_data('2');
  write_data('3');
  write_com(0xc0);
  while(table[i]!='\0')
  {
   write_data(table[i]);
   i++;
  }
}
}
個(gè)人覺得最難的部分是寫數(shù)據(jù)與寫命令子函數(shù)中或和與的邏輯關(guān)系,程序?qū)懥艘簧衔,但是光找錯(cuò)誤就找了一天多,真心不容易啊,希望大家以后注意!!

評(píng)分

參與人數(shù) 1黑幣 +5 收起 理由
ssfc + 5

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏3 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:53072 發(fā)表于 2013-9-21 13:44 | 只看該作者
你真是很厲害呀
回復(fù)

使用道具 舉報(bào)

板凳
ID:55219 發(fā)表于 2013-9-27 21:37 | 只看該作者
IIC本人就 是卡在寫數(shù)據(jù)和命令上,因此感覺用IIC就是不爽,一般很少用,了解的不多
回復(fù)

使用道具 舉報(bào)

地板
ID:52286 發(fā)表于 2013-10-14 00:08 | 只看該作者
支持支持、、
回復(fù)

使用道具 舉報(bào)

5#
ID:56665 發(fā)表于 2013-12-15 10:07 | 只看該作者
不佩服都不行。
回復(fù)

使用道具 舉報(bào)

6#
ID:164217 發(fā)表于 2017-7-14 17:58 | 只看該作者
舅服你!
回復(fù)

使用道具 舉報(bào)

7#
ID:141497 發(fā)表于 2017-7-15 01:32 | 只看該作者
LZ說得沒錯(cuò),編程對(duì)邏輯關(guān)系特別重要。
回復(fù)

使用道具 舉報(bào)

8#
ID:207702 發(fā)表于 2017-7-17 11:29 | 只看該作者
1602液晶還是12864啊
回復(fù)

使用道具 舉報(bào)

9#
ID:207702 發(fā)表于 2017-7-17 11:34 | 只看該作者
要是1602屏幕不用D0-D7那數(shù)據(jù)不是可以少幾根線這樣不是很方便
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表