msp430f149+mlx90614求助

2019-03-24 10:25发布


本人毕设用msp430f149+mlx90614想做个测温度的装置

但不知为何 在实验板上测试了两周也无法得到mlx90614的ack

还请各位大神帮助 本人算430半小白

情况 供电无问题 90614 SCL SDA引脚均接了上拉电阻 然后

直接连到430引脚上 本人想用SMbus通信

还请各位大神帮助!!!

下面是程序:#include  <msp430x14x.h>

void Delay(unsigned int n);

void start_bit();

void stop_bit();

void send_bit(unsigned char bit_out);

unsigned char receive_bit();

unsigned char slave_ack();

void TX_byte(unsigned char TX_buffer);

unsigned char RX_byte(unsigned char ack_nack);

unsigned char PEC_cal(unsigned char pec[],int n);

unsigned long int MEM_READ( unsigned char slave_addR, unsigned char cmdR );                                                

void CALTEMP(unsigned long int TEMP);



void mlx90614_SCL_0() { P1OUT &= ~0x02;}  // define P1.1 ---> SCL

void mlx90614_SCL_1() { P1OUT |= 0x02;}



void mlx90614_SDA_0() { P1OUT &= ~0x04;}  // define P1.2 ---> SDA

void mlx90614_SDA_1() { P1OUT |= 0x04;}



#define _SDA_OUTPUT P1DIR |=0x04; //Set SDA as Output

#define _SDA_INPUT P1DIR &=~0x04; //Set SDA as Input



#define SDA ((P1IN & BIT2)>>1) //define input pin



void main(void)

{

  WDTCTL = WDTPW + WDTHOLD;         // Stop Watch dog timer

   BCSCTL1 = RSEL0 + RSEL1 + RSEL2;          // XT2on, max RSEL

    DCOCTL = DCO0 + DCO1 + DCO2;              // Max DCO



   P1DIR = 0xFF;     // All P1.x outputs

  P1OUT = 0;        // All P1.x reset



while(1)                          // While 1 is equal to 1 (forever)

  {

   unsigned long int DATA;



   DATA=MEM_READ(0x5a,0x07);

   CALTEMP(DATA);



  }

}



void Delay(unsigned int n)

{

  unsigned int i;

  for(i=0;i<n;i++)

  _NOP();

}





//----------------------------------------------------------------------------------------------------------------------------------------//

//Name: start_bit

//----------------------------------------------------------------------------------------------------------------------------------------//

void start_bit()

{

  _SDA_OUTPUT; //Set SDA as output

  Delay(5);

  mlx90614_SDA_1();

  //Delay(30);

  mlx90614_SCL_1();



  Delay(30);

  mlx90614_SDA_0();

  Delay(30);

  mlx90614_SCL_0();

  Delay(30);



}



//----------------------------------------------------------------------------------------------------------------------------------------//

//Name: stop_bit

//----------------------------------------------------------------------------------------------------------------------------------------//

void stop_bit()

{

  _SDA_OUTPUT; //Set SDA as output

  Delay(5);

  //mlx90614_SCL_0();

  mlx90614_SDA_0();

  Delay(30);

  mlx90614_SCL_1();

  Delay(30);

  mlx90614_SDA_1();

}



//----------------------------------------------------------------------------------------------------------------------------------------//

//Name: send_bit

//----------------------------------------------------------------------------------------------------------------------------------------//

void send_bit(unsigned char bit_out)

{

  _SDA_OUTPUT; //Set SDA as output

  Delay(5);

  if(bit_out==0) {mlx90614_SDA_0();}else{mlx90614_SDA_1();}

  Delay(5);

  mlx90614_SCL_1();

  Delay(30);

  mlx90614_SCL_0();

  Delay(30);

}



//----------------------------------------------------------------------------------------------------------------------------------------//

//Name: receive_bit

//----------------------------------------------------------------------------------------------------------------------------------------//

unsigned char receive_bit()

{

  unsigned char bit_in;

  _SDA_INPUT; //Set SDA as input

  Delay(5);

  mlx90614_SCL_1();

  Delay(5);

  if(SDA==1){bit_in=1;}else{bit_in=0;}

  Delay(10);

  mlx90614_SCL_0();

  Delay(30);

  return bit_in;

}



//----------------------------------------------------------------------------------------------------------------------------------------//

//Name: slave_ack

//1 - ACK

//0 -NACK

//----------------------------------------------------------------------------------------------------------------------------------------//

unsigned char slave_ack()

{

  unsigned char ack;

  ack=0;

  _SDA_INPUT; //Set SDA as input

  Delay(5);

  mlx90614_SCL_1();

  Delay(10);

  if(SDA==0){ack=0;}else{ack=1;}

  Delay(10);

  mlx90614_SCL_0();

  Delay(30);

  return ack;

}



//----------------------------------------------------------------------------------------------------------------------------------------//

//Name: TX_byte

//----------------------------------------------------------------------------------------------------------------------------------------//

void TX_byte(unsigned char TX_buffer)

{

  unsigned char Bit_counter;

  unsigned char bit_out;

  for(Bit_counter=8;Bit_counter;Bit_counter--)

  {

    if(TX_buffer&0x80){bit_out=1;}else{bit_out=0;}

    send_bit(bit_out); //Send the current bit on SMBus

    TX_buffer<<=1; //Get next bit to check

  }

}



//----------------------------------------------------------------------------------------------------------------------------------------//

//Name: RX_byte

//Parameters: unsigned char ack_nack (acknowledgment bit)

//0 - Master device sends ACK

//1 - Master device sends NACK

//----------------------------------------------------------------------------------------------------------------------------------------//

unsigned char RX_byte(unsigned char ack_nack)

{

        unsigned char RX_buffer;

        unsigned char Bit_counter;

        for(Bit_counter=8;Bit_counter;Bit_counter--)

        {

         if(receive_bit()==1) //Read a bit from the SDA line

          {

           RX_buffer<<=1; //If the bit is HIGH save 1 in RX_buffer

           RX_buffer|=0x01;

          }

         else //If the bit is LOW save 0 in RX_buffer

          {

           RX_buffer<<=1;

           RX_buffer&=0xfe;

          }

        }

        send_bit(ack_nack); //Sends acknowledgment bit

        return RX_buffer;

}





//----------------------------------------------------------------------------------------------------------------------------------------//

//CALCULATE THE PEC PACKET

//----------------------------------------------------------------------------------------------------------------------------------------//

unsigned char PEC_cal(unsigned char pec[],int n)

{

    unsigned char crc[6];

    unsigned char Bitposition=47;

    unsigned char shift;

    unsigned char i;

    unsigned char j;

    unsigned char temp;

  do{

    crc[5]=0; //Load CRC value 0x000000000107

    crc[4]=0;

    crc[3]=0;

    crc[2]=0;

    crc[1]=0x01;

    crc[0]=0x07;

    Bitposition=47; //Set maximum bit position at 47

    shift=0;        //Find first 1 in the transmitted bytes

    i=5; //Set highest index (package byte index)

    j=0; //Byte bit index, from lowest

    while((pec[i]&(0x80>>j))==0 && (i>0))

    {

     Bitposition--;

     if(j<7){ j++;}

     else {j=0x00;i--;}

    }//the position of highest "1" bit in Bitposition is calculated

    shift=Bitposition-8; //Get shift value for CRC value



    while(shift)

    {

      for(i=5;i<0xFF;i--)

      {

       if((crc[i-1]&0x80) && (i>0)) //Check if the MSB of the byte lower is "1"

        { //Yes - current byte + 1

         temp=1; //No - current byte + 0

        } //So that "1" can shift between bytes

       else { temp=0;}

      crc[i]<<=1;

      crc[i]+=temp;

      }

    shift--;

    }

    //Exclusive OR between pec and crc

    for(i=0;i<=5;i++) { pec[i]^=crc[i]; }

    }

    while(Bitposition>8);

    return pec[0];

    }



//----------------------------------------------------------------------------------------------------------------------------------------//

//READ DATA FROM RAM/EEPROM

//----------------------------------------------------------------------------------------------------------------------------------------//

unsigned long int MEM_READ(unsigned char slave_addR, unsigned char cmdR)

{

  unsigned char DataL; //

  unsigned char DataH; //Data packets from MLX90614

  unsigned char PEC; //

  unsigned long int Data; //Register value returned from MLX90614

  unsigned char Pecreg; //Calculated PEC byte storage

  unsigned char arr[6]; //Buffer for the sent bytes

  unsigned char ack_nack;

  unsigned char SLA;

  SLA=(slave_addR<<1);

begin:

  start_bit(); //Send start bit

  TX_byte(SLA); //Send slave address, write

  if(slave_ack()==0){stop_bit();goto begin;} //Send command

  TX_byte(cmdR);

  if(slave_ack()==0){stop_bit();goto begin;}//Send Repeated start bit

  start_bit(); //Send slave address, read

  TX_byte(SLA+1);

  if(slave_ack()==0){stop_bit();goto begin;}

  DataL=RX_byte(0); //

  //Read two bytes data

  DataH=RX_byte(0); //

  PEC=RX_byte(ack_nack); //Read PEC from MLX90614

  if(ack_nack==1) //Master sends ack or nack

  //This depends on the pec calculation,

  //if the PEC is not correct, send nack and goto begin

  {stop_bit();goto begin;} //Send stop bit

  stop_bit();

  arr[5]=(SLA);

  arr[4]=cmdR;

  arr[3]=(SLA+1);

  arr[2]=DataL;

  arr[1]=DataH;

  arr[0]=0;

  Pecreg=PEC_cal(arr,6); //Calculate CRC

  if(PEC==Pecreg){ ack_nack=0;}

  else{ ack_nack=1;}

  Data=(DataH*256)+DataL;

  return Data;

}



//---------------------------------------

//Name: CALTEMP           

//Temperature data is T=(Data)*0.02-273.15

//---------------------------------------

void CALTEMP(unsigned long int TEMP)

{

   unsigned long int T;

   unsigned int a,b;

   T=TEMP*2;

   if(T>=27315)

   {

     T=T-27315;

     a=T/100;

     b=T-a*100;

     if(a>=100){A4=a/100;a=a%100;A5=a/10;a=a%10;A6=a;}

     else if(a>=10){A4=0;A5=a/10;a=a%10;A6=a;}

     else {A4=0;A5=0;A6=a;}

     if(b>=10){A7=b/10;b=b%10;A8=b;}

     else{A7=0;A8=b;}

    }

   else

    {

     T=27315-T;

     a=T/100;

     b=T-a*100;

     A4=9;

     if(a>=10){A5=a/10;a=a%10;A6=a;}

     else{A5=0;A6=a;}

     if(b>=10){ A7=b/10;b=b%10;A8=b;}

     else{A7=0;A8=b;}

    }

}



显示等程序被我剪掉了 现在接收不到ack Data一直等于0

我自己怀疑是延时和时钟设置问题 但不知如何修改

还请各位前辈帮帮我 我先提前谢谢各位了。。。

此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
1条回答
ljj3166
2019-03-24 21:42
 精彩回答 2  元偷偷看……0人看过

一周热门 更多>

相关问题

    相关文章