|
通过检测波的两个峰值之间的时间,计算出频率,但是我接对于2500HZ和小于2500HZ的波都P1.0驱动的灯亮,最后查看寄存器看到TAR显示是1,计时器肯定不会溢出的频率算过之后,所以Period就是1了。。。而且给他峰值是100mV还会ADC10MEM显示1F9差不多是十进制500多,我参考电压3.5V啊。将近1.75V的电压怎么给他识别出来的。。。硬件电路我就将信号发生器的负端接芯片地,正端通过一个18K限流电阻接到AD口上,P1.0和P1.6是芯片上的LED连接。真心看不出程序什么错了,来个大神帮忙看下呗!
#include"msp430g2553.h"
unsigned int m=0,qushu=0,Period=0,TA_OverflowCnt,a;
void ConfigTimer() //设置定时器
{
TACTL=TASSEL_1+MC_2+TAIE+TACLR+ID_0; //TA基准时钟为32768HZ、
}
int main(void)
{
WDTCTL=WDTPW+WDTHOLD;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled
ADC10CTL1 = INCH_1; // input A1
ADC10AE0 |= 0x02; // PA.1 ADC option select
TA_OverflowCnt=0;
P1DIR |= 0x41;
P1OUT &=~0x41;
for (;;)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit
if (ADC10MEM > qushu)
qushu=ADC10MEM;
else{
ConfigTimer();
TACTL|=TACLR; //TA清零,重新计数
break;}
}
for (;;)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit
if(qushu<=ADC10MEM){
Period=TA_OverflowCnt*65536+TAR;
TACTL=MC_0;
break;}}
a=32768/Period;
for (;;){
if(a<2500){
P1OUT|=0x40; // P1.6频率小于2500
P1OUT&=~0x01;}
else{
P1OUT|=0x01; // P1.0频率大于2500
P1OUT&=~0x40;}
}
}
// Timer_A中断程序
#pragma vector=TIMER0_A1_VECTOR
__interrupt void Timer_A(void)
{
switch(TA0IV) // TA中断方式选择
{
case 2:break;
case 4:break;
case 10: TA_OverflowCnt++; // 溢出中断计数加1
break;
}
}
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
__bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR)
}
|
|