完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
所有IO口配置,芯片:STM32F103R8T6 一、配置LED /**************************** 函数名称:LED_Config 函数作用:LED初始化 管脚: LED1 PB0 LED2 PB1 ****************************/ LED.c 寄存器: #include "led.h" void LED_Config(void) { RCC->APB2ENR |= (1 << 3);//GPIOB GPIOB->CRL &= ~(0XF <<0);//清零操作 GPIOB->CRL |= (0X3 << 0); GPIOB->CRL &= ~(0XF << 4);//清零操作 GPIOB->CRL |= (0X3 << 4);//模式配置:通用推挽输出 LED1(0); LED2(0); } 库函数: void LED_Config(void) { GPIO_InitTypeDef GPIO_InitStructure = {0}; //时钟使能 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//PB0 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;//PB1 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速率 GPIO_Init(GPIOB,&GPIO_InitStructure);//结构体初始化 //初始化状态 GPIO_SetBits(GPIOB,GPIO_Pin_0); GPIO_SetBits(GPIOB,GPIO_Pin_1); } LED.h #ifndef _LED_H_ #define _LED_H_ #include "stm32f10x.h" #define LED1(x) (x)?(GPIO_ResetBits(GPIOB,GPIO_Pin_0)):(GPIO_SetBits(GPIOB,GPIO_Pin_0)) #define LED2(x) (x)?(GPIO_ResetBits(GPIOB,GPIO_Pin_1)):(GPIO_SetBits(GPIOB,GPIO_Pin_1)) void LED_Config(void); #endif 二、配置KEY /**************************** 函数名称:KEY_Config 函数作用:按键初始化 管脚:KEY1 PA0 ****************************/ KEY.c 寄存器: #include "key.h" void KEY_Config(void) { RCC->APB2ENR |= (1 << 2);//使能GPIOA GPIOA->CRL &= ~(0XF << 0); GPIOA->CRL |= (0X4 << 0); } /**************************** 函数名称:KEY_GetVal 函数作用:按键初始化 函数参数:无 函数返回值: 0 无按键按下 1 KEY1按下 管脚:PA0 ****************************/ uint8_t KEY_GetVal(void) { uint8_t Key_Val = 0; if(!KEY1)//如果按键按下 { Delay_nop_nus(15); if(!KEY1)//真的有人按下按键 { while(!KEY1);//松手检测 Key_Val = 1; } } return Key_Val; } 库函数: void KEY_Config(void) { GPIO_InitTypeDef GPIO_InitStructure = {0}; //时钟使能 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //模式配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//设置浮空输入模式 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//选中管脚 0 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//最高输出速率 50MHz GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化结构体 } KEY.h #ifndef _KEY_H_ #define _KEY_H_ #include "stm32f10x.h" #include "systick.h" #include "delay.h" #define KEY1 (GPIOA->IDR & (1 << 0)) //#define KEY1 (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)) void KEY_Config(void); uint8_t KEY_GetVal(void); #endif 三、配置DELAY Delay.c #include "delay.h" /**************************** 函数名称:Delay_nop_1us 函数作用:延时1微妙 ****************************/ void Delay_nop_1us(void) { __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); __nop();__nop(); } /**************************** 函数名称:Delay_nop_nus 函数作用:延时n微妙 函数参数:time 延时时间 ****************************/ void Delay_nop_nus(uint32_t time) { while(time--) Delay_nop_1us(); } Delay.h #ifndef _DELAY_H_ #define _DELAY_H_ #include "stm32f10x.h" #define Delay_nop_nms(x) Delay_nop_nus(x*1000) void Delay_nop_nus(uint32_t time); #endif 四、配置BEEP BEEP.c 寄存器 /**************************** 函数名称:BEEP_Config 函数作用:蜂鸣器初始化 管脚:BEEP PA1 ****************************/ #include "BEEP.h" void BEEP_Config(void) { RCC->APB2ENR |= (1<<2);//GPIOC时钟使能 GPIOA->CRL &= ~(0XF << 4);//清零操作 GPIOA->CRL |= (0X3 << 4);//模式配置:通用推挽输出 BEEP(0); } 库函数 void BEEP_BspConfig(void) { GPIO_InitTypeDef GPIO_InitStruct;//GPIO结构体 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//打开GPIOA时钟 GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;//工作模式为通用推挽输出 GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1;//选择工作管脚 GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;//选择工作速率 GPIO_Init(GPIOA,&GPIO_InitStruct); GPIO_ResetBits(GPIOA,GPIO_Pin_1);//初始状态关闭蜂鸣器 } BEEP.h #ifndef _BEEP_H_ #define _BEEP_H_ #include "stm32f10x.h" #define BEEP(x) (x)?(GPIOA->ODR |= (1 << 1)):(GPIOA->ODR &= ~(1 << 1)) //#define BEEP(X) (X)?(GPIO_SetBits(GPIOA,GPIO_Pin_1)):(GPIO_ResetBits(GPIOA,GPIO_Pin_1)) void BEEP_BspConfig(void); #endif 五、配置USART USART.c 寄存器 #include "usart.h" /**************************** 函数名称:USART_Config 函数作用:串口初始化 引脚: PA9 RX PA10 TX ****************************/ void USART_Config(uint32_t brr) { float USARTDIV = 0.0; int DIV_Mantissa=0,DIV_Fraction=0; //1) 时钟使能—GPIOA、USART1 RCC->APB2ENR |= (1 << 2)|(1 << 14); //2) GPIO模式配置 GPIOA->CRH &= ~(0XFF << 4); //PA9 复用推挽输出 GPIOA->CRH |= (0XB << 4); //PA10 浮空输入 GPIOA->CRH |= (0X4 << 8); //3) 串口配置 USART1->CR1 &= ~(1 << 12); //一个起始位,8个数据位 USART1->CR1 &= ~(1 << 10); //禁止校验控制 USART1->CR1 &= ~(0X3 << 2);//接收器、发送器先清0 USART1->CR1 |= (0X3 << 2); //接收器、发送器 USART1->CR2 &= ~(0X3 << 12);//1个停止位 //4) 波特率配置 USARTDIV = (72000000.0)/(16*brr);//468.75 DIV_Mantissa = (int)USARTDIV;//得到468 DIV_Fraction = (USARTDIV-DIV_Mantissa)*16;//小数部分左移4位 USART1->BRR = (DIV_Mantissa<<4)|DIV_Fraction; //USART1->BRR=0x1D4C //5) 使能串口 USART1->CR1 |= (1 << 13); //USART模块使能 } /**************************** 函数名称:fputc 函数作用:USART1重定向函数 ****************************/ int fputc(int c, FILE *stream) { while((USART1->SR & (1 << 6)) == 0); USART1->DR = c; return c; } /**************************** 函数名称:USART_Ech0 函数作用:串口回显 ****************************/ void USART_Ech0(void) { u8 data=0x00; //读数据 while((USART1->SR & (1 << 5)) == 0);//状态寄存器,读数据寄存器非空 data=USART1->DR;//数据寄存器 //写数据 while((USART1->SR & (1 << 6)) == 0);//等待上次数据发送完成 USART1->DR=data; } 库函数: void USART1_Config(u16 brr) { /********结构体********/ GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /********时钟使能********/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); /********GPIO配置********/ //USART1_TX GPIOA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9 //USART1_RX GPIOA.10初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10 /********USART1串口配置********/ USART_InitStructure.USART_BaudRate=brr;//波特率配置 USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx ;//收发模式 USART_InitStructure.USART_Parity=USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_StopBits=USART_StopBits_1;//停止位选择1 USART_InitStructure.USART_WordLength=USART_WordLength_8b;//字长为8位数据格式 /********USART1串口使能配置********/ USART_Init(USART1, &USART_InitStructure); //初始化串口1 } /**************************** 函数名称:fputc 函数作用:USART1重定向函数 ****************************/ int fputc(int c, FILE *stream) { while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); USART_SendData(USART1,c); return c; } /**************************** 函数名称:USART_Ech0 函数作用:串口回显 函数参数:无 函数返回值:无 创建时间:2020.08.14 修改时间:2020.08.14 ****************************/ void USART_Ech0(void) { u8 data=0x00; while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) == 0);//接收数据完成 data = USART_ReceiveData(USART1); while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == 0);//发送数据 USART_SendData(USART1,data); } /**************************** 函数名称:USART1_Send_Byte 函数作用:串口发送一个字节 ****************************/ void USART1_Send_Byte(unsigned char byte) { USART_SendData(USART1, byte); while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET); } /**************************** 函数名称:UART1_Send_Str 函数作用:串口发送发送字符串函数 ****************************/ void UART1_Send_Str(unsigned char *s) { unsigned char i=0; while(s!=' |