USART_串口回显控制

一、程序内容

1、串口发送并回显

2、串口发送控制

二、库函数

1、串口接收数据函数

uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
dat=USART_ReceiveData(USART1);
    @brief  串口接收数据
    @param  USARTx            (1-5)
    @retval  接收的数据

三、程序源码

写在    stm32f103_it.c    文件中

void USART1_IRQHandler(void)				//中断服务函数
{
	if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
	{
		uint8_t dat;
		dat=USART_ReceiveData(USART1);		//串口接收数据
		USARTSend_Byes(USART1,dat);		//将接受的数据返回
		
		if(dat=='1')				//判断接受的函数并作出反应
		{
			PBout(0)=0;			//点亮LED
		}
		if(dat=='0')
		{
			PBout(0)=1;			//关闭LED
		}
	}
}

#include “usart.h” //串口初始化 void Init_USART(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // Enable USART1 clock RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // Enable GPIOA clock GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // TX and RX pins GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Alternate function mode GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); // TX pin // GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); // RX pin // Configure USART1 USART_InitStructure.USART_BaudRate = 115200; // Set baud rate USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // No hardware flow control USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_Mode = USART_Mode_Tx; // Set mode to transmit and receive USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); } //发送一个字符 void USART_SendChar(uint8_t ch) { while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait until TX buffer is empty USART_SendData(USART1, ch); } //接收一个字符 uint8_t USART_ReceiveChar(void) { while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); // Wait until RX buffer is not empty return USART_ReceiveData(USART1); } //配置时钟 void SystemClock_Config(void) { RCC_DeInit(); // Reset the RCC clock configuration to the default reset state RCC_HSEConfig(RCC_HSE_ON); // Enable HSE (High-Speed External oscillator) while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); // Wait until HSE is ready RCC_PLLConfig(RCC_PLLSource_HSE, 4, 168, 2, 4); // Configure PLL (example values) RCC_PLLCmd(ENABLE); // Enable PLL while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); // Wait until PLL is ready RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Select PLL as system clock while (RCC_GetSYSCLKSource() != RCC_CFGR_SWS_PLL); // Wait until PLL is used as system clock RCC_HCLKConfig(RCC_SYSCLK_Div1); // Set AHB clock (HCLK) to SYSCLK RCC_PCLK1Config(RCC_HCLK_Div4); // Set APB1 clock (PCLK1) to HCLK/4 RCC_PCLK2Config(RCC_HCLK_Div2); // Set APB2 clock (PCLK2) to HCLK/2 } #include “stm32f4xx.h” #include “usart.h” #include “delay.h” int main() { SystemClock_Config(); // Configure the system clock Init_USART(); // Initialize USART // Delay initialization // Delay_ms(100); // Wait for USART to stabilize USART_SendChar(‘H’); // Send a character to test USART USART_SendChar(‘e’); // Send another character USART_SendChar(‘l’); // Send another character USART_SendChar(‘l’); // Send another character USART_SendChar(‘o’); // Send another character USART_SendChar(’ ‘); // Send a space USART_SendChar(‘W’); // Send another character USART_SendChar(‘o’); // Send another character USART_SendChar(‘r’); // Send another character USART_SendChar(‘l’); // Send another character USART_SendChar(‘d’); // Send another character USART_SendChar(’!‘); // Send an exclamation mark USART_SendChar(’\r’); // Send carriage return USART_SendChar(‘\n’); // Send newline // Main loop // Uncomment the following lines to continuously send characters or receive characters while(1) { //接收字符 uint8_t ch = USART_ReceiveChar(); // uint8_t ch = USART1->DR; USART_SendChar(ch); USART_SendChar(‘\r’); USART_SendChar(‘\n’); } } 程序有什么问题吗 为什么串口接收到的数据部分乱码
最新发布
07-20
帮我解释一下:#include "stm32f10x.h" // Device header #include <stdio.h> #include <stdarg.h> uint8_t Serial_RxData; uint8_t Serial_RxFlag; void Serial_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_Init(USART1, &USART_InitStructure); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_Init(&NVIC_InitStructure); USART_Cmd(USART1, ENABLE); } void Serial_SendByte(uint8_t Byte) { USART_SendData(USART1, Byte); while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); } void Serial_SendArray(uint8_t *Array, uint16_t Length) { uint16_t i; for (i = 0; i < Length; i ++) { Serial_SendByte(Array[i]); } } void Serial_SendString(char *String) { uint8_t i; for (i = 0; String[i] != '\0'; i ++) { Serial_SendByte(String[i]); } } uint32_t Serial_Pow(uint32_t X, uint32_t Y) { uint32_t Result = 1; while (Y --) { Result *= X; } return Result; } void Serial_SendNumber(uint32_t Number, uint8_t Length) { uint8_t i; for (i = 0; i < Length; i ++) { Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0'); } } int fputc(int ch, FILE *f) { Serial_SendByte(ch); return ch; }
06-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值