北京航空航天大学出版社出版的《C8051F系列单片机开发与C语言编程》的附盘程序。
源代码在线查看: pwm.c
/////////PCA CEX0为PWM 输出模式//////////////
/////////PWM.c//////////////////////////
#include
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long ulong;
sfr16 RCAP2=0xca;
sfr16 RCAP3=0xca;
sfr16 RCAP4=0xca;
sfr16 TMR4=0xcc;
sfr16 TMR3=0xcc;
sfr16 TMR2=0xcc;
sfr16 PCA0CP0=0xfb;
sfr16 PCA0CP1=0xfd;
sfr16 PCA0CP2=0xe9;
sfr16 PCA0CP3=0xeb;
sfr16 PCA0CP4=0xed;
sfr16 PCA0CP5=0xe1;
uchar temppage;
void delay1ms(uint time){//延迟1ms
uint i;
uint j;
for (i=0;i for(j=0;j }
}
void pca0ini(){//CEX0为8bit PWM输出模式
SFRPAGE = 0x00;
PCA0CPM0=0X42;
}
void PWM0_set(uchar low){//占空比设置,高电平占空比为(256-low)/256
SFRPAGE = 0x00;
PCA0CPH0=low;
}
void pcaini(){
SFRPAGE=0x00;
PCA0MD=0x08;//PCA采用系统时钟,且PCA溢出中断禁止
PCA0CN=0x40;//启动PCA计数器
//EIE1|=0x08;//将此代码屏蔽,禁止PCA中断
}
void config(){
//看门狗禁止
WDTCN = 0x07;
WDTCN = 0xDE;
WDTCN = 0xAD;
SFRPAGE = 0x0F;
//交叉开关配置,CEX0-CEX5配置到P0.0-P0.5
XBR0 = 0x30;
XBR1 = 0x00;
XBR2 = 0x40;
XBR3 = 0x00;
//管脚输出配置,P0口为开漏输出,其中P0.6接上拉电阻,P0为数字输入口
SFRPAGE = 0x0F;
P0MDOUT = 0x00;
P1MDIN = 0xFF;
//晶振配置,采用内部晶振8分频
SFRPAGE = 0x0F;
CLKSEL = 0x00;
OSCXCN = 0x00;
OSCICN = 0x84;
}
void main(void){
xdata uchar lowset;
config();
pcaini();
pca0ini();
lowset=0;
PWM0_set(lowset);
while(1){
delay1ms(1000);
lowset+=10;
PWM0_set(lowset);
}
}