当前位置:网站首页>STM32F103RCT6

STM32F103RCT6

2022-06-25 21:58:00 Dear_ Atri

In the configuration structure TIM_OCInitTypeDef when , The elements are as follows :

  • (1) TIM_OCMode: Compare output mode selection , There are eight in all , The common one is PWM1/PWM2. It sets CCMRx register OCxM[2:0] The value of a .
  • (2) TIM_OutputState: Compare output enable , Determine the final output comparison signal OCx Whether to output through external pins . It sets TIMx_CCER register CCxE/CCxNE The value of a .
  • (3) TIM_OutputNState: Compare complementary output enable , decision OCx The complementary signals OCxN Whether to output through external pins . It sets CCER register CCxNE The value of a .
  • (4) TIM_Pulse: Compare the output pulse width , Actually set the comparison register CCR Value , Determine the pulse width . The settable range is 0 to 65535.
  • (5) TIM_OCPolarity: Compare the output polarity , Optional OCx For high level active or low level active . It determines the effective level of the timer channel . It sets CCER The register of CCxP The value of a .
  • (6) TIM_OCNPolarity: Compare complementary output polarity , Optional OCxN For high level active or low level active . It sets TIMx_CCER The register of CCxNP The value of a .
  • (7) TIM_OCIdleState: Channel output level setting in idle state , Optional output 1 Or output 0, That is, in the idle state (BDTR_MOE Position as 0) when , After the dead time, the timer channel outputs high level or low level . It sets CR2 The register of OISx The value of a .
  • (8) TIM_OCNIdleState: Complementary channel output level setting in idle state , Optional output 1 Or output 0, That is, in the idle state (BDTR_MOE Position as 0) when , After the dead time, the timer complementary channel outputs high level or low level flat , The set value must be the same as TIM_OCIdleState contrary . It's set to be CR2 The register of OISxN The value of a .

Initialize structure TIM_TimeBaseInitTypeDef and TIM_OCInitTypeDef after , You can set the duty cycle to PWM Output .

void TIM3_PWM_Init(u32 ARR , u32 PSC)
{		 					 
	// This part needs to be modified manually IO Port setup 
	
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);  	//TIM3 Clock enable     
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 	// Can make PORTA The clock 	
	
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_TIM3); //GPIOA6 Reuse as timer 3
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;           //GPIOFA
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        // Reuse function 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;	// Speed 100MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      // Push pull multiplex output 
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        // Pull up 
	GPIO_Init(GPIOA,&GPIO_InitStructure);              // initialization PA6
	  
	TIM_TimeBaseStructure.TIM_Prescaler=PSC;  // Timer frequency division 
	TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; // Upcount mode 
	TIM_TimeBaseStructure.TIM_Period=ARR;   // Automatic reload load value 
	TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);// Initialize the timer 3
	
	// initialization TIM14 Channel1 PWM Pattern 	 
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // Select timer mode :TIM Pulse width modulation mode 2
 	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // Compare output enable 
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // Output polarity :TIM The output is less polar 
	TIM_OC1Init(TIM3, &TIM_OCInitStructure);  // according to T The specified parameter initializes the peripheral TIM1 4OC1

	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);  // Can make TIM3 stay CCR1 Pre loaded registers on 
 
    TIM_ARRPreloadConfig(TIM3,ENABLE);//ARPE Can make  
	
	TIM_Cmd(TIM3, ENABLE);  // Can make TIM3										  
} 
int main(void)
{
	u16 ledpwmval=0;
	u8 flag=1;
	delay_init(168); 	// Initialization delay function 
  LED_GPIO_Config();     // initialization LED
  TIM3_PWM_Init(100-1,84-1); // Initialize the timer 
	
	while(1)
	{
		delay_ms(10);
		if(flag)ledpwmval++;
		else ledpwmval--;
		if(ledpwmval==0)flag=1;
		if(ledpwmval==40)flag=0;
		
		TIM_SetCompare1(TIM3,ledpwmval);	// Modify the comparison value , Change the duty cycle 
 	}
}

原网站

版权声明
本文为[Dear_ Atri]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202181143219322.html