第十一届蓝桥杯国赛程序题

 main.c

#include "stc15f2k60s2.h"
#include "smg.h"
#include "ds1302.h"
#include "onewire.h"
#include "iic.h"

unsigned char Time0[3] = {0x16,0x59,0x50};					//用于保存时间初值
unsigned char Time1[3] = {0,0,0};										//用于保存时间数据
unsigned int dat_temp;															//用于保存温度数据
unsigned int dat_dianya;														//用于保存光敏电阻电压
unsigned char para_time = 17;												//用于保存时间参数
unsigned char para_temp = 25;												//用于保存温度参数
unsigned char para_led = 4;													//用于保存led参数
unsigned char flag_S4;															//用于界面切换
unsigned char flag_S5_1;														//用于数据切换
unsigned char flag_S5_2;														//用于参数切换
unsigned char flag_light;														//用于判断光敏电阻状态
unsigned int count_liang;														//用于判断处于亮状态的时间
unsigned int count_an;															//用于判断处于暗状态的时间

sbit C1 = P4^4;
sbit C2 = P4^2;
sbit R1 = P3^2;
sbit R2 = P3^3;

sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;
sbit L5 = P0^4;
sbit L6 = P0^5;
sbit L7 = P0^6;
sbit L8 = P0^7;
//系统初始化函数
void Init_Sys()
{
	Select(4);
	P0 = 0xff;
	Select(5);
	P0 = 0x00;
	Select(0);
}
//ds1302初始化函数
void Init_ds1302()
{
	Write_Ds1302_Byte(0x8e,0x00);
	Write_Ds1302_Byte(0x84,Time0[0]);
	Write_Ds1302_Byte(0x82,Time0[1]);
	Write_Ds1302_Byte(0x80,Time0[2]);
	Write_Ds1302_Byte(0x8e,0x80);	
}
//时间读取函数
void Read_Time()
{
	unsigned char t;
//	t = Read_Ds1302_Byte(0x85);
//	Time1[0] = (t >> 4)*10 + (t & 0x0f);
//	t = Read_Ds1302_Byte(0x83);
//	Time1[1] = (t >> 4)*10 + (t & 0x0f);
//	t = Read_Ds1302_Byte(0x81);
//	Time1[2] = (t >> 4)*10 + (t & 0x0f);
	t = Read_Ds1302_Byte(0x85);
	Time1[0] = (t / 16) * 10 + t % 16;
	t = Read_Ds1302_Byte(0x83);
	Time1[1] = (t / 16) * 10 + t % 16;
	t = Read_Ds1302_Byte(0x81);
	Time1[2] = (t / 16) * 10 + t % 16;
}
//时间显示函数
void Display_Time()
{
	DisplaySMG_Bit(0,SMG_duanma[Time1[0] / 10]);	Delay(100);
	DisplaySMG_Bit(1,SMG_duanma[Time1[0] % 10]);	Delay(100);	
	DisplaySMG_Bit(2,0xbf);	Delay(100);
	DisplaySMG_Bit(3,SMG_duanma[Time1[1] / 10]);	Delay(100);
	DisplaySMG_Bit(4,SMG_duanma[Time1[1] % 10]);	Delay(100);	
	DisplaySMG_Bit(5,0xbf);	Delay(100);
	DisplaySMG_Bit(6,SMG_duanma[Time1[2] / 10]);	Delay(100);
	DisplaySMG_Bit(7,SMG_duanma[Time1[2] % 10]);	Delay(100);	
	Display_all(0xff);
}
//温度读取函数
void Read_temperature()
{
	dat_temp = Read_Temp() * 100;
}
//温度显示函数
void Display_temp()
{
	DisplaySMG_Bit(0,0xc6);	Delay(100);
	
	DisplaySMG_Bit(1,0xff);	Delay(100);
	DisplaySMG_Bit(2,0xff);	Delay(100);
	DisplaySMG_Bit(3,0xff);	Delay(100);
	DisplaySMG_Bit(4,0xff);	Delay(100);
	
	DisplaySMG_Bit(5,SMG_duanma[dat_temp / 1000]);	Delay(100);
	DisplaySMG_Bit(6,SMG_duanma[dat_temp / 100 % 10] & 0x7f);	Delay(100);
	DisplaySMG_Bit(7,SMG_duanma[dat_temp / 10 % 10]);	Delay(100);
	Display_all(0xff);
}
//光敏电阻电压读取函数
void Read_dianya()
{
	dat_dianya = get_dianya() * 50.0 / 255 * 10;
}
//亮暗显示函数
void Display_light()
{
	DisplaySMG_Bit(0,0x86);	Delay(100);
	
	DisplaySMG_Bit(1,0xff);	Delay(100);
	
	DisplaySMG_Bit(2,SMG_duanma[dat_dianya / 100] & 0x7f);	Delay(100);
	DisplaySMG_Bit(3,SMG_duanma[dat_dianya / 10 % 10]);	Delay(100);
	DisplaySMG_Bit(4,SMG_duanma[dat_dianya % 10]);	Delay(100);
	
	DisplaySMG_Bit(5,0xff);	Delay(100);
	DisplaySMG_Bit(6,0xff);	Delay(100);
	if(dat_dianya >= 150)
	{
		DisplaySMG_Bit(7,SMG_duanma[0]);	Delay(100);
	}
	else
	{
		DisplaySMG_Bit(7,SMG_duanma[1]);	Delay(100);		
	}
	Display_all(0xff);	
}
//时间参数显示函数
void Display_para_time()
{
	DisplaySMG_Bit(0,0x8c);	Delay(100);
	DisplaySMG_Bit(1,SMG_duanma[1]);	Delay(100);
	
	DisplaySMG_Bit(2,0xff);	Delay(100);
	DisplaySMG_Bit(3,0xff);	Delay(100);
	DisplaySMG_Bit(4,0xff);	Delay(100);
	DisplaySMG_Bit(5,0xff);	Delay(100);
	
	DisplaySMG_Bit(6,SMG_duanma[para_time / 10]);	Delay(100);
	DisplaySMG_Bit(7,SMG_duanma[para_time % 10]);	Delay(100);
	Display_all(0xff);	
}
//温度参数显示函数
void Display_para_temp()
{
	DisplaySMG_Bit(0,0x8c);	Delay(100);
	DisplaySMG_Bit(1,SMG_duanma[2]);	Delay(100);
	
	DisplaySMG_Bit(2,0xff);	Delay(100);
	DisplaySMG_Bit(3,0xff);	Delay(100);
	DisplaySMG_Bit(4,0xff);	Delay(100);
	DisplaySMG_Bit(5,0xff);	Delay(100);
	
	DisplaySMG_Bit(6,SMG_duanma[para_temp / 10]);	Delay(100);
	DisplaySMG_Bit(7,SMG_duanma[para_temp % 10]);	Delay(100);
	Display_all(0xff);	
}
//led参数显示函数
void Display_para_led()
{
	DisplaySMG_Bit(0,0x8c);	Delay(100);
	DisplaySMG_Bit(1,SMG_duanma[3]);	Delay(100);
	
	DisplaySMG_Bit(2,0xff);	Delay(100);
	DisplaySMG_Bit(3,0xff);	Delay(100);
	DisplaySMG_Bit(4,0xff);	Delay(100);
	DisplaySMG_Bit(5,0xff);	Delay(100);	
	DisplaySMG_Bit(6,0xff);	Delay(100);
	
	DisplaySMG_Bit(7,SMG_duanma[para_led]);	Delay(100);
	Display_all(0xff);	
}
//显示总函数
void Displays()
{
	if(flag_S4 == 0)
	{
		if(flag_S5_1 == 0)
		{
			Display_Time();
		}
		if(flag_S5_1 == 1)
		{
			Display_temp();
		}
		if(flag_S5_1 == 2)
		{
			Display_light();
		}
	}
	if(flag_S4 == 1)
	{
		if(flag_S5_2 == 0)
		{
			Display_para_time();
		}
		if(flag_S5_2 == 1)
		{
			Display_para_temp();
		}
		if(flag_S5_2 == 2)
		{
			Display_para_led();
		}	
	}
}
//显示增强函数
void Display()
{
	unsigned char t = 12;
	while(t--)
		Displays();
}
void Scan_key()
{
	R1 = R2 = 1;
	C1 = 0;C2 = 1;
	//S4界面切换
	if(R2 == 0)
	{
		Delay(100);
		if(R2 == 0)
		{
			while(R2 == 0)
				Display();
			flag_S4++;
			if(flag_S4 == 2)
				flag_S4 = 0;
			flag_S5_1 = 0;
			flag_S5_2 = 0;
		}
	}
	//S5切换按键
	if(R1 == 0)
	{
		Delay(100);
		if(R1 == 0)
		{
			while(R1 == 0)
				Display();
			if(flag_S4 == 0)
			{
				flag_S5_1++;
				if(flag_S5_1 == 3)
					flag_S5_1 = 0;
			}
			if(flag_S4 == 1)
			{
				flag_S5_2++;
				if(flag_S5_2 == 3)
					flag_S5_2 = 0;				
			}
		}
	}

	R1 = R2 = 1;
	C1 = 1;C2 = 0;
	//S8参数减
	if(R2 == 0)
	{
		Delay(100);
		if(R2 == 0)
		{
			while(R2 == 0)
				Display();
			if(flag_S4 == 1 && flag_S5_2 == 0)
			{
				para_time = para_time - 1;
				if(para_time == -1)
					para_time = 0;
			}
			if(flag_S4 == 1 && flag_S5_2 == 1)
			{
				para_temp = para_temp - 1;
				if(para_temp == -1)
					para_temp = 0;
			}
			if(flag_S4 == 1 && flag_S5_2 == 2)
			{
				para_led = para_led - 1;
				if(para_led == 3)
					para_led = 4;
			}
		}
	}
	//S9参数加
	if(R1 == 0)
	{
		Delay(100);
		if(R1 == 0)
		{
			while(R1 == 0)
				Display();
			if(flag_S4 == 1 && flag_S5_2 == 0)
			{
				para_time = para_time + 1;
				if(para_time == 24)
					para_time = 23;
			}
			if(flag_S4 == 1 && flag_S5_2 == 1)
			{
				para_temp = para_temp + 1;
				if(para_temp == 100)
					para_temp = 99;
			}
			if(flag_S4 == 1 && flag_S5_2 == 2)
			{
				para_led = para_led + 1;
				if(para_led == 9)
					para_led = 8;
			}
		}
	}
}
//光敏电阻亮暗状态判断函数
void working()
{
	//暗
	if(dat_dianya <= 250)
	{
		flag_light = 1;
	}
	else
	{
		flag_light = 0;
	}
}
void Timer2Init(void)		//10毫秒@12.000MHz
{
	AUXR &= 0xFB;		//定时器时钟12T模式
	T2L = 0xF0;		//设置定时初值
	T2H = 0xD8;		//设置定时初值
	AUXR |= 0x10;		//定时器2开始计时
	
	IE2 |= 0x04;
	EA = 1;
}

void Uart_Timer2() interrupt 12
{
	if(flag_light == 1)
	{
		count_an++;
	}
	if(flag_light == 0)
	{
		count_liang++;
	}
}

//led工作函数
void led()
{
	//L1工作
	if(para_time > 8)
	{
		if(Time1[0] >= 8 && Time1[0] <= para_time)
		{
			Select(4);
			L1 = 0;
			Select(0);
		}
		else
		{
			Select(4);
			L1 = 1;
			Select(0);			
		}
	}
	else if(para_time <= 8)
	{
		if(Time1[0] <= 8 && Time1[0] >= para_time)
		{
			Select(4);
			L1 = 0;
			Select(0);
		}
		else
		{
			Select(4);
			L1 = 1;
			Select(0);			
		}		
	}
	//L2工作
	if(dat_temp < para_temp*100)
	{
		Select(4);
		L2 = 0;
		Select(0);		
	}
	else
	{
		Select(4);
		L2 = 1;
		Select(0);		
	}
	//L3工作
	if(count_liang >= 300)
	{
		Select(4);
		L3 = 1;
		Select(0);
		if(flag_light == 0)
			count_an = 0;
	}
	else if(count_an >= 300)
	{
		Select(4);
		L3 = 0;
		Select(0);
		if(flag_light == 1)		
			count_liang = 0;
	}
	if(flag_light == 1)
	{
		switch(para_led)
		{
			case 4:
				Select(4);
				L4 = 0;L5 = 1;L6 = 1;L7 = 1;L8 = 1;
				Select(0);
			break;
			case 5:
				Select(4);
				L4 = 1;L5 = 0;L6 = 1;L7 = 1;L8 = 1;
				Select(0);
			break;
			case 6:
				Select(4);
				L4 = 1;L5 = 1;L6 = 0;L7 = 1;L8 = 1;
				Select(0);
			break;
			case 7:
				Select(4);
				L4 = 1;L5 = 1;L6 = 1;L7 = 0;L8 = 1;
				Select(0);
			break;
			case 8:
				Select(4);
				L4 = 1;L5 = 1;L6 = 1;L7 = 1;L8 = 0;
				Select(0);
			break;
		}
	}
	else
	{
		Select(4);
		L4 = 1;L5 = 1;L6 = 1;L7 = 1;L8 = 1;
		Select(0);		
	}
}
void main()
{
	Init_Sys();
	Init_ds1302();
	Timer2Init();
	while(1)
	{
		Read_Time();
		Read_temperature();
		Read_dianya();
		Display();
		Scan_key();
		led();
		working();
	}
}

smg.h

#ifndef __SMG_H
#define __SMG_H

code unsigned char SMG_duanma[] = 
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
};

void Delay(unsigned int t);
void Select(unsigned char channel);
void DisplaySMG_Bit(unsigned char pos,unsigned char dat);
void Display_all(unsigned char dat);

#endif

smg.c

#include "stc15f2k60s2.h"

void Delay(unsigned int t)
{
	while(t--);
}

void Select(unsigned char channel)
{
	switch(channel)
	{
		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;
		case 0: P2 = (P2 & 0x1f) | 0x00; break;		
	}
}

void DisplaySMG_Bit(unsigned char pos,unsigned char dat)
{
	Select(6);
	P0 = 0x01 << pos;
	Select(7);
	P0 = dat;
	Select(0);
}

void Display_all(unsigned char dat)
{
	Select(6);
	P0 = 0xff;
	Select(7);
	P0 = dat;
	Select(0);
}

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

ds1302.c

/*	# 	DS1302代码片段说明
	1. 	本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
	2. 	参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
		中对单片机时钟频率的要求,进行代码调试和修改。
*/								
#include "stc15f2k60s2.h"
#include "intrins.h"

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

//
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);			
}

onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

float Read_Temp();

#endif

onewire.c

/*	# 	单总线代码片段说明
	1. 	本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
	2. 	参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
		中对单片机时钟频率的要求,进行代码调试和修改。
*/

//
#include "stc15f2k60s2.h"

sbit DQ = P1^4;

void Delay_OneWire(unsigned int t)  
{
	unsigned char i;
	while(t--){
		for(i=0;i<12;i++);
	}
}

//
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);
}

//
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;
}

//
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;
}

float Read_Temp()
{
	float temp;
	unsigned char low,high;
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);	
	
	low = Read_DS18B20();
	high = Read_DS18B20();
	
	temp = (high << 8 | low) * 0.0625;
	
	return temp;
}

iic.h

#ifndef __IIC_H
#define __IIC_H

unsigned char get_dianya(void);

#endif

iic.c

/*	#   I2C代码片段说明
	1. 	本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
	2. 	参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
		中对单片机时钟频率的要求,进行代码调试和修改。
*/
#include "stc15f2k60s2.h"
#include "intrins.h"

sbit sda = P2^1;
sbit scl = P2^0;

#define DELAY_TIME	5

//
static void I2C_Delay(unsigned char n)
{
    do
    {
        _nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();		
    }
    while(n--);      	
}

//
void I2CStart(void)
{
    sda = 1;
    scl = 1;
	I2C_Delay(DELAY_TIME);
    sda = 0;
	I2C_Delay(DELAY_TIME);
    scl = 0;    
}

//
void I2CStop(void)
{
    sda = 0;
    scl = 1;
	I2C_Delay(DELAY_TIME);
    sda = 1;
	I2C_Delay(DELAY_TIME);
}

//
void I2CSendByte(unsigned char byt)
{
    unsigned char i;
	
    for(i=0; i<8; i++){
        scl = 0;
		I2C_Delay(DELAY_TIME);
        if(byt & 0x80){
            sda = 1;
        }
        else{
            sda = 0;
        }
		I2C_Delay(DELAY_TIME);
        scl = 1;
        byt <<= 1;
		I2C_Delay(DELAY_TIME);
    }
	
    scl = 0;  
}

//
unsigned char I2CReceiveByte(void)
{
	unsigned char da;
	unsigned char i;
	for(i=0;i<8;i++){   
		scl = 1;
		I2C_Delay(DELAY_TIME);
		da <<= 1;
		if(sda) 
			da |= 0x01;
		scl = 0;
		I2C_Delay(DELAY_TIME);
	}
	return da;    
}

//
unsigned char I2CWaitAck(void)
{
	unsigned char ackbit;
	
    scl = 1;
	I2C_Delay(DELAY_TIME);
    ackbit = sda; 
    scl = 0;
	I2C_Delay(DELAY_TIME);
	
	return ackbit;
}

//
void I2CSendAck(unsigned char ackbit)
{
    scl = 0;
    sda = ackbit; 
	I2C_Delay(DELAY_TIME);
    scl = 1;
	I2C_Delay(DELAY_TIME);
    scl = 0; 
	sda = 1;
	I2C_Delay(DELAY_TIME);
}

unsigned char get_dianya(void)
{
	unsigned char temp;
	
	I2CStart();
	I2CSendByte(0x90);
	I2CWaitAck();
	
	I2CSendByte(0x01);
	I2CWaitAck();	
	
	I2CStart();
	I2CSendByte(0x91);
	I2CWaitAck();	
	
	temp = I2CReceiveByte();
	I2CSendAck(1);
	I2CStop();
	
	return temp;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Zz耳机

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

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

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

打赏作者

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

抵扣说明:

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

余额充值