加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
HiPWM.c 2.46 KB
一键复制 编辑 原始数据 按行查看 历史
dk鲁晓东 提交于 2020-02-24 19:56 . 增强PWM
#include "HiPWM.h"
u16 code T_SinTable[128]={
128,134,140,146,152,158,164,170,
176,182,187,193,198,203,208,213,
217,222,226,230,233,236,240,242,
245,247,249,251,252,253,254,254,
255,254,254,253,252,251,249,247,
245,242,240,236,233,230,226,222,
217,213,208,203,198,193,187,182,
176,170,164,158,152,146,140,134,
128,121,115,109,103,97,91,85,
79,73,68,62,57,52,47,42,
38,33,29,25,22,19,15,13,
10,8,6,4,3,2,1,1,
1,1,1,2,3,4,6,8,
10,13,15,19,22,25,29,33,
38,42,47,52,57,62,68,73,
79,85,91,97,103,109,115,121
};
void HiPWM_config()
{
//引脚初始化
// 打开XSFR XSFR_EN();
////选择PWM引脚 PWM2_Sele(PWM2_Pin_P37);
////初始电平设置 PWM2_PinLevel(Low);
////设置各支路的T1和T2初始值 PWM2_T1(1); PWM2_T2(0);
////支路输出使能 PWM2_PinOutEn(OK);
////设置模块时钟源、周期 PWM_CLK_Source(PWM_CLK_sys);PWM_CLK_sysDIV(0);PWM_CYCLE_Set(PWM_CYCLE);
////最后使能PWM模块 PWM_En(OK);
// 关闭XSFR XSFR_DIS();
// u8 xdata *px;
XSFR_EN(); // 访问XFR
PWM3_Sele(PWM3_Pin_P21);
PWM3_T1(65); PWM3_T2(1220);
PWM3_PinOutEn(OK);
PWM3_PinLevel(Low);
PWM4_Sele(PWM4_Pin_P22);
PWM4_T1(65-PWM_DeadZone); PWM4_T2(1220+PWM_DeadZone);//
PWM4_PinLevel(High);
PWM4_PinOutEn(OK);
PWM_CLK_Source(PWM_CLK_sys);
PWM_CLK_sysDIV(0);
PWM_CYCLE_Set(PWM_CYCLE);//
XSFR_EN(); // 恢复访问XRAM EAXRAM();
PWM_En(OK);//PWMCR |= ENPWM; // 使能PWM波形发生器,PWM计数器开始计数
PWM_IntEN(YES); // 允许PWM计数器归零中断
}
void PWM2_SetPwmWide(unsigned short Wide)
{//p37
if (Wide == 0)
{PWM2_PinOutEn(NO);//关闭输出
PWM2 = 0; //直接输出0
}
else if (Wide == PWM_CYCLE)
{PWM2_PinOutEn(NO);
PWM2 = 1; //直接输出1
}
else
{
XSFR_EN();
PWM2_T1(Wide);PWM2_T2(0) ;
XSFR_DIS();
PWM2_PinOutEn(OK);
}
}
void PWM3_SetPwmWide(unsigned short Wide)
{//p21
if (Wide == 0)
{
PWM3_PinOutEn(NO);//关闭输出
PWM3 = 0;
}
else if (Wide == PWM_CYCLE)
{
PWM3_PinOutEn(NO);//关闭输出
PWM3 = 1;
}
else
{
XSFR_EN();
PWM3_T1(Wide);PWM3_T2(0) ;
XSFR_DIS();
PWM3_PinOutEn(OK);
}
}
void HiPWM_Routine(void) interrupt 22
{
//u8 xdata *px;
static u8 PWM_Index=0;
u16 j;
u8 SW2_tmp;
if(PWMIF & CBIF) //PWM计数器归零中断标志
{
PWMIF &= ~CBIF; //清除中断标志
SW2_tmp = P_SW2; //保存SW2设置
XSFR_EN();
j = T_SinTable[PWM_Index]+65;//因为前面设置T1从65开始的
PWM3_T2(j);
j += PWM_DeadZone; //死区
PWM4_T2(j);
P_SW2 = SW2_tmp; //恢复SW2设置
if(++PWM_Index >= 128) PWM_Index = 0;
}
}
void HiPWMFD_Routine(void) interrupt 23
{
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化