STM32串口接收字符串并控制LED

串口相关配置

GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
	
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;	         
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_Init(GPIOA, &GPIO_InitStructure);		   

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;	        
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	

  USART_InitStructure.USART_BaudRate = 115200;
  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_ITConfig(USART1, USART_IT_RXNE, ENABLE);   //接收中断	 
  //USART_ITConfig(USART1, USART_IT_TXE, ENABLE);	   //发送中断
  USART_Cmd(USART1, ENABLE);
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //接收中断使能

串口中断配置

NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); 						
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;   
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  NVIC_Init(&NVIC_InitStructure); 

重定向fputc,并勾选Options - Target 中的Use Micro LIB

	USART_SendData(USART1, (uint8_t) ch);
	
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
		  {}

	return ch;

STM32串口接收字符串并控制LED_第1张图片

串口中断服务函数,接收字符串的关键

u8 i=0; 
    if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET) 
    {
			if(USART_ReceiveData(USART1)=='\n')	 //串口调试助手必须是点上换行符
				{ 
					sp=0;
					USART_REC_Finish_FLAG = 1;
					for(i=0;i<20;i++)           //i 字符串长度
					Rec_Buffer[i]='\0';
				}
				else
					{
						if(sp==0)
							{
								for(i=0;i<20;i++)
								SendData[i]='\0';
							}	
							Rec_Buffer[sp] = USART_ReceiveData(USART1);
							SendData[sp]= Rec_Buffer[sp];
							sp=sp+1;
					}
		}
		if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) 
    { 
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);	//清除接收中断标志
				
    }

主函数

while(USART_REC_Finish_FLAG == 1)						//等待串口接收完成标志位置1
		{	
			delay(1000);			//延时1ms
			
			USART_REC_Finish_FLAG = 0;		//使用完成后将串口标志位置〇
			
			
			
			if(strcmp(SendData, LED1ON)==0)						//比较字符串 
				ch = '1';
			else if(strcmp(SendData, LED1OFF)==0)
				ch = '2';
			else
				ch = '3';
			printf("%c  \r\n",ch);
		
	
			switch(ch)
			{
				case '1':
					LED1_ON;
					printf("LED1 ON\r\n");
				break;
				case '2':
					LED1_OFF;
					printf("LED1 OFF\r\n");
				break;
				case '3':
					LED2_TOGGLE;
					printf("LED2 TOGGLE\r\n");
				break;
				default:
					printf("Error!\r\n");
				break;
			}
			
		}

效果如图
STM32串口接收字符串并控制LED_第2张图片
通过STM32 串口1 接收字符串实现LED控制,程序通过判断\n识别字符串是否发送完成,所以串口调试助手上需勾选发送新行,发送LED1+ON点亮LED1,发送LED1+OFF关闭LED1,发送其他字符串翻转LED2

源程序:https://download.csdn.net/download/u013002186/10584289

你可能感兴趣的:(硬件,STM32,嵌入式软件)