当前位置:网站首页>Using ADC to control brushless motor source program STM32 library function
Using ADC to control brushless motor source program STM32 library function
2022-06-24 23:34:00 【mialo163】
Statement Personal learning content For reference only
STM32f103c8t6 Minimum system Rocker potentiometer No brush motor Electrical regulation

PWM Initialization function
void pwm_init()
{
GPIO_InitTypeDef GPIO_InitStructure; //ÉùÃ÷Ò»¸ö½á¹¹Ìå±äÁ¿£¬ÓÃÀ´³õʼ»¯GPIO
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;//ÉùÃ÷Ò»¸ö½á¹¹Ìå±äÁ¿£¬ÓÃÀ´³õʼ»¯¶¨Ê±Æ÷
TIM_OCInitTypeDef TIM_OCInitStructure;//¸ù¾ÝTIM_OCInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIMx
/* ¿ªÆôʱÖÓ */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
/* ÅäÖÃGPIOµÄģʽºÍIO¿Ú */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA,&GPIO_InitStructure);
//TIM3¶¨Ê±Æ÷³õʼ»¯
TIM_TimeBaseInitStructure.TIM_Period = 1439; //²»·ÖƵ,PWM ƵÂÊ=72000/900=8Khz//ÉèÖÃ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ 5Khz
TIM_TimeBaseInitStructure.TIM_Prescaler = 99;//ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓÆµÂÊÔ¤·ÖƵֵ£¬100Khz¼ÆÊýƵÂÊ
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;//ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Down; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM3, & TIM_TimeBaseInitStructure);
//GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);//¸Ä±äÖ¸¶¨¹Ü½ÅµÄÓ³Éä //pC7
//PWM³õʼ»¯ //¸ù¾ÝTIM_OCInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIMx
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//PWMÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC2Init(TIM3,&TIM_OCInitStructure);
//×¢Òâ´Ë´¦³õʼ»¯Ê±TIM_OC2Init¶ø²»ÊÇTIM_OCInit£¬·ñÔò»á³ö´í¡£ÒòΪ¹Ì¼þ¿âµÄ°æ±¾²»Ò»Ñù¡£
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);//ʹÄÜ»òÕßʧÄÜTIMxÔÚCCR2ÉϵÄÔ¤×°ÔØ¼Ä´æÆ÷
TIM_Cmd(TIM3,ENABLE);//ʹÄÜ»òÕßʧÄÜTIMxÍâÉè
}
obtain 500HZ pwm The signal
ADC initialization
void adc_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_ADC1,ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);//12M ×î´ó14M ÉèÖÃADCʱÖÓ£¨ADCCLK£
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;//ADC
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN; //Ä£ÄâÊäÈë
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
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_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
//ÉèÖÃÖ¸¶¨ADCµÄ¹æÔò×éͨµÀ£¬ÉèÖÃËüÃǵÄת»¯Ë³ÐòºÍ²ÉÑùʱ¼ä
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_239Cycles5);
ADC_Cmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);//ÖØÖÃÖ¸¶¨µÄADCµÄУ׼¼Ä´æÆ÷
while(ADC_GetResetCalibrationStatus(ADC1));//»ñÈ¡ADCÖØÖÃУ׼¼Ä´æÆ÷µÄ״̬
ADC_StartCalibration(ADC1);//¿ªÊ¼Ö¸¶¨ADCµÄУ׼״̬
while(ADC_GetCalibrationStatus(ADC1));//»ñȡָ¶¨ADCµÄУ׼³ÌÐò
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//ʹÄÜ»òÕßʧÄÜÖ¸¶¨µÄADCµÄÈí¼þת»»Æô¶¯¹¦ÄÜ
}
The main function
#include "stm32f10x.h"
#include "adc.h"
#include "pwm.h"
int main(void)
{
u32 ad=0;
u8 i;
adc_init(); //ADC³õʼ»¯
pwm_init();
// printf_init(); //printf³õʼ»¯
while(1)
{
ad=0;
for(i=0;i<50;i++)//¶ÁÈ¡50´ÎµÄADÊýֵȡÆäƽ¾ùÊý½ÏΪ׼ȷ
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//ת»»½áÊø±ê־λ
ad=ad+ADC_GetConversionValue(ADC1);//·µ»Ø×î½üÒ»´ÎADCx¹æÔò×éµÄת»»½á¹û
}
ad=ad/50;
TIM_SetCompare2(TIM3, ad*1439/4096);
// printf("ad=%fV\n",ad*3.3/4096);
// delay_ms(1000);
}
}Be careful Range shall be set for the first use of electric regulation
边栏推荐
- HarmonyOS访问数据库实例(3)--用ORM Bee测下HarmonyOS到底有多牛
- 二分查找数组下标
- HMS core discovery Episode 13 live broadcast Preview - building the real world in mobile games
- Latest development of jetpack compose
- Hydropower project construction scheme based on 3D GIS Development
- R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用exp函数、confint函数、coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比的置信区间
- R语言dplyr包select函数将dataframe数据中的指定数据列移动到dataframe数据列中的第一列(首列)
- Uninstall hero League
- R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、通过双样本t检验分析(双独立样本t检验)来判断倾向性评分匹配后样本中的所有协变量的平衡情况
- Selective sort method
猜你喜欢

【js】-【數組、棧、隊列、鏈錶基礎】-筆記

Mousse shares listed on Shenzhen Stock Exchange: becoming popular by mattress and "foreign old man", with a market value of 22.4 billion yuan
![[JS] - [tree] - learning notes](/img/62/de4fa2a7c5e52c461b8be4a884a395.png)
[JS] - [tree] - learning notes

InnoDB, the storage engine of MySQL Architecture Principle_ Redo log and binlog
![[JS] - [array application] - learning notes](/img/8a/808fde0cc86e0ec5e1f5558ba196b4.png)
[JS] - [array application] - learning notes

Spark's wide dependence and narrow dependence yyds dry goods inventory

Enterprise data leakage prevention solution sharing

idea创建模块提示已存在

都2022年了,你还不了解什么是性能测试?

Hydropower project construction scheme based on 3D GIS Development
随机推荐
golang convert json string to map
Yyds dry goods counting uses xshell to implement agent function
[JS] - [linked list - application] - learning notes
Ningde times will increase RMB 45billion: Hillhouse subscribes RMB 3billion and Zeng Yuqun still controls 23% of the equity
7-2 求解买股票问题
7-7 求解众数问题
[JS] - [tree] - learning notes
明天就是PMP考试了(6月25日),这些大家都了解了吗?
去处电脑桌面小箭头
[JS] - [stack, team - application] - learning notes
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、对匹配后的样本的不同分组对应的目标变量的均值进行Welch双样本t检验分析、双独立样本t检验
RT-thread使用rt-kprintf
Simpledateformat concrete classes for formatting and parsing dates
golang map clear
Websocket learning
[introduction to UVM== > episode_8] ~ sequence and sequencer, sequence hierarchy
Hyperledger Fabric 2. X dynamic update smart contract
What you must know about time series database!
js监听页面或元素scroll事件,滚动到底部或顶部