3-4-2系统框图及硬件接线
3-4-3系统代码框架搭建
4.软件—tasks.c文件编写
-
排队控制系统状态机
-
tasks.c
#include "tasks.h"
#include "led.h"
#include "beep.h"
#include "exti.h"
#include "lcd1602.h"
#include "gate.h"
#include "stdio.h"enum//枚举,定义状态
{PASS_STATE,WAIT_STATE
};uint32_t led1_task_cnt = 0;
uint32_t led2_task_cnt = 0;
uint32_t wait_cnt = 0;
uint32_t passenger = 0;uint8_t led1_task_flag = 0;
uint8_t led2_task_flag = 0;
uint8_t state = PASS_STATE;char message[16] = {0};//定义字符数组void systick_isr(void)
{//如果处于允许通行的状态if(state == PASS_STATE){//LED1以1秒的频率闪烁if (led1_task_cnt < 1000)led1_task_cnt++;else{led1_task_flag = 1;led1_task_cnt = 0;}//LED2不闪烁led2_off();//蜂鸣器不响beep_off();//开门gate_off();}//如果处于不允许通行的状态else if(state == WAIT_STATE){//LED2以200ms的频率闪烁if (led2_task_cnt < 200)led2_task_cnt++;else{led2_task_flag = 1;led2_task_cnt = 0;}//LED1不闪led1_off();//蜂鸣器响beep_on();//关门gate_on();//计时3秒,之后if(wait_cnt < 3000)wait_cnt++;else{wait_cnt = 0;//进入允许通行状态state = PASS_STATE;//LCD显示状态lcd1602_show_line(1, 1, "PASS...");}}
}void led1_task(void)
{if(led1_task_flag == 0)return;led1_task_flag = 0;led1_toggle();
}void led2_task(void)
{if(led2_task_flag == 0)return;led2_task_flag = 0;led2_toggle();
}void sensor_task(void)
{//如果检测到有人通过if(ia_flag_get() == TRUE && state == PASS_STATE){//计数加1passenger++;//LCD显示状态sprintf(message, "PASS...%02d/05", passenger);//sprintf()函数lcd1602_show_line(1, 1, message);}//如果通过的人数超过5个if(passenger >= 5){//计数清零passenger = 0;//进入不允许通行状态state = WAIT_STATE;//LCD显示状态lcd1602_show_line(1, 1, "WAIT...00/05");}
}
- tasks.h
#ifndef __TASKS_H__
#define __TASKS_H__#include "sys.h"void systick_isr(void);
void led1_task(void);
void led2_task(void);
void sensor_task(void);#endif
5.实物效果
ST-Link下载方式