我的问题是:采集数据比如50个数据,中间会有几个频率值不对的情况。
if( cap_tar==0)
{
///CH=0;CL=0;
C16BIT.CH[1] = CCAP0L;
C16BIT.CH[0] = CCAP0H;
}
如果将PCA的计数器清零,采集的数据的就可以正常的。但是因为还要用到PCA 软件定时功能,所以不能将计数器清零。请帮我找一下问题是在哪里?应该怎么更正。
#include "stc2052.h"
#define uchar unsigned char
#define uint unsigned int
/*Declare SFR associated with the PCA */
sbit EPCAI = IE^6;
sbit PCA_LED = P1^6; //PCA test LED
uchar idata SERSEND[16];
uint L_cnt = 0;
uchar Send_count;
bit Ready;
uint cap_tar;
unsigned long count;
unsigned long getfr;
unsigned int Fre_range=800;
uint value,T1K_LOW,T1K_HIGH;
union // 4-byte?LONG ????
{
unsigned long Result;
uint C16BIT[2];
uchar CH[4];
}C32BIT;
union // 2-byte?uint ????
{
uint Result;
uchar CH[2];
}C16BIT;
void Set_Parm();
void RS232(void);
void PCA_isr() interrupt 6 using 1
{
static bit FLAG;
if (CCF0) // PCA模块0捕捉中断入口
{
CCF0 = 0; // Clear interrupt flag
if( cap_tar==0)
{
C16BIT.CH[1] = CCAP0L;
C16BIT.CH[0] = CCAP0H;
}
cap_tar++;
C32BIT.CH[2] =CCAP0H;
C32BIT.CH[3] = CCAP0L;;
C32BIT.CH[1] = L_cnt;
Ready = 1; // Enable
}
if(CF) // PCA溢出中断入口
{
L_cnt ++; //PCA计数器溢出的值
CF = 0; // Clear interrupt flag
}
if (CCF1) // PCA模块1中断入口
{
CCF1 = 0; // Clear interrupt flag
T1K_LOW=12000-T1K_HIGH; // 占空比的值计算
if (FLAG)
{
FLAG = 0;
PCA_LED = 1; //
value = value+T1K_HIGH;
CCAP1L = value; // First
CCAP1H = value >>8; // Second
}
else
{
FLAG = 1;
PCA_LED = 0; // PWM???
value=value+T1K_LOW;
CCAP1L = value ; // First
CCAP1H = value >>8; // Second
}
}
}
void main()
{
T1K_LOW = 1480; // ????,???????(?=12000)
T1K_HIGH = 10520; // ????,???????(?=12000)
CCAP1L = T1K_LOW;
CCAP1H = T1K_HIGH;
Set_Parm();
for(;;)
{
if (Ready)
{
count = C32BIT.Result - C16BIT.Result;
if(count>12000000) ////定时器1S到采集频率
{
L_cnt=0;
getfr=cap_tar;
cap_tar=0;
}
if (Send_count == 0)
{
SERSEND[1] = getfr;
SERSEND[2] = getfr>>8;
}
Ready = 0;
}
}
}
void Set_Parm() // 程序初始化
{
TMOD = 0x21; // T1???????, T0?8????????
SCON = 0x5c; // ??????:MODE3
PCON = 0x80; // SMOD = 1
TH1 = 0xf3; // ????/9600BPS
TR1 = 1; // T1??(1=??; 0=??), ????????
AUXR = 0x00;
SCON = 0x40;
REN = 1; // ???1????
CCON = 0x80; // 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 = 0x03; // Set PCA timer clock source as Fosc/2
// Disable PCA timer overflow interrupt
CCAPM0 = 0x11; // PCA module-0 capture by a negative tigger on CEX0(P3.7) and enable PCA interrupt
CCAPM1 = 0x49; // PCA module-0 capture by a negative tigger on CEX0(P3.7) and enable PCA interrupt
CR = 1; // PCA timer start run
IPH = 0x80;
PPCA_LVD = 1; //PCA中断优先级高
EPCAI = 1;
ES = 1;
EA = 1;
}
void RS232(void) interrupt 4 using 3 /////串口接收发送中断
{
uchar RS232CMD;
uchar i;
if (TI)
{
TI = 0;
Send_count --;
if (Send_count > 0)
{
for (i=0; i<Send_count; i++)
SERSEND = SERSEND[i+1];
SBUF = SERSEND[0];
}
return;
}
if (!RI) return;
RI = 0;
RS232CMD = SBUF;
if (RS232CMD == 0x05)
{
if (Send_count == 0)
{
SERSEND[0] = 0xFF;
Send_count = 3;
SBUF = SERSEND[0];
}
}
} |