【iCore4 双核心板_ARM】例程八:定时器PWM实验——呼吸灯
实验原理:
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链接: