新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > STM32F101CBT6---GPIO练习

STM32F101CBT6---GPIO练习

作者: 时间:2016-11-18 来源:网络 收藏
最近刚学下STM32,焊接了一个电路,并且调试了下程序,想用一个I/O口控制LED闪烁。对STM32还没上手,想控制GPIO还真不容易。花了一些时间看了下数据手册,终于把LED点亮了,实现了STM32的最基础的一个程序。

硬件电路如下:

本文引用地址:https://www.eepw.com.cn/article/201611/315995.htm

Keil编译环境和J-LINK调试器配置
要用GPIO首先需要熟悉具体寄存器配置,具体看STM32F101xx Reference manual

源程序:

///////////////////////////////////////////////
// Study for STM32
// 芯片型号: STM32F101CBT6
// 编译环境: Keil-uVision4
// 调试器 : J-LINK
// 程序名称: I/O控制LED闪烁
// 作者: ClimberWin
// 编写日期 : 2011年04月19日
////////////////////////////////////////////////

//引脚定义 PB12->LED

#include /*库文件的调用 */
#include /*数学计算库文件的调用 */

#define LED (1<<12) // PB12//端口定义


void delayms(int ms); //延时程序
void RCC_DeInit(void); //时钟初始化程序
void GPIO_init(void); //GPIO初始化程序


/***延时程序***/
void delayms(int ms)
{
int a;
while(ms--)
{
for(a=0; a<2000; a++)
{;}
}
}
/****************************************/

/////////////////////////////////////////
//GPIO初始化
void GPIO_init(void)
{

RCC->APB2ENR|=1<<3; //使能PORTB时钟 将 RCC_APB2ENR的位3置1,其他不变Page95/995 of RM0008 Reference manual
/*Each of the general-purpose I/O ports has Page138/995 of RM0008 Reference manual
two 32-bit configuration registers (GPIOx_CRL, GPIOx_CRH),
two 32-bit data registers (GPIOx_IDR, GPIOx_ODR)
a 32-bit set/reset register (GPIOx_BSRR)
a 16-bit reset register (GPIOx_BRR)
a 32-bit locking register (GPIOx_LCKR).
*/
GPIOB->CRH&=0XFFF000FF; // Port configuration register high (GPIOx_CRH) (x=A..G)
GPIOB->CRH|=0X00030000; //PB12设置为通用推挽输出
GPIOB->ODR=0X00001000; //PB12 输出高 Port output data register (GPIOx_ODR) (x=A..G)

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//时钟初始化程序
void RCC_DeInit(void)
{
RCC->APB2RSTR = 0x00000000;//外设复位
RCC->APB1RSTR = 0x00000000;
RCC->AHBENR = 0x00000014; //时钟使能寄存器 FLITF时钟,SRAM时钟使能 Page93/995 of RM0008 Reference manual
RCC->APB2ENR = 0x00000000; //外设时钟关闭. Page95/995 of RM0008 Reference manual
RCC->APB1ENR = 0x00000000;
RCC->CR |= 0x00000001; //HSION=1;内部高速时钟使能(Internal 8 MHz RC ) Page83/995 of RM0008 Reference manual
RCC->CFGR &= 0xF88FFFFF; //Clock configuration register Page84/995 of RM0008 Reference manual
RCC->CR &= 0xFEF2FFFF; //HSEON,HSEBYP,CSSON,PLLON置0
RCC->CIR = 0x00000000; //Clock interrupt register disenable Page87/995 of RM0008 Reference manual
}


///////////////////////////////主程序////////////////////////////////////////
main (void)
{
RCC_DeInit();
GPIO_init();
while(1)
{
GPIOB->ODR=0X00001000; //PB12 输出高
delayms(500);
GPIOB->ODR=0X00000000; //PB12 输出低
delayms(500);
}


}



关键词: STM32F101CBT6GPI

评论


技术专区

关闭