你好!
我正在尝试在我的 Nucleo64
STM32L476 板上的 PA8 引脚上输出 LSE 时钟。
我写的代码如下:
- #include
- #define RCC_BASE_ADDR 0x40021000UL
- #define RCC_CFGR_REG_OFFSET 0x08UL
- #define RCC_CFGR_REG_ADDR (RCC_BASE_ADDR + RCC_CFGR_REG_OFFSET )
- #define GPIOA_BASE_ADDR 0x48000000UL
- int main(void)
- {
- uint32_t *pRccCfgrReg = (uint32_t*) RCC_CFGR_REG_ADDR;
- //1. Configure the RCC_CFGR MCOSEL bit fields to select LSE as clock source: 0111: LSE clock selected
- *pRccCfgrReg &=~(0U << 27);
- *pRccCfgrReg |= (1U << 26);
- *pRccCfgrReg |= (1U << 25);
- *pRccCfgrReg |= (1U << 24);
- //2. Configure PA8 to AF0 mode to behave as MCO signal
- //a ) Enable the peripheral clock for GPIOA peripheral
- // RCC_AHB2ENR
- uint32_t *pRCCAhb2Enr = (uint32_t*)(RCC_BASE_ADDR + 0x4C);
- *pRCCAhb2Enr |= (1U << 0); //Enable GPIOA peripheral clock
- //b ) Configure the mode of GPIOA pin 8 as alternate function mode
- // GPIOx_MODER
- uint32_t *pGPIOAModeReg = (uint32_t*)(GPIOA_BASE_ADDR + 0x00UL);
- *pGPIOAModeReg &=~(1U << 16); // clear bit 16
- *pGPIOAModeReg |= (1U << 17); // set bit 17
- //c ) Configure the alternation function register to set the mode 0 for PA8
- // GPIOx_AFRH
- uint32_t *pGPIOAAltFunHighReg = (uint32_t*)(GPIOA_BASE_ADDR + 0x24);
- *pGPIOAAltFunHighReg &=~(1U << 0);
- *pGPIOAAltFunHighReg &=~(1U << 1);
- *pGPIOAAltFunHighReg &=~(1U << 2);
- *pGPIOAAltFunHighReg &=~(1U << 3);
- uint32_t *pRCC_BDCR = (uint32_t*)(RCC_BASE_ADDR + 0x90);
- // Turn on LSE clock (LSEON)
- *pRCC_BDCR |= (1U << 0);
- for (;;);
- }
任何人都可以看到一个明显的错误吗?我忘记了什么吗?我读到 LSE 时钟可能需要一些时间才能启动,我该如何改进我的代码来解决这个问题?
我观察到 LSERDY 位从未设置为 1,我怀疑这是问题所在。是什么导致这个位一直为0?