Programmation du PWM de la coupe Lanqiao intégrée

Tout d'abord, nous confirmons si la broche PWM de sortie est
Insérez la description de l'image ici
PA6 TIM3 CH1 en tant que sortie 1
PA7 TIM3 CH2 en tant que sortie 2
ou ouvrons le fichier de la bibliothèque de micrologiciels 32 sur la sortie PWM. Le
chemin est le suivant:
\ STM32 micrologiciel bibliothèque v3.5 \ STM32F10x_StdPeriph_Lib_V3.5.0 \ Project \ STM32F10x_StdPeriph_Examples \
Trouvez la fonction principale de TIM \ PWM_Output et copiez tout le contenu dans la fonction
void TIM3_PWM_Init (void) que nous avons créée

void TIM3_PWM_Init(void{
    
    }

Parce que la quantité de code est trop grande pour affecter la lecture, donc je ne le posterai pas

Ensuite, nous copions le nom du destructeur

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;

Ensuite, nous écrivons la valeur d'écran pré-divisé

uint16_t PrescalerValue = 0;

Ensuite, la valeur de CCR1 est écrite

uint16_t CCR1_Val = 333;

Ensuite, nous supprimons les fonctions configurées
par le CCR restant. Les fonctions du CCR restant sont les suivantes

  /* PWM1 Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
  TIM_OC2Init(TIM3, &TIM_OCInitStructure);
  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
  /* PWM1 Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
  TIM_OC3Init(TIM3, &TIM_OCInitStructure);
  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
  /* PWM1 Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
    TIM_OC4Init(TIM3, &TIM_OCInitStructure);
  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

Ensuite, configurez les deux fonctions que nous avons rapportées.
En fait, la bibliothèque de firmware utilise des références de fonctions.
Il suffit de remplacer les noms des modules fonction correspondants.

Le premier est le fichier RCC

void RCC_Configuration(void)
{
    
    
  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                         RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
}

La sortie PWM que nous utilisons est
TIM3 CCR2 correspondant à la broche
PA7 TIM2 CCR2 correspondant à la broche PA1
Insérez la description de l'image ici

Insérez la description de l'image ici

On ne garde donc que les ports TIM3 et PA et l'horloge multiplexée

  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

Vient ensuite le port IO de la broche GPIO.
Identique au processus de configuration précédent,
recherchez ce code

  /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);	

Alors gardez simplement PA7

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

Ensuite, nous avons configuré l'entrée et la sortie d'un port IO.
Ensuite, calculons la fréquence PWM de sortie.
Jetons un coup d'œil à ce commentaire.

  /* -----------------------------------------------------------------------
    TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
    The TIM3CLK frequency is set to SystemCoreClock (Hz), to get TIM3 counter
    clock at 24 MHz the Prescaler is computed as following:
     - Prescaler = (TIM3CLK / TIM3 counter clock) - 1
    SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
    and Connectivity line devices and to 24 MHz for Low-Density Value line and
    Medium-Density Value line devices

    The TIM3 is running at 36 KHz: TIM3 Frequency = TIM3 counter clock/(ARR + 1)
                                                  = 24 MHz / 666 = 36 KHz
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
    TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
    TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
  ----------------------------------------------------------------------- */

Il dit qu'il a décomposé 72Mhz en 24Mhz,
je suppose que c'est cette phrase

  PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;

La valeur de pré-allocation est égale à l'horloge divisée par 24000000-1
, puis l'horloge est divisée par la valeur du prédécaleur
. L'avantage d'écrire de cette façon est que peu importe combien votre fréquence d'horloge est divisée par cette PrescalerValue, vous
obtenez 24Mhz

Puis j'ai vu que 24Mhz était décomposé en 36KHz,
je suppose que c'était cette phrase

  TIM_TimeBaseStructure.TIM_Period = 665;

Réglez le cycle à 665 pour terminer un cycle et la fréquence devient 36 KHz
. Si nous voulons obtenir 40 KHz,
nous devons le calculer en sens inverse.240 000 K / 40 K = 600,
donc nous changeons

  TIM_TimeBaseStructure.TIM_Period = 599;

Il devrait donc être possible d'obtenir une sortie d'onde carrée de 40 KHz pour une utilisation par ultrasons. Ce qui
suit est le code complet de la configuration PWM


#include "timer.h"
_Bool TIM2_led_flag =0; 
u8 Tim2_conter =0;

void TIM2_Config(void)
{
    
    
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrure;
  NVIC_InitTypeDef NVIC_InitStrure;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	
	TIM_TimeBaseInitStrure.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStrure.TIM_Period=1000;//¼ÆÊýµ½1000 µÈÓÚ³ËÒÔÁË1000
	TIM_TimeBaseInitStrure.TIM_Prescaler=  71 ; //72HMz/72=1Mhz = 1 000 000
	TIM_TimeBaseInitStrure.TIM_RepetitionCounter= 0;                //Öظ´ÇëÇó¼ÆÊýÆ÷
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStrure);
	
  TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
	TIM_Cmd(TIM2,ENABLE);
	
	
	NVIC_InitStrure.NVIC_IRQChannel=TIM2_IRQn;
	NVIC_InitStrure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStrure.NVIC_IRQChannelPreemptionPriority=0;
	NVIC_InitStrure.NVIC_IRQChannelSubPriority=1;
	NVIC_Init(&NVIC_InitStrure);
	
}
void TIM2_IRQHandler()
{
    
    
	if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
	{
    
    
		Tim2_conter++;
		
		TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
		if(Tim2_conter > 250)
		{
    
    
		Tim2_conter=0;
		TIM2_led_flag = !TIM2_led_flag;
		}
	}
}

void TIM3_PWM_Init(void)
{
    
    
 uint16_t PrescalerValue = 0;
 uint16_t CCR1_Val = 333;
 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 TIM_OCInitTypeDef  TIM_OCInitStructure;
 GPIO_InitTypeDef GPIO_InitStructure;   
  /* System Clocks Configuration */
  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

  /* GPIO Configuration */
    /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);	


  /* -----------------------------------------------------------------------
    TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
    The TIM3CLK frequency is set to SystemCoreClock (Hz), to get TIM3 counter
    clock at 24 MHz the Prescaler is computed as following:
     - Prescaler = (TIM3CLK / TIM3 counter clock) - 1
    SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
    and Connectivity line devices and to 24 MHz for Low-Density Value line and
    Medium-Density Value line devices

    The TIM3 is running at 36 KHz: TIM3 Frequency = TIM3 counter clock/(ARR + 1)
                                                  = 24 MHz / 666 = 36 KHz
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
    TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
    TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
  ----------------------------------------------------------------------- */
  /* Compute the prescaler value */
  PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 599;
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC2Init(TIM3, &TIM_OCInitStructure);

  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

  TIM_ARRPreloadConfig(TIM3, ENABLE);

  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);

}




void TIM2_PWM_Init(void)
{
    
    
 uint16_t PrescalerValue = 0;
 uint16_t CCR1_Val = 300;
 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 TIM_OCInitTypeDef  TIM_OCInitStructure;
 GPIO_InitTypeDef GPIO_InitStructure;   
  /* System Clocks Configuration */
  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

  /* GPIO Configuration */
    /*GPIOB Configuration: TIM2 channel1, 2, 3 and 4 */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE);	


  /* -----------------------------------------------------------------------
    TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
    The TIM3CLK frequency is set to SystemCoreClock (Hz), to get TIM3 counter
    clock at 24 MHz the Prescaler is computed as following:
     - Prescaler = (TIM3CLK / TIM3 counter clock) - 1
    SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
    and Connectivity line devices and to 24 MHz for Low-Density Value line and
    Medium-Density Value line devices

    The TIM3 is running at 36 KHz: TIM3 Frequency = TIM3 counter clock/(ARR + 1)
                                                  = 24 MHz / 666 = 36 KHz
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
    TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
    TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
  ----------------------------------------------------------------------- */
  /* Compute the prescaler value */
  PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 599;
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC2Init(TIM2, &TIM_OCInitStructure);

  TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);

  TIM_ARRPreloadConfig(TIM2, ENABLE);

  /* TIM3 enable counter */
  TIM_Cmd(TIM2, ENABLE);

}

Je suppose que tu aimes

Origine blog.csdn.net/m0_46179894/article/details/108170788
conseillé
Classement