Enable OCx output through the combination of CCxE, CCxNE, MOE, OSSI and OSSR bits (TIMx_CCER and TIMx_BDTR registers). In fact, the MOE and AOE bits of the TIMx_BDTR register need to be set to 1. This is different from the normal configuration of PWM output. The code is as follows↓
/*******************************************************************
* Function:
* Author: Jahol Fan
* Parameters:
* Return value:
* Others: PA9 PWM output controls the speed, PA9's multiplexed function 1 is TIM1_CH2,
TIM1: 168MHZ
***********************************************************************/
void TIM1_PwmInit(u16 psc,u16 arr)
{
GPIO_Init(GPIOA,PIN9,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_PU);
GPIO_AFSet(GPIOA,9,1); //PA9, AF1
RCC->APB2ENR |= 1<<0;//Enable timer 1 clock
TIM1->CR1 |= 1<<7;//Use TIM1_ARR Shadow register of register
TIM1->CR1 &=~(1<<3);//Select timing mode: cyclic timing
TIM1->CR1 &=~(1<<2);//Select update event source: counter overflow or UG position 1 can generate update event
TIM1->CR1 &=~(1<<1);//Enable update event generation
TIM1->PSC = psc-1;//Set prescaler value
TIM1->ARR = arr-1;//Set ARR auto-reload value (upper limit).
TIM1->CCMR1 &=~(7<<12); //Clear
TIM1->CCMR1 |= 6<<12;//Set to PWM mode 1
TIM1->CCMR1 |=(1<<11);//Enable shadow register of TIM1_CCR2
TIM1->CCMR1 &=~(3<<8);//Timer channel is configured as output.
TIM1->CCER |=(1<<5);//Configure the effective level of the timer channel to be low level
TIM1->CCR2 = arr/2;//Set the value of CCR2.
TIM1->BDTR |= 1<<15;//Master output enable
TIM1->BDTR |= 1<<14;//MOE can be set to 1 by software, or automatically set to 1 when the next update event occurs (if the circuit breaker input is invalid)
//TIM1->BDTR |= 1<<10;
//TIM1->BDTR |= 1<<11;
TIM1->EGR |= 1<<0;//UG position 1, actively generate update events, purpose: initialize registers including updating shadow registers.
TIM1->SR = 0;//Clear the status flag
TIM1->CCER|=1<<4;//Turn on PWM output
TIM1->CR1 |= 1<<0;//Enable counter
}
Previous article:Based on STM32 serial port ring buffer queue processing mechanism - entry level (single byte)
Next article:What is the maximum size of an STM32 local variable array?
- Popular Resources
- Popular amplifiers
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
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Sandia Labs develops battery failure early warning technology to detect battery failures faster
- Ranking of installed capacity of smart driving suppliers from January to September 2024: Rise of independent manufacturers and strong growth of LiDAR market
- Industry first! Xiaopeng announces P7 car chip crowdfunding is completed: upgraded to Snapdragon 8295, fluency doubled
- P22-009_Butterfly E3106 Cord Board Solution
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- How to configure the pins used by LCD as GPIO?
- 【GD32E231_DIY】②DS1302 real-time clock module information
- Like this BOOST boost topology, if a lithium battery 4.2V & 1000mA is used, the load current is a momentary signal (no need to...
- [NXP Rapid IoT Review] + When connecting to a device via mobile app, the message "Unable to verify the device's hardware credentials" appears
- EEWORLD University Hall ---- Fundamentals of Control Engineering Guo Meifeng, Tsinghua University
- ASM330LHH automotive 6-axis inertial module data sheet, application notes, driver code, schematic library, PCB library
- Development board based on TI AM5728 (floating point dual DSP C66x + dual ARM Cortex-A15)
- Agitek High-tech Joint Electronics Laboratory fully supports the National Undergraduate Electronics Design Competition
- Can the microcontroller detect negative values if it is powered by a single power supply?
- Signals and Systems (3rd Edition)