【iCore4 雙核心板_ARM】例程四:USART實驗——通過命令控制LED

XiaomaGee發表於2017-08-09

實驗原理:

  開發板上自帶一片CH340晶片,完成本實驗電腦需要安裝CH340驅動,

CH340的TXD連線STM32的GPIO(PXC7),CH340的RXD連線STM32的

GPIO(PC6),通過串列埠傳送命令控制LED的亮滅。串列埠命令如下:

LED_RED_ON\CR\LF---------------------LED紅燈亮
LED_RED_OFF\CR\LF-------------------LED紅燈滅
LED_BLUE_ON\CR\LF-------------------LED藍燈亮
LED_BLUE_OFF\CR\LF------------------LED藍燈滅
LED_GREEN_ON\CR\LF-----------------LED綠燈亮
LED_GREEN_OFF\CR\LF---------------LED綠燈滅

實驗現象:

  開啟串列埠軟體通過串列埠輸入相應的命令,控制LED的狀態。

int main(void)
{

  /* USER CODE BEGIN 1 */
    int i;
    char buffer[20];
  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART6_UART_Init();

  /* USER CODE BEGIN 2 */
    usart6.initialize(9600);                                                                                          //´®¿Ú²¨ÌØÉèÖÃ
    usart6.printf("Hello, I am iCore4!\r\n");                                                            //´®¿ÚÐÅÏ¢Êä³ö 

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
        if(usart6.receive_ok_flag){                                          //½ÓÊÕÍê³É
            usart6.receive_ok_flag = 0;
            for(i = 0;i < 20;i++){
                buffer[i] = tolower(usart6.receive_buffer[i]);
            }
            
            if(memcmp(buffer,"led_red_on",strlen("led_red_on")) == 0){
                LED_RED_ON;
                usart6.printf("ok!\r\n");
            }
            if(memcmp(buffer,"led_red_off",strlen("led_red_off")) == 0){
                LED_RED_OFF;
                usart6.printf("ok!\r\n");
            }    
            if(memcmp(buffer,"led_green_on",strlen("led_green_on")) == 0){
                LED_GREEN_ON;
                usart6.printf("ok!\r\n");
            }    
            if(memcmp(buffer,"led_green_off",strlen("led_green_off")) == 0){
                LED_GREEN_OFF;
                usart6.printf("ok!\r\n");
            }    
            if(memcmp(buffer,"led_blue_on",strlen("led_blue_on")) == 0){
                LED_BLUE_ON;
                usart6.printf("ok!\r\n");
            }    
            if(memcmp(buffer,"led_blue_off",strlen("led_blue_off")) == 0){
                LED_BLUE_OFF;
                usart6.printf("ok!\r\n");
            }                
        }
  }
  /* USER CODE END 3 */

}

 

原始碼下載連結:

連結:http://pan.baidu.com/s/1pLbnxqV 密碼:a849

iCore4連結:

相關文章