實驗原理:
STM32的定時器有PWM功能,iCore4的藍色LED連線在定時器的輸出介面上,
可以通過定時器的PWM輸出控制LED的亮度,從而實驗呼吸燈的功能。
核心程式碼:
int main(void) { /* USER CODE BEGIN 1 */ int brightness = 0; float temp = 0.0; int data = 0; /* 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_TIM1_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 */ HAL_Delay(30); if(brightness == 100){ brightness = 0; } temp = 199 * (sin(2 * PI / 99.0 * brightness) * 0.5 + 0.5); data = (int)temp; set_compare(data); brightness ++; } /* USER CODE END 3 */ }
原始碼下載連結:
連結:http://pan.baidu.com/s/1hsQnbEW 密碼:z12v
iCore4連結: