提示:文章寫完后,目錄可以自動(dòng)生成,如何生成可參考右邊的幫助文檔
目錄
前言
1.實(shí)驗(yàn)現(xiàn)象
2.實(shí)驗(yàn)接線及原理圖
接線圖
原理圖?
電機(jī)接線圖
3.代碼部分
1.主函數(shù)?
main.c
2.按鍵部分?
?key.c
?key.h
pwm代碼?
?pwm.c
?pwm.h
電機(jī)驅(qū)動(dòng)?
?motor.c?
?motor.h
?OLED顯示
oled.c
oled.h?
編碼器捕獲部分
?encoder.c
?encoder.h
Tim2初始化
總結(jié)
前言
筆者使用的是JGB37-520減速直流電機(jī),使用stm32定時(shí)器輸出比較生成PWM控制電機(jī)輸出,使用編碼器接口對(duì)電機(jī)進(jìn)行測速,并通過OLED顯示PWM輸出占空比和電機(jī)轉(zhuǎn)速。如有錯(cuò)誤敬請(qǐng)大佬們斧正。
1.實(shí)驗(yàn)現(xiàn)象
直流減速電機(jī)輸出控制及測速
2.實(shí)驗(yàn)接線及原理圖
接線圖
原理圖?
?
?電機(jī)接線圖
3.代碼部分
1.主函數(shù)?
main.c
#include "sys.h"
#include "key.h"
#include "motor.h"
#include "encoder.h"
#include "oled.h"
#include "tim2.h"
int16_t Speed,Speed1,KeyNum,KeyCnt
int main(void)
{
Motor_Init();
OLED_Init();
Encoder_Init();
TIM2_Init();
KEY_Init();
OLED_ShowString(1,1,"Motor Speed:");//顯示占空比
OLED_ShowString(3,1,"Encoder Speed");//顯示電機(jī)每分鐘轉(zhuǎn)速
while(1)
{
KeyNum=Key();
if(KeyNum==1)
{
switch(KeyCnt)//按鍵按下電機(jī)速度切換
{
case 0:KeyCnt++;Speed = 10;break;
case 1:KeyCnt++;Speed = 20;break;
case 2:KeyCnt++;Speed = 30;break;
case 3:KeyCnt++;Speed = 40;break;
case 4:KeyCnt++;Speed = 50;break;
case 5:KeyCnt++;Speed = 60;break;
case 6:KeyCnt++;Speed = 70;break;
case 7:KeyCnt++;Speed = 80;break;
case 8:KeyCnt++;Speed = 90;break;
case 9:KeyCnt++;Speed = 100;break;
case 10:KeyCnt++;Speed = 90;break;
case 11:KeyCnt++;Speed = 80;break;
case 12:KeyCnt++;Speed = 70;break;
case 13:KeyCnt++;Speed = 60;break;
case 14:KeyCnt++;Speed = 50;break;
case 15:KeyCnt++;Speed = 40;break;
case 16:KeyCnt++;Speed = 30;break;
case 17:KeyCnt++;Speed = 20;break;
case 18:KeyCnt++;Speed = 10;break;
case 19:KeyCnt=0;Speed = 0;break;
}
}
Motor_SetSpeed(Speed);
OLED_ShowSignedNum(2,1,Speed,3);
OLED_ShowSignedNum(4,1,Speed1/44,5);//電機(jī)一圈11個(gè)脈沖,每個(gè)脈沖有四次邊沿觸發(fā)
}
}
void TIM2_IRQHandler(void)
{
static uint16_t Tim2Count0,Tim2Count1;
if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET)
{
Tim2Count0++;
if(Tim2Count0>=1000)//定時(shí)器2每隔1s刷新一次脈沖個(gè)數(shù)
{
Speed1=Encoder_Get();
Tim2Count0=0;
}
Tim2Count1++;
if(Tim2Count1>=20)//定時(shí)器2每隔20ms掃描一次按鍵消抖
{
Key_Loop();
Tim2Count1=0;
}
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
}
2.按鍵部分?
?key.c
按鍵部分使用定時(shí)器進(jìn)行消抖,不會(huì)影響到主函數(shù)代碼的執(zhí)行。
#include "key.h" // Device header
uint8_t Key_KeyNumber;
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;//定義一個(gè)GPIO初始化結(jié)構(gòu)體GPIO_InitStruct
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
uint8_t Key(void)
{
uint8_t Temp=0;
Temp=Key_KeyNumber;
Key_KeyNumber=0;
return Temp;
}
uint8_t Key_GetState(void)
{
uint8_t KeyNumber=0;
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0){KeyNumber=1;}
else KeyNumber=0;
return KeyNumber;
}
void Key_Loop(void)
{
static uint8_t NowState,LastState;
LastState=NowState; //按鍵狀態(tài)更新
NowState=Key_GetState(); //獲取當(dāng)前按鍵狀態(tài)
//如果上個(gè)時(shí)間點(diǎn)按鍵按下,這個(gè)時(shí)間點(diǎn)未按下,則是松手瞬間,以此避免消抖和松手檢測
if(LastState==1 && NowState==0)
{
Key_KeyNumber=1;
}
}
void TIM3_IRQHandler(void)
{
static uint16_t Tim3Count0;
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)
{
Tim3Count0++;
if(Tim3Count0>=10)
{
Key_Loop();
Tim3Count0=0;
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
}
key.h
#ifndef __KEY_H
#define __KEY_H
#include "sys.h"
#define KEY1 PAin(4) // PA4
void KEY_Init(void);
void Key_Loop(void);
uint8_t Key_GetState(void);
uint8_t Key(void);
#endif
3.pwm代碼?
?pwm.c
#include "pwm.h"
void PWM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
TIM_OCStructInit(&TIM_OCInitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_InternalClockConfig(TIM2);
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period=100-1;//20khz周期
TIM_TimeBaseInitStructure.TIM_Prescaler=36-1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=0;
TIM_OC3Init(TIM2,&TIM_OCInitStructure);
TIM_Cmd(TIM2,ENABLE);
}
void PWM_SetCompare3(uint16_t Compare)
{
TIM_SetCompare3(TIM2, Compare);
}
pwm.h
#ifndef __PWM_H
#define __PWM_H
#include "sys.h"
void PWM_Init(void);
void PWM_SetCompare3(uint16_t Compare);
#endif
4.電機(jī)驅(qū)動(dòng)?
motor.c?
#include "motor.h"
#include "pwm.h"
void Motor_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PB,PE端口時(shí)鐘
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5; //Motor 方向控制腳
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度為50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure);
PWM_Init();
}
void Motor_SetSpeed(int8_t Speed)
{
if (Speed >= 0)
{
GPIO_SetBits(GPIOA, GPIO_Pin_4);
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
PWM_SetCompare3(Speed/10);
}
else
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
GPIO_SetBits(GPIOA, GPIO_Pin_5);
PWM_SetCompare3(-Speed/10);
}
}
?motor.h
#ifndef __Motor_H
#define __Motor_H
#include "sys.h"
#define Motor_Foreward PAout(4)
#define Motor_Reverse PAout(5)
void Motor_Init(void);
void Motor_SetSpeed(int8_t Speed);
#endif
5. OLED顯示
oled.c
#include "stm32f10x.h"
#include "oled.h"
#include "OLED_Font.h"
/*引腳配置*/
#define OLED_W_SCL(x) GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))
#define OLED_W_SDA(x) GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))
/*引腳初始化*/
void OLED_I2C_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);
OLED_W_SCL(1);
OLED_W_SDA(1);
}
/**
* @brief I2C開始
* @param 無
* @retval 無
*/
void OLED_I2C_Start(void)
{
OLED_W_SDA(1);
OLED_W_SCL(1);
OLED_W_SDA(0);
OLED_W_SCL(0);
}
/**
* @brief I2C停止
* @param 無
* @retval 無
*/
void OLED_I2C_Stop(void)
{
OLED_W_SDA(0);
OLED_W_SCL(1);
OLED_W_SDA(1);
}
/**
* @brief I2C發(fā)送一個(gè)字節(jié)
* @param Byte 要發(fā)送的一個(gè)字節(jié)
* @retval 無
*/
void OLED_I2C_SendByte(uint8_t Byte)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
OLED_W_SDA(Byte & (0x80 >> i));
OLED_W_SCL(1);
OLED_W_SCL(0);
}
OLED_W_SCL(1); //額外的一個(gè)時(shí)鐘,不處理應(yīng)答信號(hào)
OLED_W_SCL(0);
}
/**
* @brief OLED寫命令
* @param Command 要寫入的命令
* @retval 無
*/
void OLED_WriteCommand(uint8_t Command)
{
OLED_I2C_Start();
OLED_I2C_SendByte(0x78); //從機(jī)地址
OLED_I2C_SendByte(0x00); //寫命令
OLED_I2C_SendByte(Command);
OLED_I2C_Stop();
}
/**
* @brief OLED寫數(shù)據(jù)
* @param Data 要寫入的數(shù)據(jù)
* @retval 無
*/
void OLED_WriteData(uint8_t Data)
{
OLED_I2C_Start();
OLED_I2C_SendByte(0x78); //從機(jī)地址
OLED_I2C_SendByte(0x40); //寫數(shù)據(jù)
OLED_I2C_SendByte(Data);
OLED_I2C_Stop();
}
/**
* @brief OLED設(shè)置光標(biāo)位置
* @param Y 以左上角為原點(diǎn),向下方向的坐標(biāo),范圍:0~7
* @param X 以左上角為原點(diǎn),向右方向的坐標(biāo),范圍:0~127
* @retval 無
*/
void OLED_SetCursor(uint8_t Y, uint8_t X)
{
OLED_WriteCommand(0xB0 | Y); //設(shè)置Y位置
OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //設(shè)置X位置低4位
OLED_WriteCommand(0x00 | (X & 0x0F)); //設(shè)置X位置高4位
}
/**
* @brief OLED清屏
* @param 無
* @retval 無
*/
void OLED_Clear(void)
{
uint8_t i, j;
for (j = 0; j < 8; j++)
{
OLED_SetCursor(j, 0);
for(i = 0; i < 128; i++)
{
OLED_WriteData(0x00);
}
}
}
/**
* @brief OLED顯示一個(gè)字符
* @param Line 行位置,范圍:1~4
* @param Column 列位置,范圍:1~16
* @param Char 要顯示的一個(gè)字符,范圍:ASCII可見字符
* @retval 無
*/
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
{
uint8_t i;
OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //設(shè)置光標(biāo)位置在上半部分
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_F8x16[Char - ' '][i]); //顯示上半部分內(nèi)容
}
OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //設(shè)置光標(biāo)位置在下半部分
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); //顯示下半部分內(nèi)容
}
}
/**
* @brief OLED顯示字符串
* @param Line 起始行位置,范圍:1~4
* @param Column 起始列位置,范圍:1~16
* @param String 要顯示的字符串,范圍:ASCII可見字符
* @retval 無
*/
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
{
uint8_t i;
for (i = 0; String[i] != '\0'; i++)
{
OLED_ShowChar(Line, Column + i, String[i]);
}
}
/**
* @brief OLED次方函數(shù)
* @retval 返回值等于X的Y次方
*/
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
uint32_t Result = 1;
while (Y--)
{
Result *= X;
}
return Result;
}
/**
* @brief OLED顯示數(shù)字(十進(jìn)制,正數(shù))
* @param Line 起始行位置,范圍:1~4
* @param Column 起始列位置,范圍:1~16
* @param Number 要顯示的數(shù)字,范圍:0~4294967295
* @param Length 要顯示數(shù)字的長度,范圍:1~10
* @retval 無
*/
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i;
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
}
}
/**
* @brief OLED顯示數(shù)字(十進(jìn)制,帶符號(hào)數(shù))
* @param Line 起始行位置,范圍:1~4
* @param Column 起始列位置,范圍:1~16
* @param Number 要顯示的數(shù)字,范圍:-2147483648~2147483647
* @param Length 要顯示數(shù)字的長度,范圍:1~10
* @retval 無
*/
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
{
uint8_t i;
uint32_t Number1;
if (Number >= 0)
{
OLED_ShowChar(Line, Column, '+');
Number1 = Number;
}
else
{
OLED_ShowChar(Line, Column, '-');
Number1 = -Number;
}
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
}
}
/**
* @brief OLED顯示數(shù)字(十六進(jìn)制,正數(shù))
* @param Line 起始行位置,范圍:1~4
* @param Column 起始列位置,范圍:1~16
* @param Number 要顯示的數(shù)字,范圍:0~0xFFFFFFFF
* @param Length 要顯示數(shù)字的長度,范圍:1~8
* @retval 無
*/
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i, SingleNumber;
for (i = 0; i < Length; i++)
{
SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
if (SingleNumber < 10)
{
OLED_ShowChar(Line, Column + i, SingleNumber + '0');
}
else
{
OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
}
}
}
/**
* @brief OLED顯示數(shù)字(二進(jìn)制,正數(shù))
* @param Line 起始行位置,范圍:1~4
* @param Column 起始列位置,范圍:1~16
* @param Number 要顯示的數(shù)字,范圍:0~1111 1111 1111 1111
* @param Length 要顯示數(shù)字的長度,范圍:1~16
* @retval 無
*/
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i;
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
}
}
/**
* @brief OLED初始化
* @param 無
* @retval 無
*/
void OLED_Init(void)
{
uint32_t i, j;
for (i = 0; i < 1000; i++) //上電延時(shí)
{
for (j = 0; j < 1000; j++);
}
OLED_I2C_Init(); //端口初始化
OLED_WriteCommand(0xAE); //關(guān)閉顯示
OLED_WriteCommand(0xD5); //設(shè)置顯示時(shí)鐘分頻比/振蕩器頻率
OLED_WriteCommand(0x80);
OLED_WriteCommand(0xA8); //設(shè)置多路復(fù)用率
OLED_WriteCommand(0x3F);
OLED_WriteCommand(0xD3); //設(shè)置顯示偏移
OLED_WriteCommand(0x00);
OLED_WriteCommand(0x40); //設(shè)置顯示開始行
OLED_WriteCommand(0xA1); //設(shè)置左右方向,0xA1正常 0xA0左右反置
OLED_WriteCommand(0xC8); //設(shè)置上下方向,0xC8正常 0xC0上下反置
OLED_WriteCommand(0xDA); //設(shè)置COM引腳硬件配置
OLED_WriteCommand(0x12);
OLED_WriteCommand(0x81); //設(shè)置對(duì)比度控制
OLED_WriteCommand(0xCF);
OLED_WriteCommand(0xD9); //設(shè)置預(yù)充電周期
OLED_WriteCommand(0xF1);
OLED_WriteCommand(0xDB); //設(shè)置VCOMH取消選擇級(jí)別
OLED_WriteCommand(0x30);
OLED_WriteCommand(0xA4); //設(shè)置整個(gè)顯示打開/關(guān)閉
OLED_WriteCommand(0xA6); //設(shè)置正常/倒轉(zhuǎn)顯示
OLED_WriteCommand(0x8D); //設(shè)置充電泵
OLED_WriteCommand(0x14);
OLED_WriteCommand(0xAF); //開啟顯示
OLED_Clear(); //OLED清屏
}
oled.h?
#ifndef __OLED_H
#define __OLED_H
void OLED_Init(void);
void OLED_Clear(void);
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
#endif
6.編碼器捕獲部分
encoder.c
#include "encoder.h"
void Encoder_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_Period=65536-1;
TIM_TimeBaseInitStructure.TIM_Prescaler=1-1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter=0xf;
TIM_ICInit(TIM3,&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_2;
TIM_ICInitStructure.TIM_ICFilter=0xf;
TIM_ICInit(TIM3,&TIM_ICInitStructure);
TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);
TIM_Cmd(TIM3,ENABLE);
}
int16_t Encoder_Get(void)
{
int16_t Temp;
Temp=TIM_GetCounter(TIM3);
TIM_SetCounter(TIM3,0);
return Temp;
}
?encoder.h
#ifndef __ENCODER_H
#define __ENCODER_H
#include "sys.h"
void Encoder_Init(void);
int16_t Encoder_Get(void);
#endif
7.Tim2初始化
?tim2.c
#include "tim2.h"
void TIM2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_InternalClockConfig(TIM2);
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period=10-1;
TIM_TimeBaseInitStructure.TIM_Prescaler=7200-1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_ClearFlag(TIM2,TIM_IT_Update);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2,ENABLE);
}
總結(jié)
本文僅僅簡單介紹了使用STM32定時(shí)器輸出比較功能產(chǎn)生PWM波,控制電機(jī)運(yùn)動(dòng),其次是使用定時(shí)器的輸入捕獲功能對(duì)直流電機(jī)進(jìn)行測速。
通過本次學(xué)習(xí)對(duì)于直流減速電機(jī)、霍爾編碼器、定時(shí)器輸入捕獲和輸出比較功能有了一定的了解。文章來源:http://www.zghlxwxcb.cn/news/detail-502673.html
本文有不足之處還請(qǐng)大佬指正。文章來源地址http://www.zghlxwxcb.cn/news/detail-502673.html
到了這里,關(guān)于【32單片機(jī)學(xué)習(xí)】(3)霍爾編碼器減速直流電機(jī)控制及測速的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!