实验原理:
通过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 ()
{
/* USER CODE END WHILE */ /* USER CODE BEGIN 3 */
if(systick.second_flag == ){
systick.second_flag = ;
led_work_status += ;
if(led_work_status > )led_work_status = ;
switch (led_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)
{
//中断时间1ms,每ims进入一次中断
static int counter = ; if((counter ++ % ) == ){
systick.second_flag = ;
}
}
源代码下载链接:
链接:http://pan.baidu.com/s/1eSclNOI 密码:cddu
iCore4链接: