单片机数码管乱码是为什么? DS1302 日历 蓝桥杯

#蓝桥杯
#单片机
这个程序的数码管现在变成乱码了,不知道为什么,想请大家帮忙看一看。
是用DS1302做的日历,显示年月日、星期几、时分秒

时钟.c


#include "reg52.h"
#include "ds1302.h"

unsigned char SMGNoDot_CA[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; 
unsigned char SMGDot_CA[10] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; 
unsigned char Write_DS1302_adrr[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char Read_DS1302_adrr[7] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};

unsigned char Timer[7] = {0x34,0x59,0x23,0x01,0x04,0x06,0x23};

void DS1302_Config()
{
    unsigned char i;
    Write_Ds1302_Byte(0x8e,0x00);  

    for(i = 0;i < 7;i++)
    {
     Write_Ds1302_Byte(Write_DS1302_adrr[i],Timer[i]);
    }
    Write_Ds1302_Byte(0x8e,0x80); 
}

void Read_DS1302_Timer()
{
    unsigned char i;
    for(i = 0;i < 7;i++)
    {
   Timer[i] = Read_Ds1302_Byte(Read_DS1302_adrr[i]); 
    }
}

//==================================
void DisPlay_All(unsigned char dat)
{
    P2 = 0xc0;
    P0=0xff;
    P2 = 0xe0;
    P0=dat;
}

void DisplaySMG_Bit(unsigned char value,unsigned char pos)
{
    P2 = 0xc0;
    P0=0x01 << pos;
    P2 = 0xe0;
    P0=value;
}

void Delay(unsigned int t)
{
    while(t--);
}

void Display_DS1302()
{
    DisplaySMG_Bit(0,SMGNoDot_CA[Timer[2]/16]);
    Delay(100);
    DisplaySMG_Bit(1,SMGNoDot_CA[Timer[2]%16]);
    Delay(100);
    
  DisplaySMG_Bit(2,0xbf);  //1011 1111
    Delay(100);
    DisplaySMG_Bit(3,SMGNoDot_CA[Timer[1]/16]);
    Delay(100);
    DisplaySMG_Bit(4,SMGNoDot_CA[Timer[1]%16]);
    Delay(100);
    
    DisplaySMG_Bit(5,0xbf);  //1011 1111
    Delay(100);
    DisplaySMG_Bit(6,SMGNoDot_CA[Timer[0]/16]);
    Delay(100);
  DisplaySMG_Bit(7,SMGNoDot_CA[Timer[0]%16]);
    Delay(100);
    
    DisPlay_All(0xff);
}
//================================================
void main()
{
    DS1302_Config();
    while(1)
    {
        Read_DS1302_Timer();
        Display_DS1302();
    }
}
ds1302.c
```c

#include <reg52.h>
#include <intrins.h>

sbit SCK=P1^7;        
sbit SDA=P2^3;        
sbit RST = P1^3;                     

void Write_Ds1302(unsigned  char temp) 
{
    unsigned char i;
    for (i=0;i<8;i++)         
    { 
        SCK=0;
        SDA=temp&0x01;
        temp>>=1; 
        SCK=1;
    }
}   

void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{
     RST=0;    _nop_();
     SCK=0;    _nop_();
     RST=1;     _nop_();  
     Write_Ds1302(address);    
     Write_Ds1302(dat);        
     RST=0; 
}

unsigned char Read_Ds1302_Byte ( unsigned char address )
{
     unsigned char i,temp=0x00;
     RST=0;    _nop_();
     SCK=0;    _nop_();
     RST=1;    _nop_();
     Write_Ds1302(address);
     for (i=0;i<8;i++)     
     {        
        SCK=0;
        temp>>=1;    
         if(SDA)
         temp|=0x80;    
         SCK=1;
    } 
     RST=0;    _nop_();
     SCK=0;    _nop_();
    SCK=1;    _nop_();
    SDA=0;    _nop_();
    SDA=1;    _nop_();
    return (temp);            
}



ds1302.h


```bash


```c
#ifndef __DS1302_H
#define __DS1302_H

void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );  
#endif


请先找一下硬件问题

已经知道了,是段选位选写反了

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这篇文章:【完美解决】ds1302外置时钟模块重复初始化时间问题(单片机掉电,模块电池供电) 也许有你想要的答案,你可以看看
  • 除此之外, 这篇博客: 蓝桥杯单片机-DS1302时钟模块中的 1、在ds1302.c文件中定义三个数组 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    unsigned char code READ_A[7]={0X81,0X83,0X85,0X87,0X89,0X8B,0X8D};  //读操作相应寄存器的地址
    unsigned char code WRITE_A[7]={0X80,0X82,0X84,0X86,0X88,0X8A,0X8C}; //写操作相应寄存器的地址
    unsigned char Time[7]={0x50,0x59,0x23,0x00,0x00,0x00,0x00};         //秒 分 时 日 月 周 年
    • 关键字code:将数据写入ROM,是数据不可被改变


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^