赐予我力量吧 2023-04-01 21:05 采纳率: 80%
浏览 58
已结题

单片机数码管乱码是为什么? 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


  • 写回答

3条回答 默认 最新

  • qq_40627021 2023-04-01 21:24
    关注

    请先找一下硬件问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月9日
  • 已采纳回答 4月1日
  • 修改了问题 4月1日
  • 创建了问题 4月1日

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛