#include "reg51.h"
#include "intrins.h"
#define FOSC 11059200L
/* define constants */
#define MODE1T
#ifdef MODE1T
#define T1MS (65536-FOSC/20000) //20ms
timer calculation method in 1T mode
#else
#define T1MS (65536-FOSC/12/20000) //20ms timer calculation method in 12T mode
#endif
/* define SFR */
sfr AUXR = 0x8e;
typedef unsigned char BYTE;
typedef unsigned int WORD;
/*Declare SFR associated with the PCA */
sfr CCON = 0xD8; //PCA control register
***it CCF0 = CCON^0; //PCA module-0 interrupt flag
***it CCF1 = CCON^1; //PCA module-1 interrupt flag
***it CR = CCON^6; //PCA timer run control bit
***it CF = CCON^7; //PCA timer overflow flag
sfr CMOD = 0xD9; //PCA mode register
sfr CL = 0xE9; //PCA base timer LOW
sfr CH = 0xF9; //PCA base timer HIGH
sfr CCAPM0 = 0xDA; //PCA module-0 mode register
sfr CCAP0L = 0xEA; //PCA module-0 capture register LOW
sfr CCAP0H = 0xFA; //PCA module-0 capture register HIGH
sfr CCAPM1 = 0xDB; //PCA module-1 mode register
sfr CCAP1L = 0xEB; //PCA module-1 capture register LOW
sfr CCAP1H = 0xFB; //PCA module-1 capture register HIGH
sfr PCAPWM0 = 0xf2;
sfr PCAPWM1 = 0xf3;
void pwm_init(void)
{
CCON = 0; //Initial PCA control register
//PCA timer stop running
//Clear CF flag
//Clear all module interrupt flag
CL = 0; //Reset PCA base timer
CH = 0;
CMOD|=0x04; //设置PWM时钟使用定时器0
AUXR = 0x80; //timer0 work in 1T mode
TMOD = 0x01; //set timer0 as mode1 (16-bit)
TL0 = T1MS; //initial timer0 low byte
TH0 = T1MS >> 8; //initial timer0 high byte
TR0 = 1;
CCAPM0 = 0x42;
CCAP0H = CCAP0L = 0x12;//占空比7% 约等于1.4ms
CCAP1L=CCAP1H=0x80;//占空比调整至5%约等于1ms
PCAPWM0=0x00; //stc12c5a60s2是8位PWM,控制占空比的第九位为0
CCAPM1 = 0x42;
PCAPWM1 = 0x00;
//PCA module-1 work in 8-bit PWM mode and no PCA interrupt
CR = 1;
}
void main()
{
pwm_init(); //PCA timer start run
}
0