蓝桥杯单片机第7届国赛程序题参考答案

蓝桥杯单片机第7届国赛程序题参考答案,第1张

        答案仅供参考,有错误欢迎指正,非常感谢!!!

        文件较多,代码较长,感谢查看!!!

目录

题目分析

main.h

main.c

Init.h

Init.c

SMG.h

SMG.c

DSQ.h

DSQ.c

YanShi.h

YanShi.c

JZKey.h

JZKey.c

ds1302.h

ds1302.c

iic.h

iic.c

LN555.h

LN555.c

题目分析

       其实这届真题每什么难点,居然连LED都没有,就很舒服。主要还是一个逻辑问题,至于题目难理解的地方倒是没有,而且题目也都说得很清楚了。

        直接上代码。

main.h
#ifndef __MAIN_H_
#define __MAIN_H_

#define uchar unsigned char
#define uint unsigned int

#include 

#include "Init.h"
#include "SMG.h"
#include "DSQ.h"
#include "YanShi.h"
#include "JZKey.h"
#include "ds1302.h"
#include "iic.h"
#include "LN555.h"

void SMG_XianShi();
void ShiJian_JiLu();

#endif
main.c
#include "main.h"

uchar flag1=0;//定时器1标志位
long int Hz=0;//频率
unsigned int Hz_time=0;//计算频率的时间
uchar DianYa_Date=0;//电压数据
uchar CaiDan=0;//菜单				0:时钟;1:电压;2频率;3:查询
uchar JieMian_ShiJian=0;//时间界面	0;时间显示界面;1:时间设置界面
uchar JieMian_DianYa=0;//电压界面	0:电压测量界面;1:电压阈值设定界面
uchar JieMian_Hz=0;//频率界面		0:频率测量显示界面-频率;1:频率测量显示界面-周期
uchar JieMian_ChaXun=0;//查询界面	0:事件查询界面-事件类型;1:事件查询界面-发生事件
uchar ShiJian_SheZhi_flag=2;//时间设置标志位	0:小时;1:分钟;2:秒钟
uchar ShangXia_flag=1;//电压上下限选择标志位	0:上限VH;1:下限VL
char ShangXian_VH=20;//电压上限
char XiaXian_VL=10;//电压下限
char Shi_temp;//临时的时
char Fen_temp;//临时的分
char Miao_temp;//临时的秒
uint ShanShuo_time=0;//闪烁的时间
uint DianYa;//电压
long int ZhouQi=0;//周期
uchar EEPROM_flag;//EEPROM标志位
uchar ShiJian_shi;//事件发生的时
uchar ShiJian_fen;//事件发生的分
uchar ShiJian_miao;//事件发生的秒
char ShangXian_VH_1=20;//电压上限,确定值
char XiaXian_VL_1=10;//电压下限,确定值
uchar JiLu_flag=1;//记录标志位		0:停止记录;1:允许记录
uchar ShiJian_LeiXing;//事件类型
uchar ADC_flag=1;//ADC采集标志位

void main(){
	uchar key_num;
	uchar i;
	Init_BZ();
	SMG_Init();
	DSQ_1_Init();
	JZKey_Init();
	LN555_Init();
	DS1302_XieDate(23,59,55);
	EEPROM_flag=IIC_EEPROM_GetDate(0x10);
	Delay10ms();
	if(EEPROM_flag!=1){//第一次上电
		IIC_EEPROM_XieDate(0x10,1);
		Delay10ms();
		for(i=1;i<=6;i++){
			IIC_EEPROM_XieDate(0x10+i,100);
			Delay10ms();
		}
		IIC_EEPROM_XieDate(0x20,20);
		Delay10ms();
		IIC_EEPROM_XieDate(0x21,10);
		Delay10ms();
	}
	else if(EEPROM_flag==1){//多次上电
		ShangXian_VH_1=IIC_EEPROM_GetDate(0x20);
		Delay10ms();
		XiaXian_VL_1=IIC_EEPROM_GetDate(0x21);
		Delay10ms();
		ShangXian_VH=ShangXian_VH_1;
		XiaXian_VL=XiaXian_VL_1;
	}
	while(1){
		//获取时间
		EA=0;
		DS1302_GetDate();
		EA=1;
		
		//采集电压
		if(ADC_flag>=1){
			ADC_flag=0;
			DianYa_Date=IIC_ADC_GetDate(3);
			DianYa=((DianYa_Date*1.0)/51.0)*1000;
		}
		
		//事件记录
		if(((DianYa*1.0)/100.0)>=XiaXian_VL_1&&((DianYa*1.0)/100.0)<=ShangXian_VH_1){//电压在上下限之间
			JiLu_flag=1;
		}
		else if(((DianYa*1.0)/100.0)ShangXian_VH_1){//电压高于上限
			if(JiLu_flag){
				ShiJian_JiLu();
				IIC_EEPROM_XieDate(0x14,1);
				Delay10ms();
			}
			JiLu_flag=0;
		}
		
		//获取按键值
		key_num=JZKey_GetKeynum();
		if(key_num==7){//“时钟”按键
			if(JieMian_ShiJian){//从时间设置界面转到时间显示界面
				DS1302_XieDate(Shi_temp,Fen_temp,Miao_temp);
			}
			CaiDan=0;
			JieMian_ShiJian=0;//时间显示界面
			ShiJian_SheZhi_flag=2;
		}
		else if(key_num==6){//“电压测量”按键
			if(JieMian_DianYa){//从电压阈值设定界面转到电压测量界面
				ShangXian_VH_1=ShangXian_VH;
				XiaXian_VL_1=XiaXian_VL;
				IIC_EEPROM_XieDate(0x20,ShangXian_VH_1);
				Delay10ms();
				IIC_EEPROM_XieDate(0x21,XiaXian_VL_1);
				Delay10ms();
			}
			CaiDan=1;
			JieMian_DianYa=0;//电压测量界面
			ShangXia_flag=1;
		}
		else if(key_num==5){//“频率测量”按键
			CaiDan=2;
			JieMian_Hz=0;//频率测量显示界面-频率
		}
		else if(key_num==9){//“查询”按键
			CaiDan=3;
			JieMian_ChaXun=0;//事件查询界面-事件类型
			ShiJian_LeiXing=IIC_EEPROM_GetDate(0x14);
			Delay10ms();
		}
		else if(key_num==4){//功能
			switch(CaiDan){
				case 0:{//时钟
					if(JieMian_ShiJian==0){//从时间显示界面切换到时间设置界面
						Shi_temp=shi;
						Fen_temp=fen;
						Miao_temp=miao;
					}
					JieMian_ShiJian=1;//时间设置界面
					ShiJian_SheZhi_flag++;
					ShiJian_SheZhi_flag=ShiJian_SheZhi_flag%3;
					break;
				}
				case 1:{//电压
					JieMian_DianYa=1;//电压阈值设定界面
					ShangXia_flag++;
					ShangXia_flag=ShangXia_flag%2;
					break;
				}
				case 2:{//频率
					JieMian_Hz++;
					JieMian_Hz=JieMian_Hz%2;
					break;
				}
				case 3:{//查询
					JieMian_ChaXun++;
					JieMian_ChaXun=JieMian_ChaXun%2;
					if(JieMian_ChaXun){
						ShiJian_shi=IIC_EEPROM_GetDate(0x11);
						Delay10ms();
						ShiJian_fen=IIC_EEPROM_GetDate(0x12);
						Delay10ms();
						ShiJian_miao=IIC_EEPROM_GetDate(0x13);
						Delay10ms();
						ShiJian_LeiXing=IIC_EEPROM_GetDate(0x14);
						Delay10ms();
					}
					break;
				}
			}
		}
		else if(key_num==11){//加
			switch(CaiDan){
				case 0:{//时钟
					if(JieMian_ShiJian){//位于时间设置界面
						switch(ShiJian_SheZhi_flag){
							case 0:{//选择了时
								Shi_temp++;
								if(Shi_temp>=23){
									Shi_temp=23;
								}
								break;
							}
							case 1:{//选择了分
								Fen_temp++;
								if(Fen_temp>=59){
									Fen_temp=59;
								}
								break;
							}
							case 2:{//选择了秒
								Miao_temp++;
								if(Miao_temp>=59){
									Miao_temp=59;
								}
								break;
							}
						}
					}
					break;
				}
				case 1:{//电压
					if(JieMian_DianYa){//位于电压阈值设定界面
						if(ShangXia_flag){//设置下限
							XiaXian_VL=XiaXian_VL+5;
							if(XiaXian_VL>ShangXian_VH){//下限超过了上限
								XiaXian_VL=XiaXian_VL-5;
							}
						}
						else{//设置上限
							ShangXian_VH=ShangXian_VH+5;
							if(ShangXian_VH>=95){//最大是9500mV
								ShangXian_VH=95;
							}
						}
					}
					break;
				}
			}
		}
		else if(key_num==10){//减
			switch(CaiDan){
				case 0:{//时钟
					if(JieMian_ShiJian){//位于时间设置界面
						switch(ShiJian_SheZhi_flag){
							case 0:{//选择了时
								Shi_temp--;
								if(Shi_temp<=0){
									Shi_temp=0;
								}
								break;
							}
							case 1:{//选择了分
								Fen_temp--;
								if(Fen_temp<=0){
									Fen_temp=0;
								}
								break;
							}
							case 2:{//选择了秒
								Miao_temp--;
								if(Miao_temp<=0){
									Miao_temp=0;
								}
								break;
							}
						}
					}
					break;
				}
				case 1:{//电压
					if(JieMian_DianYa){//位于电压阈值设定界面
						if(ShangXia_flag){//设置下限
							XiaXian_VL=XiaXian_VL-5;
							if(XiaXian_VL<=0){//下限小于0
								XiaXian_VL=0;
							}
						}
						else{//设置上限
							ShangXian_VH=ShangXian_VH-5;
							if(ShangXian_VH=1000){
		Hz_time=0;
		Hz=((unsigned int)TH0<<8)|(unsigned int)TL0;
		ZhouQi=(1.0/Hz)*1000000;
		TH0=0;
		TL0=0;
	}
	
	//闪烁时间计算
	if((CaiDan==0&&JieMian_ShiJian==1)||(CaiDan==1&&JieMian_DianYa==1)){//位于时间设置界面或者位于电压阈值设定界面
		ShanShuo_time++;
	}
	else{
		ShanShuo_time=0;
	}
	if(ShanShuo_time>=2000){
		ShanShuo_time=0;
	}
	
	if(flag1>=1){
		flag1=0;
		SMG_XianShi();
	}
}

void SMG_XianShi(){//数码管显示
	switch(CaiDan){
		case 0:{//时钟
			if(JieMian_ShiJian){//位于时间设置界面
				if(ShanShuo_time>0&&ShanShuo_time<=1000){//亮
					SMG_Show(Shi_temp/10,Shi_temp%10,17,Fen_temp/10,Fen_temp%10,17,Miao_temp/10,Miao_temp%10);
				}
				else if(ShanShuo_time>1000&&ShanShuo_time<=2000){//灭
					switch(ShiJian_SheZhi_flag){
						case 0:{//选择了时
							SMG_Show(16,16,17,Fen_temp/10,Fen_temp%10,17,Miao_temp/10,Miao_temp%10);//时,灭
							break;
						}
						case 1:{//选择了分
							SMG_Show(Shi_temp/10,Shi_temp%10,17,16,16,17,Miao_temp/10,Miao_temp%10);//分,灭
							break;
						}
						case 2:{//选择了秒
							SMG_Show(Shi_temp/10,Shi_temp%10,17,Fen_temp/10,Fen_temp%10,17,16,16);//秒。灭
							break;
						}
					}
				}
			}
			else{//位于时间显示界面
				SMG_Show(shi/10,shi%10,17,fen/10,fen%10,17,miao/10,miao%10);
			}
			break;
		}
		case 1:{//电压
			if(JieMian_DianYa){//电压阈值设定界面
				if(ShanShuo_time>0&&ShanShuo_time<=1000){//亮
					SMG_Show(ShangXian_VH/10,ShangXian_VH%10,0,0,XiaXian_VL/10,XiaXian_VL%10,0,0);
				}
				else if(ShanShuo_time>1000&&ShanShuo_time<=2000){//灭
					if(ShangXia_flag){//设置下限显示
						SMG_Show(ShangXian_VH/10,ShangXian_VH%10,0,0,16,16,16,16);
					}
					else{//设置上限显示
						SMG_Show(16,16,16,16,XiaXian_VL/10,XiaXian_VL%10,0,0);
					}
				}
			}
			else{//电压测量界面
				if(DianYa>=1000){
					SMG_Show(17,1,17,16,DianYa/1000,(DianYa/100)%10,(DianYa/10)%10,DianYa%10);
				}
				else if(DianYa>=100){
					SMG_Show(17,1,17,16,16,DianYa/100,(DianYa/10)%10,DianYa%10);
				}
				else if(DianYa>=10){
					SMG_Show(17,1,17,16,16,16,DianYa/10,DianYa%10);
				}
				else if(DianYa>=0){
					SMG_Show(17,1,17,16,16,16,16,DianYa);
				}
			}
			break;
		}
		case 2:{//频率
			if(JieMian_Hz){//频率测量显示界面-周期
				SMG_Show(17,2,17,ZhouQi/10000,(ZhouQi/1000)%10,(ZhouQi/100)%10,(ZhouQi/10)%10,ZhouQi%10);
			}
			else{//频率测量显示界面-频率
				SMG_Show(17,2,17,Hz/10000,(Hz/1000)%10,(Hz/100)%10,(Hz/10)%10,Hz%10);
			}
			break;
		}
		case 3:{//查询
			if(JieMian_ChaXun){//事件查询界面-发生时间
				SMG_Show(ShiJian_shi/10,ShiJian_shi%10,17,ShiJian_fen/10,ShiJian_fen%10,17,ShiJian_miao/10,ShiJian_miao%10);
			}
			else{//事件查询界面-事件类型
				SMG_Show(16,16,16,16,16,16,0,ShiJian_LeiXing);
			}
			break;
		}
	}
}

void ShiJian_JiLu(){//事件记录
	IIC_EEPROM_XieDate(0x11,shi);
	Delay10ms();
	IIC_EEPROM_XieDate(0x12,fen);
	Delay10ms();
	IIC_EEPROM_XieDate(0x13,miao);
	Delay10ms();
	IIC_EEPROM_XieDate(0x15,ShangXian_VH_1);
	Delay10ms();
	IIC_EEPROM_XieDate(0x16,XiaXian_VL_1);
	Delay10ms();
}
Init.h
#ifndef __INIT_H_
#define __INIT_H_

#include 

void Init_BZ();

#endif
Init.c
#include "Init.h"

void Init_BZ(){
	P2=(P2&0x1f)|0xa0;
	P0=0x00;
	P2=(P2&0x1f)|0x80;
	P0=0xff;
}
SMG.h
#ifndef __SMG_H_
#define __SMG_H_

#include 

void SMG_Init();
void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8);

#endif
SMG.c
#include "SMG.h"

unsigned char code SMG_ZK[]={                       //????
//   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black  -     H    J    K    L    N    o   P    U     t    G    Q    r   M    y
    0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1

void SMG_Init(){
	P2=(P2&0x1f)|0xc0;
	P0=0xff;
	P2=(P2&0x1f)|0xe0;
	P0=0xff;
}

void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8){
	static unsigned char i=0;
	i++;
	i=i%8;
	P2=(P2&0x1f)|0xc0;
	switch(i){
		case 1:{
			P0=0x01;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n1];
			break;
		}
		case 2:{
			P0=0x02;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n2];
			break;
		}
		case 3:{
			P0=0x04;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n3];
			break;
		}
		case 4:{
			P0=0x08;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n4];
			break;
		}
		case 5:{
			P0=0x10;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n5];
			break;
		}
		case 6:{
			P0=0x20;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n6];
			break;
		}
		case 7:{
			P0=0x40;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n7];
			break;
		}
		case 0:{
			P0=0x80;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n8];
			break;
		}
	}
	P2=(P2&0x1f)|0x00;
}
DSQ.h
#ifndef __DSQ_H_
#define __DSQ_H_

#include 

void DSQ_1_Init();

#endif
DSQ.c
#include "DSQ.h"

void DSQ_1_Init(){
	AUXR |= 0x40;		//定时器时钟1T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0x20;		//设置定时初值
	TH1 = 0xD1;		//设置定时初值
	TF1 = 0;		//清除TF1标志
	TR1 = 1;		//定时器1开始计时
	EA=1;
	ET1=1;
}
YanShi.h
#ifndef __YANSHI_H_
#define __YANSHI_H_

#include 
#include "intrins.h"

void Delay10ms();

#endif
YanShi.c
#include "YanShi.h"

void Delay10ms()		//@12.000MHz
{
	unsigned char i, j;

	i = 117;
	j = 184;
	do
	{
		while (--j);
	} while (--i);
}
JZKey.h
#ifndef __JZKEY_H_
#define __JZKEY_H_

#include "YanShi.h"

void JZKey_Init();
unsigned char JZKey_GetKeynum();

#endif
JZKey.c
#include "JZKey.h"

void JZKey_Init(){
	P30=1;
	P31=1;
	P32=1;
	P33=1;
	P34=0;
	P35=0;
	P42=0;
	P44=0;
}

unsigned char JZKey_GetKeynum(){
	unsigned char key_num=0;
	if(P30==0){
		P30=0;
		P34=1;
		P35=1;
		P42=1;
		P44=1;
		if(P34==0){
			Delay10ms();
			if(P34==0){
				key_num=19;
			}
			while(P34==0);
		}
		else if(P35==0){
			Delay10ms();
			if(P35==0){
				key_num=15;
			}
			while(P35==0);
		}
		else if(P42==0){
			Delay10ms();
			if(P42==0){
				key_num=11;
			}
			while(P42==0);
		}
		else if(P44==0){
			Delay10ms();
			if(P44==0){
				key_num=7;
			}
			while(P44==0);
		}
	}
	else if(P31==0){
		P31=0;
		P34=1;
		P35=1;
		P42=1;
		P44=1;
		if(P34==0){
			Delay10ms();
			if(P34==0){
				key_num=18;
			}
			while(P34==0);
		}
		else if(P35==0){
			Delay10ms();
			if(P35==0){
				key_num=14;
			}
			while(P35==0);
		}
		else if(P42==0){
			Delay10ms();
			if(P42==0){
				key_num=10;
			}
			while(P42==0);
		}
		else if(P44==0){
			Delay10ms();
			if(P44==0){
				key_num=6;
			}
			while(P44==0);
		}
	}
	else if(P32==0){
		P32=0;
		P34=1;
		P35=1;
		P42=1;
		P44=1;
		if(P34==0){
			Delay10ms();
			if(P34==0){
				key_num=17;
			}
			while(P34==0);
		}
		else if(P35==0){
			Delay10ms();
			if(P35==0){
				key_num=13;
			}
			while(P35==0);
		}
		else if(P42==0){
			Delay10ms();
			if(P42==0){
				key_num=9;
			}
			while(P42==0);
		}
		else if(P44==0){
			Delay10ms();
			if(P44==0){
				key_num=5;
			}
			while(P44==0);
		}
	}
	else if(P33==0){
		P33=0;
		P34=1;
		P35=1;
		P42=1;
		P44=1;
		if(P34==0){
			Delay10ms();
			if(P34==0){
				key_num=16;
			}
			while(P34==0);
		}
		else if(P35==0){
			Delay10ms();
			if(P35==0){
				key_num=12;
			}
			while(P35==0);
		}
		else if(P42==0){
			Delay10ms();
			if(P42==0){
				key_num=8;
			}
			while(P42==0);
		}
		else if(P44==0){
			Delay10ms();
			if(P44==0){
				key_num=4;
			}
			while(P44==0);
		}
	}
	JZKey_Init();
	return key_num;
}
ds1302.h
#ifndef __DS1302_H
#define __DS1302_H

#include 
#include 

sbit SCK = P1^7;		
sbit SDA1 = P2^3;		
sbit RST = P1^3; 

extern unsigned char shi,fen,miao;

void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );

void DS1302_XieDate(unsigned char h,unsigned char m,unsigned char s);
void DS1302_GetDate();

#endif
ds1302.c
#include "ds1302.h"  	

unsigned char shi,fen,miao;

//写字节
void Write_Ds1302(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK = 0;
		SDA1 = temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

//向DS1302寄存器写入数据
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; 
}

//从DS1302寄存器读出数据
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(SDA1)
 		temp|=0x80;	
 		SCK=1;
	} 
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
	SCK=1;	_nop_();
	SDA1=0;	_nop_();
	SDA1=1;	_nop_();
	return (temp);			
}

void DS1302_XieDate(unsigned char h,unsigned char m,unsigned char s){
	h=(h/10)*16+(h%10);
	m=(m/10)*16+(m%10);
	s=(s/10)*16+(s%10);
	Write_Ds1302_Byte(0x8e,0x00);
	Write_Ds1302_Byte(0x84,h);
	Write_Ds1302_Byte(0x82,m);
	Write_Ds1302_Byte(0x80,s);
	Write_Ds1302_Byte(0x8e,0x80);
}

void DS1302_GetDate(){
	shi=Read_Ds1302_Byte(0x85);
	fen=Read_Ds1302_Byte(0x83);
	miao=Read_Ds1302_Byte(0x81);
	shi=(shi/16)*10+(shi%16);
	fen=(fen/16)*10+(fen%16);
	miao=(miao/16)*10+(miao%16);
}
iic.h
#ifndef _IIC_H
#define _IIC_H

#include 
#include "intrins.h"

sbit SDA = P2^1;
sbit SCL = P2^0;

void IIC_Start(void); 
void IIC_Stop(void);  
bit IIC_WaitAck(void);  
void IIC_SendAck(bit ackbit); 
void IIC_SendByte(unsigned char byt); 
unsigned char IIC_RecByte(void); 

unsigned char IIC_ADC_GetDate(unsigned char di_zhi);
void IIC_DAC_XieDate(unsigned char date);
void IIC_EEPROM_XieDate(unsigned char di_zhi,unsigned char date);
unsigned char IIC_EEPROM_GetDate(unsigned char di_zhi);

#endif
iic.c
#include "iic.h"

#define DELAY_TIME 5

//I2C总线内部延时函数
void IIC_Delay(unsigned char i)
{
    do{_nop_();}
    while(i--);        
}

//I2C总线启动信号
void IIC_Start(void)
{
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

//I2C总线停止信号
void IIC_Stop(void)
{
    SDA = 0;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//发送应答或非应答信号
void IIC_SendAck(bit ackbit)
{
    SCL = 0;
    SDA = ackbit;  					
    IIC_Delay(DELAY_TIME);
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SCL = 0; 
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//等待应答
bit IIC_WaitAck(void)
{
    bit ackbit;
	
    SCL  = 1;
    IIC_Delay(DELAY_TIME);
    ackbit = SDA;
    SCL = 0;
    IIC_Delay(DELAY_TIME);
    return ackbit;
}

//I2C总线发送一个字节数据
void IIC_SendByte(unsigned char byt)
{
    unsigned char i;

    for(i=0; i<8; i++)
    {
        SCL  = 0;
        IIC_Delay(DELAY_TIME);
        if(byt & 0x80) SDA  = 1;
        else SDA  = 0;
        IIC_Delay(DELAY_TIME);
        SCL = 1;
        byt <<= 1;
        IIC_Delay(DELAY_TIME);
    }
    SCL  = 0;  
}

//I2C总线接收一个字节数据
unsigned char IIC_RecByte(void)
{
    unsigned char i, da;
    for(i=0; i<8; i++)
    {   
    	SCL = 1;
	IIC_Delay(DELAY_TIME);
	da <<= 1;
	if(SDA) da |= 1;
	SCL = 0;
	IIC_Delay(DELAY_TIME);
    }
    return da;    
}

unsigned char IIC_ADC_GetDate(unsigned char di_zhi){
	unsigned char date;
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(di_zhi);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	EA=0;
	IIC_SendByte(0x91);
	//EA=1;
	IIC_WaitAck();
	//EA=0;
	date=IIC_RecByte();
	EA=1;
	IIC_SendAck(1);
	IIC_WaitAck();
	IIC_Stop();
	return date;
}

void IIC_DAC_XieDate(unsigned char date){
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x40);
	IIC_WaitAck();
	IIC_SendByte(date);
	IIC_WaitAck();
	IIC_Stop();
}

void IIC_EEPROM_XieDate(unsigned char di_zhi,unsigned char date){
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(di_zhi);
	IIC_WaitAck();
	IIC_SendByte(date);
	IIC_WaitAck();
	IIC_Stop();
}

unsigned char IIC_EEPROM_GetDate(unsigned char di_zhi){
	unsigned char date;
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(di_zhi);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0xa1);
	IIC_WaitAck();
	date=IIC_RecByte();
	IIC_SendAck(1);
	IIC_WaitAck();
	IIC_Stop();
	return date;
}
LN555.h
#ifndef __LN555_H_
#define __LN555_H_

#include 

void LN555_Init();

#endif
LN555.c
#include "LN555.h"

void LN555_Init(){
	TMOD=(TMOD&0xf0)|0x05;
	TL0=0;
	TH0=0;
	TR0=1;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1330931.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-12
下一篇 2022-06-12

发表评论

登录后才能评论

评论列表(0条)

保存