蓝桥杯单片机第六届国赛题目-智能物料传送系统
Posted WILLPOWER-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了蓝桥杯单片机第六届国赛题目-智能物料传送系统相关的知识,希望对你有一定的参考价值。
题目
要点
几个注意的点:
- 倒计时以及倒计时的停止的写法。
- 注意状态在什么时候被改变了,这次的改变可能导致你本来在这里变化进入程序,但是下一瞬间就被切换到另外的状态了
- 蜂鸣器和继电器一定要同时操作,否则会相互影响(1开启,0关闭)
- 可用^取反操作
uchar LedChange(uchar _data, uchar _dig, uchar _en)
{
if(_en == 1)
{
_data &= ~(1 << _dig);
}
else if(_en == 0)
{
_data |= (1 << _dig);
}
else
{
_data ^= (1 << _dig);
}
SL(4, _data);
return(_data);
}
文档树结构
程序
main.c
#include "init.h"
enum{LED = 4, EXT, SEL, CODE};
//display
uchar curDig;
uchar code CA[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99,
0x92, 0x82, 0xf8, 0x80, 0x90,
0xff};
uchar buf[] = {0, 1, 2, 3, 4, 5, 6, 7};
//按键
enum{KS_GT, KS_AS, KS_WA}keyState=KS_GT;
uchar keyCnt, key, tmpKey;
//时间
t_delay delay_200 = {200, 0, 0};
t_delay delay_250 = {250, 0, 0};
t_delay delay_300 = {300, 0, 0};
t_delay delay_transform = {300, 0, 0};
//变量
float distance;
float adcVal;
uchar time_Transform[2] = {2, 4};
enum{D_KZ, D_FK, D_GZ, D_TF}Detect = D_KZ;
uchar ledData = 0xff;
uchar relayData = 0;
uchar digCnt = 0;
void Timer1Handle() interrupt 3
{
//显示
SL(CODE, 0xff); SL(SEL, 1 << curDig); SL(CODE, CA[buf[curDig]]);
curDig = (curDig + 1)%8;
//按键
switch(keyState)
{
case KS_GT:
keyCnt = 0; keyState = KS_AS; tmpKey = GetKey();
break;
case KS_AS:
if(keyCnt++ < 10);
else if(tmpKey == GetKey())
{
if(key != tmpKey)
{
keyState = KS_WA;
key = tmpKey;
}
else
keyState = KS_GT;
}
else
keyState = KS_GT;
break;
}
//时间
TimeRun(&delay_200);
TimeRun(&delay_250);
TimeRun(&delay_300);
//如果开始计时才计时
if(delay_transform.ok == 2)
{
TimeBack(&delay_transform);
}
}
void SensorProcess()
{
if(delay_200.ok == 1)
{
delay_200.ok = 0;
Sonic_R();
//ADC检测
adcVal = Adc_Read(0x43, 200);
//如果在没有传送的时候才允许换状态
if(Detect != D_TF)
{
if(adcVal >= 4.0)
Detect = D_GZ;
else if(adcVal >=1)
Detect = D_FK;
else
Detect = D_KZ;
}
}
}
void EventProcess()
{
if(delay_300.ok == 1)
{
delay_300.ok = 0;
if(digCnt == 1)
{
if(buf[3] != F_C)
BF(3, F_C, F_C, F_C,
F_C, F_C, FR(time_Transform[1], 1),
FR(time_Transform[1], 0));
else
BF(3, F_C, F_C, FR(time_Transform[0], 1),
FR(time_Transform[0], 0), F_C, FR(time_Transform[1], 1),
FR(time_Transform[1], 0));
}
else if(digCnt == 2)
{
if(buf[6] != F_C)
BF(3, F_C, F_C, FR(time_Transform[0], 1),
FR(time_Transform[0], 0), F_C, F_C,
F_C);
else
BF(3, F_C, F_C, FR(time_Transform[0], 1),
FR(time_Transform[0], 0), F_C, FR(time_Transform[1], 1),
FR(time_Transform[1], 0));
}
}
//超声波
if(Detect == D_FK)
{
uchar leixing = 0;
if(distance > 30)
leixing = 2;
else
leixing = 1;
BF(1, F_C, F_C, FR(distance, 1), FR(distance, 0), F_C, F_C, leixing);
}
//开启传送
if(Detect == D_TF)
{
if(delay_transform.ok == 1)
{
delay_transform.ok = 0;
RLOFF;
Detect = D_KZ;//随便给个状态,它会在sensor中刷新的
}
else
{
BF(2, F_C, F_C, F_C, F_C, F_C, FR(delay_transform.max/500.0,1), FR(delay_transform.max/500.0,0));
}
}
//空载关闭数码管(注意要digCnt == 0保证没有设置传送时间)
if(Detect == D_KZ && (digCnt == 0))
{
if(buf[0] != F_C)
{
memset(buf, F_C, 8);
}
}
}
void LedProcess()
{
static uchar s_Detect = 255;
if((s_Detect != Detect) || Detect == D_GZ)
{
switch(Detect)
{
case D_KZ:
ledData = LedChange(ledData, 0, 1);
ledData = LedChange(ledData, 1, 0);
ledData = LedChange(ledData, 2, 0);
BPOFF;
if(relayData == 1)
{
RLON;
}
else
{
RLOFF;
}
break;
case D_FK:
ledData = LedChange(ledData, 1, 1);
ledData = LedChange(ledData, 0, 0);
ledData = LedChange(ledData, 2, 0);
BPOFF;
if(relayData == 1)
{
RLON;
}
else
{
RLOFF;
}
break;
case D_GZ:
if(s_Detect != Detect)
{
ledData = LedChange(ledData, 0, 0);
ledData = LedChange(ledData, 1, 0);
BPON;RLOFF;
}
if(delay_250.ok == 1)
{
delay_250.ok = 0;
ledData = LedChange(ledData, 2, 2);
}
break;
}
s_Detect = Detect;
}
//如果传送开启,但是计时没有开启
if((Detect == D_TF) && (delay_transform.ok == 0))
{
if(delay_250.ok == 1)
{
delay_250.ok = 0;
ledData = LedChange(ledData, 3, 2);
}
}
}
void KeyProcess()
{
if(Detect == D_KZ)
{
if(key == 6)
{
if(digCnt == 0)
{
BF(3, F_C, F_C, FR(time_Transform[0], 1),
FR(time_Transform[0], 0), F_C, FR(time_Transform[1], 1),
FR(time_Transform[1], 0));
digCnt++;
}
else
digCnt++;
if(digCnt == 3)
{
Code_Write(0,time_Transform,2);
digCnt = 0;
memset(buf, F_C, 8);
}
}
if(key == 7)
{
if(digCnt != 0)
{
if(digCnt == 1)
{
if(time_Transform[0]++ < 10);
else
time_Transform[0] = 1;
}
if(digCnt == 2)
{
if(time_Transform[1]++ < 10);
else
time_Transform[1] = 1;
}
BF(3, F_C, F_C, FR(time_Transform[0], 1),
FR(time_Transform[0], 0), F_C, FR(time_Transform[1], 1),
FR(time_Transform[1], 0));
}
}
}
//如果在非空情况下,按下启动传送
if(Detect == D_FK)
{
if(key == 4)
{
Detect = D_TF;
delay_transform.max = 500*(time_Transform[buf[7] - 1] + 1);
//开启计时
delay_transform.ok = 2;
BF(2, F_C, F_C, Fuu_C,
F_C, F_C, FR(time_Transform[buf[7] - 1], 1),
FR(time_Transform[buf[7] - 1], 0));
RLON;BPOFF;
}
}
if(Detect == D_TF)
{
if(key == 5)
{
//停止计时传送
if(delay_transform.ok == 2)
{
delay_transform.ok = 0;
BPOFF;RLOFF;
}
//打开传送
else
{
delay_transform.ok = 2;
BPOFF;RLON;
ledData = LedChange(ledData, 3, 0);
}
}
}
}
void main()
{
//初始化
BPOFF; RLOFF;
SL(LED, 0xff);
Timer1Init();
memset(buf, F_C, 8);
Code_Read(0,time_Transform,2);
//超声波初始化
CMOD |= 0x08;
while(1)
{
SensorProcess();
EventProcess();
LedProcess();
if(keyState == KS_WA)
{
KeyProcess();
keyState=KS_GT;
}
}
}
init.c
#include "init.h"
void SL(uchar _dev, uchar _data)
{
P0 = _data; SEL(_dev);
}
void Timer1Init(void) //2毫秒@12.000MHz
{
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x40; //设置定时初值
TH1 = 0xA2; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1= 1;
EA = 1;
}
uchar GetKey()
{
if(!K1) return 7;
if(!K2) return 6;
if(!K3) return 5;
if(!K4) return 4;
return 0;
}
uchar FR(float _data, uchar _dig)
{
uint i = 1;
while(_dig--)
{
i = i * 10;
}
return((uint)_data/i%10);
}
void BF(uchar _0, uchar _1, uchar _2, uchar _3,
uchar _4, uchar _5, uchar _6, uchar _7)
{
buf[0] = _0; buf[1] = _1; buf[2] = _2; buf[3] = _3;
buf[4] = _4; buf[5] = _5; buf[6] = _6; buf[7] = _7;
}
void TimeRun(t_delay* time)
{
if(time->cnt++ < time->以上是关于蓝桥杯单片机第六届国赛题目-智能物料传送系统的主要内容,如果未能解决你的问题,请参考以下文章