串口接收字符/字符串/定向printf串口发送/用串口控制led灯的亮灭

##usart.h//串口头文件

#include "stm32f10x.h"
#include <stdio.h>
void usart_init();
void USARTsendbyte(USART_TypeDef* USARTx, uint16_t Data);
void USARTsendbytes(USART_TypeDef* USARTx, char *str);

##usart.c



```c
#include "usart.h"
#include "stm32f10x.h"


void usart_init()
{
	GPIO_InitTypeDef gpioinitStructure;
	USART_InitTypeDef usartinitStructure;
	NVIC_InitTypeDef NVICinitStructure;//串口发送指令控制(中断初始化)
	
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1, ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVICinitStructure.NVIC_IRQChannel=USART1_IRQn;
	NVICinitStructure.NVIC_IRQChannelPreemptionPriority=1;
	NVICinitStructure.NVIC_IRQChannelSubPriority=1;
	NVICinitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVICinitStructure);
	
	//配置pa9 tx 输出引脚
	gpioinitStructure.GPIO_Mode  =GPIO_Mode_AF_PP;
	gpioinitStructure.GPIO_Pin   =GPIO_Pin_9;
	gpioinitStructure.GPIO_Speed =GPIO_Speed_50MHz;
	
	
	GPIO_Init( GPIOA,&gpioinitStructure);
	
	//配置pa10 rx 输入引脚
	gpioinitStructure.GPIO_Mode  =GPIO_Mode_IN_FLOATING ;
	gpioinitStructure.GPIO_Pin   =GPIO_Pin_10;
	GPIO_Init( GPIOA,&gpioinitStructure);
	
	
	//配置usart结构体
	usartinitStructure.USART_BaudRate=115200;
	usartinitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
	usartinitStructure.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;
	usartinitStructure.USART_Parity=USART_Parity_No;
	usartinitStructure.USART_StopBits=USART_StopBits_1;
	usartinitStructure.USART_WordLength=USART_WordLength_8b;
	
	USART_Init( USART1, &usartinitStructure);
	USART_ITConfig( USART1, USART_IT_RXNE,ENABLE );

	USART_Cmd( USART1, ENABLE);
	
	
}
//发送字符函数
void USARTsendbyte(USART_TypeDef* USARTx, uint16_t Data)
{
	USART_SendData(USARTx, Data);
	while(USART_GetFlagStatus( USARTx,  USART_FLAG_TXE)==RESET);
}
//发送字符串函数
void USARTsendbytes(USART_TypeDef* USARTx, char *str)
{
	uint16_t i=0;
	do
	{
		USARTsendbyte( USARTx,*(str+i));
		i++;
	
	}while(*(str+i)!='\0');
	while(USART_GetFlagStatus( USARTx,  USART_FLAG_TC)==RESET);
}

//用printf发送字符串
int fputc(int ch, FILE *f)
{
	USART_SendData(USART1, (uint8_t)ch);
	while(USART_GetFlagStatus( USART1,  USART_FLAG_TXE)==RESET);
	return (ch);
}

##main.c文件

```#include "stm32f10x.h"
#include "main.h"
#include "led.h"
#include "relay.h"
#include "shake.h"
#include "exti.h"
#include "usart.h"
void delay(uint16_t time)
{
		uint16_t i=0;
		while(time--)
		{
				i=12000;
				while(i--);
		}
}

int  main()
{

	LED_Init();
	usart_init();
//  USARTsendbyte( USART1, 'O');//发送字符
//	USARTsendbyte( USART1, 'K');
//	USARTsendbytes( USART1, "我要拿高薪");//发送字符串
//		printf("我要拿高薪");//printf发送字符串
	GPIO_SetBits(GPIOA, GPIO_Pin_1);//pa1设置高电平先使其灭
	while(1)
	{
		
	}
		 
   
}
void USART1_IRQHandler()//usart1中断来控制感应灯的亮灭
{
	char temp;
	if(USART_GetITStatus( USART1, USART_IT_RXNE )!=RESET)
	{
		temp=USART_ReceiveData( USART1);
		if(temp=='O')
		{
			GPIO_ResetBits( GPIOA,  GPIO_Pin_1);
			USARTsendbytes(USART1, "LED IS OK");
		}
		if(temp=='C')
		{
			
			GPIO_SetBits( GPIOA,  GPIO_Pin_1);
			USARTsendbytes(USART1, "LED IS DOWN");
		}
	}	
}


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于单片机串口通信的问题。以下是一个简单的参考代码,可以实现串口发送字符控制LED。 ```c #include <reg52.h> /* 定义串口通信参数 */ #define BAUDRATE 9600 #define TIMER_PRESCALER 12 /* 定义LED连接的引脚 */ sbit LED = P1^0; /* 定义串口发送函数 */ void send_data(unsigned char dat) { SBUF = dat; // 将数据放入发送缓冲区 while (TI == 0); // 等待数据发送完成 TI = 0; // 清除发送完成标志 } /* 定义串口初始化函数 */ void init_serial() { TMOD &= 0x0F; // 设置定时器1为工作模式1 TMOD |= 0x20; TH1 = 256 - (11059200UL / 12 / 16 / BAUDRATE); // 计算波特率 TL1 = TH1; TR1 = 1; // 启动定时器1 SCON = 0x50; // 设置串口工作模式1 ES = 1; // 允许串口中断 EA = 1; // 开启总中断 } /* 定义串口中断函数 */ void serial_isr() interrupt 4 { if (RI == 1) // 判断是否接收到数据 { RI = 0; // 清除接收标志 switch (SBUF) // 根据接收到的数据进行操作 { case '1': LED = 1; break; case '0': LED = 0; break; default: break; } } } /* 主函数 */ void main() { init_serial(); // 初始化串口 while (1); // 等待串口中断 } ``` 在上面的代码中,我们定义了串口通信的参数,并且使用定时器和中断的方式来实现串口通信。在主函数中,我们先调用 `init_serial()` 函数进行串口初始化,然后进入一个死循环等待串口中断。当接收到字符 '1' 时,LED起来,当接收到字符 '0' 时,LED会熄

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值