四、串口控制LED灯

串口控制LED灯

1.USART_GetFlagStatus(获取串口状态)

2.USART_ReceiveData (串口接收函数)

3.编写main函数
4.实验结果

1.USART_GetFlagStatus:获取串口状态

参数1:USARTX串口(如:USART1)
参数2:USART_FLAG_xxx   
USART_FLAG_TXE   发送                                
USART_FLAG_RXNE  接收   
//下面笔者还没有研究。初学者             
USART_FLAG_IDLE                      
USART_FLAG_ORE                       
USART_FLAG_NE                       
USART_FLAG_FE                        
USART_FLAG_PE    
USART_FLAG_TC  
USART_FLAG_CTS                      
USART_FLAG_LBD                      

2.USART_ReceiveData 数据接收函数

参数:USARTX串口(如:USART1)

3.编写main.c

#include "stm32f10x.h"
#include "led.h"
#include "stm32f10x_rcc.h"
#include "misc.h"
#include "usart.h"
#include <stdio.h>
void Delay(uint32_t nCount){
	uint32_t i;
  for(i=0;i<nCount;i++);
}
char ReceivedData = NULL;
int main(){
	LED_GPIO_Config();
  USART1_Config();
	while(1){
		 /* Wait until a byte is received */
   while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
    {
    }
    /* read byte and print via usart2 it */
    ReceivedData = USART_ReceiveData(USART1);
    printf("Positionen: %d \r\n", ReceivedData);     
		if (ReceivedData == '1'){
			printf("LED1(ON)");
			LED1(ON);
			Delay(0xFFFFFF);	
		}else if(ReceivedData=='0'){
			LED1(OFF);
			Delay(0xFFFFFF);
			printf("LED1(OFF)");
		}else{
			break;
		}
	
	}
}

4.实验结果

发1 LED ON
发0 LED OFF

源代码:http://download.csdn.net/detail/aa375809600/9717144

博主开发的第三方CSDN客户端.体验很棒哦,快来体验下载吧
在这里插入图片描述

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页