找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 531|回复: 0
收起左侧

stc89c51单片机+pcf8574(默认地址0x27)+lcd1602没有亮

[复制链接]
ID:845027 发表于 2023-3-21 16:03 | 显示全部楼层 |阅读模式
stc89c51+pcf8574(默认地址0x27)+lcd1602代码烧录上去,lcd没有亮,不知道到底是哪里出现了问题,LCD1602是在某一个宝上面买的,只有个C8051的破的代码和arduino都用不了,我们学校要求用stc89c51单片机去编写,求大佬看看哪里出现了问题。
1679385714895.jpg
单片机源程序如下:
  1. #include <reg52.h>
  2. #include "intrins.h"
  3. #define uchar unsigned char
  4. #define uint unsigned int

  5. //#define                        L1                0x80            // 第一行写入地址
  6. //#define                        L2                0xc0            // 第二行写入地址

  7. sbit SCL = P2^1;
  8. sbit SDA = P2^0;

  9. uchar woder[]={"wangzijie"};

  10. //char ADDR = 0x4E;    // PCF8574  T  模块的地址码
  11. //char ADDR = 0x7e;    // PCF8574   AT  模块的地址码
  12. char ADDR = 0x27;    // PCF8574   AT  模块的地址码


  13. //***************************** 延时 y  ms ***********************************************


  14. void delay1(int y)   //
  15. {
  16.          ;
  17.         while(y--)
  18.         {
  19.         unsigned char a,b,c;
  20.         for(c=1;c>0;c--)
  21.         for(b=142;b>0;b--)
  22.         for(a=2;a>0;a--);
  23.         }
  24. }






  25. //******************************** IIC 串口开始 ********************************************


  26. void IIC_start(void)
  27. {
  28.         SDA=1;
  29.         _nop_();
  30.         SCL=1;
  31.         _nop_();
  32.         _nop_();
  33.         _nop_();
  34.         _nop_();
  35.         _nop_();
  36.         SDA=0;
  37.         _nop_();
  38.         _nop_();
  39.         _nop_();
  40.         _nop_();
  41.         _nop_();
  42.         SCL=0;
  43. }




  44. //********************************** IIC 串口写1个字节 ******************************************


  45. void IIC_writeByte(char temp)
  46. {
  47.         char i;
  48.         for(i=0;i<8;i++)
  49.         {
  50.                 SDA=(bit)(temp & 0x80) ;   // 根据规定1602的数据最高位必须为  1  
  51.                 temp <<=1;
  52.                 _nop_();
  53.                 _nop_();
  54.                 SCL=1;
  55.                 _nop_();
  56.                 _nop_();
  57.                 _nop_();
  58.                 _nop_();
  59.                 _nop_();
  60.                 SCL=0;
  61.         }
  62.         _nop_();
  63.         _nop_();
  64.         _nop_();
  65.         _nop_();
  66.         SDA=1;
  67.         _nop_();
  68.         _nop_();
  69.         _nop_();
  70.         _nop_();
  71.         SCL=1;
  72.         _nop_();
  73.         _nop_();
  74.         _nop_();
  75.         while(SDA);
  76.         _nop_();
  77.         SCL=0;
  78. }




  79. //******************************** 1602写命令 ********************************************


  80. void LCD_write_command(char comm)
  81. {
  82.         char tmp;
  83.         IIC_start();          // 串口开始
  84.         IIC_writeByte(ADDR);  // 先选PCF 8574T 的地址  (应该是相当于选中的意思吧)

  85.         tmp = comm & 0xF0;    // 与0xf0 应该是取第四位的意思吧
  86.         tmp |= 0x0C;         //保留高4位为指令的高四位,低四位为   RS = 0, RW = 0, EN = 1  
  87.         IIC_writeByte(tmp);  //从串口送出
  88.         delay1(20);
  89.         tmp &= 0xFB;        //Make EN = 0
  90.         IIC_writeByte(tmp);

  91.         tmp = (comm & 0x0F) << 4 ;  //将指令的低四位 送到高位置保存
  92.         tmp |= 0x0C;        //RS = 0, RW = 0, EN = 1
  93.         IIC_writeByte(tmp);
  94.         delay1(20);
  95.         tmp &= 0xFB; // Make EN = 0
  96.         IIC_writeByte(tmp);

  97. }
  98. //******************************** 1602写数据 ********************************************


  99. void LCD_write_data(char data1)
  100. {
  101.         char tmp;
  102.         IIC_start();
  103.         IIC_writeByte(ADDR);   // 先选PCF8574T 的地址  (应该是相当于选中的意思吧)

  104.         tmp = data1 & 0xF0;
  105.         tmp |= 0x0D; //RS = 0, RW = 0, EN = 1
  106.         IIC_writeByte(tmp);
  107.         delay1(20);
  108.         tmp &= 0xFB; //Make EN = 0
  109.         IIC_writeByte(tmp);

  110.         tmp = (data1 & 0x0F) << 4 ;
  111.         tmp |= 0x0D; //RS = 0, RW = 0, EN = 1
  112.         IIC_writeByte(tmp);
  113.         delay1(20);
  114.         tmp &= 0xFB ; // Make EN = 0
  115.         IIC_writeByte(tmp);
  116. }


  117. //******************************** 1602初始化 ********************************************


  118. void Init_Lcd(void)
  119. {
  120.         LCD_write_command(0x33); //将8位总线转为4位总线
  121.         delay1(50) ;
  122.         LCD_write_command(0x32); //
  123.         delay1(50) ;
  124.         LCD_write_command(0x28); // 4位数据线0x28,显示2行,5*7点阵字符  !如果是0x38  则为8位数据线,显示2行,5*7点阵字符
  125.         delay1(50) ;
  126.         LCD_write_command(0x0C); // 开显示,关闭光标,不闪烁
  127.         delay1(50) ;  
  128.         LCD_write_command(0x06); // 设定输入方式,增量不位移
  129.         delay1(50) ;
  130.         LCD_write_command(0x01); // 清屏
  131.         delay1(50) ;
  132. //                LCD_write_command(0x80);  //设置数据指针起点
  133. //                delay1(50) ;
  134. }








  135. //*************************************** 在指定位置显示字符串 *************************************


  136. void Write_LCD(int x, int y, char *str)
  137. {
  138.         char addr;
  139.         if( x < 0)
  140.         {
  141.                 x = 0;
  142.         }
  143.         if(x > 15)
  144.         {
  145.                 x = 15;
  146.         }
  147.         if(y<0)
  148.         {
  149.                 y = 0;
  150.         }
  151.         if(y > 1)
  152.         {
  153.                 y = 1;
  154.         }

  155.         addr = 0x80 + 0x40 * y + x;   // Move cursor  移动光标
  156.         LCD_write_command(addr);
  157.         while (*str)
  158.         {
  159.                 LCD_write_data(*str++);
  160.         }
  161. }


  162. //-------------------------------------------- 显示字符串的函数 ----------------------------------------------------


  163. void LCD_write_word(unsigned char *s)                  //显示字符串的函数
  164. {
  165.         while(*s>0)
  166.         {
  167.                 LCD_write_data(*s);
  168.                 s++;
  169.         }
  170. }

  171. void main()
  172. {               
  173.          uchar i;
  174.         Init_Lcd();
  175. //        for(i=0;i<9;i++)
  176. //        {
  177.         Write_LCD(0,0,(uchar *)woder[i]);
  178.         LCD_write_word((uchar *)woder[i]);
  179. //        }
  180.         while(1);                                
  181. }


  182. //********************************* 指定位置显示一个字符*******************************************


  183. /*
  184. void Print_Char (unsigned char line,unsigned char num,unsigned char date)
  185. {
  186.                 LCD_write_command(line+num);
  187.                 LCD_write_data(date);
  188. }


  189. */




  190. //按指定位置显示一个字符(针对1602液晶)-用在温度显示


  191. //void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
  192. //{
  193. //Y &= 0x1;
  194. //X &= 0xF;                 //限制X不能大于15,Y不能大于1
  195. //if (Y) X |= 0x40;        //当要显示第二行时地址码+0x40;
  196. //X |= 0x80;               // 算出指令码
  197. //LCD_write_command(X);    //这里不检测忙信号,发送地址码
  198. //LCD_write_data(DData);
  199. //}
复制代码


回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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