蓝桥杯单片机ds1302时钟

该博客主要介绍了如何使用STM32单片机配合DS1302时钟芯片实现时间的读取和显示,并通过矩阵按键进行功能切换,包括显示时分秒和年月日。程序中包含了初始化设置、数码管显示函数以及按键处理函数,实现了实时时间的更新和控制。
摘要由CSDN通过智能技术生成

main函数

#include <STC15F2K60S2.H>
#include <intrins.h>
#include <ds1302.h>

#define uc unsigned char
#define ui unsigned int

extern uc sj[7];
uc b;

sbit H1=P3^0;
sbit H2=P3^1;
sbit H3=P3^2;
sbit H4=P3^3;
sbit L1=P3^4;
sbit L2=P3^5;
sbit L3=P4^2;
sbit L4=P4^4;
uc code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6};//0-9,-,关闭,C
uc code tac[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
void hc138(uc n)//138译码器
{
    switch(n)
     {
	 case 0:P2=(P2&0x1f);break;
	 case 4:P2=(P2&0x1f)|0x80;break;
     case 5:P2=(P2&0x1f)|0xa0;break;
	 case 6:P2=(P2&0x1f)|0xc0;break;
     case 7:P2=(P2&0x1f)|0xe0;break;
	 }
}


void delay()//延时n毫秒
{
unsigned char i, j;

	_nop_();
	_nop_();
	_nop_();
	i = 11;
	j = 190;
	do
	{
		while (--j);
	} while (--i);
}

void OutputSMG(uc gqb,uc yxl)//数码管输出函数
{
  
  hc138(6);
  P0=0x01<<gqb;
  hc138(7);
  P0=yxl;	
  delay();

}

void OutputP0(uc add,uc dat)//输出函数
{
  hc138(add);
  P0=dat;
}
void Initsystem()//初始化
{
  OutputP0(4,0xff);
  OutputP0(5,0x00);
  OutputP0(6,0x00);
  OutputP0(7,0x00);
}
void SMGgn_sz1()//数码管功能 时分秒
{
 
	
    OutputSMG(0,tab[sj[2]/10]);
	OutputSMG(1,tac[sj[2]%10]);
	OutputSMG(2,tab[sj[1]/10]);
	OutputSMG(3,tac[sj[1]%10]);
	OutputSMG(4,tab[sj[0]/10]);
	OutputSMG(5,tab[sj[0]%10]);
    OutputSMG(6,tab[10]);
	OutputSMG(7,tab[sj[5]]);
}
void SMGgn_sz2()//数码管功能 年月日
{
 
	
    OutputSMG(0,tab[sj[6]/10]);
	OutputSMG(1,tab[sj[6]%10]);
	OutputSMG(2,tab[10]);
	OutputSMG(3,tab[sj[4]/10]);
	OutputSMG(4,tab[sj[4]%10]);
	OutputSMG(5,tab[10]);
	OutputSMG(6,tab[sj[3]/10]);
	OutputSMG(7,tab[sj[3]%10]);

}
void keycan()//矩阵按键
{
 H1=0;
 H2=H3=H4=1;
 L1=L2=L3=L4=1;
 if(L4==0)
 {delay();while(L4==0);b++;if(b==2)b=0;}//s7
 H2=0;
 H1=H3=H4=1;
 L1=L2=L3=L4=1;
 if(L4==0)
 {delay();while(L4==0);Ds1302_Stop();}//s6
  H3=0;
 H1=H2=H4=1;
 L1=L2=L3=L4=1;
 if(L4==0)
 {delay();while(L4==0); Ds1302_Start();}//s5
}

void main()//主函数
{
    Initsystem();
	DS1302_init();  
    while(1)
    {
	readtime();
	keycan();
	if(b==0)
	{SMGgn_sz1();}
	if(b==1)
	{SMGgn_sz2();}
    }
}

ds1302.c

#include "ds1302.h"
/***新加***/
unsigned char sj[7]; //读取时间缓存区域
unsigned char code tabsj[]={50,59,23,31,12,5,21};
				          //秒 分 时 日 月 星期 年 


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

void Write_Ds1302( unsigned char address,unsigned char dat )     
{
   unsigned  char aum; //加
 	RST=0;
	_nop_();
 	SCK=0;
	_nop_();
 	RST=1;	
   	_nop_();
 	Write_Ds1302_Byte(address);
	aum=(dat/10<<4)|(dat%10);//加	
 	Write_Ds1302_Byte(aum);		
 	RST=0; 
}
/***新加时间初始化***/
void DS1302_init()
{
	unsigned  char i;
	unsigned  char add=0x80;		//定义初始写入地址
	Write_Ds1302(0x8e,0x00);		//对WP置零,允许写操作
	for(i=0;i<7;i++)				//循环7次依次
	{								//
		Write_Ds1302(add,tabsj[i]);	//初始化 秒 分 时 日 月 星期 年
		add=add+2;					//变更写入地址
	}
	Write_Ds1302(0x8e,0x80);	    //开启写保护
}

unsigned char Read_Ds1302 ( unsigned char address )
{
 	unsigned char i,temp=0x00;
	unsigned char dat1,dat2; //新加
 	RST=0;
	_nop_();
 	SCK=0;
	_nop_();
 	RST=1;
	_nop_();
 	Write_Ds1302_Byte(address);
 	for (i=0;i<8;i++) 	
 	{		
		SCK=0;
		temp>>=1;	
 		if(SDA)
 		temp|=0x80;	
 		SCK=1;
	} 
 	RST=0;
	_nop_();
 	RST=0;
	SCK=0;
	_nop_();
	SCK=1;
	_nop_();
	SDA=0;
	_nop_();
	SDA=1;
	dat1=temp/16;	 //新加,变为16进制
	dat2=temp%16;
	temp=dat1*10+dat2;
	return (temp);			
}
/***读取时间新加***/
void readtime()		 //与初始化函数类似
{
	unsigned char i;
	unsigned char add=0x81;	       //定义初始读取地址
	Write_Ds1302(0x8e,0x00);	   //关闭写保护
	for(i=0;i<7;i++)
	{
		sj[i]=Read_Ds1302(add);  //将读出地址依次存入缓存区域
		add=add+2;			       //变更读取地址
	}
	Write_Ds1302(0x8e,0x80);	   //开启写保护
}

void Ds1302_Stop()
{
	unsigned char temp;
	Write_Ds1302(0x8e,0x00);
	temp=Read_Ds1302(0x81);
	Write_Ds1302(0x80,temp|0x80);
	Write_Ds1302(0x8e,0x80);
}

void Ds1302_Start()
{
	unsigned char temp;
	Write_Ds1302(0x8e,0x00);
	temp=Read_Ds1302(0x81);
	Write_Ds1302(0x80,temp&0x7f);
	Write_Ds1302(0x8e,0x80);
}









ds1302.h

#ifndef __DS1302_H
#define __DS1302_H

#include "stc15f2k60s2.h"
#include "intrins.h"

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

void DS1302_init();
void Write_Ds1302_Byte(unsigned  char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );
void readtime();
void Ds1302_Stop();
void Ds1302_Start();

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

唐诗宋詞

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

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

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

打赏作者

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

抵扣说明:

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

余额充值