STM32系列 PWM輸出

不折不扣發表於2024-06-04

STM32系列 PWM輸出
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "timer.h"
//ALIENTEK戰艦STM32開發板實驗9
//PWM輸出實驗  
//技術支援:www.openedv.com
//廣州市星翼電子科技有限公司 

void sendback(void)
{
      unsigned char t;
    
        for(t=0;t<myusart.recount;t++)
        {
            USART_SendData(USART1, myusart.rcbuf[t]);//向串列埠1傳送資料
            while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待傳送結束
        }    
}
void Com_Handle(void)
{
    unsigned long long arr=0;
    unsigned int psc=0;
  //資料格式判斷
    if((myusart.rcbuf[0]!= 0xaa) || (myusart.rcbuf[myusart.recount-1]!=0xff))//頭碼是否為0xaa,結束碼是否為0xff
    {
        return;

    }
    else
    {
        //如何處理四個資料,四位元組轉成一個數
        //00  00   A0  28
        arr=(myusart.rcbuf[1]<<24)+(myusart.rcbuf[2]<<16)+ (myusart.rcbuf[3]<<8) + myusart.rcbuf[4];
        psc=5000;         //佔空比50%=5000/10000;
        set_pwm1(arr,psc);
    //是否需要回復,發什麼回覆什麼
        sendback();
            
  }
    
}


int main(void)
{        
    delay_init();             //延時函式初始化      
    NVIC_Configuration();      //設定NVIC中斷分組2:2位搶佔優先順序,2位響應優先順序
    uart_init(115200);     //串列埠初始化為115200
    LED_Init();                 //LED埠初始化
//    TIM3_PWM_Init(900-1,0);     //不分頻。PWM頻率=72000k/900=80Khz
    TIM3_PWM_Init(3-1,250-1);     //
    TIM2_Int_Init(999,71); //1ms
    while(1)
    {
        if(myusart.reflag>0)
        {
            Com_Handle();//收到什麼回覆什麼;    
            myusart.recount=0;
            myusart.reflag=0;
        }

    }     
}
main
STM32系列 PWM輸出
//TIM3 PWM部分初始化 
//PWM輸出初始化
//arr:自動重灌值
//psc:時鐘預分頻數
void TIM3_PWM_Init(u16 arr,u16 psc)
{  
 GPIO_InitTypeDef GPIO_InitStructure;
 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 TIM_OCInitTypeDef  TIM_OCInitStructure;
 
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); // 使能GPIOB時鐘
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
 // 配置IO模式
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;// 複用推輓複用輸出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
 GPIO_Init(GPIOA, &GPIO_InitStructure);            
 //初始化TIM3的計數模式、分頻值、重灌載值等
 TIM_TimeBaseStructure.TIM_Period = arr;  //ARR
 TIM_TimeBaseStructure.TIM_Prescaler = psc; //PSC
 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;// 向上計數模式 
 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

 /*TIM3_CH1輸出佔空比為20%的PWM波的定時配置*/
 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;// 選擇定時器模式:TIM脈衝寬度調製模式1
 TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable;// 比較輸出使能
 TIM_OCInitStructure.TIM_Pulse = (arr+1)/2;  //CCR
 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;// 輸出極性:TIM輸出比較極性高
 TIM_OC1Init(TIM3, &TIM_OCInitStructure);

 TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);// 使能TIM3在OC1上的預裝載暫存器
 
 TIM_Cmd(TIM3, ENABLE);//使能TIM3時鐘
}

void set_pwm1(unsigned long long arr,unsigned int psc)//例如set_pwm1(41000,1000);// 這樣就是41KHZ,10%  1000/10000=10%
{
        double T_ARR,DPwm_CCR2;
        unsigned int PWM_Per=0,TIM1_CCR1=0;

//        T_ARR=(double)72000000/arr; 
        T_ARR=(double)288000/arr; 
        DPwm_CCR2=(double)(T_ARR * psc)/10000;

        T_ARR=T_ARR+0.5;//四捨五入演算法
        DPwm_CCR2=DPwm_CCR2+0.5;//四捨五入演算法

        //    PWM_Config_step((u16)T_ARR, (u16)DPwm_CCR2, 1);
        PWM_Per = (unsigned int)T_ARR;

        TIM1_CCR1 =DPwm_CCR2;
    
      TIM_SetAutoreload(TIM3,PWM_Per-1);
      TIM_SetCompare1(TIM3,TIM1_CCR1-1);
    
}
TIM3_PWM_Init

相關文章