当前位置:网站首页>STM32 steering gear controller
STM32 steering gear controller
2022-06-13 01:58:00 【richardgann】
This section will introduce you to stm32 SCM to do a steering controller , By rotating the potentiometer , To control the output angle of the steering gear .

Old rules , Let's share the source code project with you ,
Pay attention to WeChat public number : Guangyi electronics (dlrcclub), Reply key : Steering gear tester .
Before talking about the source code, let's review the control principle of the steering gear , At frequency 50hz Next , Give me a 0.5ms-2.5ms Duty cycle of pulse width , You can perform a test on the steering gear 45°-180° The rotation of .

How can we realize it through the single chip computer ?
1、 To configure ADC modular , Collect analog quantity of potentiometer , Change the collected data into pwm The duty cycle to be output .
2、 To configure PWM modular , Generate a frequency 50Hz, Pulse width at 1ms-2.0ms The square wave .
1ms-2.0ms The corresponding angle is 45°-135°.
3、 Range conversion function . The data range collected by the potentiometer is 0-4096, And ours PWM The range of values required by the capture function is 1000-2000. therefore , Need to put 0-4096 The value range of is converted to 1000-2000.
Let's enumerate the functions of each part ,ADC and PWM All from atoms stm32 Routine , You can also extract from the routine directly .
ADC.c, adc Some basic configurations .
#include "adc.h"
#include "delay.h"
//³õʼ»¯ADC
//ÕâÀïÎÒÃǽöÒÔ¹æÔòͨµÀΪÀý
//ÎÒÃÇĬÈϽ«¿ªÆôͨµÀ0~3
void Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1 , ENABLE ); //ʹÄÜADC1ͨµÀʱÖÓ
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //ÉèÖÃADC·ÖƵÒò×Ó6 72M/6=12,ADC×î´óʱ¼ä²»Äܳ¬¹ý14M
//PA1 ×÷ΪģÄâͨµÀÊäÈëÒý½Å
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Ä£ÄâÊäÈëÒý½Å
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1); //¸´Î»ADC1,½«ÍâÉè ADC1 µÄÈ«²¿¼Ä´æÆ÷ÖØÉèΪȱʡֵ ADC1 ADC2 ADC3
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC¹¤×÷ģʽ:ADC1ºÍADC2¹¤×÷ÔÚ¶ÀÁ¢Ä£Ê½
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Ä£Êýת»»¹¤×÷ÔÚµ¥Í¨µÀģʽ
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //Ä£Êýת»»¹¤×÷ÔÚµ¥´Îת»»Ä£Ê½
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //ת»»ÓÉÈí¼þ¶ø²»ÊÇÍⲿ´¥·¢Æô¶¯
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADCÊý¾ÝÓÒ¶ÔÆë
ADC_InitStructure.ADC_NbrOfChannel = 1; //˳Ðò½øÐйæÔòת»»µÄADCͨµÀµÄÊýÄ¿
ADC_Init(ADC1, &ADC_InitStructure); //¸ù¾ÝADC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèADCxµÄ¼Ä´æÆ÷
ADC_Cmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1
ADC_ResetCalibration(ADC1); //ʹÄܸ´Î»Ð£×¼
while(ADC_GetResetCalibrationStatus(ADC1)); //µÈ´ý¸´Î»Ð£×¼½áÊø
ADC_StartCalibration(ADC1); //¿ªÆôADУ׼
while(ADC_GetCalibrationStatus(ADC1)); //µÈ´ýУ׼½áÊø
// ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1µÄÈí¼þת»»Æô¶¯¹¦ÄÜ
}
//»ñµÃADCÖµ
//ch:ͨµÀÖµ 0~3
u16 Get_Adc(u8 ch)
{
//ÉèÖÃÖ¸¶¨ADCµÄ¹æÔò×éͨµÀ£¬Ò»¸öÐòÁУ¬²ÉÑùʱ¼ä PB1¶ÔÓ¦ADC9
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADCͨµÀ,²ÉÑùʱ¼äΪ239.5ÖÜÆÚ
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1µÄÈí¼þת»»Æô¶¯¹¦ÄÜ
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//µÈ´ýת»»½áÊø
return ADC_GetConversionValue(ADC1); //·µ»Ø×î½üÒ»´ÎADC1¹æÔò×éµÄת»»½á¹û
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=Get_Adc(ch);
delay_ms(5);
}
return temp_val/times;
}
PWM.c,PWM Part of the basic configuration .
#include "pwm.h"
//PWMÊä³ö³õʼ»¯
//arr£º×Ô¶¯ÖØ×°Öµ
//psc£ºÊ±ÖÓÔ¤·ÖƵÊý
void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //ʹÄÜGPIOÍâÉèʱÖÓʹÄÜ
//ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM1 CH1µÄPWMÂö³å²¨ÐÎ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ 80K
TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓÆµÂʳýÊýµÄÔ¤·ÖƵֵ ²»·ÖƵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆÄ£Ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_Pulse = 0; //ÉèÖôý×°Èë²¶»ñ±È½Ï¼Ä´æÆ÷µÄÂö³åÖµ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔ¸ß
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //¸ù¾ÝTIM_OCInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIMx
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE Ö÷Êä³öʹÄÜ
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1Ô¤×°ÔØÊ¹ÄÜ
TIM_ARRPreloadConfig(TIM1, ENABLE); //ʹÄÜTIMxÔÚARRÉϵÄÔ¤×°ÔØ¼Ä´æÆ÷
TIM_Cmd(TIM1, ENABLE); //ʹÄÜTIM1
}
Range conversion function , Personally, I think this function is the focus of the whole project , I hope you can remember this function , This function is taken from arduino The function in , Remember and you can use it skillfully .
float map(float value,float fromLow,float fromHigh,float toLow,float toHigh)
{
return ((value-fromLow)*(toHigh-toLow)/(fromHigh-fromLow)+toLow);
}The main function main.c
int main(void)
{
u16 pwmval_adc=0;
u16 pwmval_to_range;
//u8 adc_value;
delay_init(); //ÑÓʱº¯Êý³õʼ»¯
TIM1_PWM_Init(1999,719); //Fre_PWM = 72000/(719+1)/(1999+1)=50hz
uart_init(9600);
Adc_Init();
printf("this is a test");
while(1)
{
// printf("this is a test");
delay_ms(10);
pwmval_adc = Get_Adc_Average(ADC_Channel_1,10); //adcx Ϊ»ñÈ¡µ½ ADCµÄÖµ
//printf("adc_value= %d \n\r",pwmval_adc); //´òÓ¡³öADC ²É¼¯µ½µÄµçλÆ÷Öµ 4090
pwmval_to_range = (int)map(pwmval_adc,0,4092,1000,2000);
delay_ms(100);
printf("range =%d\t\r\n",pwmval_to_range); //Êä³ö ÔÚ1000-2000·¶Î§ÄÚתÍ귶ΧµÄADC Öµ
TIM_SetCompare1(TIM1,pwmval_to_range);
}
}Download the program , We can see that the steering gear follows 45°-135° To move back and forth .
Welcome to WeChat official account. : Guangyi electronics (dlrcclub)
边栏推荐
- Leetcode question 20
- Developer contributions amd Xilinx Chinese Forum sharing - wisdom of questioning
- Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)
- Introduction to Google unit testing tools GTEST and gmoke
- Magics 23.0 how to activate and use the slice preview function of the view tool page
- 四、入库管理功能的完善
- 万字讲清 synchronized 和 ReentrantLock 实现并发中的锁
- Decoding iFLYTEK open platform 2.0 is a fertile land for developers and a source of industrial innovation
- Detailed explanation of audience characteristics
- Day 1 of the 10 day smart lock project (understand the SCM stm32f401ret6 and C language foundation)
猜你喜欢

指针链表的实现
![[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)](/img/f7/b2463d8ffe75113d352cae332046db.jpg)
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)

一、搭建django自动化平台(实现一键执行sql)

4、 Improvement of warehousing management function

STM32 3*3矩阵按键(寄存器版本)

Quickly set the computer to turn off automatically

Interruption of 51 single chip microcomputer learning notes (external interruption, timer interruption, interrupt nesting)

Using OpenCV in go

Qt实现思维导图功能(二)
![[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys](/img/fc/f03c7dc4d5ee12aaa301f54e4cd3f4.jpg)
[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys
随机推荐
Torch. Distributions. Normal
四、入库管理功能的完善
Pyflink implements custom sourcefunction
Compiling minicom-2.7.1 under msys2
Ruixing coffee moves towards "national consumption"
pringboot之restfull接口规范注解(二)
Ruixing coffee 2022, extricating itself from difficulties and ushering in a smooth path
How do you use your own data to achieve your marketing goals?
Leetcode question 20
Vscode configuration header file -- Take opencv and its own header file as an example
Introduction to common ROS commands
Restrict cell input type and display format in CXGRID control
谷歌加大型文字广告是什么?怎么用?
uniapp 预览功能
The commercial value of Kwai is being seen by more and more brands and businesses
Devaxpress Chinese description -- tdxgallerycontrol object (gallery component)
Implementation and design of JMeter interface test database assertion for CSDN salary increase technology
Combining strings and numbers using ssstream
Using atexit to realize automatic destruct of singleton mode
LabVIEW大型项目开发提高质量的工具