我用DIP-20封装的stc12c5410AD单片机的P3.7的捕获功能测量频率,通过串口返回到电脑,得到数据始终为零。求解答?
#include<stc12c5410AD.h>
#define uchar unsigned char
sbit LED = P1^7;
unsigned int rate_val = 0;
unsigned long count_1 = 0;
unsigned long count_2 = 0;
unsigned char flag = 0;
unsigned char ov_flag = 0;
unsigned char time_cnt = 0;
bit r_flag = 0;
void PCA_INIT(void)
{
CMOD = 0x01; // 开PCA模块中断,设置空闲模式时关闭PCA计数阵列
CCAPM0 = 0x11; // 下降沿捕获使能
CCON = 0x00; // 设置初始PCA计数阵列计数初值
CH = 0; //
CL = 0; // 设置初始PCA计数阵列计数初值
CCAP0H = 0; //
CCAP0L = 0; // 清各标志位
CR = 1;
EPCA_LVD = 1; // 开中断和低压检测中断位
}
void TIMER_INIT(void)
{
TMOD = 0x21;
SCON |= 0x50;
TH0 = (65535 - 50000) / 256;
TL0 = (65535 - 50000) % 256;
TH1 = 0xfd;
TL1 = 0xfd;
ET0 = 1;
TR0 = 1;
TR1 = 1;
//ES = 1;
EA = 1;
}
void UART_send_byte(unsigned char x)
{
TI = 0;
SBUF = x;
while(TI == 0);
TI = 0;
}
void UART_send_Nbyte(unsigned int x)
{
unsigned char array[2] = 0;
unsigned char i = 0;
array[0] = x >> 8;
array[1] = x & 0xff;
for(i = 0;i < 2; i++)
{
UART_send_byte(array[i]);
}
}
void main(void)
{
PCA_INIT();
TIMER_INIT();
LED = 0;
while(1)
{
if(r_flag)
{
r_flag = 0;
LED = ~LED;
UART_send_Nbyte(rate_val);
TR0 = 1;
}
}
}
void TIMER0(void) interrupt 1
{
TH0 = (65535 - 50000) / 256;
TL0 = (65535 - 50000) % 256;
time_cnt ++;
if(time_cnt == 2)
{
time_cnt = 0;
r_flag = 1;
TR0 = 0;
}
}
void PCA_ISR(void) interrupt 7 //PCA 中断服务函数
{
if(CCF0)
{
CCAP0H = CH; //
CCAP0L = CL;
if(flag == 0)
{
count_1 = ov_flag * 65535 + CCAP0H * 256 + CCAP0L;
flag ++;
}
else
{
flag = 0;
count_2 = ov_flag * 65535 + CCAP0H * 256 + CCAP0L;
rate_val = 12000000 / 12 /(count_2 -count_1);
CH = 0;
CL = 0;
CCAP0H = 0;
CCAP0L = 0;
ov_flag = 0;
}
CCF0 = 0;
}
if(CF)
{
CF = 0;
ov_flag ++;
}
} |