1 /********************************** (C) COPYRIGHT *******************************
2 * File Name          : CH57x_pwm.c
3 * Author             : WCH
4 * Version            : V1.0
5 * Date               : 2018/12/15
6 * Description
7 *******************************************************************************/
8 
9 #include "CH57x_common.h"
10 
11 
12 /*******************************************************************************
13 * Function Name  : PWMX_CycleCfg
14 * Description    : PWM4-PWM11基准时钟配置
15 * Input          : cyc:
16                     refer to PWMX_CycleTypeDef
17 * Return         : None
18 *******************************************************************************/
PWMX_CycleCfg(PWMX_CycleTypeDef cyc)19 void PWMX_CycleCfg( PWMX_CycleTypeDef cyc )
20 {
21     switch( cyc )
22     {
23         case PWMX_Cycle_256:
24             R8_PWM_CONFIG = R8_PWM_CONFIG & 0xf0;
25             break;
26 
27         case PWMX_Cycle_255:
28             R8_PWM_CONFIG = (R8_PWM_CONFIG & 0xf0) | 0x01;
29             break;
30 
31         case PWMX_Cycle_128:
32             R8_PWM_CONFIG = (R8_PWM_CONFIG & 0xf0) | (1<<2);
33             break;
34 
35         case PWMX_Cycle_127:
36             R8_PWM_CONFIG = (R8_PWM_CONFIG & 0xf0) | (1<<2) | 0x01;
37             break;
38 
39         case PWMX_Cycle_64:
40             R8_PWM_CONFIG = (R8_PWM_CONFIG & 0xf0) | (2<<2);
41             break;
42 
43         case PWMX_Cycle_63:
44             R8_PWM_CONFIG = (R8_PWM_CONFIG & 0xf0) | (2<<2) | 0x01;
45             break;
46 
47         case PWMX_Cycle_32:
48             R8_PWM_CONFIG = (R8_PWM_CONFIG & 0xf0) | (3<<2);
49             break;
50 
51         case PWMX_Cycle_31:
52             R8_PWM_CONFIG = (R8_PWM_CONFIG & 0xf0) | (3<<2) | 0x01;
53             break;
54 
55         default :
56             break;
57     }
58 }
59 
60 /*******************************************************************************
61 * Function Name  : PWMX_ACTOUT
62 * Description    : PWM4-PWM11通道输出波形配置
63 * Input          : ch:  select channel of pwm
64                     refer to channel of PWM define
65                    da:  effective pulse width
66                    pr:  select wave polar
67                     refer to PWMX_PolarTypeDef
68                    s :  control pwmx function
69                     ENABLE  - 输出PWM
70                     DISABLE - 关闭PWM
71 * Return         : None
72 *******************************************************************************/
PWMX_ACTOUT(UINT8 ch,UINT8 da,PWMX_PolarTypeDef pr,UINT8 s)73 void PWMX_ACTOUT( UINT8 ch, UINT8 da, PWMX_PolarTypeDef pr, UINT8 s)
74 {
75     UINT8 i;
76 
77     if(s == DISABLE)    R8_PWM_OUT_EN &= ~(ch);
78     else
79     {
80         (pr)?(R8_PWM_POLAR|=(ch)):(R8_PWM_POLAR&=~(ch));
81         for(i=0; i<8; i++)
82         {
83             if((ch>>i)&1)       *((PUINT8V)((&R8_PWM4_DATA)+i)) = da;
84         }
85         R8_PWM_OUT_EN |= (ch);
86     }
87 }
88 
89 /*******************************************************************************
90 * Function Name  : PWMX_AlterOutCfg
91 * Description    : PWM 交替输出模式配置
92 * Input          : ch:  select group of PWM alternate output
93                     RB_PWM4_5_STAG_EN   -  PWM4 和 PWM5 通道交替输出
94                     RB_PWM6_7_STAG_EN   -  PWM6 和 PWM7 通道交替输出
95                     RB_PWM8_9_STAG_EN   -  PWM8 和 PWM9 通道交替输出
96                     RB_PWM10_11_STAG_EN -  PWM10 和 PWM11 通道交替输出
97                    s :  control pwmx function
98                     ENABLE  - 打开交替输出功能
99                     DISABLE - 关闭交替输出功能
100 * Return         : None
101 *******************************************************************************/
PWMX_AlterOutCfg(UINT8 ch,UINT8 s)102 void PWMX_AlterOutCfg( UINT8 ch, UINT8 s)
103 {
104     if(s == DISABLE)        R8_PWM_CONFIG &= ~(ch);
105     else                    R8_PWM_CONFIG |= (ch);
106 }
107 
108 
109 
110 
111