aj9999
Junior Member level 3
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;
}
}