#include "stm32f10x.h"
/*RCC clock configuration*/
void RCC_config()
{
ErrorStatus HSEStartUpStatus;
/* RCC registers are set to default configuration */
RCC_DeInit();
/* Turn on external high speed clock */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait for the external high-speed clock to stabilize*/
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Set HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* Set PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* Set PCLK1 = HCLK / 2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
// /* Set FLASH code delay */
// FLASH_SetLatency(FLASH_Latency_2);
// /* Enable prefetch cache */
// FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Set the PLL clock source to HSE multiplier 9 72MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait for PLL to stabilize */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/* Set PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait for the system clock source to switch to PLL */
while(RCC_GetSYSCLKSource() != 0x08);
}
}
/* GPIO configuration */
void GPIO_config()
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOA clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
/* Set the GPIOA clock to default parameters */
GPIO_DeInit(GPIOA);
/* Pull-down output */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
/* Pin 13 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
/* Output frequency 50MHz */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* Initialize GPIOA_0 */
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/* Timer configuration */
void TIMER_config()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
/* Enable TIM2 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Set the Timer 2 register to initial value */
TIM_DeInit(TIM2);
/* Set timer 2 to use internal clock */
TIM_InternalClockConfig(TIM2);
/* Prescaler value*/
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
/* Clock division */
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
/* count up */
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
/* Automatic reload value*/
TIM_TimeBaseStructure.TIM_Period = 1000 - 1;
/* Initialize timer 2 */
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Channel 1 */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
/* rising edge trigger */
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
/* Map to TI1 */
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
/* Frequency division*/
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
/* Filter */
TIM_ICInitStructure.TIM_ICFilter = 0x00;
/* Initialize input capture */
TIM_ICInit(TIM2, &TIM_ICInitStructure);
/* Clear interrupt flag */
TIM_ClearFlag(TIM2, TIM_IT_Update | TIM_IT_CC1);
/* Enable TIM2 interrupt */
TIM_ITConfig(TIM2, TIM_IT_Update | TIM_IT_CC1, ENABLE);
/* Start timer 2 */
TIM_Cmd(TIM2, ENABLE);
}
/* Abort placement */
void NVIC_config()
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Select interrupt group 1 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Select the interrupt channel of TIM2 */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
/* Set the preemptive interrupt priority to 0 */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
/* Set the responsive interrupt priority to 0 */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
/* Enable interrupt */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/* Interrupt initialization */
NVIC_Init(&NVIC_InitStructure);
}
/* Delay in milliseconds */
void delay_ms(uint16_t time)
{
uint16_t i = 0;
while(time--)
{
i = 12000;
while(i--);
}
}
/* Timer 2 interrupt vector */
void TIM2_IRQHandler(void)
{
static uint32_t capture = 0;
/* Input capture interrupt */
if(TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
/* Clear interrupt flag */
capture += TIM_GetCapture1(TIM2);
/* Clear the counter value */
TIM_SetCounter(TIM2, 0);
capture = 0;
}
/* Update interrupt */
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
capture += 1000;
}
TIM_ClearFlag(TIM2, TIM_IT_CC1 | TIM_IT_Update);
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 | TIM_IT_Update);
}
int main()
{
/* Clock configuration */
RCC_config();
/* GPIO configuration */
GPIO_config();
/* Interrupt placement*/
NVIC_config();
/* Timer 2 configuration */
TIMER_config();
while(1)
{
delay_ms(1000);
}
}
Previous article:[STM32] Input capture of general timer (Example: Input capture)
Next article:stm32-Serial port uses IDLE interrupt to accept variable length data method
Recommended ReadingLatest update time:2024-11-16 11:39
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- 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
- Design examples + a little error for new students to learn from
- Code Analysis of Event Kernel in RSL10 SDK (Part 2)
- Noise conditions and countermeasures in 5G communication environment
- A complete solution for testing, modeling and verification of GaN RF power devices
- Rental housing accidents happen frequently, how can we solve them thoroughly?
- [Chuanglong Technology Allwinner A40i Development Board] C and Python Development Experience
- How should ferrite beads be used and how should they be selected for gate drive circuits?
- TriQuint and RFMD have merged to form Qorvo, a new global leader in RF solutions.
- Summary of wifi7 popular science posts
- Blood oxygen saturation is detected by receiving the energy ratio of infrared light and red light, and then it can be calculated by adding an algorithm. Then the heart rate...