蓝桥杯单片机15模板总和每个模块综合

main

#include <stc15f2k60s2.h>
#include "intrins.h"
#include "absacc.h"
#include "ds18b20.h"
#include "IIC.h"
#include "KEY.h"
#include "Ding_Shi_Qi.h"
#include <ds1302.h>
#include "ding_yi.h"//全部定义都在这
                                                                                                                                             
//初始化
void cls_buzz()
{
		P2 =((P2&0X1F)|0XA0);
		P0 = 0X00;
		P2 &= 0X1F;
}
void cls_led()
{
	  P2 =((P2&0X1F)|0X80);
		P0 = 0Xff;
		P2 &= 0X1F;
}
//开初始显示
void display()
{
	static unsigned char dspcom = 0;
	P2 = (P2 & 0x1f) | 0xe0;
	P0 = 0xff;
	P2 = 0x1f;
	
	P2 = (P2 & 0x1f) | 0xc0;
	P0 = 1<<dspcom;
	P2 = 0x1f;
	
	P2 = (P2 & 0x1f) | 0xe0;
	P0 = tab[dspbuf[dspcom]];
	P2 = 0x1f;
	
	if(++dspcom == 8)dspcom = 0;
}
void Delay5ms()		//@12.000MHz
{
	unsigned char i, j;

	i = 59;
	j = 90;
	do
	{
		while (--j);
	} while (--i);
}

//========================界面切换函数=====================================//
void display_smg()
{
  switch(s4_state)
   {
	case 0:
	  display_wen();break;  //温度显示
	case 1:
	  DAC_guang();break;    //光敏显示
	case 2:
	  DAC_dian();break;     //DA输出显示
	case 3:
		display_ping();break;//频率显示
	case 4:
		set_t=0;              //放最后
//	 display_zho();//周期显示
//	 chao_sheng_bo();//超声波
//	 can_shu();//参数界面
	dispaly_shijian();//时间界面

		break;
   }
}
主函数======================//
void main()
{
	Timer0Init();//频率计数专用
//	Timer0_1Init();//超声波专用
	Timer1Init();
	Timer2Init();
	cls_buzz();
	cls_led();
	DS1302_Init();
	DS1302_Config();
	while(1)
	{
		display_smg(); //界面函数
		key_duli();  //独立按键
//		key_duli_1();  //独立按键
//		key_JU_Zheng();//矩阵按键
//=========================温度读取//
	if(temper_flag)
	{
		temper_flag = 0;
		temperature = wen_du_zhen_shu();
		temperature_1=wen_du_xiao_shu();
	}
//=========================温度读取//
	if(led_flag)
	{
		led_flag=0;
		display_LED();
	}

	}
}
//主函数/=======================//

//========================NE555计数专用=====================================//
void time0_service() interrupt 1
{
	count_f++;
}

//========================超声波专用=====================================//

//void isr_timer_0(void)  interrupt 1  //超声波专用
//{
//    TH0 = (65536-2000)/256;
//    TL0 = (65536-2000)%256;  //定时器重载 

//	if(++intr_1 == 200)
//	{
//        s_flag = 1;
//        intr_1 = 0;
//   }
//}

void time1_service() interrupt 3
{
	
	count_1++;
	if(count_1>=180)
	{
		count_1=0;
		pin_lu_f=count_f;
		count_f=0;
		if(s4_state==1)
		{
				get_guang();  //光敏//采集对界面对其他界面有影响
		}
		if(s4_state==2)
		{
				get_dian();   //电位器//采集对界面对其他界面有影响
		}
		    get_z();      //周期
	 }
	
}
bit led_temp=0;
void Timer2_Int()	 interrupt 12          
{
	
static unsigned	int count_2;
	
	led_count++;
	if(led_count == 100)
	{
		led_flag = 1;
		led_count = 0;
	}
//=============温度转换
	if(++intr == 50)
	{
		temper_flag = 1;
		intr = 0;
	}
	display();                 //界面显示放这里最好
//=============温度转换
//================
//按键长按实现功能	
//================
	if(P30==0)
	{
		count_2++;
		
		if(count_2>=1000&&led_temp==0)//2秒
		{
			count_2=0;led_temp=1;
			if(led_worke==1)
			{
				led_worke=0;
			}
			else
			{
				led_worke=1;
			}
		}
	}
	else 
	{
		count_2=0;
		led_temp=0;
	}	
//=========独立按键状态机按键
	count++;
 if(count==10)
 {
  count=0;
  key_flag=1;
 }
//=========独立按键状态机按键
//=========矩阵按键状态机按键
	count_4++;
 if(count_4==10)
 {
  count_4=0;
  key_flag_2=1;
 }
//=========矩阵按键状态机按键
 
}
//========================温度读取函数=====================================//
void display_wen()
{
	
	dspbuf[0]=25;
	dspbuf[1]=20;
	dspbuf[2]=20;
	dspbuf[3]=20;
	dspbuf[4] = temperature / 10;
	dspbuf[5] = temperature % 10+10;//+10点
	dspbuf[6] = temperature_1 / 10;
	dspbuf[7] = temperature_1 % 10;
}

//========================获取光敏值=====================================//
void get_guang()
{
	guang_1=guang();
	guang_1=guang();
}
void DAC_guang()
{
	dspbuf[0]=1;
	dspbuf[1]=20;
	dspbuf[2]=20;
	dspbuf[3]=20;
	dspbuf[4]=20;
	dspbuf[5] = (guang_1 /100)+10;
	dspbuf[6] = guang_1  / 10 %10;
	dspbuf[7] = guang_1  % 10;
}
//========================获取电位器值====================================//
void get_dian()
{
	dian_2=dian();
	dian_2=dian();
}
void DAC_dian()
{
	dspbuf[0]=2;
	dspbuf[1]=20;
	dspbuf[2]=20;
	dspbuf[3]=20;
	dspbuf[4]=20;
	dspbuf[5] = (dian_2 /100)+10;
	dspbuf[6] = dian_2  / 10 %10;
	dspbuf[7] = dian_2  % 10;
}
//========================超声波显示函数====================================//
void chao_sheng_bo()
{
	chao_sheng(); 
	dspbuf[0]=5;
	dspbuf[1]=20;
	dspbuf[2]=20;
	dspbuf[3]=20;
	dspbuf[4]=20;
	dspbuf[5]=20;
	dspbuf[6] = distance%1000/10;//最大80-90cm=0.0017
	dspbuf[7] = distance%10;
	
}
//========================参数设置函数====================================//
void can_shu()
{
	dspbuf[0]=6;
	dspbuf[1]=20;
	dspbuf[2]=20;
	dspbuf[3]=20;
	dspbuf[4]=20;
	dspbuf[5]=20;
	dspbuf[6] = canshu/10;
	dspbuf[7] = canshu%10;
}
//========================温度储存设置函数====================================//
void dispaly_shijian()
{
	Get_Time();
	dspbuf[0]=nowTime[2]/16;
	dspbuf[1]=nowTime[2]%16;
	dspbuf[2]=22;
	dspbuf[3]=nowTime[1]/16;
	dspbuf[4]=nowTime[1]%16;
	dspbuf[5]=22;
	dspbuf[6]=nowTime[0]/16;
	dspbuf[7]=nowTime[0]%16;
}

//========================频率显示函数=========================================//
void display_ping()
{
	  dspbuf[0] = 3;
		dspbuf[1] = 20;
		if(pin_lu_f>100000)
		{
		dspbuf[2] = pin_lu_f%1000000/100000;
		}
		else
		{
		dspbuf[2] = 20;
		}
		if(pin_lu_f > 10000)        
		{
		dspbuf[3] = pin_lu_f%100000/10000;
		}
		else                       
		{
		dspbuf[3] = 20;
		}
		
		if(pin_lu_f > 1000)        
		{
		dspbuf[4] = pin_lu_f%10000/1000;
		}
		else                      
		{
    dspbuf[4] = 20;
		}
		
		if(pin_lu_f >= 100)       
		{
		dspbuf[5] = pin_lu_f%1000/100;
		}
		else                     
		{
		dspbuf[5] = 20;
		}
		
    if(pin_lu_f >= 10 )        
		{			
		dspbuf[6] = pin_lu_f%100/10;
		}
		else
		{
		dspbuf[6] = 20;	
		}
		dspbuf[7] = pin_lu_f % 10;
}

//========================周期获取显示=========================================//
void get_z()
{
	zho_qi=1000000/pin_lu_f;
}
void display_zho()
{
	dspbuf[0] = 4;
		if(zho_qi>=1000000)
		{
		dspbuf[1] = zho_qi%10000000/1000000;	
		}
		else
		{
		dspbuf[1] = 20;
		}	
		if(zho_qi>=100000)
		{
		dspbuf[2] = zho_qi%1000000/100000;
		}
		else
		{
		dspbuf[2] = 20;	
		}
		if(zho_qi>=10000)
		{
		dspbuf[3] = zho_qi%100000/10000;
		}
		else
		{
		dspbuf[3] = 20;	
		}
		if(zho_qi>=1000)
		{
		dspbuf[4] = zho_qi%10000/1000;
		}
		else
		{
		dspbuf[4] = 20;	
		}
		if(zho_qi>=100)
		{
		dspbuf[5] = zho_qi%1000/100;
		}
		else
		{
		dspbuf[5] = 20;	
		}
		if(zho_qi>=10)
		{
		dspbuf[6] = zho_qi%100/10;
		}
		else
		{
		dspbuf[6] = 20;	
		}
		dspbuf[7] = zho_qi%10;
}

//========================状态机独立按键扫描=====================================//
void key_duli()
{
if(key_flag==1)
 {
  key_flag=0;
  key_value=KEY_BTN();
  switch(key_value)
  {
   case 4://按键S4
	 {
		 s4_state++;
	  if(s4_state == 1)
	  {
		 set_t=1;
	  }
	  if(s4_state == 5)//界面数量
	  {
	   s4_state=0;
	  }
		}
		 break;
   case 5:
		
		{
			temp_state++;
			if(temp_state>=3)
			{
				temp_state=0;
			}
			
			
		}
		 break;
   case 6:
		 {
			  write_EEPROM(0X01,pin_lu_f);
			  Delay5ms();
			  eeprom_ping_liu=read_EEPROM(0X01);
        Delay5ms();
		 }
		 break;
   case 7:
		{
			nowTime[2]++;
			if(nowTime[2]>=24)
			{
				nowTime[2]=0;
			}
     DS1302_Config();	
		}
		 break;
  }
 }
 }
//==================================================完美长按键版
void Delay10ms()		//@12.000MHz
{
	unsigned char i, j;

	i = 117;
	j = 184;
	do
	{
		while (--j);
	} while (--i);
}

//sbit S7=P3^0;
//void key_duli_1()
//{
//	if(S7==0)
//	{
//		if(S7==0)
//		Delay10ms();
//		{
//		  write_EEPROM(0X00,temperature);
//			Delay5ms();
//		  eeprom_temperature=read_EEPROM(0X00);
//		}
		while(!S7);
//	}
//	
//}
//===============================状态机矩阵按键扫描==============================//
 void key_JU_Zheng()
{
if(key_flag_2==1)
 {
  key_flag_2=0;
  key_value_2=KEY_KBD();
  switch(key_value_2)
  {
   case 4://按键S4
	 {
		 s4_state++;
	  if(s4_state == 1)
	  {
		 set_t=1;
	  }
	  if(s4_state == 5)//界面数量
	  {
	   s4_state=0;
	  }
		}
		 break;
   case 5://S5按键
		{
			
		}
		 break;
   case 6://S6按键
		 {
			  write_EEPROM(0X01,pin_lu_f);
			  Delay5ms();
			  eeprom_ping_liu=read_EEPROM(0X01);
        Delay5ms();
		}
		 break;
   case 7://S7按键
		{
			write_EEPROM(0x00,temperature);
			Delay5ms();
		  eeprom_temperature=read_EEPROM(0X00);
      Delay5ms();
		}
		 break;
	 case 8://S8按键
		{
			 s8_state++;//参数倒计时停止键
			if(s8_state>=1)
			{
				s8_state=1;//1是启动
			}
		}
		break;
	 case 9://S9按键
		 {
			 s8_state=s8_state+2;;//参数倒计时启动键
			if(s8_state>1)
			{
				s8_state=0;//0是关闭
			}
    }
		 break;
		case 10://S9按键
		 {
			 s8_state=0;
      canshu=11;//返回参数11停止
     }
		 break;
  }
 }
 }
//====================================================LED操作=============================================//
void display_LED()
{
//数据指示灯操作
	if(led_worke==0)
	{
	if(temperature>eeprom_temperature)//L1//正确
	{
		  led=(led&0xfe)|0x00;
			choose_573(0);//先锁存
			P0=led;
			choose_573(4);//后位选
	}
	else if(temperature<=eeprom_temperature)//L1//正确
	{
		  led=(led&0xfe)|0x01;
			choose_573(0);//先锁存
			P0=led;
			choose_573(4);//后位选
	}
	
	if(pin_lu_f>eeprom_ping_liu)//L7正确
	{
		  led=(led&0xbf)|0x00;
			choose_573(0);//先锁存
			P0=led;
			choose_573(4);//后位选
	}
	else if(pin_lu_f<=eeprom_ping_liu)//L7正确
	{
		  led=(led&0xbf)|0xc0;
			choose_573(0);//先锁存
			P0=led;
			choose_573(4);//后位选
	}
}
	else
	{
		led=0xff;
		choose_573(0);
		P0=led;
		choose_573(4);
	}
}

//===============================选通函数LED专用附属函数==============================//
void choose_573(int i)
{
	switch(i)
	{
		case(0):P2=(P2&0x1f)|0x00;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 DS1302_Config()
{
	char i;
	Write_Ds1302(0x8e,0x00);
	for(i=0;i<7;i++)
	{
		Write_Ds1302(w_add[i],nowTime[i]);
	}
	Write_Ds1302(0x8e,0x80);
}

//===============================超声波附属函数==============================//
 void chao_sheng()
{
	        /** 200毫秒更新一次数据 */
		if(s_flag)
        {
            s_flag = 0;
            /** 关闭定时器0中断:计算超声波发送到返回的时间 */

            send_wave();  //发送方波信号
            TR1 = 1;  //启动计时
			while((RX == 1) && (TF1 == 0));  //等待收到脉冲
			TR1 = 0;  //关闭计时

			//发生溢出
			if(TF1 == 1)
			{
				TF1 = 0;
				distance = 9999;  //超量程
			}
			else
			{
				/**  计算时间  */
				t = TH1;
				t <<= 8;
				t |= TL1;
				distance = (t*0.0017);  //计算距离					
			};
			TL1 = 0;
			TH1 = 0;
        }
		/** 数据处理                      */
}
//TX引脚发送40KHz方波信号驱动超声波发送探头
//使用软件延时注意RC振荡器频率
void send_wave(void)
{
	unsigned char i = 8;  //发送8个脉冲
	do
	{
		TX = 1;
		somenop;somenop;somenop;somenop;somenop;somenop;
		somenop;somenop;somenop;somenop;		
		TX = 0;
		somenop;somenop;somenop;somenop;somenop;somenop;
		somenop;somenop;somenop;somenop;		
	}
	while(i--);
}

ds18b20.c

#include "reg52.h"

sbit DQ = P1^4;  //单总线接口

//单总线延时函数
void Delay_OneWire(unsigned int t)  //STC89C52RC
{
	char  i;
	while(t--)
		for(i = 0;i<12;i++);
}

//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(5);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(5);
}

//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
			dat |= 0x80;
		}	    
		Delay_OneWire(5);
	}
	return dat;
}

//DS18B20设备初始化
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(12);
  	DQ = 0;
  	Delay_OneWire(80);
  	DQ = 1;
  	Delay_OneWire(10); 
    initflag = DQ;     
  	Delay_OneWire(5);
  
  	return initflag;
}

unsigned char wen_du_zhen_shu(void)
{
	unsigned char low,high;
	char temp;
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	Delay_OneWire(200);
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	low = Read_DS18B20();
	high = Read_DS18B20();
	temp = high <<4;
	temp |=(low >> 4);
	
	return temp;
}
//小数处理
unsigned char wen_du_xiao_shu()
{
	unsigned char temp,low,high;
	init_ds18b20();
	Write_DS18B20(0xCC);
	Write_DS18B20(0x44);
	Delay_OneWire(200);
	
	init_ds18b20();
	Write_DS18B20(0xCC);
	Write_DS18B20(0xBE);
	low=Read_DS18B20();
	temp=low&0x0F;
	return temp;
}

ds18b20.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

unsigned char wen_du_zhen_shu(void);  // 整数读取
unsigned char wen_du_xiao_shu();      // 小数读取
#endif

IIC.C

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

#include "reg52.h"
#include "intrins.h"

#define DELAY_TIME 5

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

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

void IIC_Delay(unsigned char i)
{
    do{_nop_();}
    while(i--);        
}
//总线启动条件
void IIC_Start(void)
{
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

//总线停止条件
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;  					// 0:应答,1:非应答
    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;    
}

//===========================================================
//光敏电阻读取RB1
//===========================================================
int guang()              
{
	int temp;
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x01);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	temp=IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	temp=temp*1.96+0.5;
	return temp;
}
//===========================================================
电位器读取RB2
//===========================================================
int dian()               
{
	int temp;
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x03);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	temp=IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	temp=temp*1.96+0.5;
	return temp;
}
//===========================================================
//通过 PCF8591 完成 DAC 模拟电压输出功能   输出到DA口
//===========================================================
void dac_pcf8591(unsigned char dat)
{
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x40);  //DAC输出模式
	IIC_WaitAck();
	
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
}
//===========================================================
写EEPROM的
//===========================================================
void write_EEPROM(unsigned int add,unsigned int dat)  
{
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
}
//===========================================================
//读EEPROM的
//===========================================================
int read_EEPROM(unsigned int add)           
{
  unsigned int temp;
	IIC_Start();
	IIC_SendByte(0xA0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0xA1);
	IIC_WaitAck();
	temp=IIC_RecByte();
	return temp;
}

IIC.H

#ifndef _IIC_H
#define _IIC_H

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); 
int guang();
int dian();


void dac_pcf8591(unsigned char dat);
void write_EEPROM(int add,int dat);
int read_EEPROM(int add);
#endif

DS1302.C

#include <intrins.h>
#include "ds1302.h"
#include <STC15F2K60S2.H>
sbit SCK = P1^7;		
sbit SDA = P2^3;		
sbit RST = P1^3;   // DS1302?′??
extern unsigned char nowTime[7];//??·?ê±13:30:00
code unsigned char r_add[] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
/**
  * @brief  Send a byte to the DS1302.
  * @param  temp: value to write to DS1302.
  * @retval None
  */
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;
	}
}  
 
/**
  * @brief  Writes to the selected DS1302 register.
  * @param  address: address of the selected register.
  * @param  dat: value to write to the selected register.
  * @retval None
  */
void Write_Ds1302( unsigned char address,unsigned char dat )     
{
 	RST=0;
	_nop_();
 	SCK=0;
	_nop_();
 	RST=1;	
   	_nop_();  
 	Write_Ds1302_Byte(address);	
 	Write_Ds1302_Byte(dat);		
 	RST=0; 
}

/**
  * @brief  Reads the selected DS1302 Register.
  * @param  address: address of the selected register.
  * @retval DS1302 Register Value.
  */
unsigned char Read_Ds1302 ( unsigned char address )
{
 	unsigned char i,temp=0x00;
 	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;
	_nop_();
	return (temp);			
}
void DS1302_Init(void)
{
	Write_Ds1302(REG_PROTECT,0x00); 
	Write_Ds1302(REG_SECOND,0);
	Write_Ds1302(REG_MINUTES,0);
	Write_Ds1302(REG_HOUR,0);
	Write_Ds1302(REG_PROTECT,0x80);	
}
void Get_Time(void)
{
	unsigned char i;
	EA = 0;
	for(i=0;i<7;i++){
		nowTime[i] = Read_Ds1302(r_add[i]);
	}
	EA = 1;
}

DS1302.H

#ifndef __DS1302_H
#define __DS1302_H
#define REG_SECOND 0x80
#define REG_MINUTES 0x82
#define REG_HOUR    0x84
#define REG_DATE    0x86
#define REG_MONTH   0x88
#define REG_WEEK    0x8A
#define REG_YEAR    0x8C
#define REG_PROTECT 0x8E
void Write_Ds1302_Byte(unsigned  char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );


void DS1302_Init(void);
void Get_Time(void);

#endif

Ding_shi_qi.c

#include <stc15f2k60s2.h>
#include "Ding_Shi_Qi.h"
///===================================================中断函数
void Timer0Init(void)		//100微秒NE555计数的专用
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD = 0x04;		//设置定时器模式
	TL0 = 0xff;		//设置定时初始值
	TH0 = 0xff;		//设置定时初始值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0=1;
	EA=1;
}
void Timer0_1Init(void) //超声波专用
{
	  TMOD |= 0x11;  //配置定时器工作模式
    TH0 = (65536-2000)/256;
    TL0 = (65536-2000)%256;  
    TH1 = 0;
    TL1 = 0;  
  
    EA = 1;
    ET0 = 1;  //打开定时器0中断
    TR0 = 1;  //启动定时器   
}

void Timer1Init(void)		//5毫秒@11.0592MHz
{
	AUXR |= 0x40;		//定时器时钟1T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0x00;		//设置定时初始值
	TH1 = 0x28;		//设置定时初始值
	TF1 = 0;		//清除TF1标志
	TR1 = 1;		//定时器1开始计时
	ET1=1;
	EA=1;
}
void Timer2Init(void)		//1毫秒@11.0592MHz
{
	AUXR |= 0x04;		//定时器时钟1T模式
	T2L = 0xCD;		//设置定时初始值
	T2H = 0xD4;		//设置定时初始值
	AUXR |= 0x10;		//定时器2开始计时
	
	EA=1;
	IE2 |=0X04;
}

Ding_shi_qi.h

#ifndef __Ding_Shi_Qi_H
#define __Ding_Shi_Qi_H
void Timer0Init(void);		//100微秒NE555计数的专用
void Timer0_1Init(void);  //超声波专用
void Timer1Init(void);		//5毫秒@11.0592MHz
void Timer2Init(void);		//1毫秒@11.0592MHz
#endif

KEY.C

#include <STC15F2K60S2.H>
#include "key.h"
//独立按键状态机
//***************************************************************************
//***************************************************************************
#define key_state_0 0
#define key_state_1 1
#define key_state_2 2
unsigned char KEY_BTN()
{
  static char key_state=0;
 unsigned char key_press,key_value=0;
 key_press=P3&0X0f;
 switch(key_state)
 {
case key_state_0: if(key_press!=0x0f) key_state=1; break;
case key_state_1: 
   if(key_press!=0x0f) 
   {
      if(key_press==0x0e) key_value=7;//key_press而不是P3
      if(key_press==0x0d) key_value=6;
      if(key_press==0x0b) key_value=5;
      if(key_press==0x07) key_value=4;
              key_state=2;
   }
   else 
    key_state=0;
    break;
case key_state_2: 
     if(key_press==0x0f)
    key_state=0;
   break;
}
  return key_value;
}
//按键状态扫描
//-----------------------------------------------//状态机扫描矩阵按键
#define key_state_0  0
#define key_state_1  1
#define key_state_2  2
unsigned char KEY_KBD(void)
{
    static char key_state = 0;
    unsigned char key_return=0, key_press;
    unsigned char key1,key2;
    P44=1;P42=1;P35=1;P34=1;P33=0;P32=0;P31=0;P30=0;
    if(P44==0)  key1=0x70;
    else if(P42==0)  key1=0xb0;
    else if(P35==0)  key1=0xd0;
    else if(P34==0)  key1=0xe0;
    else key1=0xf0;
    P44=0;P42=0;P35=0;P34=0;P33=1;P32=1;P31=1;P30=1;
    if(P33==0)  key2=0x07;
    else if(P32==0)  key2=0x0b;
    else if(P31==0)  key2=0x0d;
    else if(P30==0)  key2=0x0e;
    else key2=0x0f;
    key_press =key1|key2;

    switch (key_state)
    {
        case key_state_0:
            if (key_press!=0xff) key_state = key_state_1;
            break;

        case key_state_1:
            if (key_press !=0xff)
            {
                if(key_press==0x77) key_return = 4;
                if(key_press==0x7b) key_return = 5;
                if(key_press==0x7d) key_return = 6;
                if(key_press==0x7e) key_return = 7;

                if(key_press==0xb7) key_return = 8;
                if(key_press==0xbb) key_return = 9;
                if(key_press==0xbd) key_return = 10;
                if(key_press==0xbe) key_return = 11;

                if(key_press==0xd7) key_return = 12;
                if(key_press==0xdb) key_return = 13;
                if(key_press==0xdd) key_return = 14;
                if(key_press==0xde) key_return = 15;

                if(key_press==0xe7) key_return = 16;
                if(key_press==0xeb) key_return = 17;
                if(key_press==0xed) key_return = 18;
                if(key_press==0xee) key_return = 19;
                key_state = key_state_2;
            }
            else
                key_state = key_state_0;
            break;
        case key_state_2:
            if (key_press==0xff) key_state = key_state_0;
            break;
    }
    return key_return;
}

KEY.h

#ifndef _KEY_H
#define _KEY_H


unsigned char KEY_BTN();    //独立按键
unsigned char KEY_KBD(void);//矩阵按键


#endif

ding_yi.c

#include <stc15f2k60s2.h>

//==================================================================================定义区//

ding_yi.h

#ifndef _DING_YI_H
#define _DING_YI_H
//                      0    1     2   3    4    5    6    7    8     9    //数字0~9无点,索引值0~9
unsigned char tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
//	                    0.   1.    2.  3.   4.   5.   6.   7.   8.    9.   //数字0~9有点,索引值(0~9)+10
                       0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,  
																																	  0xff,// "关"   索引值20
																																	  0x7F,// "."    索引值21
																																	  0xbf,// "-"    索引值22
																																	  0x88,// "A"    索引值23
																																	  0x83,// "B"    索引值24
																																	  0xC6,// "C"    索引值25
};
unsigned char dspbuf[8] = {10,10,10,10,10,10,10,10};

code unsigned char w_add[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char nowTime[7]={0x23,0x59,0x23,};//0x23是时23,0x59是分59,0x23是秒23
//unsigned char nowTime_1[25]={0x00};//0x23是时23,0x59是分59,0x23是秒23
//================================================初始化区//
void display_wen();
void display();
void DAC_guang();
void DAC_dian();
void get_guang();
void get_dian();
void display_ping();
void get_z();
void display_zho();
void key_duli_1();
void key_duli();
void key_JU_Zheng();
void chao_sheng_bo();

void Timer0Init(void);
void send_wave(void);
void chao_sheng();


void display_LED();
void choose_573(int i);

void can_shu();
void DS1302_Config();

void dispaly_shijian();
void dispaly_canshu_temp();
//=====================================================================================初始化区//
//==================================================定义区//
unsigned int intr = 0,temperature,temperature_1;//温度初值设定
unsigned char temper_flag = 1;                   //温度标志位
int zho_qi;                                      //周期值
int guang_1=0;                                   //光敏值
unsigned int dian_2=0;                           //电位值

unsigned int pin_lu_f=0;                         //频率初值
 char s4_state=0;                                 //按键初值=界面标志位
bit s5_state=0;                                  //按键取反
bit set_t=0;                                     //界面标志位
char s8_state=0;

int count_f=0;                                   //NE555计数
int count_1;                                     //频率的
int led_count=0;                                 //led计数
int led_flag;                                    //led标志
unsigned int canshu=11;                          //参数值
int led=0xff;
char led_worke;
unsigned char temp_state=0;

//独立按键定义
 char key_flag;                ///
 char key_value;               ///
 char key_return;              ///
static char count=0;           ///
//独立按键定义
//矩阵按键定义
 char key_flag_2;              ///
 char key_value_2;             ///
 char key_return_2;            ///
static char count_4=0;         ///
//矩阵按键定义


//============================================eeprom取值定义
unsigned int eeprom_ping_liu=0;
unsigned int eeprom_dian_2;
unsigned int eeprom_temperature;


//================================================超声波定义
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();\
                 _nop_();_nop_();_nop_();_nop_(); _nop_();}
sbit TX = P1^0;  //发射引脚
sbit RX = P1^1;  //接收引脚
unsigned int distance;
unsigned int intr_1 = 0;
bit s_flag;
unsigned int t = 0;


								 
								 
								 
								 
								 
								 
								 
								 
								 
#endif

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lin18876076218

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

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

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

打赏作者

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

抵扣说明:

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

余额充值