// STC單片機PWM脈寬調制功能模塊#include
本文引用地址:
http://m.butianyuan.cn/article/201611/321201.htm// PWM初始化
// 將PCA初始化為PWM模式,初始占空比為0
// 參數(shù):0,1,2,3,分別表示對PWM0、PWM1、PWM2、PWM3初始化
void PWM_Init(int i)
{
CMOD = 0x02; // 設置PCA定時器頻率為內部時鐘fosc/2
CL = 0x00;
CH = 0x00;
switch(i)
{
case 0:
CCAPM0 = 0x42; // PWM0設置PCA工作方式為PWM方式(0100 0010)
CCAP0L = 0x00; // 設置PWM0初始值與CCAP0H相同
CCAP0H = 0x00; // PWM0初始為0
break;
case 1:
CCAPM1 = 0x42;
CCAP1L = 0x00;
CCAP1H = 0x00;
break;
case 2:
CCAPM2 = 0x42;
CCAP2L = 0x00;
CCAP2H = 0x00;
break;
case 3:
CCAPM3 = 0x42;
CCAP3L = 0x00;
CCAP3H = 0x00;
break;
default:
break;
}
CR = 1; // 啟動PCA定時器
}
// PWM占空比設置
// 參數(shù)1:0,1,2,3,分別表示對PWM0、PWM1、PWM2、PWM3設置占空比
// 參數(shù)2:0x00~0xFF(或0~255),0x00時全部為低電平,0xFF時全部為高電平
void PWM_Set(int i, unsigned char c)
{
switch(i)
{
case 0:
CCAP0L = c;
CCAP0H = c;
break;
case 1:
CCAP1L = c;
CCAP1H = c;
break;
case 2:
CCAP2L = c;
CCAP2H = c;
break;
case 3:
CCAP3L = c;
CCAP3H = c;
break;
default:
break;
}
}
評論