【iCore1S 双核心板_ARM】例程十:SYSTICK定时器实验——定时点亮LED
实验原理:
通过STM32的三个GPIO口驱动三色LED的三个通道,设定GPIO为推挽输出,采用
灌电流的方式与LED连接,输出高电平LED灭,输出低电平LED亮,通过系统定时器实现
1s定时,每秒变换一次LED颜色。
核心代码:
int main(void) { /* USER CODE BEGIN 1 */ static int work_status = 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(); //GPIO ³õʼ»¯ /* 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; work_status += 1; if(work_status > 2)work_status = 0; } switch (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) { static int counter = 0; if((counter ++ % 1000) == 0){ systick.second_flag = 1; } }
源代码下载链接:
链接:http://pan.baidu.com/s/1i51iVxb 密码:70sj