Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

help with SPI temp sensor PIC18f4520 in c

Status
Not open for further replies.

aj9999

Junior Member level 3
Joined
Feb 5, 2010
Messages
28
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Location
Missouri
Activity points
1,456
I am attempting to get a temp sensor working over a SPI using a TC77, PIC184520, MPLAB and C18. I am trying to continuously get 16bit data from the TC77, which should be set to send it continously on start up. I cant get any usable data back,as a test i tried sending 0xff to tell the TC77 to send back its ID(54) however that didnt work. The PIC datasheet says that SDI is controlled by the uC but if i comment out the TRISC statments it stops sending anything and it appears to hold that pin low. Im also unclear if i have to send a write cmd before every 8bit read. Thanks in advance.
Code:
#include <p18f4520.h>
//#include <delay.h>

#define LED1 PORTBbits.RB1
#define LED3 PORTBbits.RB3
#define SCK  PORTCbits.RC3
#define SDI  PORTCbits.RC4
#define CS   PORTAbits.RA5

int get_spi(int h);

int temp_l,temp_h;

int get_spi(int lh)
{
  TRISA = 0x00;
  TRISB = 0x00;
  TRISC = 0x00;
  PORTA = 0x00;
  PORTB = 0x00;
  PORTC = 0x00;
  ADCON1 = 0x0f;			  
  msdelay(255);			//wait for TC778 to warm up
  CS = 1;
  
  SSPSTAT = 0x00;
  SSPSTATbits.SMP = 1;
  SSPSTATbits.CKE = 0;  //idle to active(rising edge) transmission
  SSPCON1 = 0x00;
  SSPCON1bits.CKP = 1;			
  SSPCON1bits.SSPEN = 1;
  SSPCON1bits.SSPM0 = 1;
  
  while(1)
  {
//  TRISC = TRISC & 0xef;     		//sets SDI to output
	SSPBUF = 0xff;//   TRISC = TRISC | 0x10;     		//sets SDI to input
    CS = 0;  							//start communication
    while(SSPSTATbits.BF == 0)
    {//wait for buffer to fill
    }
    temp_h = SSPBUF;
      
//  TRISC = TRISC & 0xef;     		//sets SDI to outpu
    SSPBUF = 0xff;   				    //
 // TRISC = TRISC | 0x10;     		//sets SDI to input
    
    while(SSPSTATbits.BF == 0)
    {//wait
    }
    temp_l = SSPBUF;
  
    CS = 1;								//stop communication
    msdelay(1);
    if (lh = 1)
	{
      return temp_l;
	}else;
      return temp_h;
  }
}
 

Hi,
I haven't gone through the entire code or the TC77 datasheet but at quick glance, this seems to be wrong:
In the PIC module, when you receive data from SPI, it is received through SDI, but when you transmit data, it is transmitted through SDO pin. Maybe that causes the problem.
Hope this helps.
Tahmid.
 

Your comments got me thinking, the TC77 is a microwire device, so according to the DS it only neeeds the one line(SDI). So i tried the code below and it appears the the SDI line is held low by the uC causing it to not recieve any signal,but im not sure why.

Code:
  while(1)
  {  
    CS = 0;
    SSPBUF = junk;
    while(SSPSTATbits.BF == 0)
    {//wait
    }
    temp_h = SSPBUF;
    PORTB = temp_h;

    SSPBUF = junk;

    while(SSPSTATbits.BF == 0)
    {//wait
    }
    temp_l = SSPBUF;
    PORTB = temp_l;
  
    CS = 1;
    msdelay(1);
    }
}
 

hey hii..pls help me out wid the code for SPI temperature sensing for pic18f4520..i cant understand how to go ahead wid it..pls help..
 

What sensor are you using? and please post your code.
 

Hi piya_mitruka,

I have not used SPI temperature sensors before, but you could use the TC72 from Microchip.
There are also other temperature sensors with other interfaces, such as linear, one-wire, etc.

Hope this helps.
Tahmid.
 

hi..
i have written the following code for reading the temperature using TC74. I m using PIC18f4520. pls help me out coz i cant get the reading. I dont knoe whr i m goin wrong.
Thanks in advance

#include "p18f4520.h"
#include "usart.h"
#include "i2c.h"

#pragma config OSC = INTIO67


void main()
{

char inputstr;
char temp=0;
char temp2=0;

//USART Part
OSCCONbits.IRCF2=1;
OSCCONbits.IRCF1=1;
OSCCONbits.IRCF0=1;

BAUDCONbits.BRG16=0;
SPBRG=51;
TRISC<7>=1;
TRISC<6>=1;

TXSTAbits.TX9=0;
TXSTAbits.TXEN=1;
TXSTAbits.SYNC=0;
TXSTAbits.BRGH=1;

RCSTAbits.SPEN=1;
RCSTAbits.RX9=0;
RCSTAbits.CREN=1;

//Temperature Sensor Part

TRISC = 0x00; // turn on tri-state register and
// make all output pins
PORTC = 0x00; // make all output pins LOW

OpenI2C( MASTER, SLEW_OFF);
SSPADD = 0x33;

StartI2C(); // begin I2C communication
IdleI2C();
WriteI2C( 0x95 ); // sends address to the
// device
IdleI2C();
WriteI2C( 0x01 ); // sends a control byte to
// the device
IdleI2C();
WriteI2C( 0x20 ); // sends configuration byte –
// continuous conversion, 9
// bit res
IdleI2C();
StopI2C();

StartI2C();
IdleI2C();
WriteI2C( 0x95 );
IdleI2C();
WriteI2C( 0x00 );
IdleI2C();
RestartI2C(); // Initiate a RESTART command
IdleI2C();
WriteI2C( 0x95 ); // address device w/read
IdleI2C();
temp=ReadI2C(); // Returns the MSB byte
// and stores it in
// 'temp'
IdleI2C();
NotAckI2C(); // Send back Acknowledge
IdleI2C();
temp2=ReadI2C(); // returns the LSB of
// the temperature
IdleI2C();
NotAckI2C();
IdleI2C();
StopI2C();
putrsUSART("\r\nTHE TEMPERATURE IS:");
putsUSART(temp);
putsUSART(temp2);
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top