[范例教程] 【M0】 MG32F02A 学习笔记⑰ PWM互补输出 带死区

[复制链接]
1231|0
 楼主| noctor 发表于 2018-11-12 15:38 | 显示全部楼层 |阅读模式
    上回我们说到了MG32F02A的PWM触发ADC的使用。帖子详情:https://bbs.21ic.com/icview-2572420-1-1.html

     感觉没有啥特别想更新的了,如果有啥想问的可以直接回帖问我哦。
     那上次说过了PWM触发ADC,那么死区控制相信大家肯定也是需要用的,所以来放上代码。

  1. #include "MG32x02z_DRV.H"
  2. #include <stdio.h>

  3. typedef uint8_t u8;
  4. typedef uint16_t u16;
  5. typedef uint32_t u32;
  6. typedef uint64_t u64;

  7. #define TM36_PrescalerCounter_Range 120
  8. #define TM36_MainCounter_Range      1000

  9. //if user wants to delay 1ms and CK_TM00_PR is 12MHz.
  10. //The Total clocks is 12M*1ms = 12000.
  11. //User can set "clock prescaler"=100 and "pulse width"=120 .   


  12. void Sample_TM36_PWM(void)
  13. {  

  14.                 TM_TimeBaseInitTypeDef TM_TimeBase_InitStruct;   
  15.                 TM_DeInit(TM36);
  16.                 TM_TimeBaseStruct_Init(&TM_TimeBase_InitStruct);
  17.                 TM_TimeBase_InitStruct.TM_MainClockDirection =TM_UpCount;
  18.                 TM_TimeBase_InitStruct.TM_Period = TM36_MainCounter_Range - 1;
  19.                 TM_TimeBase_InitStruct.TM_Prescaler = TM36_PrescalerCounter_Range - 1;
  20.                 TM_TimeBase_InitStruct.TM_CounterMode = Cascade;
  21.                
  22.                 TM_TimeBase_Init(TM36, &TM_TimeBase_InitStruct);
  23.                 TM_CH0Function_Config(TM36, TM_16bitPWMDTG);
  24.                 TM_CH2Function_Config(TM36, TM_16bitPWMDTG);
  25.           
  26.                 TM_OC01Output_Cmd(TM36,ENABLE);    //  TM36_OC01  
  27.                 TM_InverseOC0z_Cmd(TM36, DISABLE);
  28.                 TM_OC0zOutputState_Init(TM36, CLR);   
  29.                 TM_SetCC0A(TM36,500);        
  30.                 TM_SetCC0B(TM36,500);       
  31.                 TM_SetDeadZoneClockDivider(TM36,TM_DTDIV8);      //DeadTime DIV
  32.                 TM_SetDeadTime(TM36,200);                     //DeadTime count
  33.                
  34.                 TM_OC2Output_Cmd(TM36,ENABLE);     //  TM36_OC2
  35.                 TM_InverseOC2_Cmd(TM36, DISABLE);
  36.                 TM_OC2OutputState_Init(TM36, CLR);   
  37.                 TM_OC2NOutput_Cmd(TM36, ENABLE);           //DeadTime should be used with OCN
  38.                 TM_InverseOC2N_Cmd(TM36, DISABLE);
  39.                 TM_SetCC2A(TM36,300);        
  40.                 TM_SetCC2B(TM36,300);

  41.                 TM_AlignmentMode_Select(TM36, TM_EdgeAlign);
  42.                 TM_ClearFlag(TM36, TMx_CF0A | TMx_CF1A | TMx_CF2A |TMx_CF3A);
  43.                 TM_Timer_Cmd(TM36,ENABLE);
  44.         }

  45. void CSC_Init (void)
  46. {
  47.         CSC_PLL_TyprDef CSC_PLL_CFG;
  48.        
  49.   UnProtectModuleReg(MEMprotect);             // Setting flash wait state
  50.   MEM_SetFlashWaitState(MEM_FWAIT_ONE);        // 50MHz> Sysclk >=25MHz
  51.   ProtectModuleReg(MEMprotect);

  52.   UnProtectModuleReg(CSCprotect);
  53.         CSC_CK_APB_Divider_Select(APB_DIV_1);        // Modify CK_APB divider        APB=CK_MAIN/1
  54.         CSC_CK_AHB_Divider_Select(AHB_DIV_1);        // Modify CK_AHB divider        AHB=APB/1

  55.        
  56.         /* CK_HS selection */
  57.         CSC_IHRCO_Select(IHRCO_12MHz);                        // IHRCO Sel 12MHz
  58.         CSC_IHRCO_Cmd(ENABLE);
  59.         while(CSC_GetSingleFlagStatus(CSC_IHRCOF) == DRV_Normal);
  60.         CSC_ClearFlag(CSC_IHRCOF);
  61.         CSC_CK_HS_Select(HS_CK_IHRCO);                        // CK_HS select IHRCO
  62.        
  63.         /* PLL */
  64.         /**********************************************************/
  65.         CSC_PLL_CFG.InputDivider=PLLI_DIV_2;        // 12M/2=6M
  66.         CSC_PLL_CFG.Multiplication=PLLIx16;                // 6M*16=96M
  67.         CSC_PLL_CFG.OutputDivider=PLLO_DIV_2;        // PLLO=96M/2=48M
  68.         CSC_PLL_Config(&CSC_PLL_CFG);
  69.         CSC_PLL_Cmd(ENABLE);
  70.         while(CSC_GetSingleFlagStatus(CSC_PLLF) == DRV_Normal);
  71.         CSC_ClearFlag(CSC_PLLF);
  72.         /**********************************************************/

  73.        
  74.         /* CK_MAIN */
  75.         CSC_CK_MAIN_Select(MAIN_CK_HS);       


  76.         /* Configure ICKO function */
  77.                
  78.         /* Configure peripheral clock */
  79.         CSC_PeriphOnModeClock_Config(CSC_ON_PortA,ENABLE);                                          // Enable Port A clock
  80.         CSC_PeriphOnModeClock_Config(CSC_ON_PortB,ENABLE);                                          // Enable Port B clock
  81.         CSC_PeriphOnModeClock_Config(CSC_ON_PortC,ENABLE);                                          // Enable Port C clock
  82.         CSC_PeriphOnModeClock_Config(CSC_ON_PortD,ENABLE);                                          // Enable Port D clock
  83.        
  84.         CSC_PeriphProcessClockSource_Config(CSC_TM36_CKS, CK_APB);
  85.         CSC_PeriphOnModeClock_Config(CSC_ON_TM36, ENABLE);                                          // Enable TIM36 module clock

  86.   ProtectModuleReg(CSCprotect);
  87.    
  88. }


  89. int main()
  90. {
  91.         int i;
  92.         double x;
  93.         PIN_InitTypeDef PINX_InitStruct;
  94.        
  95.         CSC_Init();
  96.         Sample_TM36_PWM();
  97.                                        
  98.         PINX_InitStruct.PINX_Mode            = PINX_Mode_PushPull_O;                 // Pin select Push Pull mode
  99.         PINX_InitStruct.PINX_PUResistant     = PINX_PUResistant_Enable;             // Enable pull up resistor
  100.         PINX_InitStruct.PINX_Speed           = PINX_Speed_Low;                        
  101.         PINX_InitStruct.PINX_OUTDrive        = PINX_OUTDrive_Level0;                 // Pin output driver full strength.
  102.         PINX_InitStruct.PINX_FilterDivider   = PINX_FilterDivider_Bypass;            // Pin input deglitch filter clock divider bypass
  103.         PINX_InitStruct.PINX_Inverse         = PINX_Inverse_Disable;                 // Pin input data not inverse       
  104.        
  105.         PINX_InitStruct.PINX_Alternate_Function = 6;                                //AFS = TM36OC01
  106.         GPIO_PinMode_Config(PINB(8),&PINX_InitStruct);                             

  107.   PINX_InitStruct.PINX_Alternate_Function = 6;                                //AFS =  TM36OC2N
  108.         GPIO_PinMode_Config(PIND(1),&PINX_InitStruct);                 

  109.          
  110.         i=0;

  111.         while(1)
  112.         {
  113.         }       

  114. }




您需要登录后才可以回帖 登录 | 注册

本版积分规则

26

主题

82

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部