用stm32mini版做的避障小车,这是根据原子哥的历程改的,状态机部分只需要稍微修改一下即可使用。PA0接Echo,PA1接tring。论坛新手请多多指教。
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "timer.h"
- #include "usart.h"
- #include "ceju.h"
- #include "pwm.h"
- //接线说明echo-PA0;trig-PA1;vcc接5v
- //TM3使超声波每隔一秒发送,TM2输入捕获。
- float Distance;
- extern u8 TIM2CH1_CAPTURE_STA; //输入捕获状态
- extern u16 TIM2CH1_CAPTURE_VAL; //输入捕获值
- int main(void)
- {
-
- u32 temp=0;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
- TIM3_PWM_Init(799,0);
- delay_init(); //延时函数初始化
- uart_init(9600); //9600
- LED_Init(); //初始化与LED连接的硬件接口
- TIM3_Int_Init(4999*2,7199); //一秒发送一次。
- TIM2_Cap_Init(0XFFFF,7199); //以10khz的频率计数
- ceju_Init();
- while(1)
- {
-
- if(TIM2CH1_CAPTURE_STA&0X80)//成功捕获到了一次高电平
- {
- temp=TIM2CH1_CAPTURE_STA&0X3F;
- temp*=65536; //溢出时间总和
- temp+=TIM2CH1_CAPTURE_VAL; //得到总的高电平时间
- Distance=temp*5*34*2/200.0;
- printf("distance:%f cm\r\n",Distance);
- if(Distance<10)
- {
- LED0=!LED0;
- TIM_SetCompare1(TIM3,0);
- TIM_SetCompare2(TIM3,0);
- TIM_SetCompare3(TIM3,799);
- TIM_SetCompare4(TIM3,0);
- }
-
- TIM2CH1_CAPTURE_STA=0; //开启下一次捕获
- }
- }
- }
复制代码- #include "pwm.h"
- #include "sys.h"
- void TIM3_PWM_Init(u16 arr,u16 psc)
- {
- GPIO_InitTypeDef GPIO_InitStruct;//GPIOC初始化结构体
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;//定时器3初始化结构体
- TIM_OCInitTypeDef TIM_OCInitStruct;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//使能定时器3时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//使能GPIOC时钟
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//使能重映射AFIO时钟
-
- GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);//开启重映射
-
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6;
- GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOC,&GPIO_InitStruct);//GPIOC6初始化,设置为复用功能输出
-
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Pin=GPIO_Pin_7;
- GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOC,&GPIO_InitStruct);//GPIOC7初始化,设置为复用功能输出
-
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;
- GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOC,&GPIO_InitStruct);//GPIOC8初始化,设置为复用功能输出
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;
- GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOC,&GPIO_InitStruct);//GPIOC9初始化,设置为复用功能输出
-
- TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseInitStruct.TIM_Period=arr;//自动重装载值
- TIM_TimeBaseInitStruct.TIM_Prescaler=psc;//预分频系数
- //TIM_TimeBaseInitStruct.TIM_RepetitionCounter=
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);//定时器初始化函数
-
- //TIM_OCInitStruct.TIM_OCIdleState
- TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM2;//PWM设为模式2,比较值比arr大的时候是有效电平,即下面设置的高电平
- //TIM_OCInitStruct.TIM_OCNIdleState
- //TIM_OCInitStruct.TIM_OCNPolarity
- TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;//有效极性输出为高
- //TIM_OCInitStruct.TIM_OutputNState
- TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;//enable
- //TIM_OCInitStruct.TIM_Pulse
- TIM_OC3Init(TIM3,&TIM_OCInitStruct);//
-
- //TIM_OCInitStruct.TIM_OCIdleState
- TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM2;//PWM设为模式2,比较值比arr大的时候是有效电平,即下面设置的高电平
- //TIM_OCInitStruct.TIM_OCNIdleState
- //TIM_OCInitStruct.TIM_OCNPolarity
- TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;//有效极性输出为高
- //TIM_OCInitStruct.TIM_OutputNState
- TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;//enable
- //TIM_OCInitStruct.TIM_Pulse
- TIM_OC1Init(TIM3,&TIM_OCInitStruct);//
-
- //TIM_OCInitStruct.TIM_OCIdleState
- TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM2;//PWM设为模式2,比较值比arr大的时候是有效电平,即下面设置的高电平
- //TIM_OCInitStruct.TIM_OCNIdleState
- //TIM_OCInitStruct.TIM_OCNPolarity
- TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;//有效极性输出为高
- //TIM_OCInitStruct.TIM_OutputNState
- TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;//enable
- //TIM_OCInitStruct.TIM_Pulse
- TIM_OC2Init(TIM3,&TIM_OCInitStruct);//
-
- //TIM_OCInitStruct.TIM_OCIdleState
- TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM2;//PWM设为模式2,比较值比arr大的时候是有效电平,即下面设置的高电平
- //TIM_OCInitStruct.TIM_OCNIdleState
- //TIM_OCInitStruct.TIM_OCNPolarity
- TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;//有效极性输出为高
- //TIM_OCInitStruct.TIM_OutputNState
- TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;//enable
- //TIM_OCInitStruct.TIM_Pulse
- TIM_OC4Init(TIM3,&TIM_OCInitStruct);//
-
- TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);//1
- TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);//2
- TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);//定时器3使能预装载值
- TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable);//4
-
- TIM_Cmd(TIM3,ENABLE);//使能定时器
-
-
- }
复制代码
所有资料51hei提供下载:
小车避障.rar
(296.56 KB, 下载次数: 59)
|