AnsweredAssumed Answered

ADT7310 interfacing with PIC

Question asked by mnanand1988 on Jan 3, 2013
Latest reply on Jan 5, 2013 by mnanand1988

Hi,

 

i am using adt7310 with PIC microcontroller.

 

I am trying to read the temperature register value in continuous mode. In the beginning I am resetting the sensor by sending more than 32 ones to the DIN of ADT7310. After that I am reading the ID, but I am getting all zero. then I am writing 0x80 to the configuration register and sending the command 0x54 for getting the temperature value continuously. I am getting some gunk values or all zeros (0x0000). it is not matching with the temperature.

 

This is the code that I am using. Please help me to get the values correctly.

 

#include <p18f25k80.h>

#include<math.h>

#include<Delays.h>

#include<spi.h>

 

 

#pragma config WDTEN=OFF, FOSC=HS2, FCMEN=ON, IESO=OFF, XINST=OFF

 

 

#define SPI_CS PORTCbits.RC2

#define SPI_CK PORTCbits.RC3

#define SPI_DO PORTCbits.RC5

#define SPI_DI PORTCbits.RC4

 

 

void adt_init();

void adt_temp();

void resetadt();

 

 

void usart1_init();

void usart1_send(int);

void float_char(char,float, int);

void newline();

 

double adt_tval;

long int adt_th,adt_tl, adt_sgn,adt_t;

char Temp_adt[2],sgn_adt = '+',data[16],ID;

int val[] = {'0','1','2','3','4','5','6','7','8','9'},i;

 

 

/////////////////////////usart functions///////////////////

void usart1_init()

{

 

 

          SPBRG1 = 0X81;

          SPBRGH1 = 0X00;

          TXSTA1 = 0b00000010;

          RCSTA1 = 0b10110000;

          BAUDCON1 = 0b01001000;

}

 

 

void newline()

{

          TXSTA1bits.TXEN = 1;

 

 

          TXREG1 = '\n';

 

          while(!TXSTA1bits.TRMT);

 

 

 

 

TXSTA1bits.TXEN = 0;

}

 

 

void float_char(char h,float l,int k)

{

          long int m,n,o,g;

          m = l*1000;

          n = m%1000;

          m = m/1000;

data[0] = h;

 

 

for(k--,i = 1;k>=0;k--,i++)

{

o = pow(10,k);

g = m/o;

m = m%o;

data[i] = val[g];

}

data[i] =  '.';

 

 

for(k = 2,i++;k>=0;k--,i++)

{

          o = pow(10,k);

          g = n/o;

            n = n%o;

data[i] = val[g];

}

 

 

}

 

 

void usart1_send(int a)

{

TXSTA1bits.TXEN = 1;

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

{

          TXREG1 = data[i];

                    while(!TXSTA1bits.TRMT);

 

 

}

          TXREG1 = '\t';

 

 

          while(!TXSTA1bits.TRMT);

          TXREG1 = '\t';

 

 

          while(!TXSTA1bits.TRMT);

 

 

 

 

TXSTA1bits.TXEN = 0;

}

 

 

/////////////////////////////////Delay fuctions///////////////////

 

 

//1 ms delay 6250cycles=1ms

void msdelay(unsigned int num)

{

    unsigned int i;

    for(i=1;i<=num;i++)

    {

        Delay1KTCYx(6);

        Delay10TCYx(25);

    }

}

 

 

//.1 ms delay

void mssdelay(unsigned int num)

{

    unsigned int j;

    for(j=1;j<=num;j++)

    {

        Delay10TCYx(62);

        Delay1TCY();

        Delay1TCY();

        Delay1TCY();

        Delay1TCY();

        Delay1TCY();

    }

}

 

 

//.01 ms delay

void msssdelay(unsigned int num)

{

    unsigned int k;

    for(k=1;k<=num;k++)

    {

        Delay10TCYx(6);

        Delay1TCY();

        Delay1TCY();

    }

}

 

 

void DelayFor18TCY(void)

{

Nop( ); Nop( ); Nop( ); Nop( ); // 18 cycle delay

Nop( ); Nop( ); Nop( ); Nop( );

Nop( ); Nop( ); Nop( ); Nop( );

Nop( ); Nop( );

return;

}

 

 

////////////////////////////ADT7310 functions///////////////////////

 

 

void adt_temp()

{

//adt_th = Temp_adt[0];

//adt_tl = Temp_adt[1];

adt_sgn = adt_th & 0x80;

adt_t = (adt_th*256)+adt_tl;

//adt_t = adt_t>>3;

if(adt_sgn == 0x80)

{

sgn_adt = '-';

adt_tval = adt_t - 65536;

adt_tval = adt_tval/128;

}

else

{

sgn_adt = '+';

adt_tval = adt_t/128;

}

}

 

 

void adt_init()

{

SPI_CS = 0;

putcSPI(0x08);

putcSPI(0x80);

SPI_CS = 1;

msdelay(1000);

}

 

 

 

 

void resetadt()

{

unsigned int i;

SPI_CS=0;/*

SPI_DO=1;

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

{

SPI_CK=0;

mssdelay(1);

SPI_CK=1;

mssdelay(1);

}

SPI_DO=0;

//putcSPI(0x54);*/

putcSPI(0xFF);

putcSPI(0xFF);

putcSPI(0xFF);

putcSPI(0xFF);

SPI_CS=1;

}

 

 

//////////////////////////////////Main function///////////////////////

 

 

void main()

{

 

 

TRISCbits.TRISC6 = 0;

TRISCbits.TRISC7 = 1;

 

 

TRISCbits.TRISC2= 0;   // RB2/CS - Output (Chip select)

TRISCbits.TRISC3= 0;   // RC3/SCK - Output (Clock)

TRISCbits.TRISC4= 1;   // RC4/SDI - Input (Serial Data In)

TRISCbits.TRISC5= 0;   // RC7/SDO - Output (Serial Data Out)

 

 

ADCON1=0x0F;

 

 

          usart1_init();

OpenSPI(SPI_FOSC_64, MODE_10,SMPMID);

 

 

while(1)

{

//SPI_CS = 1;

//msdelay(1000);

resetadt();

 

 

SPI_CS = 0;

putcSPI(0x58);

SPI_CS = 1;

msdelay(1);

 

 

SPI_CS = 0;

ID = ReadSPI();

SPI_CS = 1;

 

 

adt_init();

 

 

SPI_CS= 0;

putcSPI(0x54);

msdelay(1);

SPI_CS = 1;

 

 

msdelay(2000);

 

 

SPI_CS = 0;

//while(SSPSTATbits.BF == 0);

adt_th = ReadSPI();

adt_tl = ReadSPI();

msdelay(1);

 

 

adt_temp();

 

 

if(adt_tval == 0)

{

resetadt();

adt_init();

}

//if(adt_tval != 0)

{

float_char(' ',adt_th,5);

usart1_send(i);

float_char(' ',adt_tl,5);

usart1_send(i);

float_char(' ',adt_t,5);

usart1_send(i);

float_char(' ',adt_tval,3);

usart1_send(i);

float_char(' ',ID,3);

usart1_send(i);

newline();

}

msdelay(2000);

resetadt();

}

}

Outcomes