#蓝桥杯
#单片机
这个程序的数码管现在变成乱码了,不知道为什么,想请大家帮忙看一看。
是用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