stm32 learning record - induction trash can for steering gear and ultrasonic ranging (with engineering code)

 

The first is the code for ultrasonic ranging. The principle is that the timer generates PWM waves to control the steering gear to rotate to a specified angle.

void HC_SR04Gpio1Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0; //ECHO
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
					
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;	//接TRIG 触发控制信号输入
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;	
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	         
	GPIO_Init(GPIOB, &GPIO_InitStructure);
}


void Timer3_Init(uint16 psc,uint16 arr)
{
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	
	TIM_DeInit(TIM3);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_Prescaler=psc;
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period=arr;
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
	
	TIM_ICInitTypeDef TIM_ICInitStructure;
	TIM_ICInitStructure.TIM_Channel=TIM_Channel_3;
	TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
	TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
	TIM_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
	TIM_ICInitStructure.TIM_ICFilter=0;
	TIM_ICInit(TIM3,&TIM_ICInitStructure);
	
	TIM_ClearITPendingBit(TIM3,TIM_IT_Update|TIM_IT_CC3);
	TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC3,ENABLE);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	TIM_Cmd(TIM3,ENABLE);

}
InputCapture icCapture={RESET,0,0,0,0};
uint32 Distance=0;
void TIM3_IRQHandler(void)
{
 if(icCapture.icFinishFlag==0)
 {
		if(TIM_GetITStatus(TIM3,TIM_IT_CC3)!= RESET)
		{
			if(icCapture.Edge==RESET)
			{
				icCapture.Edge=SET;
				TIM_SetCounter(TIM3,0);
				TIM_OC3PolarityConfig(TIM3,TIM_OCPolarity_Low);
			}
			else
			{
				icCapture.icFinishFlag=1;
				icCapture.icCaptureVal=TIM_GetCapture3(TIM3);
				Distance=(icCapture.icUpdateCnt*65535+icCapture.icCaptureVal)*340/20000;  
				icCapture.icUpdateCnt=0;
				icCapture.Edge=RESET;
				TIM_OC3PolarityConfig(TIM3,TIM_OCPolarity_High);
				icCapture.icFinishFlag=0;
			}
		
		}
		if(TIM_GetITStatus(TIM3,TIM_IT_Update)!= RESET)
		{
			if(icCapture.Edge==SET)
			{
			   icCapture.icUpdateCnt++;
					printf("The icUpdateCnt is:%d\r\n",icCapture.icUpdateCnt);
			}
		}
	}
   TIM_ClearITPendingBit(TIM3,TIM_IT_Update|TIM_IT_CC3);	
}

Then there is the steering gear.

void Duoji_Tim4_Init(u16 arr,u16 psc)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);	                     //使能定时器4时钟
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  //使能GPIO外设和AFIO复用功能模块时钟	326389
 
//设置该引脚为复用输出功能,输出TIM4 CH2的PWM脉冲波形	GPIOB.6
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ;                  //TIM_CH1-4
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;             //复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);                      //初始化GPIO
 
//**************初始化TIM4*****************//
	TIM_TimeBaseStructure.TIM_Period = arr;                     //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
	TIM_TimeBaseStructure.TIM_Prescaler =psc;                   //设置用来作为TIMx时钟频率除数的预分频值 
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;                //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);             //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
	
//*******初始化TIM4 Channel2 PWM模式********//	 
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;                //选择定时器模式:TIM脉冲宽度调制模式2
 	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;    //比较输出使能
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;        //输出极性:TIM输出比较极性高
	TIM_OC1Init(TIM4, &TIM_OCInitStructure);                         //根据T指定的参数初始化外设TIM4 OC1
	
	TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);                //使能TIM4在CCR2上的预装载寄存器
	
  TIM_Cmd(TIM4, ENABLE);                                           //使能TIM4
}

Finally, attach the engineering code that can be used

Link: https://pan.baidu.com/s/1ADlwZHGD430xtg6XPdg8VQ 
Extraction code: 9438

Guess you like

Origin blog.csdn.net/sujiaxin12/article/details/124283481