USART串口控制外设

    技术2022-07-10  118

    #include “stm32f10x.h” #include “LQ12864.h” #include “adc.h” #include <stdio.h> #include “bsp_clkconfig.h” #include “bsp_mcooutput.h” #include “bsp_SysTick.h” #include “usart.h” void main_delay(u32 ms) { int i, j; for(i = 0; i < ms; i++) { for(j = 0; j < 1000; j++) { } } } //定义函数,串口初始化 void uart_config(){ //创建USART串口(USART1串口)类型的初始化结构体变量 USART_InitTypeDef USART_InitStructure; //创建GPIO引脚配置结构体变量 GPIO_InitTypeDef GPIO_InitStructure; //使能GPIOA组的时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //参数1:RCC_APB2Periph_GPIOA是GPIOA组标示 //参数2: ENABLE使能时钟的状态 //使能GPIOA重映射功能 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //参数1:RCC_APB2Periph_AFIO GPIO引脚复用标示 //参数2:使能状态ENABLE //使能片上外设串口USART1时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //参数1:RCC_APB2Periph_USART1 片上外设的标示 //参数2:使能状态ENABLE //stm32 片上外设USART1 发送 引脚 TX -复用、重新映射-> GPIO PA9(模式是复用推挽) //设置GPIO 引脚的模式(PA9模式) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置引脚号 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //设置引脚的工作的频率 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //初始化PA9引脚 GPIO_Init(GPIOA, &GPIO_InitStructure); //参数1:GPIO组标示 //参数2:引脚配置结构体变量 //stm32 片上外设USART1 接收 引脚 RX -复用、重新映射-> GPIO PA10(模式是浮空输入) //设置GPIO浮空输入模式 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置引脚号 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //初始化PA10引脚 GPIO_Init(GPIOA, &GPIO_InitStructure); //配置串口 //设置串口波特率:9600位/秒 USART_InitStructure.USART_BaudRate = 9600; //设置串口USART1数据包里面的数据位:8位 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //设置数据发送完成数据停止位:1位 USART_InitStructure.USART_StopBits = USART_StopBits_1; //设置校验:无,数据包小 无需校验USART_Parity_No USART_InitStructure.USART_Parity = USART_Parity_No; //设置硬件流控制:无控制USART_HardwareFlowControl_None USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None//设置串口的模式:输入模式、输出模式 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //初始化串口信息 USART_Init(USART1, &USART_InitStructure); //参数1:串口的标示 //参数2:串口的配置信息 //使能串口 USART_Cmd(USART1, ENABLE); //参数1:串口标示USART1 //参数2:使能串口 } int main(void) { uint16_t val; int i = 0 ; char buf[100] = { 0 }; RCC_ClocksTypeDef RCC_Clocks; //时钟结构体 HSE_SetSysClock(RCC_PLLMul_2); //16Mhz,库函数默认多数为72MHZ。此处设置为16M,主要是因为发现72M时proteus仿真串口容易出现乱码。 delay_init() ; LCD_Init() ; Adc_Init(); //调用函数,串口初始化 uart_config(); //格式化输出内容到串口 printf(“input data : “); //格式化输入函数到串口 scanf(”%s”, buf); //输出输入的内容buf printf(“get data : %s\r\n”, buf); //\r:回车,光标到行首 //\n:换行,光标到下一行 while(1) { } } #include “stm32f10x.h” #include “LQ12864.h” #include “adc.h” #include <stdio.h> #include “bsp_clkconfig.h” #include “bsp_mcooutput.h” #include “bsp_SysTick.h” #include “usart.h” void main_delay(u32 ms) { int i, j; for(i = 0; i < ms; i++) { for(j = 0; j < 1000; j++) { ; } } } void uart_config(){ USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; //GPIO_InitTypeDef init; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //init.GPIO_Mode = GPIO_Mode_Out_PP; //init.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2; //init.GPIO_Speed = GPIO_Speed_50MHz; //GPIO_Init(GPIOA,&init); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1,&USART_InitStructure); USART_Cmd(USART1,ENABLE);

    //GPIO_SetBits(GPIOA,GPIO_Pin_1); //GPIO_SetBits(GPIOA,GPIO_Pin_2); } void fan_init(){ GPIO_InitTypeDef init;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); init.GPIO_Pin = GPIO_Pin_8; init.GPIO_Mode = GPIO_Mode_Out_PP; init.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &init); GPIO_SetBits(GPIOA, GPIO_Pin_8);

    } void buzzer_init(){ GPIO_InitTypeDef init; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); init.GPIO_Pin = GPIO_Pin_2; init.GPIO_Mode = GPIO_Mode_Out_PP; init.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &init); GPIO_SetBits(GPIOA, GPIO_Pin_2); }

    void led_init(){ GPIO_InitTypeDef init;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); init.GPIO_Pin = GPIO_Pin_15; init.GPIO_Mode = GPIO_Mode_Out_PP; init.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &init); GPIO_SetBits(GPIOA, GPIO_Pin_15);

    } int main(void) { uint16_t val; int i = 0 ; char buf[100] = { 0 }; RCC_ClocksTypeDef RCC_Clocks; HSE_SetSysClock(RCC_PLLMul_2);

    delay_init() ; LCD_Init() ; Adc_Init();

    uart_config(); fan_init(); led_init(); buzzer_init();

    while(1) { printf("input data :"); scanf("%s",buf); printf("get data : %s\r\n", buf); if(buf[0]=='1'){ printf("len on\r\n"); GPIO_ResetBits(GPIOA, GPIO_Pin_15); } if(buf[0]=='2'){ printf("buzzer on\r\n"); GPIO_ResetBits(GPIOA, GPIO_Pin_2); } if(buf[0]=='3'){ printf("fan on\r\n"); GPIO_ResetBits(GPIOA, GPIO_Pin_8); } }

    }

    Processed: 0.014, SQL: 12