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

单片机数码管乱码是为什么? 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 python动态规划:N根火柴摆出的最大数字
  • ¥20 (标签-excel)
  • ¥200 求idea10和MyEclipse7.1
  • ¥20 vb6.0截取当前窗体保存为jpg文件
  • ¥20 苹果手机不使用大疆sdk怎么获取遥控器控制信息或如何接入大疆sdk并且成功上架sdk
  • ¥20 woocommerce 注册按键重定向
  • ¥100 求书法图像文字切割代码
  • ¥15 同一个波形探测距离不同的目标,为什么fft之后得到的频谱图会发生移动,不应该时移不改变幅度谱吗(标签-matlab)(相关搜索:matlab仿真)
  • ¥15 Proteus仿真程序只能执行一次
  • ¥15 语音识别websocket报错