时钟
用哪个外设就要开他对应的时钟
例子:RCC->APB2ENR |=(0x01<<3);//时钟
需要在APB2上开启对应的时钟
拓展:系统时钟如何配置
static void SetSysClockTo72(void)
{
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------/
/ 使能外部高速晶振 */
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
/* 等待高速晶振就绪 /
do
{
HSEStatus = RCC->CR & RCC_CR_HSERDY;
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if ((RCC->CR & RCC_CR_HSERDY) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if (HSEStatus == (uint32_t)0x01)//时钟开启成功
{
//单片机内部flash配置
/ Enable Prefetch Buffer /
FLASH->ACR |= FLASH_ACR_PRFTBE;
/ Flash 2 wait state /
FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
/ HCLK = SYSCLK /
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
/ PCLK2 = HCLK /
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
/ PCLK1 = HCLK /
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
/ 配置PLL时钟的倍频 /
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |
RCC_CFGR_PLLMULL));
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
/ 开启PLL时钟 /
RCC->CR |= RCC_CR_PLLON;
/ 等待PLL时钟开启 /
while((RCC->CR & RCC_CR_PLLRDY) == 0)
{
}
/ 选择PLL作为系统时钟 /
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
/ 等待时钟就绪 /
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
{
}
}
else
{ / If HSE fails to start-up, the application will have wrong clock
configuration. User can add here some code to deal with this error */
}
}
单片机如何实现延时?
NOP延时:空指令,在执行这个指令的时候,单片机啥也不干,等待一个时钟周期
单片机主频72Mhz
1个nop等待 1/72000000 秒 1/72000 ms 1/72us
define delay_1us() __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();#define delay_1us() __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
__nop();__nop();
void delay_us(uint32_t time)
{
while(time--)
{
delay_1us();
}
}
void delay_ms(uint32_t time)
{
while(time--)
{
delay_us(1000);
}
}
ST公司,为了降低功耗,默认外设的时钟是关闭的,需要使用外设时,第一步就是要先开启这个时钟
标签:__,CFGR,NOP,STM32,延时,时钟,nop,uint32,RCC From: https://www.cnblogs.com/lmkjava/p/17549572.html