您的位置 首页 测评

STM32F101CBT6—GPIO操练

最近刚学下STM32,焊接了一个电路,并且调试了下程序,想用一个I/O口控制LED闪烁。对STM32还没上手,想控制GPIO还真不容易。花了一些时间看…

最近刚学下STM32,焊接了一个电路,而且调试了下程序,想用一个I/O口操控LED闪耀。对STM32还没上手,想操控GPIO还真不容易。花了一些时刻看了下数据手册,总算把LED点亮了,完成了STM32的最根底的一个程序。

硬件电路如下:

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);
}

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ceping/267171.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部