First, configure the IO pins:
GPIO_InitTypeDef GPIO_InitStructure;
/* PA8 is set as functional pin (PWM) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*PB13 is set to reverse polarity output of PWM*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*Open clock PWM and GPIO*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/*Configure TIM1*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
void Tim1_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM1); //Reset to default values
/*TIM1 clock configuration*/
TIM_TimeBaseStructure.TIM_Prescaler = 4000; //Prescaler (clock division) 72M/4000=18K
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Count up
TIM_TimeBaseStructure.TIM_Period = 144; //Load value 18k/144=125hz, which means that 144 added up will be full
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Set the clock division, don't know how to ignore it
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0; //Period counter value, don't know how to ignore it
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); //Initialize the time base unit of TIMx
/* Channel 1 Configuration in PWM mode Channel 1 PWM */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM mode 2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //Forward channel is valid PA8
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //Reverse channel is also valid PB13
TIM_OCInitStructure.TIM_Pulse = 40; //Duty time 144 has 40 time high, complementary output is just the opposite
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Output polarity
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; //Complementary end polarity
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //Ignore the non-working state in idle state
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //Ignore it for now
TIM_OC1Init(TIM1,&TIM_OCInitStructure); //Initialize peripheral TIMx channel 1 Here the 2.0 library is TIM_OCInit
/* TIM1 counter enable start timer */
TIM_Cmd(TIM1,ENABLE);
/* TIM1 Main Output Enable enable the main output of TIM1 peripheral */
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
//Set capture register 1
void SetT1Pwm1(u16 pulse)
{
TIM1->CCR1=pulse;
}
/*Operation register changes duty time*/
/****************************************************** *************************************************** ******************
The timer channels 1 to 4 of TIM1 are PB8, PA9, PA10, and PA11 respectively, and the complementary outputs are PB13, PB14, and PB15 respectively.
Discontinue PB12.
If the output and complementary output polarity are the same, the output is high and the complementary output is low. As for the difference between PWM mode 1 and mode 2
In the following figure:
This is mode 1. Green is output and yellow is complementary.
*************************************************** ***********************************************
The above picture shows the situation of mode 2, which is exactly the opposite of mode 1. Among the 144, 40 are high and the complementary 40 are low.
*************************************************** ***********************************************/
//Add some keyboard scanning in MAIN to change the duty cycle
/************************************************************
**Experiment name: PWM
**Function: PA8 generates PWM output. PA8 is the IO that drives LED1 and the motor.
By pressing the UP or DOWN key, the duty cycle can be changed, so that ED1 and the small motor can be changed
. **Note: LED is low effective, and the motor is high effective, so the motor speed reaches the highest when all LEDs are off.
**Author: Electronic Cabbage
************************************************************/
#include "STM32Lib\\stm32f10x.h"
#include "hal.h"
extern void SetT1Pwm1(u16 pulse);
int main(void)
{
u16 pulse=40;
ChipHalInit(); //Initialize on-chip hardware
ChipOutHalInit(); //Initialize off-chip hardware
for(;;)
{
if(GET_UP())
{
while(GET_UP());
if(pulse<=144)
{
pulse+=5;
SetT1Pwm1(pulse);
}
}
if(GET_DOWN())
{
while(GET_DOWN());
if(pulse>30)
{
pulse-=5;
SetT1Pwm1(pulse);
}
}
}
}
/****************************************************** ***************************************/
Here are two more minimum system boards
/****************************************************** ***************************************/
All you need is a board like this, a little time and motivation to make this journey work.
Previous article:STM32 I2C hard usage
Next article:Unique ID code of stm32
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Summary of BlueNRG series tutorials, documents, training materials and FAQs (continuously updated)
- Dahua Black Body Ultra-Detailed Disassembly
- TypeC and Micro charging head two-choice circuit. When TYPEC and MICRO charging heads are inserted at the same time, only MICRO can charge...
- A table to understand the difference between 5G and Wi-Fi 6
- MSP430 Application Tips 4: How to Create an MSP430Ware Project
- OC5021B-OC5020B-OC5022B-OC5028B-LED car light solution (supports PWM dimming or linear dimming)
- C2000 built-in comparator error sources and correction methods--F28004x, F2807x, F2837x
- Problems with burning programs
- Common problems and countermeasures for CCS commissioning
- Analysis of the working principle of anti-interference filter