stm32小车
自己又玩了一下小车,之前玩俩轮平衡小车,现在做循迹小车温习代码,小车网上买的,其他红外,OLED,超声波模块自己买的。代码没有然后完全自己写这个工程(一年多没写代码了,刚好可以找回手感),话不多说下面我就简单描述下这个很简单的小车循迹模块过程。
首先找个示例工程正点原子的templete,正点原子的标准库不太好移植,个人建议可以直接上手野火的,用HAL库,正点原子的标准库想学的可以直接网上搜教学视频,下面进行代码的移植,因为用的例程是stm32f103RCT6的工程,需要进行一些改动,不同芯片之间的移植可以省去很多麻烦,也不是那么难,点开keil5中的魔术棒,在Device下重新选择芯片,这个小车的事stm32f103rbt6的,如果没有相应型号的keil5一般会自动更新pack,然后在Target选项下把晶振频率改为8MHz,f103系列下只有引脚数目,FLASH和封装的区别,我们更改启动文件,这里都是高密度型不用更改。
改好开发环境后,接下来就是要了解板子的资源了,这个板子的扩展板有一个按键,一个蜂鸣器,俩个led灯,以及其他接口,我们用pwm波控制驱动电机速度,还有红外避障,红外循迹,超声波测距,oled显示模块,所以要合理的分配接口资源,当然个人建议先一个一个的调模块,先关闭其他模块调通其中一个模块,基本上每个模块都是自己写的,拿别人的代码改是容易点,不过要想提高能力还是得自己写一遍。oled是用的四针的比六针的麻烦很多,用IIC通讯所以自己写IIC代码的时候最好要检验IIC的时序,有逻辑分析仪就爽多了,没有的用示波器俩个通道也可以。
#include "stm32f10x.h" #include "delay.h" #include "led.h" #include "key.h" #include "exti.h" #include "timer.h" #include "usart.h" #include "motor.h" #include "trail.h" #include "oled.h" #include "display.h" #include "ultrasonic.h" extern u8 TIM3CH4_CAPTURE_STA; extern u16 TIM3CH4_CAPTURE_VAL; extern unsigned char rhinder,lhinder; extern unsigned char rightchannel,leftchannel; extern volatile int a; int main() { HCSR04_GPIO_Config(); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); led_init(); TIM3_Int_Init(99,71); // TIM2_Int_Init(3999,35999); display(); while(1) { StartModule(); if(ECHO_DATA_IN()==0) TIM_Cmd(TIM3, ENABLE); if(ECHO_DATA_IN()==1) TIM_Cmd(TIM3, DISABLE); a=HCSR04_TEST(); display(); // delay_ms(1500); } }
#include "trail.h" #include "infrared.h" #include "delay.h" #include "motor.h" unsigned char rhinder,lhinder; unsigned char rightchannel,leftchannel; void infrared_trail(void) { infrared_init(); if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==0) lhinder=1; else lhinder=0; if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0) rhinder=1; else rhinder=0; if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0) rightchannel=0; else rightchannel=1; if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)==0) leftchannel=0; else leftchannel=1; } void avoid(void) { if(lhinder==1) { motor(300,600); delay_ms(50); } if(rhinder==1) { motor(600,300); delay_ms(50); } } void tracking(void) { if(rightchannel==0 && leftchannel==1) { motor(300,600); delay_ms(10); } if(rightchannel==1 && leftchannel==0) { motor(600,300); delay_ms(10); } }

小车的代码网上也有很多,但是每个简单的工程自己独立完成,把踩的坑抹平了,自己的能力才会上来,这是自己考研后的代码温习小项目,完成!

浙公网安备 33010602011771号