目录
客观题题目
程序题题目
程序题参考答案
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
onewire.h
onewire.c
LN555.h
LN555.c
首先吐槽一下,花300元体验国赛的难度,是真的崩溃。
3个小时写完,2个小时改bug!!!
- NE555本来应该是在国赛考的,这届省赛直接上了。
- I2C、DS1302、DS18B20,这3个省赛常考的内容,按照往届省赛的习惯应该是三选二进行考查,但是这届省赛中直接将这3个都给考了,这种做法常见于国赛当中。
- 这届赛题的逻辑也更加复杂,出现了两个采集到的数据,进行对比最大值、计算平均值、前后两次采集到的数据比较。
- 程序题的题目能达到5页之多。
- 客观题也不好做。
- 个人感觉,疫情开放后,为了减少国赛的参加人数。
#ifndef __MAIN_H_
#define __MAIN_H_
#include
#define uchar unsigned char
#define uint unsigned int
#include "Init.h"
#include "SMG.h"
#include "DSQ.h"
#include "YanShi.h"
#include "JZKey.h"
#include "ds1302.h"
#include "iic.h"
#include "onewire.h"
#include "LN555.h"
void LED_Show();
#endif
#include "main.h"
uchar key_num=0;
uchar key_num_old=0;
uchar JieMian=0;//界面。0:时间;1:回显;2:参数,3:温湿度
uchar JieMian_old;
uchar HuiXian=0;//回显。0:温度;1:湿度;2:时间
char WenDu_CanShu=30;//温度参数,0-99
uint S9_time=0;
uchar shi,fen,miao;
uchar ADC_data;
uchar ADC_data_old;
uchar ChuFa_CiShu=0;//触发次数
uchar WenDu=0;//温度
uchar WenDu_old;
uchar WenDu_temp;
uchar WenDu_Max=0;
uint WenDu_PingJun=0;
uint Hz;
uint Hz_time=0;
uchar ChuFa_Shi,ChuFa_Fen;
uchar ShiDu=0;
uchar ShiDu_old=0;
uchar ShiDu_old_temp;
uchar ShiDu_Max=0;
uint ShiDu_PingJun=0;
uchar JieMian_flag=0;//0:正常运转;1:3秒显示温湿度
uint JieMian_flag_time=0;
uchar YouXiao_flag=1;
uchar L4_time=0;
uint ADC_time=0;
void main(){
Init_BZ();
DSQ_2_Init();
SMG_Init();
JZKey_Init();
LN555_DSQ_0_Init();
Ds1302_XieData(13,3,5);
DS18B20_GetWenDu();
while(1){
key_num_old=key_num;
key_num=JZKey_GetKeynum();
if(key_num_old!=key_num&&JieMian_flag==0){
if(key_num==4){
JieMian++;
JieMian=JieMian%3;
if(JieMian==1){
HuiXian=0;
}
}
else if(key_num==5&&JieMian!=0){
HuiXian++;
HuiXian=HuiXian%3;
}
else if(key_num==8&&JieMian==2){
WenDu_CanShu++;
if(WenDu_CanShu>=99){
WenDu_CanShu=99;
}
}
else if(key_num==9&&JieMian==2){
WenDu_CanShu--;
if(WenDu_CanShu<=0){
WenDu_CanShu=0;
}
}
}
shi=Ds1302_GetShi();
fen=Ds1302_GetFen();
miao=Ds1302_GetMiao();
if(ADC_time>=200){
ADC_time=0;
ADC_data_old=ADC_data;
ADC_data=IIC_ADC_GetData(1);
if(ADC_data_old>ADC_data&&JieMian_flag==0&&ADC_data_old>90&&ADC_data<50){//触发采集
ShiDu_old_temp=ShiDu;
if(Hz<200){
ShiDu=9;
}
else if(Hz>=200&&Hz<=2000){
ShiDu=(80.0/1800.0)*(Hz*1.0)-(10.0/9.0);
}
else if(Hz>2000){
ShiDu=91;
}
if(ShiDu>9&&ShiDu<91){
ShiDu_old=ShiDu_old_temp;
if(ShiDu_Max=2000){//清除所有已记录的数据
S9_time=0;
WenDu_Max=0;
WenDu_PingJun=0;
ShiDu_Max=0;
ShiDu_PingJun=0;
ChuFa_CiShu=0;
ChuFa_Shi=0;
ChuFa_Fen=0;
}
}
Hz_time++;
if(Hz_time>=1000){
Hz_time=0;
Hz=((unsigned int)TH0<<8)|(unsigned int)TL0;
TH0=0;
TL0=0;
}
if(JieMian_flag){
JieMian_flag_time++;
if(JieMian_flag_time>=3000){
JieMian_flag_time=0;
JieMian_flag=0;
JieMian=JieMian_old;
}
}
if(WenDu>WenDu_CanShu){
L4_time++;
if(L4_time>=200){
L4_time=0;
}
}
else{
L4_time=0;
}
if(JieMian==0){
//SMG_Show(ADC_data_old/100,ADC_data_old/10%10,ADC_data_old%10,16,16,ADC_data/100,ADC_data/10%10,ADC_data%10);
SMG_Show(shi/10,shi%10,17,fen/10,fen%10,17,miao/10,miao%10);
}
else if(JieMian==1){
if(HuiXian==0){
if(ChuFa_CiShu==0){
SMG_Show(12,16,16,16,16,16,16,16);
}
else{
SMG_Show(12,16,WenDu_Max/10,WenDu_Max%10,17,WenDu_PingJun/100,((WenDu_PingJun/10)%10)+32,WenDu_PingJun%10);
}
}
else if(HuiXian==1){
if(ChuFa_CiShu==0){
SMG_Show(18,16,16,16,16,16,16,16);
}
else{
SMG_Show(18,16,ShiDu_Max/10,ShiDu_Max%10,17,ShiDu_PingJun/100,((ShiDu_PingJun/10)%10)+32,ShiDu_PingJun%10);
}
}
else if(HuiXian==2){
if(ChuFa_CiShu==0){
SMG_Show(15,ChuFa_CiShu/10,ChuFa_CiShu%10,16,16,16,16,16);
}
else{
SMG_Show(15,ChuFa_CiShu/10,ChuFa_CiShu%10,ChuFa_Shi/10,ChuFa_Shi%10,17,ChuFa_Fen/10,ChuFa_Fen%10);
}
}
}
else if(JieMian==2){
if(WenDu_CanShu>=10){
SMG_Show(24,16,16,16,16,16,WenDu_CanShu/10,WenDu_CanShu%10);
}
else if(WenDu_CanShu>=0){
SMG_Show(24,16,16,16,16,16,16,WenDu_CanShu);
}
}
else if(JieMian==3){
if(YouXiao_flag==1){
SMG_Show(14,16,16,WenDu/10,WenDu%10,17,ShiDu/10,ShiDu%10);
}
else{
SMG_Show(14,16,16,WenDu_temp/10,WenDu_temp%10,17,10,10);
}
}
LED_Show();
}
void LED_Show(){
uchar L1_2_3=0xff;
uchar L4=0xff;
uchar L5=0xff;
uchar L6=0xff;
if(JieMian==0){
L1_2_3=0xfe;
}
else if(JieMian==1){
L1_2_3=0xfd;
}
else if(JieMian==2){
L1_2_3=0xfb;
}
if(L4_time>=100){
L4=0xf7;
}
if(YouXiao_flag==0){
L5=0xef;
}
if(ChuFa_CiShu>=2&&WenDu_old
#ifndef __INIT_H_
#define __INIT_H_
#include
void Init_BZ();
#endif
#include "Init.h"
void Init_BZ(){
P2=(P2&0x1f)|0xa0;
P0=0x00;
P2=(P2&0x1f)|0x80;
P0=0xff;
}
#ifndef __SMG_H_
#define __SMG_H_
#include
void SMG_Init();
void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8);
#endif
#include "SMG.h"
unsigned char code t_display[]={ //????
// 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=~t_display[n1];
break;
}
case 2:{
P0=0x02;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n2];
break;
}
case 3:{
P0=0x04;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n3];
break;
}
case 4:{
P0=0x08;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n4];
break;
}
case 5:{
P0=0x10;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n5];
break;
}
case 6:{
P0=0x20;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n6];
break;
}
case 7:{
P0=0x40;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n7];
break;
}
case 0:{
P0=0x80;
P2=(P2&0x1f)|0xe0;
P0=~t_display[n8];
break;
}
}
P2=(P2&0x1f)|0x00;
}
#ifndef __DSQ_H_
#define __DSQ_H_
#include
void DSQ_2_Init();
#endif
#include "DSQ.h"
void DSQ_2_Init(){
AUXR |= 0x04; //定时器时钟1T模式
T2L = 0x20; //设置定时初值
T2H = 0xD1; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
AUXR=AUXR&0xf7;
IE2=IE2|0x04;
EA=1;
}
#ifndef __YANSHI_H_
#define __YANSHI_H_
#include
#include "intrins.h"
void Delay10ms();
#endif
#include "YanShi.h"
void Delay10ms() //@12.000MHz
{
unsigned char i, j;
i = 117;
j = 184;
do
{
while (--j);
} while (--i);
}
#ifndef __JZKEY_H_
#define __JZKEY_H_
#include "YanShi.h"
void JZKey_Init();
unsigned char JZKey_GetKeynum();
#endif
#include "JZKey.h"
void JZKey_Init(){
P32=1;
P33=1;
P42=0;
P44=0;
}
unsigned char JZKey_GetKeynum(){
unsigned char key_num=0;
if(P32==0){
P32=0;
P42=1;
P44=1;
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;
P42=1;
P44=1;
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;
}
#ifndef __DS1302_H
#define __DS1302_H
#include
#include
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_XieData(unsigned char shi,fen,miao);
unsigned char Ds1302_GetShi();
unsigned char Ds1302_GetFen();
unsigned char Ds1302_GetMiao();
#endif
/*
程序说明: DS1302驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "ds1302.h"
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302复位
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);
}
void Ds1302_XieData(unsigned char shi,fen,miao){
shi=(shi/10)*16+(shi%10);
fen=(fen/10)*16+(fen%10);
miao=(miao/10)*16+(miao%10);
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(0x84,shi);
Write_Ds1302_Byte(0x82,fen);
Write_Ds1302_Byte(0x80,miao);
Write_Ds1302_Byte(0x8e,0x80);
}
unsigned char Ds1302_GetShi(){
unsigned char date;
date=Read_Ds1302_Byte(0x85);
date=(date/16)*10+(date%16);
return date;
}
unsigned char Ds1302_GetFen(){
unsigned char date;
date=Read_Ds1302_Byte(0x83);
date=(date/16)*10+(date%16);
return date;
}
unsigned char Ds1302_GetMiao(){
unsigned char date;
date=Read_Ds1302_Byte(0x81);
date=(date/16)*10+(date%16);
return date;
}
#ifndef _IIC_H
#define _IIC_H
#include
#include "intrins.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);
unsigned char IIC_ADC_GetData(unsigned char di_zhi);
void IIC_DAC_XieData(unsigned char date);
unsigned char IIC_EEPROM_GetData(unsigned char di_zhi);
void IIC_EEPROM_XieData(unsigned char dizhi,date);
#endif
/*
程序说明: IIC总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "iic.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;
}
unsigned char IIC_ADC_GetData(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);
IIC_WaitAck();
date=IIC_RecByte();
//EA=1;
IIC_SendAck(1);
IIC_WaitAck();
IIC_Stop();
return date;
}
void IIC_DAC_XieData(unsigned char date){
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
}
unsigned char IIC_EEPROM_GetData(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;
}
void IIC_EEPROM_XieData(unsigned char dizhi,date){
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(dizhi);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
}
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
#include
float DS18B20_GetWenDu();
#endif
/*
程序说明: 单总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
日 期: 2011-8-9
*/
#include "onewire.h"
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
unsigned 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;
}
float DS18B20_GetWenDu(){
float wen_du,zs,xs;
unsigned char di,gao;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
di=Read_DS18B20();
gao=Read_DS18B20();
if(gao>=240){
di=~di;
gao=~gao;
if(di==0xff){
di=0;
gao++;
}
else{
di++;
}
zs=((gao<<4)|(di>>4))*1.0;
xs=(di&0x0f)*0.0625;
wen_du=-(zs+xs);
}
else{
zs=((gao<<4)|(di>>4))*1.0;
xs=(di&0x0f)*0.0625;
wen_du=(zs+xs);
}
return wen_du;
}
#ifndef __LN555_H_
#define __LN555_H_
#include
void LN555_DSQ_0_Init();
#endif
#include "LN555.h"
void LN555_DSQ_0_Init(){
TMOD=(TMOD&0xf0)|0x05;
TH0=0;
TL0=0;
ET0=0;
TR0=1;
}