本文主要是介绍【嵌入式系统实践】实验二USART参考代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
发布此文目的仅为保存先前设计代码,方便日后复盘使用
代码功能:
通过USART实现上位机与下位机的串口通讯功能,基础实现上位机向下位机传输一个消息,之后下位机复读传回该消息。此外,还实现上位机传到下位机的信息通过某种算法加密,并传回上位机;以及当上位机发出一个0-9内数字的消息后,在LED灯上二进制地显示所传数字。
#include "STM32F10X.h"
#include "stdio.h"void GPIO_SETTING(void) //LED Setting
{GPIO_InitTypeDef gpio_init;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);gpio_init.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;gpio_init.GPIO_Mode = GPIO_Mode_Out_PP; gpio_init.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB,&gpio_init);
}void USART_Config(void)
{GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA9,TXGPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_Init(GPIOA,&GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA10,RXGPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;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_Cmd(USART1, ENABLE); USART_Init(USART1, &USART_InitStructure);
}void Delay(u16 i)
{int j = 120;for(;j>=0;j--){while(i--);}
}int main(void)
{uint16_t i;GPIO_SETTING();USART_Config();printf("Please Input string ! \r\n");GPIO_WriteBit(GPIOB, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7, Bit_RESET); //Signal of Actually OpenDelay(65535);GPIO_WriteBit(GPIOB, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7, Bit_SET);while(1){GPIO_WriteBit(GPIOB, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7, Bit_SET);if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==SET){i = USART_ReceiveData(USART1);if(i == 49){printf("this is 1\r\n");GPIO_WriteBit(GPIOB, GPIO_Pin_7, Bit_RESET);Delay(65535);}else if(i == 50){printf("this is 2\r\n");GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_RESET);Delay(65535);}else if(i == 51){printf("this is 3\r\n");GPIO_WriteBit(GPIOB, GPIO_Pin_6 | GPIO_Pin_7, Bit_RESET);Delay(65535);}else if(i == 52){printf("this is 4\r\n");GPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_RESET);Delay(65535);}else if(i == 53){printf("this is 5\r\n");GPIO_WriteBit(GPIOB, GPIO_Pin_5 | GPIO_Pin_7, Bit_RESET);Delay(65535);}else if(i == 54){printf("this is 6\r\n");GPIO_WriteBit(GPIOB, GPIO_Pin_5 | GPIO_Pin_6, Bit_RESET);Delay(65535);}else if(i == 55){printf("this is 7\r\n");GPIO_WriteBit(GPIOB, GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7, Bit_RESET);Delay(65535);}else if(i == 56){printf("this is 8\r\n");GPIO_WriteBit(GPIOB, GPIO_Pin_4, Bit_RESET);Delay(65535);}else if(i == 57){printf("this is 9\r\n");GPIO_WriteBit(GPIOB, GPIO_Pin_4 | GPIO_Pin_7 , Bit_RESET);Delay(65535);}else if((i<48 && i>32 )|| i>57){USART_SendData(USART1, i+3);//printf("\r\n");}else {USART_SendData(USART1, i);}while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);}}
}
这篇关于【嵌入式系统实践】实验二USART参考代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!