找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2694|回复: 3
收起左侧

单片机iic方式驱动1602源程序 PCF8547T芯片

[复制链接]
ID:499469 发表于 2019-12-28 14:13 | 显示全部楼层 |阅读模式
sbit SCL = P3^0;
sbit SDA = P3^1;
确认芯片为PCF8547T  iic地址就不用改了(PCF8547好像也一样,8547A不太一样)
854cea96cd55ff8b28adbfa639a0851.jpg
单片机源程序如下:
  1. #include <intrins.h>
  2. sbit SCL = P3^0;
  3. sbit SDA = P3^1;
  4. bit ack;
  5. /*sbit RS=P2^0;
  6. sbit Rw=P2^1;
  7. sbit E =P2^2; */
  8. sbit deng =P1^0;

  9. unsigned char LCD_data;
  10. unsigned char code digit[ ]={"0123456789"}; //定义字符数组显示数字

  11. /*bt0=date&0x01;
  12.   bt1=date&0x02;
  13.   bt2=date&0x04;
  14.   bt3=date&0x08;
  15.   bt4=date&0x10;
  16.   bt5=date&0x20;
  17.   bt6=date&0x40;
  18.   bt7=date&0x80;  */
  19. //*****************延时************************
  20. void delay_nus(unsigned int n) //N us延时函数
  21. {
  22.         unsigned int i=0;
  23.                 for (i=0;i<n;i++)
  24.                         _nop_();
  25. }
  26. void delay_nms(unsigned int n) //N ms延时函数
  27. {
  28.         unsigned int i,j;
  29.                 for (i=0;i<n;i++)
  30.                         for (j=0;j<1140;j++);
  31. }

  32. void nop4()
  33. {
  34.          _nop_();     //等待一个机器周期
  35.          _nop_();     //等待一个机器周期
  36.          _nop_();     //等待一个机器周期
  37.          _nop_();     //等待一个机器周期
  38. }
  39. //***************************************88
  40. void Start()
  41. {
  42.          SDA=1;
  43.     _nop_();
  44.     SCL=1;
  45.         nop4();
  46.     SDA=0;
  47.         nop4();
  48.     SCL=0;
  49.     _nop_();
  50.         _nop_();
  51. }

  52. void Stop()
  53. {
  54.          SDA=0;
  55.     _nop_();        

  56.         SCL=0;
  57.         nop4();//>4us后SCL跳变
  58.         SCL=1;
  59.         nop4();
  60.         SDA=1;
  61.     _nop_();
  62.     _nop_();
  63. }
  64. //******************************************
  65. void  Write_A_Byte(unsigned char c)
  66. {
  67. unsigned char BitCnt;
  68.   for(BitCnt=0;BitCnt<8;BitCnt++)  //要传送的数据长度为8位
  69.     {
  70.      if((c<<BitCnt)&0x80)  SDA=1;   //判断发送位
  71.      else  SDA=0;               
  72.      _nop_();
  73.      SCL=1;               //置时钟线为高,通知被控器开始接收数据位
  74.      nop4();
  75.      _nop_();      
  76.      SCL=0;
  77.     }  
  78.     _nop_();
  79.     _nop_();
  80.     SDA=1;               //8位发送完后释放数据线,准备接收应答位
  81.     _nop_();
  82.     _nop_();  
  83.     SCL=1;
  84.     _nop_();
  85.     _nop_();
  86.     _nop_();
  87.     if(SDA==1)ack=0;     
  88.        else ack=1;        //判断是否接收到应答信号
  89.     SCL=0;
  90.     _nop_();
  91.     _nop_();
  92. }

  93. bit Write_Random_Address_Byte(unsigned char add,unsigned char dat)
  94. {
  95.          Start();    //启动总线
  96.         Write_A_Byte(add); //发送器件地址
  97.     if(ack==0)return(0);
  98.         Write_A_Byte(dat);   //发送数据
  99.     if(ack==0)return(0);
  100.         Stop(); //结束总线
  101.     return(1);
  102. }
  103. //********************液晶屏使能*********************
  104. void Enable_LCD_write()
  105. {
  106.     LCD_data|=(1<<(3-1));//E=1;
  107.     Write_Random_Address_Byte(0x4e,LCD_data);
  108.     delay_nus(2);
  109.     LCD_data&=~(1<<(3-1));//E=0;
  110.     Write_Random_Address_Byte(0x4e,LCD_data);
  111. }

  112. //*************写命令****************************
  113. void LCD_write_command(unsigned char command)
  114. {
  115.         delay_nus(16);
  116.         LCD_data&=~(1<<(1-1));//RS=0;
  117.         LCD_data&=~(1<<(2-1));//RW=0;
  118.     Write_Random_Address_Byte(0x4e,LCD_data);

  119.         LCD_data&=0x0f; //清高四位
  120.         LCD_data|=command & 0xf0; //写高四位
  121.     Write_Random_Address_Byte(0x4e,LCD_data);
  122.     Enable_LCD_write();

  123.         command=command<<4; //低四位移到高四位
  124.         LCD_data&=0x0f; //清高四位
  125.         LCD_data|=command&0xf0; //写低四位
  126.     Write_Random_Address_Byte(0x4e,LCD_data);
  127.     Enable_LCD_write();
  128. }
  129. //*************写数据****************************
  130. void LCD_write_data(unsigned char value)
  131. {
  132.         delay_nus(16);
  133.         LCD_data|=(1<<(1-1));//RS=1;
  134.         LCD_data&=~(1<<(2-1));//RW=0;
  135.     Write_Random_Address_Byte(0x4e,LCD_data);

  136.         LCD_data&=0X0f; //清高四位
  137.         LCD_data|=value&0xf0; //写高四位
  138.     Write_Random_Address_Byte(0x4e,LCD_data);
  139.     Enable_LCD_write();

  140.         value=value<<4; //低四位移到高四位
  141.         LCD_data&=0x0f; //清高四位
  142.         LCD_data|=value&0xf0; //写低四位
  143.     Write_Random_Address_Byte(0x4e,LCD_data);
  144.     Enable_LCD_write();
  145. }

  146. //**********************设置显示位置*********************************
  147. void set_position(unsigned char x,unsigned char y)
  148. {
  149.         unsigned char position;
  150.         if (y == 0)
  151.         position = 0x80 + x;
  152.         else
  153.                 position = 0xc0 + x;
  154.     LCD_write_command(position);
  155. }
  156. //**********************显示字符串*****************************

  157. void display_string(unsigned char x,unsigned char y,unsigned char *s)
  158. {
  159.     set_position(y-1,x-1);
  160.         while (*s)
  161.          {     
  162.                 LCD_write_data(*s);     
  163.                 s++;     
  164.          }
  165. }
  166. //********************显示数字*******************************xs
  167. void display_num(unsigned char x,unsigned char y,unsigned int num)
  168. {
  169.    unsigned char i,j,k,m,n;
  170.    set_position(y-1,x-1);
  171.     i=num/10000;
  172.     j=num/1000%10;
  173.         k=num/100%10;
  174.         m=num/10%10;
  175.     n=num%10;
  176.         LCD_write_data(digit[i]);    //将万位数字的字符常量写入LCD
  177.         LCD_write_data(digit[j]);    //将千位数字的字符常量写入LCD
  178.         LCD_write_data(digit[k]);
  179.         LCD_write_data(digit[m]);
  180.     LCD_write_data('.');
  181.         LCD_write_data(digit[n]);
  182. }
  183. //*************液晶初始化****************************
  184. void LCD_init(void)
  185. {
  186.         LCD_write_command(0x28);
  187.         delay_nus(40);
  188.         LCD_write_command(0x28);
  189.         delay_nus(40);
  190.     Enable_LCD_write();
  191.         delay_nus(40);
  192.         LCD_write_command(0x28); //4位显示!!!!!!!!!!!!!!!!!!
  193.         LCD_write_command(0x0c); //显示开
  194.         LCD_write_command(0x01); //清屏
  195.         delay_nms(2);
  196. }
  197. //
  198. //********************按键*********************
  199. void button()
  200. {
  201.         bit button1;
  202.         if (deng==0){button1=~button1;}
  203.     if(button1==0)
  204.     {
  205.     LCD_data|=(1<<(4-1));//E=1;
  206.     Write_Random_Address_Byte(0x4e,LCD_data);
  207.     }
  208.     if(button1==1)
  209.     {
  210.     LCD_data&=~(1<<(4-1));//E=0;
  211.     Write_Random_Address_Byte(0x4e,LCD_data);
  212.     }
  213. }
复制代码

所有资料51hei提供下载:
1602测试.zip (33.13 KB, 下载次数: 36)
6cb9e112edcdf85108d0589411dc9b0.jpg

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:677678 发表于 2019-12-30 18:00 | 显示全部楼层
没有源码  =余没有实际参考
回复

使用道具 举报

ID:584814 发表于 2020-1-9 17:14 | 显示全部楼层
纯抄无修正,不过可以运行的。
回复

使用道具 举报

ID:372579 发表于 2020-3-17 13:04 | 显示全部楼层
问下楼主怎么用51单片机驱动
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表