STM32 SystemCoreClock = 2097000U;是什么意思
时间: 2024-04-24 08:27:50 浏览: 32
在STM32微控制器中,SystemCoreClock是一个全局变量,用于存储系统时钟的频率。在这种情况下,2097000U表示系统时钟的频率为2,097,000赫兹(或2.097兆赫)。这个值通常是根据你的具体硬件配置和时钟设置来确定的。通过了解SystemCoreClock的值,你可以在编程中使用它来进行时间和延迟计算,以确保正确的时序和功能操作。
相关问题
SysTick_Config(SystemCoreClock / 1000000U
这是一个STM32的代码,用于配置系统定时器SysTick的时钟频率为系统时钟频率的1/1000000。SysTick是一个24位的计数器,每次计数器减1,当减到0时会产生一个SysTick中断。这个中断可以用来实现定时功能。在这段代码中,SystemCoreClock表示系统时钟频率,单位是Hz,除以1000000就可以得到每微秒的时钟周期数,然后将这个值作为参数调用SysTick_Config函数,就完成了系统定时器的配置。
stm32hal库超声波
stm32hal库可以用于控制超声波传感器。以下是一个基本的超声波测距例程:
1. 选择合适的GPIO引脚控制超声波模块的Trig口和Echo口,初始化这两个GPIO口。
2. 设置一个定时器,定时器的时钟频率需要根据实际情况选择。
3. 在Trig口发送一个10us的高电平脉冲。
4. 开启定时器计数,等待Echo口信号变为高电平,然后开始计时。
5. 等待Echo口信号变为低电平,计时器停止计数。
6. 根据超声波传播速度和计时器计数值计算距离。
以下是一个示例代码:
```
#include “stm32f4xx_hal.h”
#include “stdio.h”
#define TRIG_PIN GPIO_PIN_0
#define TRIG_PORT GPIOA
#define ECHO_PIN GPIO_PIN_1
#define ECHO_PORT GPIOA
#define TIMx TIM2
uint32_t u32Temp;
void delay_us(uint32_t nus)
{
uint32_t ticks = nus * (SystemCoreClock / 1000000);
while(ticks--);
}
void delay_ms(uint32_t nms)
{
uint32_t ticks = nms * (SystemCoreClock / 1000 / 10);
while(ticks--);
}
void send_pulse(void)
{
HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_SET);
delay_us(10);
HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_RESET);
}
float get_distance(void)
{
uint32_t u32Cnt = 0;
uint32_t u32Timeout = 0x7FFFFF;
while(!HAL_GPIO_ReadPin(ECHO_PORT, ECHO_PIN))
{
if(u32Timeout-- == 0) return 0.0f;
}
HAL_TIM_Base_Start(&htim2);
while(HAL_GPIO_ReadPin(ECHO_PORT, ECHO_PIN))
{
if(u32Timeout-- == 0) return 0.0f;
u32Cnt++;
}
HAL_TIM_Base_Stop(&htim2);
return (u32Cnt * 1.0f / 58.0f);
}
int main(void)
{
HAL_Init();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_TIM2_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = TRIG_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(TRIG_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = ECHO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(ECHO_PORT, &GPIO_InitStruct);
TIM_HandleTypeDef htim2 = {0};
htim2.Instance = TIMx;
htim2.Init.Period = 0xFFFF;
htim2.Init.Prescaler = SystemCoreClock / 1000000 - 1; // 1us的计数精度
htim2.Init.ClockDivision = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_Base_Init(&htim2);
while(1)
{
send_pulse();
u32Temp = get_distance();
printf(“distance: %d\n”, (int)u32Temp);
delay_ms(500);
}
}
```