實驗原理:
通過STM32的三個GPIO口驅動三色LED的三個通道,設定GPIO為推輓輸出模式,採
用灌電流方式與LED連線,輸出高電平LED滅,輸出低電平LED亮,通過系統定時器實現
1s定時,每秒變換一次LED顏色。
核心程式碼:
int main(void) { /* USER CODE BEGIN 1 */ static int led_work_status; /* 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(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ if(systick.second_flag == 1){ systick.second_flag = 0; led_work_status += 1; if(led_work_status > 2)led_work_status = 0; switch (led_work_status){ case 0 : LED_RED_ON; LED_GREEN_OFF; LED_BLUE_OFF; break; case 1 : LED_RED_OFF; LED_GREEN_ON; LED_BLUE_OFF; break; case 2: LED_RED_OFF; LED_GREEN_OFF; LED_BLUE_ON; break; default: break; } } } /* USER CODE END 3 */ }
void HAL_SYSTICK_Callback(void) { //中斷時間1ms,每ims進入一次中斷 static int counter = 0; if((counter ++ % 1000) == 0){ systick.second_flag = 1; } }
原始碼下載連結:
連結:http://pan.baidu.com/s/1eSclNOI 密碼:cddu
iCore4連結: