void SystemInit(void) { u32 timeout; /* Enable HSI */ RCC->CR = 0x00000001; RCC->CFGR &= (uint32_t)0xF8FFB80C; timeout = 2000; while((!(RCC->CR & 0x00000002)) && (timeout--)); RCC->CFGR2 = RCC_CFGR_PPRE_DIV1; /* Enable HSE */ RCC->CR |= 0x00010000; timeout = 2000; while((!(RCC->CR & 0x00020000)) && (timeout--)); /* Enable Prefetch Buffer and set Flash Latency */ FLASH->ACR |= FLASH_ACR_PRFTBE;// | FLASH_ACR_LATENCY; /* HCLK = SYSCLK */ RCC->CFGR |= (u32)RCC_CFGR_HPRE_DIV1; /* PCLK = HCLK */ RCC->CFGR |= (u32)RCC_CFGR_PPRE_DIV1; /* PLL configuration = HSE * 2 = 16 MHz */ RCC->CFGR &= (u32)((u32)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMUL)); RCC->CFGR |= (u32)(RCC_CFGR_PLLSRC_HSE_PREDIV | RCC_CFGR_PPRE_DIV1 | RCC_CFGR_PLLMUL2); /* Enable PLL */ RCC->CR |= RCC_CR_PLLON; /* Wait till PLL is ready */ while((RCC->CR & RCC_CR_PLLRDY) == 0) { } /* Select PLL as system clock source */ RCC->CFGR &= (u32)((u32)~(RCC_CFGR_SW)); RCC->CFGR |= (u32)RCC_CFGR_SW_PLL; /* Wait till PLL is used as system clock source */ while ((RCC->CFGR & (u32)RCC_CFGR_SWS) != (u32)RCC_CFGR_SWS_PLL) { } }
时间: 2023-02-09 10:35:14 浏览: 109
这段代码中,`void SystemInit(void)`是一个函数的声明,函数名为`SystemInit`。这个函数没有参数,返回值为`void`。
在函数体中,第一行声明了一个变量`timeout`,这个变量是一个无符号整型(`u32`)。然后是一个结构体指针的成员赋值,`RCC->CR`被赋值为`0x00000001`。接下来,又有一个结构体指针的成员赋值,`RCC->CFGR`被赋值为一个未知的值。
相关问题
解释代码void LedOn(GPIO_Module* GPIOx, uint16_t Pin) { GPIOx->PBSC = Pin; } /** * @brief Turns selected Led Off. * @param GPIOx x can be A to G to select the GPIO port. * @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15. */ void LedOff(GPIO_Module* GPIOx, uint16_t Pin) { GPIOx->PBC = Pin; } /** * @brief Turns selected Led on or off. * @param GPIOx x can be A to G to select the GPIO port. * @param Pin This parameter can be one of the following values: * @arg GPIO_PIN_0~GPIO_PIN_15: set related pin on * @arg (GPIO_PIN_0<<16)~(GPIO_PIN_15<<16): clear related pin off */ void LedOnOff(GPIO_Module* GPIOx, uint32_t Pin) { GPIOx->PBSC = Pin; } /** * @brief Toggles the selected Led. * @param GPIOx x can be A to G to select the GPIO port. * @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15. */ void LedBlink(GPIO_Module* GPIOx, uint16_t Pin) { GPIOx->POD ^= Pin; } /** * @brief Assert failed function by user. * @param file The name of the call that failed. * @param line The source line number of the call that failed. */ #ifdef USE_FULL_ASSERT void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line) { while (1) { } } #endif // USE_FULL_ASSERT /** * @brief Main program. */ int main(void) { /*SystemInit() function has been called by startup file startup_n32g45x.s*/ /* Initialize Led1~Led5 as output pushpull mode*/ LedInit(PORT_GROUP1, LED1_PIN | LED2_PIN); LedInit(PORT_GROUP2, LED3_PIN | LED4_PIN | LED5_PIN); /*Turn on Led1*/ LedOn(PORT_GROUP1, LED1_PIN); while (1) { /*LED1_PORT and LED2_PORT are the same port group.Enable Led2 blink and not effect Led1 by Exclusive-OR * operation.*/ LedBlink(PORT_GROUP1, LED2_PIN); /*LED3_PORT, LED4_PORT and LED5_PORT are the same port group.*/ /*Turn Led4 and Led5 off and not effect other ports by PBC register,correspond to * PORT_GROUP2->POD&=~(LED4_PIN|LED5_PIN);*/ LedOff(PORT_GROUP2, LED4_PIN | LED5_PIN); /* Insert delay */ Delay(0x28FFFF); /*Turn Led4 and Led5 on,turn Led3 off and not effect other ports by PBSC register,correspond to * PORT_GROUP2->POD&=~(LED3_PIN),then PORT_GROUP2->POD|=(LED4_PIN|LED5_PIN);*/ LedOnOff(PORT_GROUP2, (LED3_PIN << 16) | LED4_PIN | LED5_PIN); /* Insert delay */ Delay(0x28FFFF); /*Turn on Led3*/ LedOn(PORT_GROUP2, LED3_PIN); /* Insert delay */ Delay(0x28FFFF); } }
这段代码是一个LED控制的示例程序。具体解释如下:
1. `void LedOn(GPIO_Module* GPIOx, uint16_t Pin)`:该函数用于将指定的LED打开,通过设置GPIO相应的引脚控制寄存器来实现。传入参数`GPIOx`表示GPIO模块,`Pin`表示要打开的LED引脚。
2. `void LedOff(GPIO_Module* GPIOx, uint16_t Pin)`:该函数用于将指定的LED关闭,通过清除GPIO相应的引脚控制寄存器来实现。传入参数`GPIOx`表示GPIO模块,`Pin`表示要关闭的LED引脚。
3. `void LedOnOff(GPIO_Module* GPIOx, uint32_t Pin)`:该函数用于将指定的LED打开或关闭,通过设置或清除GPIO相应的引脚控制寄存器来实现。传入参数`GPIOx`表示GPIO模块,`Pin`表示要打开或关闭的LED引脚。
4. `void LedBlink(GPIO_Module* GPIOx, uint16_t Pin)`:该函数用于让指定的LED闪烁,通过对GPIO相应引脚控制寄存器进行异或操作来实现。传入参数`GPIOx`表示GPIO模块,`Pin`表示要闪烁的LED引脚。
5. `#ifdef USE_FULL_ASSERT` 和 `#endif // USE_FULL_ASSERT`:这是一个用于断言失败处理的代码段,当宏`USE_FULL_ASSERT`被定义时,将会调用`assert_failed()`函数。
6. `void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)`:断言失败时调用的函数,当断言失败时,该函数会进入一个死循环。
7. `int main(void)`:主函数入口。
8. `LedInit(PORT_GROUP1, LED1_PIN | LED2_PIN);` 和 `LedInit(PORT_GROUP2, LED3_PIN | LED4_PIN | LED5_PIN);`:初始化LED引脚,将LED引脚配置为输出模式。
9. `LedOn(PORT_GROUP1, LED1_PIN);`:打开LED1。
10. `LedBlink(PORT_GROUP1, LED2_PIN);`:让LED2闪烁。
11. `LedOff(PORT_GROUP2, LED4_PIN | LED5_PIN);`:关闭LED4和LED5。
12. `Delay(0x28FFFF);`:延时一段时间。
13. `LedOnOff(PORT_GROUP2, (LED3_PIN << 16) | LED4_PIN | LED5_PIN);`:打开LED4和LED5,关闭LED3。
14. `Delay(0x28FFFF);`:延时一段时间。
15. `LedOn(PORT_GROUP2, LED3_PIN);`:打开LED3。
16. `Delay(0x28FFFF);`:延时一段时间。
代码的主要作用是控制多个LED的亮灭,并通过延时函数实现闪烁效果。具体的LED引脚和GPIO模块的配置需要根据具体的硬件平台来设置。
cortex_M3处理器初始化使能代码
以下是 Cortex-M3 处理器初始化使能代码的示例:
```c
#include "stm32f10x.h"
void SystemInit(void)
{
/* Reset the RCC clock configuration to the default reset state */
/* Set HSION bit */
RCC->CR |= (uint32_t)0x00000001;
/* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
RCC->CFGR &= (uint32_t)0xF8FF0000;
/* Reset HSEON, CSSON and PLLON bits */
RCC->CR &= (uint32_t)0xFEF6FFFF;
/* Reset HSEBYP bit */
RCC->CR &= (uint32_t)0xFFFBFFFF;
/* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
RCC->CFGR &= (uint32_t)0xFF80FFFF;
/* Disable all interrupts and clear pending flags */
RCC->CIR = 0x009F0000;
/* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
/* PLL configuration */
/* PLLCLK = HSE * PLLMUL */
/* SYSCLK = PLLCLK / HCLK_DIV */
RCC->CFGR |= (uint32_t)0x001D0400;
RCC->CFGR2 &= (uint32_t)0xFFFFFFF8;
RCC->CFGR2 |= (uint32_t)0x00000002;
/* Enable HSE */
RCC->CR |= RCC_CR_HSEON;
/* Wait till HSE is ready */
while ((RCC->CR & RCC_CR_HSERDY) == 0);
/* Enable PLL */
RCC->CR |= RCC_CR_PLLON;
/* Wait till PLL is ready */
while ((RCC->CR & RCC_CR_PLLRDY) == 0);
/* Select PLL as system clock source */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08);
}
```
这段代码是针对STM32F10x系列微控制器的,其中包含了时钟配置、PLL配置、中断配置等初始化操作。可以根据需要进行修改和适配。