C51 COMPILER V9.54   PWM                                                                   12/03/2024 14:58:48 PAGE 1   


C51 COMPILER V9.54, COMPILATION OF MODULE PWM
OBJECT MODULE PLACED IN .\Objects\pwm.obj
COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE public\pwm.c OPTIMIZE(8,SPEED) BROWSE INCDIR(.\public) DEBUG OBJECTEXTEN
                    -D PRINT(.\Listings\pwm.lst) OBJECT(.\Objects\pwm.obj)

line level    source

   1          #include "pwm.h"
   2          
   3          
   4          void pwm_init(void)
   5          {
   6   1              //PWM时钟为系统时钟八分频
   7   1              PWM0CON = 0xF2; //P54
   8   1              PWM1CON = 0xF2; //P12
   9   1              PWM2CON = 0xF2; //P13
  10   1              PWM3CON = 0xF2; //P23
  11   1              GPIO_OUTPUT(P1, 2, 0);
  12   1              GPIO_OUTPUT(P1, 3, 0);
  13   1              GPIO_OUTPUT(P2, 3, 0);
  14   1        P5CR = 0x10;
  15   1              P5ODR = 0x00;
  16   1              ALFSEL0 |= 0x60;
  17   1              ALFSEL1 |= 0x80;
  18   1              
  19   1              
  20   1      }
  21          
  22          void pwm_ch(uint8_t ch, uint8_t per, uint8_t duty)
  23          {
  24   1              uint16_t dut;
  25   1              dut = (uint8_t)(per * ((float)duty / 100));
  26   1              switch(ch)
  27   1                      {
  28   2                              case 0:
  29   2                                      PWM0P = per;
  30   2                                PWM0D = dut;
  31   2                                      break;
  32   2                              case 1: 
  33   2                                      PWM1P = per;
  34   2                                PWM1D = dut;
  35   2                                      break;
  36   2                              case 2: 
  37   2                                      PWM2P = per;
  38   2                                PWM2D = dut;
  39   2                                      break;
  40   2                              case 3:         
  41   2                                      PWM3P = per;
  42   2                                PWM3D = dut;
  43   2                                      break;
  44   2                              default:
  45   2                                      break;                          
  46   2                      }
  47   1      }
  48          
  49          /*
  50          fre: 频率 表示 fre kHz,0不改变频率  系统时钟默认2分频2Mhz
  51          ch :PWM通道 0,1,2,3
  52          duty : 占空比 整数 如：10表示占空比10%
  53          */
  54          void pwm_set(uint16_t fre,uint8_t ch, uint8_t duty)
C51 COMPILER V9.54   PWM                                                                   12/03/2024 14:58:48 PAGE 2   

  55          {
  56   1              uint8_t temp;
  57   1              uint16_t per;
  58   1              if(fre != 0)
  59   1              {
  60   2                      temp = SYSCON & 0xC0;
  61   2                      switch(temp)
  62   2                      {
  63   3                              case 0x80: //2Mhz
  64   3                                      per = (uint8_t)(250000 / fre);
  65   3                                      pwm_ch(ch, per, duty);
  66   3                                      break;
  67   3                              case 0x00: //333Khz
  68   3              per = (uint8_t)(20833 / fre);
  69   3                                      pwm_ch(ch, per, duty);
  70   3                                      break;
  71   3                              case 0x40: //1Mhz
  72   3              per = (uint8_t)(125000 / fre);
  73   3                                      pwm_ch(ch, per, duty);
  74   3                                      break;
  75   3                              case 0xC0: //4Mhz
  76   3                                      per = (uint8_t)(500000 / fre);
  77   3                                      pwm_ch(ch, per, duty);
  78   3                                      break;
  79   3      
  80   3                              default:
  81   3                                      break;                          
  82   3                      }
  83   2              }
  84   1      }
  85          
  86          void PWM_Test(void)
  87          {
  88   1              pwm_init();
  89   1        pwm_set(38000, 1, 33);
  90   1              while(1);
  91   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    271    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----       8
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
