environment:
Host: XP
Development environment: MDK4.23
MCU:STM32F103CBT6
illustrate:
Use the internal 8M crystal oscillator, multiply it to 64M to supply the TIM3 timer, and generate 640K, 50% square wave on PA6 (channel 1)
source code:
Initialize the clock:
//Initialize RCC clock
void init_rcc(void)
{
//Reset the peripheral RCC register to the default value
RCC_DeInit();
//Internal crystal oscillator enabled
RCC_HSICmd(ENABLE);
//Wait for the job to stabilize
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
//LSI startup, providing watchdog clock
RCC_LSICmd(ENABLE); //Enable LSI
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY)==RESET); //Wait until LSI is stable
if(1)
{
// Enable prefetch cache
FLASH_PrefetchBufferCmd( FLASH_PrefetchBuffer_Enable );
//Set the number of FLASH memory delay clock cycles. (Set to delay 2 clock cycles)
FLASH_SetLatency( FLASH_Latency_2 );
//Set the AHB clock (HCLK) (set to = system clock at this time)
RCC_HCLKConfig( RCC_SYSCLK_Div1 );
//Set the AHB clock (HCLK2) of SDIO (set to = HCLK at this time), 64M
RCC_PCLK2Config( RCC_HCLK_Div1 );
//Set APB1 peripheral clock (HCLK1) (set to = HCLK/2 at this time), 32M
RCC_PCLK1Config( RCC_HCLK_Div2 );
//ADCCLK = PCLK2/8,1M
RCC_ADCCLKConfig( RCC_PCLK2_Div8 );
//PLL clock source and frequency multiplier, here set to 64MHz
RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_16);
// Enable PLL
RCC_PLLCmd(ENABLE);
//Check if PLL is ready
while( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET ) ;
//Set PLL as system clock
RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK ) ;
//Check if the system clock source is PLL
while( RCC_GetSYSCLKSource() != 0x08 ) ;
}
}
Initialize IO ports and timers:
//Set IO port
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Set timer 3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//Reset the Timer to its default value
TIM_DeInit(TIM3);
//Use the internal clock to provide the clock source for TIM3
TIM_InternalClockConfig(TIM3);
timInitStruct.TIM_ClockDivision = TIM_CKD_DIV2;
timInitStruct.TIM_Prescaler = 0; //Counting frequency is 64M
timInitStruct.TIM_CounterMode = TIM_CounterMode_Up; //Count up
timInitStruct.TIM_RepetitionCounter = 0;
timInitStruct.TIM_Period = 100; //This value is actually TIMX->ARR, and can be reset when the delay starts
TIM_TimeBaseInit(TIM3, &timInitStruct);
//Set PWM output
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 50;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
//ARR preload buffer enable
TIM_ARRPreloadConfig(TIM3, ENABLE);
//Start the timer
TIM_Cmd(TIM3, ENABLE);
Output waveform:
Previous article:STM32 collects multi-channel AD through DMA
Next article:STM32 standby mode wake-up test and independent watchdog test
Recommended ReadingLatest update time:2024-11-15 15:57
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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- 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
- X-NUCLEO-IKS01A3 sensor test based on STM32F401RE development board 2 LPS22HH pressure sensor
- 【FAQ】Enabling secure authentication with TrustFlex secure elements and Microsoft Azure | Microchip Seminar
- USB download cable driver installation.pdf
- Allegro Ali Dog Drawing Board. I haven't drawn on the drawing board for a long time.
- BMW Night Vision (Infrared Thermal Imaging) Camera Disassembly
- stm32 usb cdc communication
- [ATmega4809 Curiosity Nano Review] Unboxing
- How to burn MSBL format firmware
- Application of large color serial port screen in non-contact infrared thermometer
- The Hardships of Engineers——Comics | How to drive an engineer crazy?