发送文字控制led亮暗,"led_on\r\n""led_off\r\n"
单片机源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "led.h"
- #include "usart.h"
- #include "string.h"
- char cmd[200];
- int main(void)
- {
- u8 t;
- u8 len;
- delay_init();
- LED_Init();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- uart_init(115200);
- while(1)
- {
- if(USART_RX_STA&0x8000)
- {
- len=USART_RX_STA&0x3FFF;
- for(t=0;t<len;t++)
- {
- cmd[t]=USART_RX_BUF[t];
- while((USART1->SR&0X40)==0);
- }
- printf("\r\n receive successful \r\n");
- USART_RX_STA=0;
- USART_ClearFlag(USART1, USART_FLAG_RXNE);
- if(!(strcmp(cmd,"led1_on")))
- {
- LED0=0;
- memset(cmd,0,strlen(cmd)); //清除cmd[]
- }
- else if(!(strcmp(cmd,"led1_off")))
- {
- LED0=1;
- memset(cmd,0,strlen(cmd)); //清除cmd[]
- }
- else
- printf("\r\n input error");
- memset(cmd,0,strlen(cmd)); //清除cmd[]
- }
- }
- }
复制代码
所有资料51hei提供下载:
串口通信控制led.rar
(272.44 KB, 下载次数: 125)
|