实验原理:
开发板上自带一片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 */}
源代码下载链接:
链接: 密码:a849
iCore4链接: