STM32点灯

野火教程+正点原子F103精英版

main.c

#include "stm32f10x.h"
#include "stm32f10x_gpio.h"


#define LED_G_GPIO_PORT      		   	GPIOB
#define LED_G_GPIO_CLK_ENABLE		RCC->APB2ENR |= ((1)<<(3))
#define LED_G_GPIO_PIN						GPIO_Pin_5

void Delay(uint32_t count)
{
	while(count --);
}

int main(void)
{
#if 0
	*(unsigned int*)0X40021018 |= ((1)<<(3));
	
	//端口配置低寄存器CRL,输出模式,最大速度10MHz
	*(unsigned int*)0X40010C00 |= (1<<(4*5));
	
	//端口输出寄存器ODR设置PB5输出数据0
	*(unsigned int*)0X40010C0C &= ~(1<<5);
	
#elif 0
	
	RCC_APB2ENR |= ((1)<<(3));
	
	//端口配置低寄存器CRL,输出模式,最大速度10MHz
	GPIOB_CRL |= (1<<(4*5));
	
	//端口输出寄存器ODR设置PB5输出数据0
	GPIOB_ODR	&= ~(1<<5);
	
#elif 0
	
	RCC->APB2ENR |= ((1)<<(3));

	GPIOB->CRL |= (1<<(4*5));

	GPIOB->ODR	&= ~(1<<5);
	
#elif 0
	
	RCC->APB2ENR |= ((1)<<(3));

	GPIOB->CRL |= (1<<(4*5));

	GPIOB->ODR	&= ~(1<<5);
	
	GPIO_SetBits(GPIOB, GPIO_Pin_5);
	GPIO_ResetBits(GPIOB, GPIO_Pin_5);
	
#elif 0

	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC->APB2ENR |= ((1)<<(3));

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_SetBits(GPIOB, GPIO_Pin_5);
	GPIO_ResetBits(GPIOB, GPIO_Pin_5);
	
#elif 1

	GPIO_InitTypeDef GPIO_InitStructure;
	
	LED_G_GPIO_CLK_ENABLE;

	GPIO_InitStructure.GPIO_Pin = LED_G_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(LED_G_GPIO_PORT, &GPIO_InitStructure);
	
	while(1)
	{
		GPIO_SetBits(LED_G_GPIO_PORT, LED_G_GPIO_PIN);
		Delay(0xFFFF);
		GPIO_ResetBits(LED_G_GPIO_PORT, LED_G_GPIO_PIN);
		Delay(0xFFFF);
	}

	
#endif
}

stm32f10x.h

#ifndef __STM32F10X_H
#define __STM32F10X_H

#define PERIPH_BASE 					((unsigned int) 0x40000000)
#define APB1PERIPH_BASE  			PERIPH_BASE
#define APB2PERIPH_BASE  			(PERIPH_BASE + 0x10000)
#define AHBPERIPH_BASE  			(PERIPH_BASE + 0x20000)

#define RCC_BASE							(AHBPERIPH_BASE + 0x1000)
#define GPIOB_BASE						(APB2PERIPH_BASE + 0xC00)

// #define RCC_APB2ENR						*(unsigned int*)(RCC_BASE + 0x18)

// #define GPIOB_CRL							*(unsigned int*)(GPIOB_BASE + 0x00)
// #define GPIOB_CRH							*(unsigned int*)(GPIOB_BASE + 0x04)
// #define GPIOB_IDR							*(unsigned int*)(GPIOB_BASE + 0x08)
// #define GPIOB_ODR							*(unsigned int*)(GPIOB_BASE + 0x0C)
// #define GPIOB_BSRR						*(unsigned int*)(GPIOB_BASE + 0x10)
// #define GPIOB_BRR							*(unsigned int*)(GPIOB_BASE + 0x14)
// #define GPIOB_LCKR						*(unsigned int*)(GPIOB_BASE + 0x18)

typedef unsigned int          uint32_t;
typedef unsigned short        uint16_t;

typedef struct
{
	uint32_t CRL;
	uint32_t CRH;
	uint32_t IDR;
	uint32_t ODR;
	uint32_t BSRR;
	uint32_t BRR;
	uint32_t LCKR;

}GPIO_TypeDef;


typedef struct
{
	uint32_t CR;
	uint32_t CFGR;
	uint32_t CIR;
	uint32_t APB2RSTR;
	uint32_t APB1RSTR;
	uint32_t AHBENR;
	uint32_t APB2ENR;
	uint32_t APB1ENR;
	uint32_t BDCR;
	uint32_t CSR;

}RCC_TypeDef;


#define GPIOB ((GPIO_TypeDef*)GPIOB_BASE)
#define RCC ((RCC_TypeDef*)RCC_BASE)

#endif /*__STM32F10X_H*/


stm32f10x_gpio.c

#include "stm32f10x_gpio.h"

void GPIO_SetBits(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
	GPIOx->BSRR |= GPIO_Pin;
}

void GPIO_ResetBits(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
	GPIOx->BRR |= GPIO_Pin;
}

void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
 uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
 uint32_t tmpreg = 0x00, pinmask = 0x00;
 /* Check the parameters */

 
/*---------------------------- GPIO Mode Configuration -----------------------*/
 currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
 if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)
 { 
   /* Check the parameters */

   /* Output mode */
   currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
 }
/*---------------------------- GPIO CRL Configuration ------------------------*/
 /* Configure the eight low port pins */
 if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)
 {
   tmpreg = GPIOx->CRL;
   for (pinpos = 0x00; pinpos < 0x08; pinpos++)
   {
     pos = ((uint32_t)0x01) << pinpos;
     /* Get the port pins position */
     currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
     if (currentpin == pos)
     {
       pos = pinpos << 2;
       /* Clear the corresponding low control register bits */
       pinmask = ((uint32_t)0x0F) << pos;
       tmpreg &= ~pinmask;
       /* Write the mode configuration in the corresponding bits */
       tmpreg |= (currentmode << pos);
       /* Reset the corresponding ODR bit */
       if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
       {
         GPIOx->BRR = (((uint32_t)0x01) << pinpos);
       }
       else
       {
         /* Set the corresponding ODR bit */
         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
         {
           GPIOx->BSRR = (((uint32_t)0x01) << pinpos);
         }
       }
     }
   }
   GPIOx->CRL = tmpreg;
 }
/*---------------------------- GPIO CRH Configuration ------------------------*/
 /* Configure the eight high port pins */
 if (GPIO_InitStruct->GPIO_Pin > 0x00FF)
 {
   tmpreg = GPIOx->CRH;
   for (pinpos = 0x00; pinpos < 0x08; pinpos++)
   {
     pos = (((uint32_t)0x01) << (pinpos + 0x08));
     /* Get the port pins position */
     currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
     if (currentpin == pos)
     {
       pos = pinpos << 2;
       /* Clear the corresponding high control register bits */
       pinmask = ((uint32_t)0x0F) << pos;
       tmpreg &= ~pinmask;
       /* Write the mode configuration in the corresponding bits */
       tmpreg |= (currentmode << pos);
       /* Reset the corresponding ODR bit */
       if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
       {
         GPIOx->BRR = (((uint32_t)0x01) << (pinpos + 0x08));
       }
       /* Set the corresponding ODR bit */
       if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
       {
         GPIOx->BSRR = (((uint32_t)0x01) << (pinpos + 0x08));
       }
     }
   }
   GPIOx->CRH = tmpreg;
 }
}


stm32f10x_gpio.h

#ifndef __STM32F10X_GPIO_H
#define __STM32F10X_GPIO_H

#include "stm32f10x.h"

#define GPIO_Pin_0 			((uint16_t)0x0001)
#define GPIO_Pin_1 			((uint16_t)0x0002)
#define GPIO_Pin_2 			((uint16_t)0x0004)
#define GPIO_Pin_3 			((uint16_t)0x0008)
#define GPIO_Pin_4 			((uint16_t)0x0010)
#define GPIO_Pin_5 			((uint16_t)0x0020)
#define GPIO_Pin_6 			((uint16_t)0x0040)
#define GPIO_Pin_7 			((uint16_t)0x0080)

#define GPIO_Pin_8 			((uint16_t)0x0100)
#define GPIO_Pin_9 			((uint16_t)0x0200)
#define GPIO_Pin_10 		((uint16_t)0x0400)
#define GPIO_Pin_11 		((uint16_t)0x0800)
#define GPIO_Pin_12 		((uint16_t)0x1000)
#define GPIO_Pin_13 		((uint16_t)0x2000)
#define GPIO_Pin_14 		((uint16_t)0x4000)
#define GPIO_Pin_15 		((uint16_t)0x8000)
#define GPIO_Pin_All 		((uint16_t)0xFFFF)


typedef enum
{ 
  GPIO_Speed_10MHz = 1,
  GPIO_Speed_2MHz, 
  GPIO_Speed_50MHz
}GPIOSpeed_TypeDef;

typedef enum
{ GPIO_Mode_AIN = 0x0,          //模拟输入
  GPIO_Mode_IN_FLOATING = 0x04, //浮空输入
  GPIO_Mode_IPD = 0x28,         //输入下拉
  GPIO_Mode_IPU = 0x48,         //输入上拉
  GPIO_Mode_Out_OD = 0x14,      //开漏输出
  GPIO_Mode_Out_PP = 0x10,      //推挽式输出
  GPIO_Mode_AF_OD = 0x1C,       //开漏复用输出
  GPIO_Mode_AF_PP = 0x18        //推挽式复用输出
}GPIOMode_TypeDef;


typedef struct
{

  uint16_t GPIO_Pin;             
  GPIOSpeed_TypeDef GPIO_Speed;
  GPIOMode_TypeDef GPIO_Mode;
}GPIO_InitTypeDef;



void GPIO_SetBits(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);

#endif /* __STM32F10X_GPIO_H*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值