STM32笔记4:基本库函数写法(结构体指针的应用)

2019-04-15 18:24发布

//构建库函数雏形 //peripheral:外设 #include"stm32f4xx.h" //peripheral #define PERIPHERAL ((unsigned int)(0x40000000)) #define AHB1_BASE ((unsigned int)(PERIPHERAL + 0x0002 0000)) #define GPIOA_BASE ((unsigned int)(AHB1_BASE+0x0000 0000)) #define GPIOB_BASE ((unsigned int)(AHB1_BASE+0x0002 0400)) #define GPIOC_BASE ((unsigned int)(AHB1_BASE+0x0002 0800)) #define GPIOD_BASE ((unsigned int)(AHB1_BASE+0x0002 0c00)) #define GPIOE_BASE ((unsigned int)(AHB1_BASE+0x0002 1000)) #define GPIOF_BASE ((unsigned int)(AHB1_BASE+0x0002 1400) #define GPIOG_BASE ((unsigned int)(AHB1_BASE+0x0002 1800)) #define GPIOH_BASE ((unsigned int)(AHB1_BASE+0x0002 1C00)) #define GPIOI_BASE ((unsigned int)(AHB1_BASE+0x0002 2000)) // #define GPIOH_MODER *(unsigned int*)(GPIOH_BASE+0x00) // #define GPIOH_OTYPER *(unsigned int*)(GPIOH_BASE+0x04) // #define GPIOH_OSPEEDR *(unsigned int*)(GPIOH_BASE+0x08) // #define GPIOH_PUPDR *(unsigned int*)(GPIOH_BASE+0x0c) // #define GPIOH_IDR *(unsigned int*)(GPIOH_BASE+0x10) // #define GPIOH_ODR *(unsigned int*)(GPIOH_BASE+0x14) // #define GPIOH_BSRR *(unsigned int*)(GPIOH_BASE+0x18) // #define GPIOH_LCKR *(unsigned int*)(GPIOH_BASE+0x1c) // #define GPIOH_AFRL *(unsigned int*)(GPIOH_BASE+0x20) // #define GPIOH_AFRH *(unsigned int*)(GPIOH_BASE+0x24) typedef unsigned int uint32_t; typedef unsigned int uint16_t; typedef struct { uint32_t MODER; uint32_t OTYPER; uint32_t OSPEEDR; uint32_t PUPDR; uint32_t IDR; uint32_t ODR; uint16_t BSRRL; uint16_t BSSRH; uint32_t LCKR; uint32_t AFR[2]; }GPIO_Typedef; //将GPIOA_BASE变成一个结构体指针 #define GPIOA ((GPIO_Typedef *)(GPIOA_BASE)) #define GPIOB ((GPIO_Typedef *)(GPIOB_BASE)) #define GPIOC ((GPIO_Typedef *)(GPIOC_BASE)) #define GPIOD ((GPIO_Typedef *)(GPIOD_BASE)) #define GPIOE ((GPIO_Typedef *)(GPIOE_BASE)) #define GPIOF ((GPIO_Typedef *)(GPIOF_BASE)) #define GPIOG ((GPIO_Typedef *)(GPIOG_BASE)) #define GPIOH ((GPIO_Typedef *)(GPIOH_BASE)) #define GPIOI ((GPIO_Typedef *)(GPIOI_BASE)) void GPIO_SetBits(GPIO_Typedef *GPIOx, uint16_t GPIO_Pin); void GPIO_RetsetBits(GPIO_Typedef *GPIOx, uint16_t GPIO_Pin); int main(void) { // RCCAHB1PERIPH_BASE |= (1<<7); // GPIOH_MODER &= ~(3<<2*10); // GPIOH_MODER |= (1<<2*10); // GPIOH->MODER &= ~(3<<2*10); // GPIO->MODER |= (1<<2*10); // PH10输出低电平 // GPIOH_ODR &= ~(1<<10); RCC_AHB1ENR |= (1<<7); GPIO_SetBits(GPIOH, 10); //若想要找到GPIOH的definition,若是找不到,可能是keil的options for target中的output中的Brows information 没有勾上。 GPIO_ReSetBits(GPIOH, 10); while(1); } /* 函数功能:设置引脚为高电平 参数说明:GPIOx:结构体指针,指向GPIO端口。GPIO_Pin:要被置位引脚号 */ void GPIO_SetBits(GPIO_Typedef *GPIOx, uint16_t GPIO_Pin) { GPIOx->BSSRL |= (1<BSSRL |= (1<