各位 我的程序出了点问题:
思路及代码:
用TA0做上升沿捕获 P1.1为输入端,用TB0做计时器,计时到1s则用TA0捕获到的上升沿个数作为频率,用LCD输出。
#include
#include"nokia_5110.h"
#include"nokia_5110.c"
#define uchar unsigned char
unsigned int ci=0,s=0,n1=0,n0=0,s0,s1,s2,s3;
unsigned const char pinlv[][32]={
//频
{0x40,0x7C,0x40,0x7F,0x48,0x48,0x40,0xF2,0x12,0x1A,0xD6,0x12,0x12,0xF2,0x02,0x00,0x90,0x8E,0x40,0x4F,0x20,0x1E,0x80,0x4F,0x20,0x18,0x07,0x10,0x20,0x4F,0x80,0x00},
//率
{0x00,0x14,0xA4,0x44,0x24,0x34,0xAD,0x66,0x24,0x94,0x04,0x44,0xA4,0x14,0x00,0x00,0x08,0x09,0x08,0x08,0x09,0x09,0x09,0xFD,0x09,0x09,0x0B,0x08,0x08,0x09,0x08,0x00},
};
unsigned const char di[24]=
//第
{0x04,0xD3,0x52,0x56,0x52,0xF4,0x53,0x52,0x56,0x72,0x02,0x00,0x08,0x09,0x05,0x03,0x01,0x0F,0x01,0x01,0x09,0x09,0x07,0x00};
unsigned const char miao[24]=
//秒
{0x12,0xD2,0xFE,0x91,0x40,0x38,0x00,0xFF,0x00,0x04,0xB8,0x00,0x01,0x00,0x0F,0x00,0x08,0x08,0x04,0x04,0x02,0x01,0x00,0x00};
int i,j;
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1&=~XT2OFF; //打开外部晶振XT2(6MHz)
BCSCTL2|=SELS; //SMCLK选择外部晶振
IFG1&=~OFIFG; //清除振荡错误标志
while((IFG1&OFIFG)!=0)
{
for(i=0;i<0xff;i++);
IFG1&=~OFIFG; //清除振荡错误标志
}
LCD_Init();
LCD_clear();
display_chinese(0,0,pinlv[0]); //LCD模块
display_chinese(1,0,pinlv[1]); //LCD模块
for(i=0;i<2;i++) //LCD模块
{
set_pos(28,4+i); //设置显示的地址
for(j=0;j<12;j++)
{
write_data(di[j+i*12]);
}
}
for(i=0;i<2;i++) //LCD模块
{
set_pos(60,4+i); //设置显示的地址
for(j=0;j<12;j++)
{
write_data(miao[j+i*12]);
}
}
display_char(7,1,'.'); //LCD模块
display_char(11,1,'k'); //LCD模块
display_char(12,1,'H'); //LCD模块
display_char(13,1,'z'); //LCD模块
P1DIR=0X00; //P1.1输入
P1SEL |= 0x02; // Set P1.1 to TA0
CCTL0 = CM_1 + SCS + CCIS_0 + CAP + CCIE; // Rising edge + CCI0A (P1.1)
// + Capture Mode + Interrupt
TACTL = TASSEL_2 + MC_2; // SMCLK + Continuous Mode
TBCCTL0 = CCIE; //TIMERB0计时1秒
TBCCR0=50000; //6MHz/8/50000/15=1s
TBCTL=TBSSEL_2+MC_1+ID_3;
_EINT();
while(1);
}
#pragma vector=TIMERA0_VECTOR
__interrupt void TimerA0(void)
{
if(TACCTL0 & CCIFG)
TACCTL0 &= ~CCIFG;
s++; // s记录上升沿个数
}
#pragma vector=TIMERB0_VECTOR
__interrupt void Timer_B (void)
{
if(ci==15)
{
set_pos(44,4); //设置显示地址//LCD模块
for(i=0;i<6;i++) //LCD模块
write_data(font6x8[n1+16]); //写入点阵数据
set_pos(50,4); //设置显示地址
for(i=0;i<6;i++) //LCD模块
write_data(font6x8[n0+16]); //写入点阵数据
n0++; //LCD模块 n0为计时的个位 n1为计时的十位
if(n0==10){n0=0;n1++;} //进位
if(n1==10)n1=0;
s0=s/1000; //s0为频率个位 单位kHz
s1=(s%1000)/100; //s1为十分位
s2=(s%100)/10; //百分位
s3=s%10; //千分位
display_char(6,1,s0+48); //LCD模块
display_char(8,1,s1+48); //LCD模块
display_char(9,1,s2+48); //LCD模块
display_char(10,1,s3+48); //LCD模块
s=0; //记录上升沿个数清零
ci=0; //ci=15时表示记时到1s 清零
}
ci++;
}
问题如下:
TB0计时一秒没有问题,LCD显示也没问题,可是TA0记录上升沿的个数不正确,有时1秒才一次,最多50次,测的是1kHz的方波,就是不对。
到底是捕获模块的哪里出了问题,求高手指点。