找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1714|回复: 2
收起左侧

LCD1602为什么会显示这样?没有加ds1302是好的,加了之后就这样了

[复制链接]
ID:782437 发表于 2020-6-21 08:08 | 显示全部楼层 |阅读模式
10黑币
这个问题是不是那个程序程序错误?
如ds1302..h  或者 LCD1602.h
  1. #ifndef _DS1302_H_
  2. #define _DS1302_H_
  3. #define uchar unsigned char
  4. sbit SCLK=P2^3;  //ds1302时钟线定义
  5. sbit IO=P2^4;    //数据线
  6. sbit RST=P2^5;         //复位线
  7. uchar sec,min,hour,day,mon,year,week;
  8. //uchar n_year,n_mon,n_day;//农历显示的函数

  9. /*------------------------------------------------
  10.                     DS1302编程部分
  11. ------------------------------------------------*/

  12. void write_1302(uchar add,uchar dat) //写操作 (写地址+写数据)
  13. {
  14.         uchar i;
  15.         RST=1;                 //把复位线拉高
  16.         for(i=0;i<8;i++)
  17.         {
  18.                 SCLK=0;//时钟信号为低,开始写地址
  19.                 IO = add & 0x01;            
  20.                 add >>= 1;                 //把地址右移一位
  21.                 SCLK=1;                         //时钟线拿高
  22.         }
  23.         for(i=0;i<8;i++)
  24.         {
  25.                 SCLK=0;//时钟信号为低,开始写数据
  26.                 IO = dat & 0x01;            
  27.                 dat >>= 1;                 //把地址右移一位
  28.                 SCLK=1;                         //时钟线拿高
  29.         }
  30.         SCLK=0;
  31.         RST=0;//写操作结束
  32.         IO =0;
  33. }
  34. uchar read_1302(uchar add)        //读操作(写地址+读数据)
  35. {
  36.         uchar i,value;
  37.         RST=0;
  38.         SCLK=0;
  39.         RST=1;                 //把复位线拉高
  40.         for(i=0;i<8;i++)
  41.         {
  42.                 SCLK=0;//时钟信号为低,开始写地址
  43.                 IO = add & 0x01; //让IO保存add的最低位           
  44.                 add >>= 1;                 //把地址右移一位
  45.                 SCLK=1;                         //时钟线拿高
  46.         }
  47.         for(i=0;i<8;i++)
  48.         {
  49.                
  50.                 value >>= 1;         //把数据右移一位
  51.                 if(IO == 1)  value |= 0x80; // 将数据放在value的最高位,达到保存数据的结果
  52.                 SCLK=0;//时钟信号为低,开始写数据        
  53.                 SCLK=1;                         //时钟线拿高
  54.         }
  55.         SCLK=0;
  56.         RST=0;//读操作结束
  57.         IO =0;
  58.         return value;                 //返回读出来的数据
  59. }
  60. uchar BCD_Decimal(uchar bcd)
  61. {
  62. uchar Decimal;
  63. Decimal=bcd>>4;
  64. return(Decimal=Decimal*10+(bcd&=0x0F));
  65. }
  66. /*************把要的时间 年月日 都读出来***************/
  67. void read_time()
  68. {        
  69.     sec = BCD_Decimal(read_1302(0x81));
  70.         min = BCD_Decimal(read_1302(0x83));
  71.         hour  = BCD_Decimal(read_1302(0x85));
  72.         day  = BCD_Decimal(read_1302(0x87));
  73.         mon = BCD_Decimal(read_1302(0x89));
  74.         year=BCD_Decimal(read_1302(0x8d));
  75.         week=BCD_Decimal(read_1302(0x8b));

  76. //        Conversion(0,year,mon,day);        //农历转换                                    
  77. //        n_year = year_moon ;
  78. //        n_mon  = month_moon ;
  79. //        n_day  = day_moon ;

  80. }
  81.                                                 //秒  分   时   日   月  年   星期         
  82. //uchar code write_add[]={0x80,0x82,0x84,0x86,0x88,0x8c,0x8a};   //写地址
  83. //uchar code read_add[] ={0x81,0x83,0x85,0x87,0x89,0x8d,0x8b};   //读地址
  84. //uchar code tab1[]={"2020-06-19 FRI"};
  85. //uchar code tab2[]={"10:32:48"};

  86. /*************把要写的时间 年月日 都写入ds1302里***************/
  87. void write_time()
  88. {
  89.     uchar temp;
  90.     write_1302(0x80,sec|0x00);//允许写
  91.         write_1302(0x8e,0x00);                        //打开写保护
  92.         sec= (tab2[6]-0x30)*10+(tab2[7]-0x30);
  93.         temp=(sec)/10*16+(sec)%10;
  94.         write_1302(0x80,0x00|temp);

  95.         min= (tab2[3]-0x30)*10+(tab2[4]-0x30);
  96.         temp=(min)/10*16+(min)%10;
  97.         write_1302(0x82,0x00|temp);

  98.         hour= (tab2[0]-0x30)*10+(tab2[1]-0x30);
  99.         temp=(hour)/10*16+(hour)%10;
  100.         write_1302(0x84,0x00|temp);

  101.         day= (tab1[8]-0x30)*10+(tab1[9]-0x30);
  102.         temp=(day)/10*16+(day)%10;
  103.         write_1302(0x86,0x00|temp);

  104.         mon= (tab1[5]-0x30)*10+(tab1[6]-0x30);
  105.         temp=(mon)/10*16+(mon)%10;
  106.         write_1302(0x88,0x00|temp);

  107.         write_1302(0x8e,0x80);                        //关闭写保护
  108. }
  109. //*************把数据保存到ds1302 RAM中**0-31*************/
  110. //void write_1302ram(uchar add,uchar dat)
  111. //{
  112. //        add <<= 1;     //地址是从第二位开始的
  113. //        add &= 0xfe;   //把最低位清零  是写的命令
  114. //        add |= 0xc0;   //地址最高两位为 1  
  115. //        write_1302(0x8e,0x00);
  116. //        write_1302(add,dat);        
  117. //        write_1302(0x8e,0x80);
  118. //}
  119. //*************把数据从ds1302 RAM读出来**0-31*************/
  120. //uchar read_1302ram(uchar add)
  121. //{
  122. //        add <<= 1;     //地址是从第二位开始的
  123. //        add |= 0x01;   //把最高位置1  是读命令
  124. //        add |= 0xc0;   //地址最高两位为 1  
  125. //        return(read_1302(add));        
  126. //}                                                                                                                 



  127. /*************初始化ds1302时间***************/
  128. void init_ds1302()
  129. {
  130.         RST = 0;        //第一次读写数据时要把IO口拿低
  131.         SCLK=0;
  132.         IO= 0;
  133.         write_time();
  134. }

  135. #endif

  136. #ifndef _LCD1602_H_
  137. #define _LCD1602_H_
  138. #define uchar unsigned char
  139. #define uint unsigned int
  140. uchar sec,min,hour,day,min,year,week;
  141. sbit RS = P2^0;   //寄存器选择信号 H:数据寄存器          L:指令寄存器
  142. sbit RW = P2^1;          //读写选择信号 H:读          L:写
  143. sbit EN = P2^2;          //片选信号   下降沿触发
  144. #define yh 0x80  //第一行的初始位置
  145. #define er 0x80+0x40 //第二行初始位置
  146. uchar code tab1[]={"2020-06-19 FRI"};
  147. uchar code tab2[]={"10:32:48"};
  148. /***********************延时函数************************/
  149. void delay(uint xms)//延时函数
  150. {
  151.         uint x,y;
  152.         for(x=xms;x>0;x--)
  153.          for(y=110;y>0;y--);
  154. }
  155. void delay_uint(uint q)
  156. {
  157.         while(q--);
  158. }
  159. /*------------------------------------------------
  160.               写入命令函数
  161. ------------------------------------------------*/
  162. void LCD_Write_Cmd(uchar cmd)
  163. {  

  164. RS=0; //        选择指令寄存器
  165. RW=0;//写操作
  166. EN=0;//正跳变,将命令写入液晶模块
  167. P0=cmd; //将命令送入P0
  168. delay_uint(3);
  169. EN=1;
  170. delay_uint(25);
  171. EN=0;
  172. }
  173. /*------------------------------------------------
  174.               写入数据函数
  175. ------------------------------------------------*/
  176. void LCD_Write_Data(uchar Data)
  177. {
  178. RS=1; //选择数据寄存器
  179. RW=0; //写操作
  180. EN=0;
  181. P0= Data; //将数据送入P0
  182. delay_uint(3);
  183. EN=1;
  184. delay_uint(25);
  185. EN=0;
  186. }
  187. /*------------------------------------------------
  188.               写入星期函数
  189. ------------------------------------------------*/
  190. void LCD_Write_week(uchar week)
  191. {     
  192.     LCD_Write_Cmd(yh+0x0d);//将星期显示到LCD的第一行的13位
  193.          switch(week)
  194.         {
  195.                 case 1:LCD_Write_Data('M');
  196.                            LCD_Write_Data('O');
  197.                            LCD_Write_Data('N');
  198.                            break;
  199.            
  200.                 case 2:LCD_Write_Data('T');
  201.                            LCD_Write_Data('U');
  202.                            LCD_Write_Data('E');
  203.                            break;
  204.                
  205.                 case 3:LCD_Write_Data('W');
  206.                            LCD_Write_Data('E');
  207.                            LCD_Write_Data('D');
  208.                            break;
  209.                
  210.                 case 4:LCD_Write_Data('T');
  211.                            LCD_Write_Data('H');
  212.                            LCD_Write_Data('U');
  213.                            break;
  214.                
  215.                 case 5:LCD_Write_Data('F');
  216.                            LCD_Write_Data('R');
  217.                            LCD_Write_Data('I');
  218.                            break;
  219.                
  220.                 case 6:LCD_Write_Data('S');
  221.                            LCD_Write_Data('T');
  222.                            LCD_Write_Data('A');
  223.                            break;
  224.                
  225.                 case 7:LCD_Write_Data('S');
  226.                            LCD_Write_Data('U');
  227.                            LCD_Write_Data('N');
  228.                            break;  
  229.          }
  230. }

  231. /*------------------------------------------------
  232.               写入时分秒函数
  233. ------------------------------------------------*/
  234. void LCD_Write_sfm(uchar add,uchar dat)
  235. {
  236. uchar gw,sw;//定义十位 个位
  237. gw=dat%10;
  238. sw=dat/10;
  239. LCD_Write_Cmd(er+add); ///将时分秒显示到LCD的第二行的add位
  240. LCD_Write_Data(0x30+sw);
  241. LCD_Write_Data(0x30+gw);//0x30=48  十进制数+48=等于其ascll值   lcd1602为字符型  
  242. }
  243. /*------------------------------------------------
  244.               写入年月日函数
  245. ------------------------------------------------*/
  246. void LCD_Write_nyr(uchar add,uchar dat)
  247. {
  248. uchar gw,sw;//定义十位 个位
  249. gw=dat%10;
  250. sw=dat/10;
  251. LCD_Write_Cmd(yh+add); ///将时分秒显示到LCD的第二行的add位
  252. LCD_Write_Data(0x30+sw);
  253. LCD_Write_Data(0x30+gw);//0x30=48  十进制数+48=等于其ascll值   lcd1602为字符型  
  254. }
  255. /*------------------------------------------------
  256.               写入字符串函数
  257. ------------------------------------------------*/
  258. void LCD_Write_String(uchar x,uchar y,uchar *s)
  259. {     
  260. if (x == 0)
  261.          {     
  262.          LCD_Write_Cmd(0x80 + y);     //表示第一行
  263.          }
  264. else
  265.          {      
  266.          LCD_Write_Cmd(0xC0 + y);      //表示第二行
  267.          }        
  268. while (*s)
  269.          {     
  270. LCD_Write_Data( *s);     
  271. s ++;     
  272.          }
  273. }
  274. /*------------------------------------------------
  275.         开机液晶显示函数
  276. ------------------------------------------------*/
  277. void Init_1602()  //初始化液晶
  278. {  
  279.   LCD_Write_String(0,2,"Hello world");
  280. }
  281. /*------------------------------------------------
  282.               初始化函数
  283. ------------------------------------------------*/
  284. void LCD_Init(void)
  285. {
  286.    uchar i;
  287.    LCD_Write_Cmd(0x38);//显示模式设置:16x2显示,5x7点阵,8位数据接口
  288.    LCD_Write_Cmd(0x0c);    //显示模式设置:显示开,有光标
  289.    LCD_Write_Cmd(0x06);   //显示模式设置:光标右移,字符不移
  290.    LCD_Write_Cmd(0x01);        //清屏
  291.    Init_1602();
  292.    delay(1000);
  293.    LCD_Write_Cmd(yh+2);//字符写入的位置--第一行第2位
  294.            for(i=0;i<14;i++)
  295.         {
  296.         LCD_Write_Data(tab1[i]);
  297.         }
  298.         LCD_Write_Cmd(er+4);
  299.         for(i=0;i<8;i++)                                                                          
  300.         {
  301.         LCD_Write_Data(tab2[i]);
  302.         }

  303.    }


  304. #endif
复制代码



3.PNG
回复

使用道具 举报

ID:584814 发表于 2020-6-23 17:54 | 显示全部楼层
没加之前是好的,基本说明1602部分没问题,看图感觉是数据没正确读出。
回复

使用道具 举报

ID:788182 发表于 2020-6-23 22:04 | 显示全部楼层
是不是拐角连接问题,外部没问题的话,多半是程序逻辑问题了
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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