#include "Register_Bit.h" //包含位定义文件
//#define PWM_DIV1 (uint8_t)1 //不能设置1分频 否则定时器停止工作
#define PWM_DIV2 (uint8_t)2
#define PWM_DIV4 (uint8_t)4
#define PWM_DIV8 (uint8_t)8
#define PWM_CYC (uint16_t)750
#define PWM1_CPIEN_ENB PIER |= (1<<9) //使能PWM1比较中断 用于检测过流
#define PWM1_CPIEN_DIS PIER &= ~(1<<9)
#define PWM0_PIN_ENB PWMPOE |= (PWM0_OE)//PWMPOE |= (1<<0)
#define PWM0_PIN_DIS PWMPOE &= ~(PWM0_OE)
#define PWM2_PIN_ENB PWMPOE |= (PWM2_OE)
#define PWM2_PIN_DIS PWMPOE &= ~(PWM2_OE)
#define PWM4_PIN_ENB PWMPOE |= (PWM4_OE)
#define PWM4_PIN_DIS PWMPOE &= ~(PWM4_OE)
//*************************************
// 函数名称:Init_PWM
// 函数功能:PWM定时器分频系数 PWM重载值
// 入口参数:无
// 出口参数:无
//***************************************/
void Init_PWM(uint8_t psc,uint32_t Tcon)
{
uint8_t Tmp = 0 ;
/* Select PWM01 clock source as HCLK */
CLKSEL1 = (CLKSEL1 & (~PWM01_CLK)) | PWM01_HCLK;
/* Select PWM23 clock source as HCLK */
CLKSEL1 = (CLKSEL1 & (~PWM23_CLK)) | PWM23_HCLK;
/* Select PWM45 clock source as HCLK */
CLKSEL2 = (CLKSEL2 & (~PWM45_CLK)) | PWM45_HCLK;
/* Switch P2.2~P2.4 P2.6 to PWM0 2 4 output */
P2_MFP = P2_MFP & (~(P22_MFSEL | P24_MFSEL | P26_MFSEL))
| (P22_PWM0 | P24_PWM2 | P26_PWM4);
/* Reset PWM IP 外设先复位再使能 */
IPRSTC2 |= PWM_RST;
IPRSTC2 &= ~PWM_RST;
/* Enable pwm01 23 45 clock source */
APBCLK |= PWM01_CLKEN ;
APBCLK |= PWM23_CLKEN ;
APBCLK |= PWM45_CLKEN ;
//PWM 预分频
Tmp = psc - 1 ; //不能设置1分频 否则定时器停止工作
PPR |= (Tmp << 0) | (Tmp << 8) | (Tmp << 16);
//时钟源选择 在PWM输入时钟时分频 现设置不分频 T0-T5
/* Select PWM0~5 clock divider */
CSR |= (CSR0_CLK_1 | CSR1_CLK_1 | CSR2_CLK_1
| CSR3_CLK_1 | CSR4_CLK_1 | CSR5_CLK_1);
/* Select PWM edge-aligned. */
PCR &= EDGE_ALIGN;
/* PWM under auto-reload (continuous) mode, select output channel non-invertion, set independent mode */
PCR = PCR & (~PWMMODE) | INDEPENDENT; //独立模式
PCR |= (CH0_AU_RL | CH2_AU_RL | CH4_AU_RL
| CH1_AU_RL | CH3_AU_RL | CH5_AU_RL); //自动重载
//设置组模式 PWM0 PWM2 PWM4 同步由PWM0控制
PCR |= (1<<30) ;
//PWM 计数重载值
CNR0 = Tcon ;//1500
CNR2 = Tcon ;
CNR4 = Tcon ;
/*
CNR1 = Tcon ;
CNR3 = Tcon ;
CNR5 = Tcon ;
*/
//PWM占空比寄存器
CMR0 = 0 ;
CMR2 = 0 ;
CMR4 = 0 ;
/*
CMR1 = 0 ;
CMR3 = 0 ;
CMR5 = 0 ;
*/
/* Reset PWM counter */
PCR |= CLRPWM;
PWMPOE &= 0X00 ; //禁止使能PWM0 PWM2 PWM4 输出到引脚上
/* PWM circuit enable and start to run 开始计数 */
PCR |= (CH0EN | CH2EN | CH4EN);
PCR |= (CH1EN);
}
//*************************************
// 函数名称:ToPwm_Value
// 函数功能:重置PWM比较寄存器 ->设置占空比
// 入口参数:占空比参数
// 出口参数:无
//***************************************/
void ToPwm_Value(uint16_t InValue)
{
CMR0 = InValue ; //组模式下 PWM2 PWM4 与PWM0同步
// CMR2 = InValue ;
// CMR4 = InValue ;
}
//---实例应用------
//设置8KPWM
Init_PWM(PWM_DIV2,PWM_CYC) ; //12M/2=6M 计750次 8M_PWM
|