单片机比赛准备08-蓝桥杯-第六届初赛模拟题(温度采集和控制装置)

简介: 单片机比赛准备08-蓝桥杯-第六届初赛模拟题(温度采集和控制装置)

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

main.c:


#include <stc15f2k60s2.h>#include <intrins.h>#include <ds18b20.h>//-------------------------------------------#define uchar unsigned char #define uint unsigned int //------------------------------------------------ucharseg[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0xbf,
0xff};
ucharyi=16, er=16,san=16,si=16,wu=16,liu=16,qi=16,ba=16;
ucharkey_set;
ucharset=0;
ucharclr=0;
uchartmax=35,tmin=25;
uinttim;
uintt;
ucharset_0=16,set_1=16,set_2=16,set_3=16;
ucharset_cnt=0;
ucharqujian;
ucharwendu;
ucharled=0;
//--------------------------------------------------------voidDelayms(uintms);
voidDelay1ms();
voidallinit();
voiddisplay(ucharyi,er,san,si,wu,liu,qi,ba);
voidkeyscan16();
voidTimer0Init(void);
voidled_control();
voidTimer1Init(void);
voiddisp_xuan();
//--------------------------------------------------------voidmain()
{
allinit();
Timer0Init();
Timer1Init();
while(1)
    {
disp_xuan();
display(yi,er,san,si,wu,liu,qi,ba);
    }
}
voidallinit()
{
P2=0xa0;P0=0x00;
P2=0x80;P0=0xff;
P2=0xe0;P0=0xff;
P2=0xc0;P0=0xff;
}
voiddisplay(ucharyi,er,san,si,wu,liu,qi,ba)
{
P2=0xe0;P0=0xff;
P2=0xe0;//段选P0=seg[yi];
P2=0xc0;//位选P0=0x01;
Delay1ms();
Delay1ms();
Delay1ms();
P2=0xe0;P0=0xff;
P2=0xe0;//段选P0=seg[er];
P2=0xc0;//位选P0=0x02;
Delay1ms();
P2=0xe0;P0=0xff;
P2=0xe0;//段选P0=seg[san];
P2=0xc0;//位选P0=0x04;
Delay1ms();
P2=0xe0;P0=0xff;
P2=0xe0;//段选P0=seg[si];
P2=0xc0;//位选P0=0x08;
Delay1ms();
P2=0xe0;P0=0xff;
P2=0xe0;//段选P0=seg[wu];
P2=0xc0;//位选P0=0x10;
Delay1ms();
P2=0xe0;P0=0xff;
P2=0xe0;//段选P0=seg[liu];
P2=0xc0;//位选P0=0x20;
Delay1ms();
P2=0xe0;P0=0xff;
P2=0xe0;//段选P0=seg[qi];
P2=0xc0;//位选P0=0x40;
Delay1ms();
P2=0xe0;P0=0xff;
P2=0xe0;//段选P0=seg[ba];
P2=0xc0;//位选P0=0x80;
Delay1ms();
P2=0xe0;P0=0xff;
P2=0xc0;P0=0xff;
}
voidsave()
{
if(set==0)
    {
if(set_1!=16)
        {
tmax=set_0*10+set_1;
tmin=set_2*10+set_3;
        }
set_cnt=0;
    }
}
voidkeyscan16()
{ 
uchartemp;
P44=0;P42=1;P3=0X7F;
temp=P3;
temp=temp&0X0F;//11111101&00001111=00001101if(temp!=0x0f)
    {
Delayms(5);
temp=P3;
temp=temp&0X0F;
if(temp!=0x0f)
        {
temp=P3;
switch(temp)
            {
case0x7e: key_set=0;set_cnt++;break;//11111110P37----p30case0x7d: key_set=3;set_cnt++;break;//11111101case0x7b: key_set=6;set_cnt++;break;
case0x77: key_set=9;set_cnt++;break;   
            }
while(temp!=0x0f)
            {
temp=P3;
temp=temp&0X0F;
            }
        }
    }
P44=1;P42=0;P3=0XBF;//1011111temp=P3;
temp=temp&0X0F;//11111101&00001111=00001101if(temp!=0x0f)
    {
Delayms(5);
temp=P3;
temp=temp&0X0F;
if(temp!=0x0f)
        {
temp=P3;
switch(temp)
            {
case0xBe:key_set=1;set_cnt++;break;//11111110P37----p30case0xBd:key_set=4;set_cnt++;break;//11111101case0xBb:key_set=7;set_cnt++;break;
case0xB7:set=~set;ET1=~ET1;save();break;   
            }
while(temp!=0x0f)
            {
temp=P3;
temp=temp&0X0F;
            }
        }
    }
P3=0XDF;P44=1;P42=1;//11011111temp=P3;
temp=temp&0X0F;//11111101&00001111=00001101if(temp!=0x0f)
    {
Delayms(5);
temp=P3;
temp=temp&0X0F;
if(temp!=0x0f)
        {
temp=P3;
switch(temp)
            {
case0xDe:key_set=2;set_cnt++;break;//11111110P37----p30case0xDd:key_set=5;set_cnt++;break;//11111101case0xDb:key_set=8;set_cnt++;break;
case0xD7:clr=1;break;  
            }
while(temp!=0x0f)
            {
temp=P3;
temp=temp&0X0F;
            }
        }
    }
}
voidshuru()
{
if(clr==1)
    {
clr=0;
set_cnt=0;
set_0=16;
set_1=16;
set_2=16;
set_3=16;
    }
switch(set_cnt)
    {
case1:set_0=key_set;break;
case2:set_1=key_set;break;
case3:set_2=key_set;break;
case4:set_3=key_set;break; 
    }
if(set_cnt==4&&(set_0*10+set_1)<(set_2*10+set_3))
    {
yi=15;
er=16;
san=16;
si=16;
wu=16;
liu=15;
qi=16;
ba=16;
P2=0X80;P0=0XFD;
    }
}
voiddisp_xuan()
{
if(set==0)
    {
wendu=temget();
led_control();
set_0=16;
set_1=16;
set_2=16;
set_3=16;
yi=15;
er=qujian;
san=15;
si=16;
wu=16;
liu=16;
qi=wendu/10;
ba=wendu%10;
    }
else    {
shuru();
yi=15;
er=set_0;
san=set_1;
si=16;
wu=16;
liu=15;
qi=set_2;
ba=set_3;
    }
}
voidled_control()
{
if(wendu<tmin)
    {
t=800;
qujian=0;
P2=0xa0;P0=0x00;
    }
elseif(wendu>=tmin&&wendu<=tmax)
    {
t=600;
qujian=1;
P2=0xa0;P0=0x00;
    }
elseif(wendu>tmax)
    {
t=200;
qujian=2;
P2=0xa0;P0=0x10;
    }
}
voidTimer0Init(void)       //100微秒@11.0592MHz{
AUXR|=0x80;       //定时器时钟1T模式TMOD&=0xF0;       //设置定时器模式TL0=0xAE;     //设置定时初值TH0=0xFB;     //设置定时初值TF0=0;        //清除TF0标志TR0=1;    //定时器0开始计时EA=1;
ET0=1;
}
voidtimer0() interrupt1{
keyscan16();
}
voidTimer1Init(void)       //1毫秒@11.0592MHz{
AUXR|=0x40;       //定时器时钟1T模式TMOD&=0x0F;       //设置定时器模式TL1=0xCD;     //设置定时初值TH1=0xD4;     //设置定时初值TF1=0;        //清除TF1标志TR1=1;        //定时器1开始计时EA=1;
ET1=1;
}
voidtimer1() interrupt3{
tim++;
if(tim==t)
    {
tim=0;
if(led==0)
        {
led=1;
P2=0X80;P0=0XFF;
        }
else        {
led=0;
P2=0X80;P0=0XFE;
        }
    }
}
voidDelay1ms()     //@11.0592MHz{
unsignedchari, j;
_nop_();
_nop_();
_nop_();
i=11;
j=190;
do    {
while (--j);
    } while (--i);
}
voidDelayms(uintms)       //@11.0592MHz{
unsignedchari, j,k;
for(k=ms;k>0;k--)
    {
_nop_();
_nop_();
_nop_();
i=11;
j=190;
do        {
while (--j);
        } while (--i);
    }
}

ds18b20.c:


/*程序说明: 单总线驱动程序软件环境: KeiluVision4.10硬件环境: CT107单片机综合实训平台805112MHz: 2011-8-9*/#include "stc15f2k60s2.h"#include <intrins.h>#include <ds18b20.h>sbitDQ=P1^4;  //单总线接口//单总线延时函数voidDelay_OneWire(unsignedintt)  
{
unsignedchari;
while(t--){
for(i=0; i<8; i++);
    }
}
//通过单总线向DS18B20写一个字节voidWrite_DS18B20(unsignedchardat)
{
unsignedchari;
for(i=0;i<8;i++)
    {
DQ=0;
DQ=dat&0x01;
Delay_OneWire(5);
DQ=1;
dat>>=1;
    }
Delay_OneWire(5);
}
//从DS18B20读取一个字节unsignedcharRead_DS18B20(void)
{
unsignedchari;
unsignedchardat;
for(i=0;i<8;i++)
    {
DQ=0;
dat>>=1;
DQ=1;
if(DQ)
        {
dat|=0x80;
        }       
Delay_OneWire(5);
    }
returndat;
}
//DS18B20设备初始化bitinit_ds18b20(void)
{
bitinitflag=0;
DQ=1;
Delay_OneWire(12);
DQ=0;
Delay_OneWire(80);
DQ=1;
Delay_OneWire(10); 
initflag=DQ;     
Delay_OneWire(5);
returninitflag;
}
unsignedchartemget()
{
unsignedchartemp;
unsignedcharlow,high;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay500us();
Delay500us();   
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
temp=high<<4;
temp|= (low>>4);
returntemp;
}
voidDelay500us()       //@11.0592MHz{
unsignedchari, j;
_nop_();
_nop_();
i=6;
j=93;
do    {
while (--j);
    } while (--i);
}

ds18b20.h:


#ifndef __DS18B20_H#define __DS18B20_H//单总线延时函数voidDelay_OneWire(unsignedintt);
voidWrite_DS18B20(unsignedchardat);
unsignedcharRead_DS18B20(void);
bitinit_ds18b20(void);
unsignedchartemget();
voidDelay500us();
#endif

目录
相关文章
|
1月前
|
传感器 C语言 智能硬件
基于单片机的温度控制系统
基于单片机的温度控制系统
33 0
|
1月前
|
传感器 监控 IDE
基于单片机的温度监控系统设计
基于单片机的温度监控系统设计
66 0
|
3月前
|
传感器 芯片
毕业设计|基于51单片机的空气质量检测PM2.5粉尘检测温度设计
毕业设计|基于51单片机的空气质量检测PM2.5粉尘检测温度设计
|
3月前
|
传感器 芯片
毕业设计 基于51单片机霍尔电机转速测量温度PWM调速设计
毕业设计 基于51单片机霍尔电机转速测量温度PWM调速设计
|
3月前
|
传感器
毕业设计 基于51单片机的智能水表水流量计流量报警器温度设计
毕业设计 基于51单片机的智能水表水流量计流量报警器温度设计
|
4月前
|
物联网
STC51单片机-实验开发装置仿真-物联网应用系统设计
STC51单片机-实验开发装置仿真-物联网应用系统设计
55 0
|
2月前
|
机器学习/深度学习 算法 Java
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-659 比赛安排
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-659 比赛安排
28 0
|
3月前
|
传感器 监控 测试技术
单片机开发|基于51单片机的全自动黑板檫粉尘清除装置设计与实现
单片机开发|基于51单片机的全自动黑板檫粉尘清除装置设计与实现
|
3月前
|
传感器 存储 芯片
毕业设计|基于51单片机的空气质量检测PM2.5粉尘检测温度设计
毕业设计|基于51单片机的空气质量检测PM2.5粉尘检测温度设计
|
4月前
|
传感器 监控 测试技术
单片机开发|基于51单片机的全自动黑板檫粉尘清除装置设计与实现
单片机开发|基于51单片机的全自动黑板檫粉尘清除装置设计与实现