1 Description
Use STM8S internal clock (HSI);
PWM mode 2;
The duty cycle is 50% and the frequency is 2Hz (convenient for testing LED lights);
PD2 port is connected to an external LED light, and PD2 port outputs PWM wave;
System clock initialization is very important: CLK_CKDIVR |= 0x08;
2 Code
/*
TIM2_CH3 PWM
PD2 output
*/
#include
void CLK_init(void)
{
CLK_ICKR |= 0X01; //Enable internal high-speed clock HSI
CLK_CKDIVR |= 0x08; //16M internal RC is divided by 2 and the system clock is 8M
while(!(CLK_ICKR&0x02)); //HSI is ready
CLK_SWR=0xe1; //HSI is the main clock source
}
void Init_Tim2(void)
{
TIM2_CCMR3 |= 0X70; //Set the output comparison mode of timer 2 three channels (PD2)
TIM2_CCMR3 |= 0X04; //Output compare 3 preload enable
TIM2_CCER2 |= 0x03; //Channel 3 is enabled, low level is valid, and it is configured as output
//Initialize the clock divider to 1, that is, the clock frequency of the counter is Fmaster=8M/64=0.125MHZ
TIM2_PSCR = 0X06;
// Initialize the auto-load register to determine the frequency of the PWM square wave, Fpwm=0.125M/62500=2HZ
TIM2_ARRH = 62500/256;
TIM2_ARRL = 62500%256;
// Initialize the comparison register to determine the duty cycle of the PWM square wave: 5000/10000 = 50%
TIM2_CCR3H = 31250/256;
TIM2_CCR3L = 31250%256;
// Start counting; update interrupt disable
TIM2_CR1 |= 0x81;
//TIM2_IER |= 0x00;
}
void Init_GPIO(void)
{
/*Set to push-pull output, PD2 is connected to LED light*/
PD_DDR |= 0X04; //Set PD2 port to output mode
PD_CR1 |= 0X04; //Set PD2 port to push-pull output mode
PD_CR2 &= 0XFD;
PA_DDR |= 0X08; //Set PA3 port to output mode
PA_CR1 |= 0X08; //Set PA3 port to push-pull output mode
PA_CR2 |= 0XF7;
}
void main(void)
{
CLK_init();
Init_GPIO();
Init_Tim2();
while (1);
}
Previous article:STM8S---Independent button IO port setting and press event problem
Next article:STM8S---IO multiplexing configuration (STVP mode)
- 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
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- EEWORLD University ---- QEMU network programming video
- CircuitPython 8.0.0 Alpha 1 released
- Understanding Metastability
- Find component model
- Programming mode, realize activity status recognition and environmental data collection, and output through USB virtual serial port
- I3G4250D 3-axis digital output gyroscope data sheet, package, driver code
- We are just two materials away from making it 100% domestically made. Are there no domestic alternatives for USB connectors and MAX6369KA?
- Live streaming entrance is now open | Renesas Electronics R-Car Advanced Driver Assistance System Solution
- Today at 10:00 AM Live: ST's IO-Link-based condition monitoring and predictive maintenance solutions
- [TI Course] Questions about calculating the effective value of pfc inductor current