#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define FOSC 18432000L
sfr CCON=0XD8;
sbit CCF0=CCON^0;
sbit CR =CCON^6;
sbit CF=CCON^7;
sfr CMOD= 0XD9;
sfr CL=0xE9;
sfr CH=0xF9;
sfr CCAPM0 =0XDA;
sfr CCAP0L=0xEA;
sfr CCAP0H=0XFA;
sfr PCAPWM0=0xF2;
//uchar adcresult;// a 是ADC采集的数值,直接作为占空比高频
void PWM_set(uchar adcresult)
{
CCON=0;
CL=0;
CH=0;
CMOD=0x02; //设置PCA定时器 (可根据示波器来调PCA的时钟频率,这里取的是1 0 0 )
CCAPM0=0x42; //PWM0设置PCA工作方式为PWM方式(0100 0010)
// CCAP0L=0x00; //设置PWM0初始值与CCAP0H相同
// CCAP0H=0x00; // PWM0初始时为0
CCAP0H=adcresult; //这个是设置通道0的占空比的。由于是8位PWM,所以取值范围只能是0~255,这里写128,也就是说129/256的占空比。
CCAP0L=0xff-adcresult;
CR=1; //启动PCA定时器
}
void main()
{
PWM_set(0x80);
while(1); //占空比设置好应该可以直接输出了
}
这是我的程序
|