本实验开发板应用的是mini版stm32f103开发板,通过红外以实现小车循迹,同时用PWM控速,较为稳定行驶。
小车外观1 小车底板
小车外观2 红外检测原理图
stm32单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "remote.h"
- #include "pwm.h"
- int main(void)
- {
- u16 led0pwmval=0;
- u16 led1pwmval=0;
- int left;
- int right;
- delay_init(); //延时函数初始?
- LED_Init(); //初始化与LED连接的硬件接口
- TIM1_PWM1_Init(899,0);//不分频。PWM频率=72000/(899+1)=80Khz
- TIM1_PWM2_Init(899,0);
- Remote_Init(); //红外接收初始化
- while(1)
- {
- left=LEFTRECEIVE ; //左边
- right=RIGHTRECEIVE; //右边
- if(left==1&&right==1)
- {
- led0pwmval=450;
- led1pwmval=450;
- TIM_SetCompare1(TIM1,led0pwmval);
- TIM_SetCompare2(TIM1,led1pwmval);
- }
- if(left==0&&right==0)
- {
- led0pwmval=450;
- led1pwmval=450;
- TIM_SetCompare1(TIM1,led0pwmval);
- TIM_SetCompare2(TIM1,led0pwmval);
- }
- if(left==1&&right==0)
- {
- led0pwmval=899;
- led1pwmval=450;
- TIM_SetCompare1(TIM1,led0pwmval);
- TIM_SetCompare2(TIM1,led1pwmval);
- }
- if(left==0&&right==1)
- {
- led0pwmval=450;
- led1pwmval=899;
- TIM_SetCompare1(TIM1,led0pwmval);
- TIM_SetCompare2(TIM1,led1pwmval);
- }
-
- }
- }
复制代码
所有资料51hei提供下载:
红外循迹.rar
(318.74 KB, 下载次数: 335)
|