文章来源:https://www.toymoban.com/news/detail-752319.html
程序源代码:
/****************************************
第十四届蓝桥杯省赛源代码
*********************************************/
#include <STC15F2K60S2.H>
#include <intrins.h>
#include "onewire.h"
#include "ds1302.h"
#include "iic.h"
#define uchar unsigned char
#define uint unsigned int
uchar code type1[]={ 0xC0,
0xF9,
0xA4,
0xB0,
0x99,
0x92,
0x82,
0xF8,
0x80,
0x90,0xff,0xc6,0x89,0x8e,0x8c,0x86,0xbf,0x88};
//11C 12H 13F 14P 15E 16- 17AA
uchar code type2[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar num1,num2,num3,num4,num5,num6,num7,num8;
bit f_2ms;
uchar t_2ms;
uchar GN;
uchar bdata U6=0xff;
sbit led1=U6^0;
sbit led2=U6^1;
sbit led3=U6^2;
sbit led4=U6^3;
sbit led5=U6^4;
sbit led6=U6^5;
sbit led7=U6^6;
sbit led8=U6^7;
uint temp,temp_1=0;
bit flag;
uint temp_wen=0,temp_wen1=0,temp_pin=0,temp_adc;
char hour,minute,second;
char hour_1,minute_1,second_1;
char model=0,model_hui=0;
uint wen_max=0,wen_pin=0,shi_max=0,shi_pin=0;
uchar trig_count=0;//触发次数
bit trig_device=0;
uchar wen_p=30;//温度参数
char f_err=0;//频率错误
uchar g_light=0;//光敏读数
bit light_trig=0,limt_light=0;
uint wen_t2ms=0;ds1302_t2ms=0,adc_t2ms=0,trig_3S=0;
bit limt_caiji=0;
uchar pick_wen=0,pick_shi=0;
uchar wen_type[99]=0,shi_type[99]=0;
uchar wen_i=0,shi_i=0;
//长按按键操作
uint S9_t2ms=0;
bit F_S9=0;
//清除数组所有数据
uchar S9_i=0;
//指示灯
uint L4_t2ms=0;
bit L4_flag=0;
void Delay10ms() //@12.000MHz
{
unsigned char i, j;
i = 117;
j = 184;
do
{
while (--j);
} while (--i);
}
void smg_display(char n1,char n2,char n3,char n4,char n5,char n6,char n7,char n8)
{
char i;
P0=0xff;
P2=(P2&0x1f)|0xc0;
P0=type2[i];
P2=(P2&0x1f);
switch(i)
{
case 0:P2=(P2&0x1f)|0xe0; P0=type1[n1]; P2=(P2&0x1f);break;
case 1:P2=(P2&0x1f)|0xe0; P0=type1[n2]; P2=(P2&0x1f);break;
case 2:P2=(P2&0x1f)|0xe0; P0=type1[n3]; P2=(P2&0x1f);break;
case 3:P2=(P2&0x1f)|0xe0; P0=type1[n4]; P2=(P2&0x1f);break;
case 4:P2=(P2&0x1f)|0xe0; P0=type1[n5]; P2=(P2&0x1f);break;
case 5:P2=(P2&0x1f)|0xe0; P0=type1[n6]; P2=(P2&0x1f);break;
case 6:P2=(P2&0x1f)|0xe0;
if(light_trig==0)
{
if(model==1)
{
if(model_hui==0 || model_hui==1)
P0=type1[n7]&0x7f;
if(model_hui==2)
P0=type1[n7];
if(trig_count==0)
P0=type1[n7];
}
if(model==0 || model==2)
P0=type1[n7];
}
else
P0=type1[n7];
P2=(P2&0x1f);break;
case 7:P2=(P2&0x1f)|0xe0; P0=type1[n8]; P2=(P2&0x1f);break;
}
if(++i==8)
i=0;
}
//*************************led显示********************//
void led_display()
{
if(model==0) //L1
{
led1=0;led2=1;led3=1;
P2=(P2&0x1f)|0x80;
P0=U6;
P2=(P2&0x1f);
}
if(model==1) //L2
{
led1=1;led2=0;led3=1;
P2=(P2&0x1f)|0x80;
P0=U6;
P2=(P2&0x1f);
}
if(model==2)//L3
{
led1=1;led2=1;led3=0;
P2=(P2&0x1f)|0x80;
P0=U6;
P2=(P2&0x1f);
}
//****************************
if(pick_wen > wen_p) //L4
{
if(L4_flag==0)
{
led4=0;
P2=(P2&0x1f)|0x80;
P0=U6;
P2=(P2&0x1f);
}
if(L4_flag==1)
{
led4=1;
P2=(P2&0x1f)|0x80;
P0=U6;
P2=(P2&0x1f);
}
}
else
{
led4=1;
P2=(P2&0x1f)|0x80;
P0=U6;
P2=(P2&0x1f);
}
//****************************
if(f_err==1) //L5
{
led5=0;
P2=(P2&0x1f)|0x80;
P0=U6;
P2=(P2&0x1f);
}
if(f_err==0)
{
led5=1;
P2=(P2&0x1f)|0x80;
P0=U6;
P2=(P2&0x1f);
}
//*******************************
if(trig_count>1) //L6
{
if((wen_type[trig_count] > wen_type[trig_count-1]) && (shi_type[trig_count] > shi_type[trig_count-1]))
{
led6=0;
P2=(P2&0x1f)|0x80;
P0=U6;
P2=(P2&0x1f);
}
else
{
led6=1;
P2=(P2&0x1f)|0x80;
P0=U6;
P2=(P2&0x1f);
}
}
}//led_display
/*************************pcf8591**********************/
void initial_adc(uchar addr)
{
IIC_Start( );
IIC_SendByte( 0x90+0 );
IIC_WaitAck( );
IIC_SendByte( addr );
IIC_WaitAck( );
IIC_Stop( );
}
uint du_adc()
{
uint D;
IIC_Start( );
IIC_SendByte( 0x90+1 );
IIC_WaitAck( );
D=IIC_RecByte( );
IIC_SendAck(1);
IIC_Stop( );
// D=(D*500.0)/255;
return D;
}
/*************************1302**********************/
void xie_ds1302()
{
Write_Ds1302_Byte( 0x8e,0x00 );
Write_Ds1302_Byte( 0x84,0x00 );
Write_Ds1302_Byte( 0x82,0x00 );
Write_Ds1302_Byte( 0x80,0x00 );
}
void du_ds1302()
{
hour=Read_Ds1302_Byte( 0x85 ); hour=(hour>>4)*10+(hour&0x0f);
minute=Read_Ds1302_Byte( 0x83 ); minute=(minute>>4)*10+(minute&0x0f);
second=Read_Ds1302_Byte( 0x81 ); second=(second>>4)*10+(second&0x0f);
}
/*************************矩阵按键**********************/
uchar key_scan()
{
uchar L,H;
P3=0xdf;P44=0;P42=0;H=(~P3)&0x0f;
if(H==0) return 0;
Delay10ms();
P3=0xcf;P44=0;P42=0;H=(~P3)&0x0f;
if(H==0) return 0;
P3=0xf0;P44=1;P42=1;L=(~P3)&0x20;
if(P44==0) L|=0x80;
if(P42==0) L|=0x40;
return (L+H);
}
void key_change()
{
uchar D;
D=key_scan();
switch(D)
{
case 0x88:GN=4;break;
case 0x84:GN=5;break;
case 0x48:GN=8;break;
case 0x44:GN=9;break;
case 0:GN=0;break;
}
}
/*************************定时器0初始化**********************/
void time0()
{
TMOD=(TMOD&0xf0)|0x05; //定时器0设置成计数模式
TH0=TL0=0; //赋初值
}
/*************************定时器1**********************/
void time1_int1()
{
EA=1;
ET1=1;
TR1=1;
TMOD=(TMOD&0x0f)|0x10;
TH1=(65536-2000)/256;
TL1=(65536-2000)%256;
}
/*************************外设初始化**********************/
void initial_all()
{
P2=(P2&0x1f)|0xa0;
P0=0x00;
P2=(P2&0x1f);
P2=(P2&0x1f)|0x80;
P0=0xff;
P2=(P2&0x1f);
P2=(P2&0x1f)|0xc0;
P0=0xff;
P2=(P2&0x1f);
P2=(P2&0x1f)|0xe0;
P0=0xff;
P2=(P2&0x1f);
}
/*************************主函数**********************/
void main()
{
initial_all();//外设初始化
time1_int1(); //定时器1初始化
time0(); //定时器0初始化
initial_18b20(); //18B20初始化
xie_ds1302(); //1302初始化
initial_adc(1); //ADC初始化
g_light=du_adc(); //ADC采集
//temp_wen=rd_temperature();
// Delay10ms();
while(1)
{
if(f_2ms==1)
{
f_2ms=0;
key_change();
led_display();
//1302读取
if(++ds1302_t2ms==50)
{
ds1302_t2ms=0;
du_ds1302();
}
//温度采集
temp_wen1=rd_temperature();
//ADC采集
if(++adc_t2ms==10)
{
adc_t2ms=0;
g_light=du_adc();
}
if(g_light < 10)
{
if(limt_light==1)
{
light_trig=1;
limt_light=0;
}
}
else
{
limt_light=1;
}
/************************非触发状态***************************/
if(light_trig==0)
{
trig_device=0; //清空触发一次
limt_caiji=0; //清空采集一次
if(GN==4)
{
model++;
if(model==3) model=0;
while(key_scan());
}
switch(model)
{
case 0:
model_hui=0;
num1=hour/10;num2=hour%10;num3=16;num4=minute/10;num5=minute%10;
num6=16;num7=second/10;num8=second%10;
break;
case 1:
if(GN==5)
{
model_hui++;
if(model_hui==3) model_hui=0;
while(key_scan());
}
switch(model_hui)
{
case 0://温度回显
if(trig_count==0)
{
num1=11;num2=num3=num4=num5=num6=num7=num8=10;
}
else
{
num1=11;num2=10;num3=wen_max/10;num4=wen_max%10;
num5=16,num6=wen_pin/100;num7=wen_pin%100/10;num8=wen_pin%10;
}
break;
case 1://湿度回显
if(trig_count==0)
{
num1=12;num2=num3=num4=num5=num6=num7=num8=10;
}
else
{
num1=12;num2=10;num3=shi_max/10;num4=shi_max%10;
num5=16,num6=shi_pin/100;num7=shi_pin%100/10;num8=shi_pin%10;
}
break;
case 2://时间回显
if(GN==9) //长按按键
{
F_S9=1;
while(key_scan());
F_S9=0;
S9_t2ms=0;
}
if(trig_count==0)
{
num1=13;num2=num3=num4=num5=num6=num7=num8=10;
}
else
{
num1=13;num2=trig_count/10;num3=trig_count%10;
num4=hour_1/10;num5=hour_1%10;num6=16;num7=minute_1/10;num8=minute_1%10;
}
break;
}
break; //case 1
case 2:
if(GN==8)
{
if(wen_p++ == 99) wen_p=99;
while(key_scan());
}
if(GN==9)
{
if(wen_p-- == 0) wen_p=0;
while(key_scan());
}
num1=14;num2=num3=num4=num5=num6=10;
num7=wen_p/10;num8=wen_p%10;
break;
}//switch
}//light_trig==0
/************************触发状态***************************/
if(light_trig==1)
{
temp_wen=temp_wen1; //采集温度
temp_1=temp; //采集湿度
//触发次数计算一次
if(trig_device==0)
{
if(trig_count++ == 99)
trig_count=99;
trig_device=1;
}
//采集一次
if(limt_caiji==0)
{
//温度最大值
if(temp_wen > wen_max)
{
wen_max=temp_wen;
}
//温度平均值计算
wen_type[trig_count]=temp_wen;
wen_pin=0;
for(wen_i=0;wen_i<trig_count;)
{
wen_i++;
wen_pin=wen_pin+wen_type[wen_i];
}
wen_pin=(((float)wen_pin*10)/trig_count);
//湿度最大值
if(temp_1 > shi_max)
{
shi_max=temp_1;
}
//湿度平均值计算
shi_type[trig_count]=temp_1;
shi_pin=0;
for(shi_i=0;shi_i<trig_count;)
{
shi_i++;
shi_pin=shi_pin+shi_type[shi_i];
}
shi_pin=(((float)shi_pin*10)/trig_count);
/********将采集到的数据返回显示**********/
pick_wen=temp_wen;
pick_shi=temp_1;
limt_caiji=1;
hour_1=hour; minute_1=minute;
}
num1=15;num2=num3=10;num4=pick_wen/10;num5=pick_wen%10;
num6=16;
if(f_err==0)
{
num7=pick_shi/10;num8=pick_shi%10;
}
if(f_err==1)
{
num7=17;num8=17;
}
}
}//f_2ms
}
}
void cl_time1_int1() interrupt 3 //定时器1
{
TH1=(65536-2000)/256;
TL1=(65536-2000)%256;
smg_display(num1,num2,num3,num4,num5,num6,num7,num8);
f_2ms=1;
if(++t_2ms==250)
{
t_2ms=0;
flag=~flag;
if(flag==0)
{
TH0=TL0=0; //清零
TR0=1;
}
else
{
TR0=0;
temp=((TH0<<8)+TL0)*2;
temp=((2*temp)+50);
temp=temp/45.0;
if(temp<10)
{
f_err=1;
temp=10;
}
else if(temp>90)
{
f_err=1;
temp=90;
}
else
f_err=0;
}
}
/*********采集后计时3S**********/
if(light_trig==1)
{
//计时3S
if(++trig_3S==1500)
{
trig_3S=0;
light_trig=0;
}
}
/*********S9长按处理**********/
if(F_S9==1)
{
if(++S9_t2ms>1000)
{
S9_t2ms=0;
trig_count=0;
wen_max=0;
shi_max=0;
for(S9_i=0;S9_i<99;S9_i++)
{
wen_type[S9_i]=0;
shi_type[S9_i]=0;
}
}
}
/*********L4闪烁处理**********/
if(temp_wen > wen_p)
{
if(++L4_t2ms==50)
{
L4_t2ms=0;
L4_flag=~L4_flag;
}
}
else
L4_flag=1;
}//中断
需要源代码一键三联,私聊我哦文章来源地址https://www.toymoban.com/news/detail-752319.html
到了这里,关于第十四届蓝桥杯(电子类)单片机开发——赛题源代码分享的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!