stc12c5a60s2單片機(jī)spwm發(fā)生程序帶仿真文件
下面是主程序部分:
#include
#include
#define uint unsigned int
uchar index;
uchar a;
void init_pca(void)//pca計(jì)數(shù)器初始化函數(shù)
{
CMOD=0x04;//計(jì)數(shù)器0的溢出為PCA計(jì)數(shù)器的時(shí)鐘源
CCON=0x00;
CCAPM0=0x42;//8位PWM輸出,無(wú)中斷
CL=0x00;//清零pca計(jì)數(shù)器
CH=0x00;
CCAP0L=pwm[0]; //初始化spwm輸出的占空比
CCAP0H=pwm[0];
CR=1;//運(yùn)行adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=17&is_app=0&jk=25357d4a26439b28&k=pca&k0=pca&kdi0=0&luki=3&n=10&p=baidu&q=98059059_cpr&rb=0&rs=1&seller_id=1&sid=289b43264a7d3525&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1831118&u=http%3A%2F%2Fwww%2E51hei%2Ecom%2Fmcu%2F2118%2Ehtml&urlid=0" id="5_nwl" mpid="5" target="_blank">pca計(jì)數(shù)器
}
void init_timer(void)//計(jì)數(shù)器初始化函數(shù)
{
TMOD= 0x12; //計(jì)數(shù)器0工作在方式模式2,計(jì)數(shù)器1工作在方式1
TH0 = 0xFF;//T0的計(jì)數(shù)值為5,把時(shí)鐘源5分頻
TL0 = 0xFF;
TH1 = 0xF6;//T1的計(jì)數(shù)值為625,若是12M晶振,則中斷頻率為12M/625=1920HZ,正弦頻率為1920/32=600HZ,即600/12=50HZ
TL1 = 0xA0; //T1的計(jì)數(shù)值低位
AUXR=0x80;//計(jì)數(shù)器均工作在8051速度
//ET0 = 1; //開(kāi)中計(jì)數(shù)器0斷
ET1 = 1; //開(kāi)計(jì)數(shù)器1中斷
TR0 = 1; //開(kāi)啟計(jì)數(shù)器0
TR1 = 1; //開(kāi)計(jì)數(shù)器1
}
void int_timer1(void) interrupt 3//計(jì)數(shù)器1中斷函數(shù)
{
CR=0;
a=a+1;
index++; //查表索引
TH1=0xFD; //重裝計(jì)數(shù)值
TL1=0x8F;
CCAP0H=pwm[index%32]*2;//改變pwm的占空比
CR=1;
}
void main()
{a=0;
EA=1;//開(kāi)總中斷
init_pca();
init_timer();
while(1);
//-.-//
}
評(píng)論