【iCore4 双核心板_ARM】例程五:SYSTICK定时器 实验——定时点亮LED
实验原理:
通过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链接: