想利用STC12的PCA16位定时器实现16位PWM输出.
计划是这样的,把PCA1设成16位定时器模式,当PCA1中断时,将IO脚拉高,当PCA的计时器溢出中断时,将IO脚拉低。这样就实现了PWM。
想着挺好。
试了下,给初始化时,将PCA1设成16位定时器,并且给CCAP1H,CCAP1L赋初值,并设PCA也可计时溢出中断。
这样,测试IO脚有固定占空比的PWM,和预想的一样。
然后想主循环里改百年CCAP1H和CCAP1L的值,来实现占空比可调。结果发现不行,IO口很快变成低电平,并始终保持。
那么我试着将CCAP1H逐渐自加吧。发现可以,占空比逐渐变化。
然后,我再尝试,在主程序里给CCAP1H及CCAP1L赋固定值,结果不行,IO口很快变低并始终保持。
/*------------------------------------------------------------------*/
/* --- STC MCU Limited ---------------------------------------------*/
/* --- STC12C56xx Series MCU PCA module as 16-bit Timer Demo -------*/
/* If you want to use the program or the program referenced in the */
/* article, please specify in which data and procedures from STC */
/*------------------------------------------------------------------*/
#include "stc12c5630ad.h"
#include "intrins.h"
#define FOSC 11059200L
#define T100Hz (FOSC / 12 / 100)
typedef unsigned char BYTE;
typedef unsigned int WORD;
sbit PCA_LED = P1^4; //PCA test LED
sbit PCA_VEE = P3^7;
sbit PCA_PWM = P3^5;
BYTE cnt;
unsigned value;
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 43;
j = 6;
k = 203;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void PCA_isr() interrupt 6 using 1
{
if(CCF0)
{
CCF0 = 0; //Clear interrupt flag
CCAP0H += 4; //Update compare value
PCA_VEE = !PCA_VEE;
}
if(CCF1)
{
CCF1 = 0;
PCA_PWM = 1;
}
if(CF)
{
CF = 0;
PCA_PWM = 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
//Enable PCA timer overflow interrupt
CCAP0L = 0;
CCAP0H = 0; //Initial PCA module-0
CCAPM0 = 0x49; //PCA module-0 work in 16-bit timer mode and enable PCA interrupt
CCAP1H = 0x0F;
CCAP1L = 0xFF;
CCAPM1 = 0x49;
CR = 1; //PCA timer start run
EPCA_LVD = 1;
EA = 1;
cnt = 0;
while (1)
{
Delay1000ms();
CCAP1H = 0x0F;
CCAP1L = 0xFF;
// CCAP1H += 100;
}
}
|