#include "reg51.h"
#include "intrins.h"
#define FOSC 18432000L
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;
***it k=P1^0;
BYTE a=0;
void main()
{
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 = 0x02; //Set PCA timer clock source as Fosc/2
//Disable PCA timer overflow interrupt
CCAP0H = CCAP0L =a; //PWM0 port output 50% duty cycle square wave
CCAPM0 = 0x42; //PCA module-0 work in 8-bit PWM mode
PCAPWM0 = 0x03; //and no PCA interrupt
CCAP1H = CCAP1L = 255; //PWM1 port output 0% duty cycle square wave
PCAPWM1 = 0x03;
CCAPM1 = 0x42; //PCA module-1 work in 8-bit PWM mode and no PCA interrupt
CR = 1; //PCA timer start run
EA=1;
EX0=1;
while(1);
}
void inter0() interrupt 0
{
a=200;
}
这是把手册上例程修改过的程序,通过中断修改a的值从而使100%的占空比变为(256-200)/256的占空比,但是达不到预期的要求,求大神指点。。。。。。。。。。
#include "reg51.h"
#include "intrins.h"
#define FOSC 18432000L
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;
***it k=P1^0;
BYTE a=0;
void main()
{
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 = 0x02; //Set PCA timer clock source as Fosc/2
//Disable PCA timer overflow interrupt
CCAP0H = CCAP0L =a; //PWM0 port output 50% duty cycle square wave
CCAPM0 = 0x42; //PCA module-0 work in 8-bit PWM mode
PCAPWM0 = 0x03; //and no PCA interrupt
CCAP1H = CCAP1L = 255; //PWM1 port output 0% duty cycle square wave
PCAPWM1 = 0x03;
CCAPM1 = 0x42; //PCA module-1 work in 8-bit PWM mode and no PCA interrupt
CR = 1; //PCA timer start run
EA=1;
EX0=1;
while(1);
}
void inter0() interrupt 0
{
a=200;
}
这是把手册上例程修改过的程序,通过中断修改a的值从而使100%的占空比变为(256-200)/256的占空比,但是达不到预期的要求,求大神指点。。。。。。。。。。
举报