找回密码
 注册

QQ登录

只需一步,快速开始

搜索

STC15W4K60S4单片机2路10位PWM基本应用程序

[复制链接]
coolfire 发表于 2021-5-6 00:26:46 | 显示全部楼层 |阅读模式
部分程序:完整程序见附件
  1. /*************        功能说明        **************

  2. STC15W4K60S4 2路10位PWM基本应用.

  3. PWM0  为10位PWM.
  4. PWM1  为10位PWM.

  5. ******************************************/

  6. #define MAIN_Fosc                24000000L        //定义主时钟

  7. #include        "STC15Fxxxx.H"

  8. #define        PCA0                        0
  9. #define        PCA1                        1
  10. #define        PCA_Counter                3
  11. #define        PCA_P12_P11_P10        (0<<4)
  12. #define        PCA_P34_P35_P36        (1<<4)
  13. #define        PCA_P24_P25_P26        (2<<4)
  14. #define        PCA_Mode_PWM                                0x42        //B0100_0010
  15. #define        PCA_Mode_Capture                        0
  16. #define        PCA_Mode_SoftTimer                        0x48        //B0100_1000
  17. #define        PCA_Mode_HighPulseOutput        0x4c        //B0100_1100
  18. #define        PCA_Clock_1T        (4<<1)
  19. #define        PCA_Clock_2T        (1<<1)
  20. #define        PCA_Clock_4T        (5<<1)
  21. #define        PCA_Clock_6T        (6<<1)
  22. #define        PCA_Clock_8T        (7<<1)
  23. #define        PCA_Clock_12T        (0<<1)
  24. #define        PCA_Clock_Timer0_OF        (2<<1)
  25. #define        PCA_Clock_ECI        (3<<1)
  26. #define        PCA_Rise_Active        (1<<5)
  27. #define        PCA_Fall_Active        (1<<4)
  28. #define        PCA_PWM_8bit        (0<<6)
  29. #define        PCA_PWM_7bit        (1<<6)
  30. #define        PCA_PWM_6bit        (2<<6)
  31. #define        PCA_PWM_10bit        (3<<6)

  32. void        PCA_config(void);
  33. void        UpdatePwm(u8 PCA_id, u16 pwm_value);


  34. /************************************************/

  35. /**********************************************/
  36. void main(void)
  37. {
  38.         PCA_config();

  39. //        EA = 1;

  40.         UpdatePwm(PCA0,800);
  41.         UpdatePwm(PCA1,400);
  42.       
  43.         while (1)
  44.         {
  45.       
  46.         }
  47. }

  48. //========================================================================
  49. // 函数: void        PCA_config(void)
  50. // 描述: PCA初始化函数。
  51. // 参数: none.
  52. // 返回: none.
  53. // 版本: VER1.0
  54. // 日期: 2014-12-15
  55. // 备注:
  56. //========================================================================
  57. void        PCA_config(void)
  58. {
  59.         CR = 0;
  60.         CH = 0;
  61.         CL = 0;

  62. //        AUXR1 = (AUXR1 & ~(3<<4)) | PCA_P12_P11_P10;        P1n_standard(0x07);        //切换到P1.2 P1.1 P1.0 (ECI CCP0 CCP1)
  63. //        AUXR1 = (AUXR1 & ~(3<<4)) | PCA_P24_P25_P26;        P2n_standard(0x70);        //切换到P2.4 P2.5 P2.6 (ECI CCP0 CCP1)
  64.         AUXR1 = (AUXR1 & ~(3<<4)) | PCA_P34_P35_P36;        P3n_standard(0x70);        //切换到P3.4 P3.5 P3.6 (ECI CCP0 CCP1)

  65.         CMOD  = (CMOD  & ~(7<<1)) | PCA_Clock_1T;        //选择时钟源
  66.         CMOD  &= ~1;                                                                //禁止溢出中断
  67. //        PPCA = 1;        //高优先级中断

  68.         CCAPM0 = PCA_Mode_PWM;        //工作模式
  69.         PCA_PWM0  = (PCA_PWM0 & ~(3<<6)) | PCA_PWM_10bit;        //PWM宽度
  70.         CCAP0L = 0xff;
  71.         CCAP0H = 0xff;

  72.         CCAPM1 = PCA_Mode_PWM;        //工作模式
  73.         PCA_PWM1  = (PCA_PWM1 & ~(3<<6)) | PCA_PWM_10bit;        //PWM宽度
  74.         CCAP1L = 0xff;
  75.         CCAP1H = 0xff;
  76.         CR = 1;
  77. }

  78. //========================================================================
  79. // 函数: UpdatePwm(u8 PCA_id, u16 pwm_value)
  80. // 描述: 更新PWM值.
  81. // 参数: PCA_id: PCA序号. 取值 PCA0,PCA1
  82. //                 pwm_value: pwm值, 0~1024, 这个值是输出高电平的时间, 0对应连续的低电平, 1024对应连续的高电平.
  83. // 返回: none.
  84. // 版本: V1.0, 2012-11-22
  85. //========================================================================
  86. void        UpdatePwm(u8 PCA_id, u16 pwm_value)
  87. {
  88.         if(pwm_value > 1024)        return;        //PWM值过大, 退出
  89.       
  90.         if(PCA_id == PCA0)
  91.         {
  92.                 if(pwm_value == 0)
  93.                 {
  94.                         PCA_PWM0 |= 0x32;
  95.                         CCAP0H = 0xff;
  96.                 }
  97.                 else
  98.                 {
  99.                         pwm_value = ~(pwm_value-1) & 0x3ff;
  100.                         PCA_PWM0 = (PCA_PWM0 & ~0x32) | ((u8)(pwm_value >> 4) & 0x30);
  101.                         CCAP0H = (u8)pwm_value;
  102.                 }
  103.         }
  104.         else if(PCA_id == PCA1)
  105.         {
  106.                 if(pwm_value == 0)
  107.                 {
  108.                         PCA_PWM1 |= 0x32;
  109.                         CCAP1H = 0xff;
  110.                 }
  111.                 else
  112.                 {
  113.                         pwm_value = ~(pwm_value-1) & 0x3ff;
  114.                         PCA_PWM1 = (PCA_PWM1 & ~0x32) | ((u8)(pwm_value >> 4) & 0x30);
  115.                         CCAP1H = (u8)pwm_value;
  116.                 }
  117.         }
  118. }
复制代码
15W4K-PCA-2路10位硬件PWM源码.rar (41.37 KB, 售价: 2 E币)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-25 00:47

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表