欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

proteus仿真stm32控制呼吸灯

程序员文章站 2022-07-10 16:05:02
proteus仿真stm32控制呼吸灯设计思路:采用stm32f103r6为主控芯片,及led-green;时钟源为晶振为8M的系统内部时钟;使用的定时器为TIM2,并且使用PWM Generation CH1为PWM输出;proteus仿真电路设计软件设计:/*** TIM2和PWM配置 ***/TIM_HandleTypeDef htim2; /* TIM2 init function */void MX_TIM2_Init(void){ TIM_Cloc...

proteus仿真stm32控制呼吸灯

  • 设计思路:

采用stm32f103r6为主控芯片,及led-green;时钟源为晶振为8M的系统内部时钟;使用的定时器为TIM2,并且使用PWM Generation CH1为PWM输出;

  • proteus仿真电路设计

proteus仿真stm32控制呼吸灯

  • 软件设计:
/***   TIM2和PWM配置    ***/
TIM_HandleTypeDef htim2; 
/* TIM2 init function */
void MX_TIM2_Init(void)
{  
 TIM_ClockConfigTypeDef sClockSourceConfig = {0};
 TIM_MasterConfigTypeDef sMasterConfig = {0}; 
 TIM_OC_InitTypeDef sConfigOC = {0};
 htim2.Instance = TIM2;  htim2.Init.Prescaler = 8000-1;
 htim2.Init.CounterMode = TIM_COUNTERMODE_UP; 
 htim2.Init.Period = 100-1; 
 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 if (HAL_TIM_Base_Init(&htim2) != HAL_OK) 
 {    
  	Error_Handler();
  } 
 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;  
 if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) 
 {    
 	Error_Handler();
  } 
 if (HAL_TIM_PWM_Init(&htim2) != HAL_OK) 
 {    
 	Error_Handler();
 } 
 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 sMasterConfig.MasterSlaveModeTIM_MASTERSLAVEMODE_DISABLE;  
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) 
 {   
 	 Error_Handler(); 
 }  
 sConfigOC.OCMode = TIM_OCMODE_PWM1;  
 sConfigOC.Pulse = 50;  
 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;  
 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; 
 if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
 {    
 	Error_Handler(); 
 } 
 HAL_TIM_MspPostInit(&htim2); 
 }
/***   开启PWM    ***/
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
/***   主函数    ***/
int main(void)
{  
 /* USER CODE BEGIN 1 */  
 /* 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 */
 ystemClock_Config();  
 /* USER CODE BEGIN SysInit */
 /* USER CODE END SysInit */  
 /* Initialize all configured peripherals */
 MX_GPIO_Init(); 
 MX_TIM2_Init();
 /* USER CODE BEGIN 2 */
 HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
 int i = 0; 
 /* USER CODE END 2 */   
 /* Infinite loop */  
 /* USER CODE BEGIN WHILE */ 
  while (1) 
  {   
    /* USER CODE END WHILE */   
    /* USER CODE BEGIN 3 */ 
    for(i=0;i<=100;i++)	
 	{		
 	 __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1,  i);
	 HAL_Delay(100);		
	 i+=9;	
	}	  
    for(i=100;i>=0;i--)	
	{		  
 	__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1,  i);
	HAL_Delay(100);		
	i-=9;	
	}   
  }  
 /* USER CODE END 3 */
}
  • Keil仿真PWM引脚输出波形:
    proteus仿真stm32控制呼吸灯

本文地址:https://blog.csdn.net/weixin_45323971/article/details/107433930