stc15系列单片机3路PCA产生50HzPWM信号程序奉上
时间:10-02
整理:3721RD
点击:
void PCA_Init() //3.6KHz已修改
{
Timer0Init();
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; //Set PCA timer clock source as timer0 out
//Disable PCA timer overflow interrupt
CCAP0H = CCAP0L = 0xff; //PWM0 port output 50% duty cycle square wave
CCAPM0 = 0x42; //PCA module-0 work in 8-bit PWM mode and no PCA interrupt
PCA_PWM0=0;
CCAP1H = CCAP1L = 0xff; //PWM1 port output 0% duty cycle square wave
PCA_PWM1=0;
CCAPM1 = 0x42; //PCA module-1 work in 8-bit PWM mode and no PCA interrupt
CCAP2H = CCAP2L = 0xff; //PWM1 port output 0% duty cycle square wave
PCA_PWM2=0;
CCAPM2 = 0x42; //PCA module-1 work in 8-bit PWM mode and no PCA interrupt
CR = 1; //PCA timer start run
}
//PCA时钟由定时器0溢出来决定,溢出频率为12.8KHz;
void Timer0Init(void) //78微秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xB2; //设置定时初值
TH0 = 0xFF; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void Pwm_Set(unsigned char Pwm_Value)
{
Pwm_Value=255-Pwm_Value;
CCAP0H = CCAP0L = Pwm_Value; //Update PWM0
CCAP1H = CCAP1L = Pwm_Value; //Update PWM1
CCAP2H = CCAP2L = Pwm_Value; //Update PWM2
}
void main()
{
PCA_Init();
Pwm_Set(128);
}
{
Timer0Init();
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; //Set PCA timer clock source as timer0 out
//Disable PCA timer overflow interrupt
CCAP0H = CCAP0L = 0xff; //PWM0 port output 50% duty cycle square wave
CCAPM0 = 0x42; //PCA module-0 work in 8-bit PWM mode and no PCA interrupt
PCA_PWM0=0;
CCAP1H = CCAP1L = 0xff; //PWM1 port output 0% duty cycle square wave
PCA_PWM1=0;
CCAPM1 = 0x42; //PCA module-1 work in 8-bit PWM mode and no PCA interrupt
CCAP2H = CCAP2L = 0xff; //PWM1 port output 0% duty cycle square wave
PCA_PWM2=0;
CCAPM2 = 0x42; //PCA module-1 work in 8-bit PWM mode and no PCA interrupt
CR = 1; //PCA timer start run
}
//PCA时钟由定时器0溢出来决定,溢出频率为12.8KHz;
void Timer0Init(void) //78微秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xB2; //设置定时初值
TH0 = 0xFF; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void Pwm_Set(unsigned char Pwm_Value)
{
Pwm_Value=255-Pwm_Value;
CCAP0H = CCAP0L = Pwm_Value; //Update PWM0
CCAP1H = CCAP1L = Pwm_Value; //Update PWM1
CCAP2H = CCAP2L = Pwm_Value; //Update PWM2
}
void main()
{
PCA_Init();
Pwm_Set(128);
}
不错不错,对于试做四旋翼有帮助