【蓝桥杯(51) STC15F2K60S2】之 “DS1302“实验

相关代码:

主函数部分:

#include “STC15F2K60S2.H”
#include “absacc.h”
#include “ds1302.h”

unsigned char SMGNoDot_CA[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char Write_DS1302[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char Read_DS1302[7] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8c};
//21年2月26日星期五24点59分30秒
unsigned char Time[7] = {0x30,0x59,0x23,0x26,0x02,0x05,0x21};

void Delay(unsigned char t)
{
while(t–);
}

void config()
{
unsigned char i;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302_Byte( Write_DS1302[i],Time[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}

void Read_DS1302C()
{
unsigned char i;
for(i=0;i<7;i++)
{
Time[i] = Read_Ds1302_Byte(Read_DS1302[i]);
}
}

void Clear()
{
XBYTE[0xc000] = 0xff;
XBYTE[0xe000] = 0xff;
}

void DisSMG(unsigned char pos,unsigned char dat)
{
XBYTE[0xe000] = 0xff;
XBYTE[0xc000] = 0x01 << pos;
XBYTE[0xe000] = dat;
}

void Display()
{
DisSMG(0,SMGNoDot_CA[Time[2] / 16]);
Delay(100);
DisSMG(1,SMGNoDot_CA[Time[2] % 16]);
Delay(100);

DisSMG(2,0xbf);
Delay(100);

DisSMG(3,SMGNoDot_CA[Time[1] / 16]);
Delay(100);
DisSMG(4,SMGNoDot_CA[Time[1] % 16]);
Delay(100);

DisSMG(5,0xbf);
Delay(100);

DisSMG(6,SMGNoDot_CA[Time[0] / 16]);
Delay(100);
DisSMG(7,SMGNoDot_CA[Time[0] % 16]);
Delay(100);

 Clear();

}

void main(void)
{
XBYTE[0x8000] = 0xff;
config();
while(1)
{
Read_DS1302C();
Display();
}
}

DS1302驱动.c文件:

/*
程序说明: DS1302驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/

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

sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302复位

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文件:

#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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

world呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值