实验原理:
通过STM32的三个GPIO口驱动三色LED的三个通道,设定GPIO为推挽输出,采用
灌电流的方式与LED连接,输出高电平LED灭,输出低电平LED亮,通过系统定时器实现
1s定时,每秒变换一次LED颜色。
核心代码:
int main(void)
{ /* USER CODE BEGIN 1 */
static int 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(); //GPIO ³õʼ»¯ /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */
/* USER CODE BEGIN WHILE */
while ()
{
//ÿ¸ôÒ»ÃëÈýÉ«µÆ½øÐн»ÌæÑ»·ÉÁ˸ /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ if(systick.second_flag == ){
systick.second_flag = ;
work_status += ;
if(work_status > )work_status = ;
}
switch (work_status){
case :
LED_RED_ON;
LED_GREEN_OFF;
LED_BLUE_OFF;
break; case :
LED_RED_OFF;
LED_GREEN_ON;
LED_BLUE_OFF;
break; case :
LED_RED_OFF;
LED_GREEN_OFF;
LED_BLUE_ON;
break; default: break;
} }
/* USER CODE END 3 */ }
void HAL_SYSTICK_Callback(void)
{
static int counter = ; if((counter ++ % ) == ){
systick.second_flag = ;
}
}
源代码下载链接:
链接:http://pan.baidu.com/s/1i51iVxb 密码:70sj