|
msp430运用定时器A和比较器A测电阻。不知道问题出在哪里
[复制链接]
#include "msp430x14x.h"
#define REF_O (P1DIR |= BIT3)
#define MEA_O (P1DIR |= BIT4)
#define REF_I (P1DIR |= BIT3)
#define MEA_I (P1DIR |= BIT4)
#define REF_H (P1OUT |= BIT3)
#define REF_L (P1OUT &= ~BIT3)
#define MEA_H (P1OUT |= BIT4)
#define MEA_L (P1OUT &= ~BIT4)
#define REF (P1OUT & BIT3)
#define MEA (P1OUT & BIT4)
#define Ref (2000)
unsigned char overflow=0,ref_flag=0,mea_flag=0;
unsigned long int t_ref=0,t_mea=0;
void delay_1s()
{
unsigned int x;
unsigned int y;
for(x=1000;x>0;x--)
for(y=8000;y>0;y--);
}
void campare_init()
{
P2SEL |= BIT3;
P2DIR &= ~BIT3;
TACTL |= TASSEL_2+ID_3+TAIE+TACLR;
CACTL1 |= CAREF_1+CAON+CAIE+CARSEL+CAIES;
CACTL2 |= P2CA0+CAF;
}
void campare_proceed()
{
P2SEL |= BIT3;
P2DIR &= ~BIT3;
REF_O;
MEA_O;
REF_H;
MEA_L;
MEA_I;
delay_1s();
REF_L;
TACTL |= MC_2;
while(!ref_flag);
REF_L;
REF_I;
MEA_O;
MEA_H;
delay_1s();
MEA_L;
while(!mea_flag);
}
unsigned int campare_calculate()
{
return t_mea*Ref/t_ref;
}
#include "msp430x14x.h"
#include "clk_init.h"
#include "clk_init.c"
#include "campare.h"
#include "campare.c"
#include "LCD1602.h"
#include "LCD1602.c"
#include "stdio.h"
int main( void )
{
unsigned int r_mea=0;
char p[7];
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
clk_init();
lcd_init();
campare_init();
lcd_display_location(1,1);
lcd_write_string("Wait...");
campare_proceed();
r_mea=campare_calculate();
sprintf(p,"%u",r_mea);
lcd_write_string(p);
while(1)
{
}
return 0;
}
#pragma vector=TIMERA1_VECTOR
__interrupt void timer()
{
overflow++;
TACTL &= ~TAIFG;
}
#pragma vector=COMPARATORA_VECTOR
__interrupt void campare_a()
{
if(REF)
{
t_ref=TAR+overflow*0xffff;
ref_flag=1;
overflow=0;
}
if(MEA)
{
t_mea=TAR+overflow*0xffff;
mea_flag=1;
}
}
|
|