蓝桥杯之单片机设计与开发(31)——_第七届_蓝桥杯_国赛——“电压、频率采集设备”

时间:2024-03-28 09:08:13

 

这一个题写的我是头皮发麻,写了一个上午才给写完。测试发现应该没什么bug,先把博客发了吧。

这个题和官方给的模拟题是一个,具体可以点击链接跳转过去看看。

http://dasai.lanqiao.cn/pages/dasai/personal_province_apply.html?key=c46ca5d7d324923baf6910e8940fdd85

 1、题目

蓝桥杯之单片机设计与开发(31)——2016_第七届_蓝桥杯_国赛——“电压、频率采集设备”

蓝桥杯之单片机设计与开发(31)——2016_第七届_蓝桥杯_国赛——“电压、频率采集设备”

蓝桥杯之单片机设计与开发(31)——2016_第七届_蓝桥杯_国赛——“电压、频率采集设备”

蓝桥杯之单片机设计与开发(31)——2016_第七届_蓝桥杯_国赛——“电压、频率采集设备”

蓝桥杯之单片机设计与开发(31)——2016_第七届_蓝桥杯_国赛——“电压、频率采集设备”

2、题目说明

先去体测跑1000米,回来再写=,=

 

3、代码

下载链接:https://download.csdn.net/download/xiaomo_haa/11025753

main.c

#include <stc15.h>
#include "sys.h"

bit flag_100ms = 0, flag_1s = 0, flag_5ms = 0, flag_500ms = 0;
bit flag_change = 0, flag_write = 0;
bit flag_valtext = 0, flag_fretext = 0, flag_val = 1;
bit flag_readtime = 1;
u8 mode_time = 0, mode_val = 0, mode_fre = 0;
u8 mode = 0;
u8 Realtime[] = {0x19, 0x03, 0x15, 0x23, 0x59, 0x55, 0x05};
u8 Backuptime[] = {0x19, 0x03, 0x15, 0x23, 0x59, 0x55, 0x05};
u8 Val_time[] = {0x19, 0x03, 0x15, 0x23, 0x59, 0x55, 0x05, 0};
u8 Real_val[] = {0xdd, 20, 15, 0, 0};
u16 counter = 0, fre, fre_time;

void Delay5ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 54;
	j = 199;
	do
	{
		while (--j);
	} while (--i);
}


void main(void)
{
	static u8 index = 0;
	
	AllInit();
	Timer0Init();
	Timer1Init();
	InitDS1302(Realtime);
	
	if(Read_E2PROM(0) == 0xdd)
	{
		Real_val[1] = Read_E2PROM(1);
		Real_val[2] = Read_E2PROM(2);
	}
	else
	{
		Write_E2PROM(0, Real_val[0]);
		Delay5ms();
		Write_E2PROM(1, Real_val[1]);
		Delay5ms();
		Write_E2PROM(2, Real_val[2]);
		Delay5ms();
	}
	if(Read_E2PROM(3) < 2)
	{
		Val_time[7] = Read_E2PROM(3);
		Val_time[3] = Read_E2PROM(4);
		Val_time[4] = Read_E2PROM(5);
		Val_time[5] = Read_E2PROM(6);
	}

	EA = 1;
	
	while(1)
	{
		if(flag_100ms)
		{
			flag_100ms = 0;
			GetRealTime(Realtime);
			Real_val[3] = (u8)(Read_AIN(0x03) / 100);
			Real_val[4] = (u8)(Read_AIN(0x03) % 100);
			
			//测电压
			if(flag_valtext)
			{
				if(flag_val == 0)
				{
					if((Real_val[3] < Real_val[1]) && (Real_val[3] > Real_val[2]))
						flag_val = 1;
				}
				
				if((Real_val[3] >= Real_val[1])	&& flag_val)	//上限
				{
					flag_write = 1;
					Val_time[7] = 1;
					flag_val = 0;
				}
				else if((Real_val[3] <= Real_val[2]) && flag_val)	//下限
				{
					flag_write = 1;
					Val_time[7] = 0;
					flag_val = 0;
				}
				if(flag_write)		//读取电压事件时的时间
				{
					Val_time[3] = Realtime[3];
					Val_time[4] = Realtime[4];
					Val_time[5] = Realtime[5];
				}
			}
		}
		
		//写事件类型和时间
		if(flag_write)
		{
			if(flag_5ms)
			{
				flag_5ms = 0;
				switch(index)
				{
					case 0: Write_E2PROM(3, Val_time[7]); break;
					case 1: Write_E2PROM(4, Val_time[3]); break;
					case 2: Write_E2PROM(5, Val_time[4]); break;
					case 3: Write_E2PROM(6, Val_time[5]); flag_write = 0; break;
				}
				index ++;
				index &= 0x03;
			}
		}
		
		//测频
		if(flag_fretext)
		{
			if(flag_500ms)
			{
				TR0 = 0;
				flag_500ms = 0;
				counter = TH0 * 256 + TL0;
				TH0 = TL0 = 0;
				fre = counter * 2;
				fre_time = (u16)(1000000 / fre);
				TR0 = 1;
			}
		}
		
		//备份时间
		if(flag_readtime == 1)	
		{
			Backuptime[3] = Realtime[3];
			Backuptime[4] = Realtime[4];
			Backuptime[5] = Realtime[5];
		}
		
		KeyPress();
		TubeShow();
	}
}

sys..c

#include "sys.h"

void AllInit(void)
{
	P2 = (P2 & 0x1f) | 0x80;
	P0 = 0xff;
	P2 = (P2 & 0x1f) | 0xc0;
	P0 = 0x00;
	P2 = (P2 & 0x1f) | 0xa0;
	P0 = 0x00;
	P2 = P2 & 0x1f;
}

void Timer1Init(void)		//1毫秒@11.0592MHz
{
	AUXR |= 0x40;		//定时器时钟1T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0xCD;		//设置定时初值
	TH1 = 0xD4;		//设置定时初值
	TF1 = 0;		//清除TF1标志
	TR1 = 1;		//定时器1开始计时
	ET1 = 1;
}

void Time1(void) interrupt 3
{
	static u16 T1count1 = 0, T1count2 = 0;
	
	T1count1 ++;
	
	if(T1count1 % 2 == 0)		//2ms
		TubeScan();
	if(T1count1 % 5 == 0)		//5ms
		flag_5ms = 1;
	if(T1count1 % 100 == 0)	//100ms
		flag_100ms = 1;
	if(T1count1 >= 500)			//500ms
	{
		T1count1 = 0;
		if(flag_fretext)
			flag_500ms = 1;
	}
	
	if(mode_time || mode_val || (mode == 3))
	{
		T1count2 ++;
		if(T1count2 >= 1000)	//1s
		{
			T1count2 = 0;
			flag_1s = ~flag_1s;
		}
	}
	else
		T1count2 = 0;
	
	KeyScan();
}

void Timer0Init(void)
{
	AUXR &= 0x7F;					//定时器时钟12T模式
  TMOD = 0x04;          //设置定时器0为16位自动重装载外部记数模式
  TH0 = TL0 = 0;     		//设置定时器0初始值
	TR0 = 1;              //定时器0开始工作
}

sys.h

#ifndef _SYS_H_
#define _SYS_H_

typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;

#include <stc15.h>
#include <intrins.h>
#include "ds1302.h"
#include "iic.h"

extern bit flag_100ms, flag_1s, flag_5ms, flag_500ms;
extern bit flag_change, flag_write;
extern bit flag_valtext, flag_fretext, flag_val;
extern bit flag_readtime;
extern u8 mode_time, mode_val, mode_fre;
extern u8 mode;
extern u8 Realtime[];
extern u8 Backuptime[];
extern u8 Val_time[];
extern u8 Real_val[];
extern u16 counter, fre, fre_time;

void AllInit(void);
void Timer0Init(void);
void Timer1Init(void);

void TubeScan(void);
void TubeShow(void);

void KeyScan(void);
void KeyPress(void);


#endif



display.c

#include "sys.h"

unsigned char code table[]={0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 
                            0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e,
														0xff, 0xbf};
unsigned char TubeBuff[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char smg1, smg2, smg3, smg4, smg5, smg6, smg7, smg8;

void TubeScan(void)
{
	static u8 index = 0;
	
	P0 = 0x00;
	P2 = (P2 & 0x1f) | 0xc0;
	P0 = 0x01 << index;
	P2 = P2 & 0x1f;
	
	P0 = 0xff;
	P2 = (P2 & 0x1f) | 0xe0;
	P0 = TubeBuff[index];
	P2 = P2 & 0x1f;
	
	index ++;
	index &= 0x07;
}

void TubeShow(void)
{
	if(mode == 1)
	{
		if(mode_time == 0)
		{
			smg1 = Realtime[3] >> 4;
			smg2 = Realtime[3] & 0x0f;
			smg4 = Realtime[4] >> 4;
			smg5 = Realtime[4] & 0x0f;
			smg7 = Realtime[5] >> 4;
			smg8 = Realtime[5] & 0x0f;
			
			if(smg8 % 2 == 0)
				smg3 = smg6 = 16;
			else
				smg3 = smg6 = 17;
		}
		else if(mode_time > 0)
		{
			smg1 = Backuptime[3] >> 4;
			smg2 = Backuptime[3] & 0x0f;
			smg3 = smg6 = 17;
			smg4 = Backuptime[4] >> 4;
			smg5 = Backuptime[4] & 0x0f;
			smg7 = Backuptime[5] >> 4;
			smg8 = Backuptime[5] & 0x0f;
			if(flag_1s)
			{
				switch(mode_time)
				{
					case 1: smg1 = smg2 = 16; break;
					case 2: smg4 = smg5 = 16; break;
					case 3: smg7 = smg8 = 16; break;
				}
			}
		}
	}
	else if(mode == 2)
	{
		if(mode_val == 0)
		{
			smg1 = smg3 = 17;
			smg2 = 1;
			smg4 = 16;
			smg5 = Real_val[3] / 10;
			smg6 = Real_val[3] % 10;
			smg7 = Real_val[4] / 10;
			smg8 = Real_val[4] % 10;
		}
		else
		{
			smg1 = Real_val[1] / 10;
			smg2 = Real_val[1] % 10;
			smg3 = smg4 = 0;
			smg5 = Real_val[2] / 10;
			smg6 = Real_val[2] % 10;
			smg7 = smg8 = 0;
			
			if(flag_1s)
			{
				if(mode_val == 1)
					smg1 = smg2 = smg3 = smg4 = 16;
				else if(mode_val == 2)
					smg5 = smg6 = smg7 = smg8 = 16;
			}
		}
	}
	else if(mode == 3)
	{
		if(flag_1s)
		{
			smg1 = smg2 = smg3 = smg4 = smg5 = smg6 = 16;
			smg7 = 0;
			smg8 = Val_time[7];
		}
		else
		{
			smg1 = Val_time[3] >> 4;
			smg2 = Val_time[3] &0x0f;
			smg3 = smg6 = 17;
			smg4 = Val_time[4] >> 4;
			smg5 = Val_time[4] &0x0f;
			smg7 = Val_time[5] >> 4;
			smg8 = Val_time[5] &0x0f;
		}
	}
	else if(mode == 4)
	{
		smg1 = smg3 = 17;
		smg2 = 2;
		
		if(mode_fre == 1)
		{
			smg4 = fre / 10000;
			smg5 = (fre % 10000) / 1000;
			smg6 = (fre % 1000) / 100;
			smg7 = (fre % 100) /10;
			smg8 = fre % 10;
		}
		else if(mode_fre == 2)
		{
			smg4 = fre_time / 10000;
			smg5 = (fre_time % 10000) / 1000;
			smg6 = (fre_time % 1000) / 100;
			smg7 = (fre_time % 100) /10;
			smg8 = fre_time % 10;
		}
	}
	else
		smg1 = smg2 = smg3 = smg4 = smg5 = smg6 = smg7 = smg8 = 16;
		
	TubeBuff[3] = table[smg1];
	TubeBuff[2] = table[smg2];
	TubeBuff[1] = table[smg3];
	TubeBuff[0] = table[smg4];
	TubeBuff[7] = table[smg5];
	TubeBuff[6] = table[smg6];
	TubeBuff[5] = table[smg7];
	TubeBuff[4] = table[smg8];
}

key.c

#include "sys.h"

sbit KeyOut1 = P4^4;
sbit KeyOut2 = P4^2;
sbit KeyOut3 = P3^5;
sbit KeyOut4 = P3^4;
sbit KeyIn1 = P3^0;
sbit KeyIn2 = P3^1;
sbit KeyIn3 = P3^2;
sbit KeyIn4 = P3^3;

unsigned char code KeyMap[4][4] = {{0x01, 0x02, 0x00, 0x00}, 
																	 {0x03, 0x04, 0x00, 0x00}, 
																	 {0x05, 0x06, 0x00, 0x00}, 
																	 {0x07, 0x08, 0x00, 0x00}};
unsigned char KeyBuff[4][4] = {{0xff, 0xff, 0xff, 0xff},
															 {0xff, 0xff, 0xff, 0xff}, 
															 {0xff, 0xff, 0xff, 0xff}, 
															 {0xff, 0xff, 0xff, 0xff}};
unsigned char KeySta[4][4] = {{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
unsigned char KeyBackup[4][4] = {{1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}};
	
void KeyScan(void)
{
	static u8 index = 0;
	u8 i;
	
	KeyBuff[0][index] = (KeyBuff[0][index] << 1) | KeyIn1;
	KeyBuff[1][index] = (KeyBuff[1][index] << 1) | KeyIn2;
	KeyBuff[2][index] = (KeyBuff[2][index] << 1) | KeyIn3;
	KeyBuff[3][index] = (KeyBuff[3][index] << 1) | KeyIn4;
	
	for(i = 0; i < 4; i ++)
	{
		if((KeyBuff[i][index] & 0x0f) == 0x00)
			KeySta[i][index] = 0;
		else if((KeyBuff[i][index] & 0x0f) == 0x0f)
			KeySta[i][index] = 1;	
	}
	
	index ++;
	index &= 0x03;
	switch(index)
	{
		case 0: KeyOut4 = 1; KeyOut1 = 0; break;
		case 1: KeyOut1 = 1; KeyOut2 = 0; break;
		case 2: KeyOut2 = 1; KeyOut3 = 0; break;
		case 3: KeyOut3 = 1; KeyOut4 = 0; break;
	}
}

void KeyAction(unsigned char keycode)
{
	if(keycode == 0x01)		//S7		显示时钟
	{
		mode = 1;
		mode_time = 0;
		if(flag_readtime == 0)
		{
			flag_readtime = 1;
			if(flag_change == 1)		//如果没有修改时间就显示实时时间
			{
				Realtime[3] = Backuptime[3]; 
				Realtime[4] = Backuptime[4]; 
				Realtime[5] = Backuptime[5];
			}
			SetRealTime(Realtime);
			flag_change = 0;
		}
	}
	else if(keycode == 0x03)		//S6	电压测量
	{
		mode = 2;
		mode_val = 0;
	}
	else if(keycode == 0x02)		//S11 加
	{
		if(mode == 1)			//时间
		{
			if(mode_time > 0)
			{
				Backuptime[mode_time + 2] ++;
				
				if(mode_time == 1)
				{
					if((Backuptime[mode_time + 2] & 0x0f) > 9)	//个位大于9
						Backuptime[mode_time + 2] += 6;						//加6修正
					if(Backuptime[mode_time + 2] >= 0x24)				//时钟上限
						Backuptime[mode_time + 2] = 0x00;
				}
				else
				{				
					if((Backuptime[mode_time + 2] & 0x0f) > 9)	//个位大于9
						Backuptime[mode_time + 2] += 6;						//加6修正
					if(Backuptime[mode_time + 2] >= 0x60)				//分秒上限
						Backuptime[mode_time + 2] = 0x00;					
				}
				
				flag_change = 1;
			}
		}
		else if(mode == 2)		//电压
		{
			if(mode_val > 0)
			{
				Real_val[mode_val] += 5;
				if(Real_val[mode_val] >= 50)
					Real_val[mode_val] = 50;
				if(Real_val[2] >= Real_val[1])
					Real_val[2] = Real_val[1];
				
				Write_E2PROM(mode_val, Real_val[mode_val]);
			}
		}
	}
	else if(keycode == 0x04)		//S10 减
	{
		if(mode == 1)			//时间
		{
			if(mode_time > 0)
			{
				if(((Backuptime[mode_time + 2] & 0x0f) == 0) && (Backuptime[mode_time + 2] > 0))
					Backuptime[mode_time + 2] -= 6;
				else if(Backuptime[mode_time + 2] == 0x00)
				{
					if(mode_time == 1)
						Backuptime[mode_time + 2] = 0x23;
					else
						Backuptime[mode_time + 2] = 0x59;
				}
				else
					Backuptime[mode_time + 2] --;
				
				flag_change = 1;
			}
		}
		else if(mode == 2)		//电压
		{
			if(mode_val > 0)
			{
				if(Real_val[mode_val] >= 5)
					Real_val[mode_val] -= 5;
				else
					Real_val[mode_val] = 0;
				
				if(Real_val[1] <= Real_val[2])
					Real_val[1] = Real_val[2];
				
				Write_E2PROM(mode_val, Real_val[mode_val]);
			}
		}
	}
	else if(keycode == 0x05)		//S5		频率测量
	{
		mode = 4;
		flag_fretext = 1;
		mode_fre = 1;
	}
	else if(keycode == 0x06)		//S9		查询
	{
		mode = 3;
		flag_valtext = 0;
	}
	else if(keycode == 0x07)		//S4
	{
		if(mode == 1)		//时间
		{
			flag_readtime = 0;
			mode_time ++;
			if(mode_time > 3)
				mode_time = 1;
		}
		else if(mode == 2)		//电压
		{
			mode_time = 0;
			mode_val ++;
			if(mode_val >= 3)
				mode_val = 1;
		}
		else if(mode == 4)		//频率
		{
			mode_fre ++;
			if(mode_fre > 2)
				mode_fre = 1;
		}
	}
	
	if(mode != 3)
		flag_valtext = 1;
	if(mode != 4)
		flag_fretext = 1;
}

void KeyPress(void)
{
	u8 i, j;
	
	for(i = 0; i < 4; i ++)
	{
		for(j = 0; j < 4; j ++)
		{
			if(KeyBackup[i][j] != KeySta[i][j])
			{
				if(KeySta[i][j] == 0)
					KeyAction(KeyMap[i][j]);
				KeyBackup[i][j] = KeySta[i][j];
			}
		}
	}
}

iic.c

#include "sys.h"

#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}    

//总线引脚定义
sbit SDA = P2^1;  /* 数据线 */
sbit SCL = P2^0;  /* 时钟线 */


//总线启动条件
void IIC_Start(void)
{
	SDA = 1;
	SCL = 1;
	somenop;
	SDA = 0;
	somenop;
	SCL = 0;	
}

//总线停止条件
void IIC_Stop(void)
{
	SDA = 0;
	SCL = 1;
	somenop;
	SDA = 1;
}

//应答位控制
void IIC_Ack(bit ackbit)
{
	if(ackbit) 
	{	
		SDA = 0;
	}
	else 
	{
		SDA = 1;
	}
	somenop;
	SCL = 1;
	somenop;
	SCL = 0;
	SDA = 1; 
	somenop;
}

//等待应答
bit IIC_WaitAck(void)
{
	SDA = 1;
	somenop;
	SCL = 1;
	somenop;
	if(SDA)    
	{   
		SCL = 0;
		IIC_Stop();
		return 0;
	}
	else  
	{ 
		SCL = 0;
		return 1;
	}
}

//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{   
		if(byt&0x80) 
		{	
			SDA = 1;
		}
		else 
		{
			SDA = 0;
		}
		somenop;
		SCL = 1;
		byt <<= 1;
		somenop;
		SCL = 0;
	}
}

//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
	unsigned char da;
	unsigned char i;
	
	for(i=0;i<8;i++)
	{   
		SCL = 1;
		somenop;
		da <<= 1;
		if(SDA) 
		da |= 0x01;
		SCL = 0;
		somenop;
	}
	return da;
}

unsigned int Read_AIN(unsigned char chn)
{
	unsigned int val;
	unsigned char dat;
	EA = 0;
	IIC_Start();						//IIC总线起始信号							
	IIC_SendByte(0x90);			//PCF8591的写设备地址		
	IIC_WaitAck();  		    //等待从机应答		
	IIC_SendByte(chn); 			//写入PCF8591的控制字节		
	IIC_WaitAck();  				//等待从机应答						
	IIC_Stop(); 						//IIC总线停止信号					
	
	IIC_Start();						//IIC总线起始信号									
	IIC_SendByte(0x91); 	  //PCF8591的读设备地址		
	IIC_WaitAck(); 			    //等待从机应答		
	dat = IIC_RecByte();	  //读取PCF8591通道3的数据 			
	IIC_Ack(0); 						//产生非应答信号				
	IIC_Stop(); 						//IIC总线停止信号		
	val = (u16)(dat * (5000 / 255));
	EA = 1;
	
	return val;	
}


void Write_E2PROM(unsigned char add, unsigned char dat)
{
	EA = 0;
  IIC_Start();
  IIC_SendByte(0xa0);	//发送器件地址
  IIC_WaitAck();
  IIC_SendByte(add);	//发送操作地址
  IIC_WaitAck();
  IIC_SendByte(dat);	//写一字节
  IIC_WaitAck();
  IIC_Stop();
  somenop;
	EA = 1;
}

unsigned char Read_E2PROM(unsigned char add)
{
  unsigned char d;
	
	IIC_Start();
	IIC_SendByte(0xa0); 	//发送器件地址
	IIC_WaitAck();
	IIC_SendByte(add);		//发送要操作的地址
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0xa1);		//发送读操作
	IIC_WaitAck();
	d = IIC_RecByte();	//读一字节
	IIC_Ack(0);
	IIC_Stop();
	return d;
}

iic.h

#ifndef _IIC_H
#define _IIC_H

//函数声明
void IIC_Start(void); 
void IIC_Stop(void);  
void IIC_Ack(bit ackbit); 
void IIC_SendByte(unsigned char byt); 
bit IIC_WaitAck(void);  
unsigned char IIC_RecByte(void); 
unsigned int Read_AIN(unsigned char chn);
void Write_E2PROM(unsigned char add, unsigned char dat);
unsigned char Read_E2PROM(unsigned char add);

#endif

ds1302.c

#include "sys.h"

sbit DS1302_IO = P2^3;
sbit DS1302_CK = P1^7;
sbit DS1302_CE = P1^3;

void DS1302ByteWrite(unsigned char dat) 
{
	unsigned char mask;
	DS1302_IO = 1;					//拉低IO总线
	for(mask = 0x01; mask != 0; mask <<= 1)	//低位在前,逐位移出
	{
		if((dat&mask) != 0)		//首先输出该位数据
			DS1302_IO = 1;
		else
			DS1302_IO = 0;
		DS1302_CK = 1;				//拉高时钟线
		DS1302_CK = 0;				//拉低时钟线,完成一个位的操作
	}
	DS1302_IO = 1;  				//写完之后确保释放IO总线
}  

unsigned char DS1302ByteRead(void)
{
	unsigned char mask, dat = 0;
	
	for(mask = 0x01; mask != 0; mask <<= 1)	//低位在前,逐位读取
	{
		if(DS1302_IO)			//首先读取此时的IO引脚,并设置dat中的对应位
			dat |= mask;

		DS1302_CK = 1;		//拉高时钟
		DS1302_CK = 0;		//再拉低时钟,完成一个位的操作
	}
	return dat;					//返回读到的字节数据
} 

void DS1302SingleWrite(unsigned char reg, unsigned char dat)     
{
	DS1302_CE = 1;					//使能片选信号
	DS1302ByteWrite((reg << 1) | 0x80);	//发送写寄存器指令
	DS1302ByteWrite(dat);		//写入字节数据
	DS1302_CE = 0;					//除能片选信号
}

unsigned char DS1302SingleRead(unsigned char reg)
{
	unsigned char dat;
	
	DS1302_CE = 1;					//使能片选信号
	DS1302ByteWrite((reg << 1) | 0x81);	//发送读寄存器指令
	dat = DS1302ByteRead();	//读取字节数据
	DS1302_CE = 0;					//除能片选信号
	
	DS1302_IO = 0;					//单字节读必须加的!
	
	return dat;         
}

void DS1302BurstWrite(unsigned char *dat)
{
	unsigned char i;
	
	DS1302_CE = 1;
	DS1302ByteWrite(0xBE);				//发送突发写寄存器指令
	for(i = 0; i < 7; i ++)				//连续写入8字节数据
		DS1302ByteWrite(*dat++);        
	DS1302_CE = 0;      
}

void DS1302BurstRead (unsigned char *dat)
{
	unsigned char i;
	
	DS1302_CE = 1;
	DS1302ByteWrite(0xBF);				//发送突发读寄存器指令
	for(i = 0; i < 7; i++)				//连续读取8个字节
		dat[i] = DS1302ByteRead();      
	DS1302_CE = 0;  
	
	DS1302_IO = 0;								//突发读必须加
}	

void GetRealTime(unsigned char *time)
{
	unsigned char buf[8];
	
	DS1302BurstRead(buf);
	time[0] = buf[6];
	time[1] = buf[4];
	time[2] = buf[3];
	time[3] = buf[2];
	time[4] = buf[1];
	time[5] = buf[0];
	time[6] = buf[5];
}

void SetRealTime(unsigned char *time)
{
	unsigned char buf[8];
	
	buf[7] = 0;
	buf[6] = time[0];
	buf[4] = time[1];
	buf[3] = time[2];
	buf[2] = time[3];
	buf[1] = time[4];
	buf[0] = time[5];
	buf[5] = time[6];
	DS1302BurstWrite(buf);
}

void InitDS1302(unsigned char *time)
{
	unsigned char dat;
	signed char i = 7;
	unsigned char timeinit[8];
	
	while(i >= 0)
	{
		timeinit[i] = time[i];
		i --;
	}
	
	DS1302_CE = 0;								//初始化DS1302通信引脚
	DS1302_CK = 0;
	dat = DS1302SingleRead(0);			//读取秒寄存器
	DS1302SingleWrite(7, 0x00);  	//撤销写保护以允许写入数据
	SetRealTime(&timeinit);					//设置DS1302时间
}

ds1302.h

#ifndef __DS1302_H
#define __DS1302_H

void DS1302ByteWrite(unsigned char dat);
unsigned char DS1302ByteRead(void);
void DS1302SingleWrite(unsigned char reg, unsigned char dat);
unsigned char DS1302SingleRead(unsigned char reg);
void DS1302BurstWrite(unsigned char *dat);
void DS1302BurstRead (unsigned char *dat);
void GetRealTime(unsigned char *time);
void SetRealTime(unsigned char *time);
void InitDS1302(unsigned char *time);

#endif