本帖最后由 followme001 于 2013-4-10 16:27 编辑
我用MG82FE216AF的PCA作定时器用,发现好像能进入中断,但只是进入一次?单片机用的是24M的晶振,程序的功能是让红色LED亮1秒,灭1秒。
#include "mg82fe216af.h"
#include <intrins.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
sfr CMOD = 0xD9;
sfr CCON = 0xD8;
sfr CCAPM0 = 0xDA;
sfr CH = 0xF9;
sfr CL = 0xE9;
sfr CCAP0H = 0xFA;
sfr CCAP0L = 0xEA;
sbit CR = CCON^6;
sbit CCF0 = CCON^0;
sbit CF = CCON^7;
sbit R1 = P2^0;
sbit G1 = P2^1;
sbit B1 = P2^2;
uint16 Value;
void osc_switch(void);
void delay_ms(uint8 ms)
{
uint8 i;
uint16 j;
for (i=0; i<ms; ++i)
{
for (j=0; j<2100; ++j)
{
;
}
}
}
void pca_init(void) //定时器初始化程序
{
CCON = 0x00;
CMOD = 0x00;
CH = 0;
CL = 0;
Value = 0x4E20; //10ms;
CCAP0H = Value >> 8;
CCAP0L = Value; //这个是设置装载值,是看STC的手册中的公式,因为笙泉的没有,不知是不是这里的问题。
Value += 0x4E20;
CCAPM0 = 0x49; //ECOM0 = 1,enable compare.MAT0 = 1.ECCF0 = 1;
EIE1 |= 1 << 2; //开启PCA中断
CR = 1; //PCA运行
EA = 1;
}
void main(void)
{
osc_switch();
pca_init();
R1 = 1; //一开始红灯灭
while (1)
{
;
}
}
void pca_isr() interrupt 10
{
static uint8 count = 0;
CCF0 = 0;
CCAP0H = Value >> 8;
CCAP0L = Value;
Value += 0x4E20;
if (++count >= 100) //10ms进入一次中断,计数100次就为1S
{
count = 0;
R1 = !R1; //如果用这段代码,则红灯不亮。如果注释掉,换成R1 = 0,则一上电红灯亮,好像能进入定时器中断的。
}
}
void osc_switch(void) //内部RC转外部晶振程序
{
IFMT = 0x06; //AUXRA write
IFD = 0x6F; //(bit3)=1,enable xtal,bit(2)=1,enable internal osc,bit[1:0]=11,osc = xtal
ISPCR |= 0x80; //bit(7)=1,ISPEN = 1
SCMD = 0x46;
SCMD = 0xB9;
_nop_();
IFMT = 0x06;
IFD = 0x2B; //disable internal osc
ISPCR |= 0x80;
SCMD = 0x46;
SCMD = 0xB9;
delay_ms(5);
}
|