background
There is a project that needs to control the speed of a fan, using STM8S003F3P6 to output PWM control, here is a detailed record of the debugging record
schematic diagram
The schematic diagram is relatively simple, the motor interface CN3
Motor Interface Schematic
The location of the connection with the MCU pins is shown in the figure below
First of all, we need to understand the principle of the motor
Simply put, a motor is an electrical equipment that realizes the mutual conversion of electrical energy and mechanical energy.
Then the principle of DC motor that we often use is:
Electric magnetism: The energized wire will generate a magnetic field, that is, the electromagnetic induction rotating magnetic field to drive the rotor to rotate. The motor is composed of a stator and a rotor. One generates a rotating magnetic field and the other is a magnetic pole. The rotor (bearing) of the motor rotates.
This realizes the conversion of electrical energy -> magnetic energy -> mechanical energy
The following two figures can be more intuitively understood:
So we don't go into details about the motor, we just need to know that the essence of the motor is the coil or the inductance element
At the same time, we know that inductance has the effect of preventing sudden changes in current
About Motor HAS10227
I don’t know who it is. I chose a Japanese motor. The manuals are all in Japanese. It looks tired.
The corresponding pin is probably to control the VSP voltage to modulate the motor speed
That is, corresponding to CN3 label 5
Here is to use pwm to adjust the voltage, and then achieve the purpose of controlling the speed
software design
pwm initialization, use timer 1 to output pwm
void Tim1_PWM_Init(void)
{
TIM1_DeInit();
/* Time Base configuration */
/*
TIM1_Period = 4095
TIM1_Prescaler = 0
TIM1_CounterMode = TIM1_COUNTERMODE_UP
TIM1_RepetitionCounter = 0
*/
//TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, 4095, 0);
TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, 1600 - 1, 0);//10KHZ
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
/*
TIM1_OCMode = TIM1_OCMODE_PWM2
TIM1_OutputState = TIM1_OUTPUTSTATE_ENABLE
TIM1_OutputNState = TIM1_OUTPUTNSTATE_ENABLE
TIM1_Pulse = CCR1_Val
TIM1_OCPolarity = TIM1_OCPOLARITY_LOW
TIM1_OCNPolarity = TIM1_OCNPOLARITY_HIGH
TIM1_OCIdleState = TIM1_OCIDLESTATE_SET
TIM1_OCNIdleState = TIM1_OCIDLESTATE_RESET
*/
//TIM1_OC1Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
// CCR1_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
// TIM1_OCNIDLESTATE_RESET);
/*TIM1_Pulse = CCR2_Val*/
//TIM1_OC2Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE, CCR2_Val,
// TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
// TIM1_OCNIDLESTATE_RESET);
/*The TIM1 CCR3 register value is equal to 0x3FF, so the TIM1 Channel 3 and TIM1
Channel 3N generate a PWM signal with a frequency equal to 488.28 Hz
and a duty cycle equal to:
TIM1 Channel3 duty cycle = TIM1_CCR3 / ( TIM1_Period + 1) = 25%.*/
/*TIM1_Pulse = CCR3_Val*/
TIM1_OC3Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_ENABLE,
CCR3_Val, TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_HIGH, TIM1_OCIDLESTATE_SET,
TIM1_OCNIDLESTATE_RESET);
/*TIM1_Pulse = CCR4_Val*/
//TIM1_OC4Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, CCR4_Val, TIM1_OCPOLARITY_LOW,
// TIM1_OCIDLESTATE_SET);
/* TIM1 counter enable */
TIM1_Cmd(ENABLE);
/* TIM1 Main Output Enable */
TIM1_CtrlPWMOutputs(DISABLE);
}
It can be seen from the manual that PC3 is the control pin of PWM, which is also the pin of the schematic design just now.
To control the motor speed is to adjust the output of PWM, as shown in the following function
void setSpeed(void)
{
CCR3_Val = ((uint16_t)1120) - ((uint16_t) 112) * (5 - speed);//最大70%,每次递减7%
TIM1->CCR3H = (uint8_t)(CCR3_Val >> 8);
TIM1->CCR3L = (uint8_t)(CCR3_Val);
TIM1_CtrlPWMOutputs(ENABLE);
}