主程序核心部分如下:
int main()
{
Sys_Delay_Init(72);
Car_GPIO_Config();
Xunji_GPIO_Config();
Chaoshengbo_Init();
timer_Init();
TIM3_PWM_Init(2000);
while(1)
{
TIM3_PWMoutPA6_Config(1500);
TIM3_PWMoutPA7_Config(1500);
Chaoshengbo_ReadJuli();
if( juli>300)
{ Ctrl();}
else
{ Stop();}
}
}
电机PWM占空比设置核心程序如下:
//TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
//TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
//TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
//TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
void TIM3_PWMoutPA6_Config(u16 CCR1_Val)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
//配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
//使能通道1
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
// 使能TIM3重载寄存器ARR
}
void TIM3_PWMoutPA7_Config(u16 CCR2_Val)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
//配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//当定时器计数值小于CCR1_Val时为高电平
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
//使能通道2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
// 使能TIM3重载寄存器ARR
}
void TIM3_PWMoutPB0_Config( u16 CCR3_Val)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
//配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//当定时器计数值小于CCR1_Val时为高电平
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
//使能通道3
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
// 使能TIM3重载寄存器ARR
}
void TIM3_PWMoutPB1_Config(u16 CCR4_Val)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
//配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//当定时器计数值小于CCR1_Val时为高电平
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
//使能通道4
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
// 使能TIM3重载寄存器ARR
}
超声波读距核心程序如下:
void Chaoshengbo_ReadJuli()
{
GPIO_SetBits(TRIG_PORT,TRIG_PIN);
//送>10US的高电平RIG_PORT,TRIG_PIN这两个在define中有
Delay_1us(20);
//延时20US
GPIO_ResetBits(TRIG_PORT,TRIG_PIN);
while(!GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)); //等待高电平
TIM_Cmd(TIM2,ENABLE); //开启时钟
while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)); //等待低电平
TIM_Cmd(TIM2, DISABLE); //定时器2失能
juli=(TIM_GetCounter(TIM2)*5*34/200);
//计算距离 juli=TIME/(计数频率)*(344/2)m
TIM_SetCounter(TIM2,0);
}
循迹功能核心程序如下:
void Ctrl()
{
u8 Temp=(u8)(GPIO_ReadInputData(GPIOB)&0xf0);
switch (Temp)
{
case 0x00:Go();
break;
case 0x10:Right();
break;
case 0x20:Right();
break;
case 0x30:Right();
break;
case 0xE0:Right();
break;
case 0xD0:Right();
break;
case 0x40:Left();
break;
case 0x80:Left();
break;
case 0xC0:Left();
break;
case 0x70:Left();
break;
case 0xB0:Left();
break;
case 0xF0:Stop();
break;
default:Back();
break;
}
Delay_1ms(100);
}