|
最近做了个项目出了,需要与一个串口设备进行通讯,并且采集7路AD,结果运行一段时间后,串口还在工作,AD数据却不更新了,那位大哥知道这是什么问题啊??
下面是我的程序:
#include <MSP430x14x.h> // Standard Definition Control Register
#define ADCMEM ((int*) 0x0140) // ADC12MEMx definition
void USART0_init(void); // Initialization USART1
void ADC12_init(void); // Initialization ADC12
void Port_init(void); // Initialization Port
void Data_process(void); // TCU data process
void Fill_txbuf1(void);
void Data_process(void);
void AD_start(void);
unsigned char send2TCU(unsigned char info);
unsigned char tx_buf1[50] = {0xeb, 0x90, 0x24,'E'} ;
unsigned char array_rx0[20];
unsigned char data_buf[4];
unsigned char tx1_i = 0, tx0_i = 0, rx0_i = 0;
unsigned char rx0_sign = 0;
unsigned char num = 0;
unsigned int sel=10,sign =0,tm=0;
void WDT_Stop( void )
{
WDTCTL = WDTPW +WDTHOLD; // Stop watchdog timer
}
void Clock_XT2( void )
{
unsigned int i;
P5SEL |= 0x30; //For test MCLK/SMCLK
P5DIR |= 0x30;
BCSCTL1 &= ~XT2OFF; //Select XT2--8Mhz
do
{
IFG1 &= ~OFIFG;
for (i = 0xFF; i > 0; i--);
}
while ((IFG1 & OFIFG) != 0);
BCSCTL2 |= SELM1 +SELS+DIVS1+DIVS0;
}
/*----------------------------------------------------------------------------*/
void USART0_init(void)
{
/* USART Settings:
UART function, Idle line multiprocessor Protocol,
9600 baud, 8 data bits, no parity, 1 stop bit */
UCTL0 &=~ SWRST; // reset SWRST bit
UCTL0 |= CHAR;
URCTL0 &= ~URXWIE;
UBR00 = 0xd0; //1000000/4800=208.66667(0x00d0)
UBR10 = 0x00;
UMCTL0 = 0x25; //0x75 5/8=0.6333333
UTCTL0 = SSEL1 + TXEPT; //sel SMCLK=MCLK
ME1 |= UTXE0 + URXE0; //ena××e USART1 TXD/RXD
IE1 |= URXIE0;
P3SEL |= 0x30; // Pin P3.6 used by USART module
P3DIR |= 0x10; // Pin P3.6 is output
}
/*----------------------------------------------------------------------------*/
void USART1_init(void)
{
UCTL1 &= ~SWRST;
UCTL1 |= CHAR; // 8-bit character,1 stop bit,no parity;SWRST=0;
UBR01 = 0x34;
UBR11 = 0x00;
UMCTL1 = 0x01;
UTCTL1 = SSEL1 + TXEPT; // UCLK = SMCLK
ME2 |= UTXE1 + URXE1; // Ena××e USART1 TXD/RXD
IE2 |= UTXIE1 + URXIE1 ; // Ena××e USART1 RX interrupt
P3SEL |= 0xC0; // P3.6,7 = USART1 TXD/RXD
P3DIR |= 0x40; // P3.6 output direction
}
/*---------------------------------------------------------------------------*/
void ADC12_init(void)
{
/* ADC12 Settings: */
ADC12CTL0 &= ~0x20; // Reset ENC
/* selection of reference and input */
ADC12MCTL0 = 0x10; // Ref = ××ss,Vref+;Input = A0
ADC12MCTL1 = 0x11; // Ref = ××ss,Vref+;Input = A1
ADC12MCTL2 = 0x12; // Ref = ××ss,Vref+;Input = A2
ADC12MCTL3 = 0x14; // Ref = ××ss,Vref+;Input = A4
ADC12MCTL4 = 0x15; // Ref = ××ss,Vref+;Input = A5
ADC12MCTL5 = 0x16; // Ref = ××ss,Vref+;Input = A6
ADC12MCTL6 = 0x17; // Ref = ××ss,Vref+;Input = A7
ADC12MCTL7 = 0x9A; // Ref = ××ss,Vref+;Input = A10
// ADC12MCTL3 is end of sequence
// (EOS bit is set!)
ADC12CTL1 = 0x0202; // first conv. result is stored in ADC12MEM0
// ADC12SC bit triggers Sample&Hold
// sample pulse is generated by Sampling Timer
// Clock Source: ADC12 internal oscillator
// Clock divider: 1
// conversion mode: one single sequence
ADC12CTL0 = 0x0070; // Sample&Hold Time 0
// Sample&Hold Time 1
// Multiple Sample&Hold:only one sample is taken
// reference voltage is off
// ADC12 module is switched on
// no interrupts
ADC12CTL0 |= 0x02; // ena××e conversion
}
/*-------------------------------------------------------------------*/
void TimerA_init(void)
{
TACTL = TASSEL1 + TACLR; //SMCLK 1/8
CCTL0 = CCIE; //CCR0 E_int
CCR0 = 30000; //60ms EOM
}
/*---------------------------------------------------------------------------*/
void TimerB_init(void)
{
TBCTL = TBSSEL1 + TBCLR; //SMCLK 1/8
TBCCTL0 = CCIE; //CCR0 E_int
TBCCR0 = 25000; //25ms EOM
TBCTL |= MC1+MC0;
}
/*---------------------------------------------------------------------------*/
void Port_init(void)
{
P5DIR |= 0x0f;
P5OUT &= 0xf0;
}
/*---------------------------------------------------------------------------*/
void Fill_txbuf1(void)
{
unsigned char checksum = 0;
unsigned char i;
for(i = 0;i<35;i++) checksum = checksum ^ tx_buf1[i];
tx_buf1[35] = checksum;
if(((UTCTL1&0x01)!=0) && (tx1_i==0))
{
TXBUF1 = tx_buf1[0];
P5OUT ^= BIT2;
}
}
/*---------------------------------------------------------------------------*/
void AD_process(void)
{
unsigned int data0,data1;
unsigned char data2,data3,data4,data5,data6,data7;
data0 = (unsigned int)((ADC12MEM0*2.5/4096)*400);
data1 = (unsigned int)((ADC12MEM1*2.5/4096)*400);
data2 = (unsigned char)((ADC12MEM2*2.5/4096)*100);
data4 = (unsigned char)((ADC12MEM3*2.5/4096)*100);
data5 = (unsigned char)((ADC12MEM4*2.5/4096)*100);
data6 = (unsigned char)((ADC12MEM5*2.5/4096)*100);
data7 = (unsigned char)((ADC12MEM6*2.5/4096)*120);
tx_buf1[30]=(unsigned char)(data0 & 0x00ff);
tx_buf1[31]=(unsigned char)(data0 >> 8);
tx_buf1[32]=(unsigned char)(data1 & 0x00ff);
tx_buf1[33]=(unsigned char)(data1 >> 8);
tx_buf1[26]=data2;
tx_buf1[27]=data4;
tx_buf1[28]=data5;
tx_buf1[29]=data6;
tx_buf1[34]=data7;
}
/*---------------------------------------------------------------------------*/
#pragma vector = UART1TX_VECTOR
__interrupt void usart1_tx (void)
{
if(tx1_i<35)
{
tx1_i++;
TXBUF1=tx_buf1[tx1_i];
}
else
{
tx1_i=0;
}
}
/*-------------------------------------------------------------------*/
#pragma vector = TIMERB0_VECTOR
__interrupt void TimerB0(void)
{
sel--;
switch (sel)
{
case 9 :
send2TCU(0x03);
sign=3;
break;
case 8 :
send2TCU(0x04);
sign=4;
break;
case 7 :
send2TCU(0x05);
sign =5;
break;
case 6:
send2TCU(0x06);
sign = 6;
break;
case 5:
send2TCU(0x07);
sign=7;
break;
case 4:
send2TCU(0x0a);
sign=8;
break;
case 0:
AD_start();
sel = 10;
break;
default:
break;
}
}
/*---------------------------------------------------------------------------*/
#pragma vector = TIMERA0_VECTOR
__interrupt void TimerA0(void)
{
P5OUT ^= BIT3;
tm=1;
}
/*---------------------------------------------------------------------------*/
void AD_start(void)
{
unsigned int i;
unsigned int j;
for (i=0;i<=7;i++) // generate trigger for each measurement
// this is needed because MSH bit = 0 !!!
{
ADC12CTL0 |= 0x01; // start sampling
ADC12CTL0 &=~0x01; // ADC12SC must be reset manually
// because ADC12 is active during the
// whole sequence (see bit description!)
for (j=0;j<=1000;j++); // wait loop (delay next measurement)
}
while ((ADC12CTL1&0x01)==1); // wait until AD conversion is finished
for (i=0;i<=4000;i++); // wait loop (delay between sequences)
AD_process();
Fill_txbuf1();
}
/*---------------------------------------------------------------------------*/
#pragma vector = UART0RX_VECTOR
__interrupt void usart0_rx (void)
{
if(rx0_i==0) //First receive
{
array_rx0[0]=RXBUF0;
rx0_i++;
TACTL |= MC0; //Start TimerA 60ms
tm =0; //TimerA flag
}
else
{
if((URCTL0&0xf1)==0x0) //no error_sign_bit
{
array_rx0[rx0_i]=RXBUF0; //loop receive data
rx0_i++;
}
else
{
//rx0_i=0;
//tm=0;
}
}
}
|
|