版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010160335/article/details/81114605
目录
[TOC]
STM32F030的PF0、PF1作为普通IO使用时无法正常输出高低电平
如下配置无法正常运行
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIO Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
/* Configure IO in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_SetBits(GPIOF, GPIO_Pin_1);
原因:
PF0,PF1在上电初始化过程中默认打开HSE时钟,这两个引脚作为晶振信号输入。
上电
—>SystemInit()
—>SetSysClock()
—>RCC->CR |= ((uint32_t)RCC_CR_HSEON)/* Enable HSE */
解决办法:
1.在时钟初始化时增加RCC->CR &= ~((uint32_t)RCC_CR_HSEON);
2.在PF0、PF1初始化时增加
GPIO_InitTypeDef GPIO_InitStructure;
/*STM32F030使用PF0 PF1,需要关闭HSE时钟*/
RCC->CR &= ~((uint32_t)RCC_CR_HSEON);
/* GPIO Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
/* Configure IO in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_SetBits(GPIOF, GPIO_Pin_1);