STM32红外避障程序
单片机源程序如下:
- //连接方式 :请参考interface.h文件
- //部份源码来源网络--请试用24小时后删除
- #include "stm32f10x.h"
- #include "interface.h"
- #include "LCD1602.h"
- #include "IRCtrol.h"
- #include "motor.h"
- #include "UltrasonicCtrol.h"
- //全局变量定义
- unsigned int speed_count=0;//占空比计数器 50次一周期
- char front_left_speed_duty=SPEED_DUTY;
- char front_right_speed_duty=SPEED_DUTY;
- char behind_left_speed_duty=SPEED_DUTY;
- char behind_right_speed_duty=SPEED_DUTY;
- unsigned char tick_5ms = 0;//5ms计数器,作为主函数的基本周期
- unsigned char tick_1ms = 0;//1ms计数器,作为电机的基本计数器
- unsigned char tick_200ms = 0;//刷新显示
- char ctrl_comm = COMM_STOP;//控制指令
- char ctrl_comm_last = COMM_STOP;//上一次的指令
- unsigned char continue_time=0;
- unsigned char duoji_count=0;
- unsigned char zhuanjiao = 11;
- char ctrl_comm1 = COMM_UP;//控制指令
- ////全局变量定义
- unsigned char const table3[]=" GOGO ";
- unsigned char const table4[]=" STOP ";
- ///获取前方的距离,进来前方向为向前
- void GetAllDistance(unsigned int *dis_direct)
- {
- CarStop();
- GetDistanceDelay();
- *dis_direct = distance_cm;
- LCD1602WriteDistance(distance_cm);
-
- }
- void BarrierProc()
- {
- if(distance_cm < 25)//前方有障碍物
- {
- unsigned int dis_direct;//前方距离
- CarStop();
- Delayms(400);
- LCD1602WriteCommand(ctrl_comm);
- GetAllDistance(&dis_direct);
-
- }
- else
- {
- CarGo();
- LCD1602WriteCommand(ctrl_comm1);
- }
- }
- int main(void)
- {
- delay_init();
- GPIOCLKInit();
- UserLEDInit();
- LCD1602Init();
- //IRCtrolInit();
- TIM2_Init();
- MotorInit();
- UltraSoundInit();
-
- Delayms(500);
- CarGo();
- while(1)
- {
- if(tick_5ms >= 5)
- {
- tick_5ms = 0;
- Distance();//计算距离
- BarrierProc();
- }
-
- }
- }
复制代码
所有资料51hei提供下载:
STM32红外避障实验.rar
(281.86 KB, 下载次数: 115)
|