標題: STM32單片機串口發(fā)送指令控制LED源代碼 [打印本頁]

作者: 232322626262    時間: 2020-10-29 19:00
標題: STM32單片機串口發(fā)送指令控制LED源代碼
  1. #include "led.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. #include "usart.h"


  5. int main(void)
  6. {        
  7.         u8 t;
  8.         u8 len;        
  9.         u16 times=0;
  10.     u8 com;
  11.     u8 count=5;
  12.         
  13.         Stm32_Clock_Init(9); //系統(tǒng)時鐘設置
  14.         delay_init(72);             //延時初始化  
  15.         uart_init(72,115200);//串口初始化為115200
  16.         LED_Init();        //初始化與LED連接的硬件接口
  17.         printf("\r\nhello world!\r\n");
  18.         while(1)
  19.         {

  20.                 if(USART_RX_STA&0X8000)
  21.                 {                                          
  22.                         //數(shù)據(jù)接收完整后進行發(fā)送的過程
  23.                         len=USART_RX_STA&0x3fFF;//得到此次接收到的數(shù)據(jù)長度
  24.                         if(len==1){
  25.                                 printf("\r\n數(shù)據(jù)的長度=%d\n",len);
  26.                                 printf("\r\n您發(fā)送的消息為:\r\n\r\n");
  27.                                 while((USART1->SR & 0x40) != 0x40);   //置位TE將使得USART在第一個數(shù)據(jù)幀前發(fā)送一空閑幀
  28.                                 for(t=0;t<len;t++)
  29.                                 {
  30.                                         USART1->DR=USART_RX_BUF[t];
  31.                                         com=USART_RX_BUF[t];
  32.                                         while((USART1->SR&0X40)==0);//等待發(fā)送結束
  33.                                 }
  34.                                 printf("\r\n\r\n");//插入換行
  35.                                 USART_RX_STA=0;
  36.                                 
  37.                                 switch(com){
  38.                                     case 0x30:
  39.                                                 //LED1變換,發(fā)送命令‘0’
  40.                                                 LED1=!LED1;
  41.                                                 delay_ms(20);
  42.                                                 break;
  43.                                         case 0x31:
  44.                                                 //LED1和LED2閃爍,發(fā)送命令‘1’
  45.                                              do{
  46.                                                    LED1=0;
  47.                                                    LED2=1;
  48.                                                    delay_ms(20);
  49.                                                    LED1=1;
  50.                                                    LED2=0;
  51.                                                    delay_ms(20);
  52.                                                    count--;
  53.                                                  }while(count);
  54.                                                  if(count==0) count=5;
  55.                                                 break;
  56.                                 
  57.                                 }
  58.                    }else{
  59.                           printf("\r\n發(fā)送的指令錯誤,請重新發(fā)送\r\n");
  60.                           USART_RX_STA=0;
  61.                    }
  62.                 }else
  63.                 {
  64.                         times++;
  65.                         if(times%5000==0)
  66.                         {
  67.                                 printf("\r\nALIENTEK NANO STM32開發(fā)板 串口實驗\r\n");
  68.                                 printf("正點原子@ALIENTEK\r\n\r\n\r\n");
  69.                         }
  70.                         if(times%200==0)printf("請輸入數(shù)據(jù),以回車鍵結束\r\n");  
  71.                         if(times%30==0)LED0=!LED0;//閃爍LED,提示系統(tǒng)正在運行.
  72.                         delay_ms(10);   
  73.                 }
  74.         }         
  75. }
復制代碼


KEIL.7z

79.65 KB, 下載次數(shù): 25, 下載積分: 黑幣 -5






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1