It is currently 20 Apr 2018 02:02

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: 22 Mar 2018 15:36 
Offline

Joined: 22 Mar 2018 14:53
Posts: 1
Hi,

I am trying to interface two TC72 temperature sensors with PIC16F887 and display the temperature on LCD.
I get the correct results for measuring odd values. But, when I change it to even, I get zero on second sensor.
Below is the code.
Can someone help me?

Regards,
Iva

//LCD module connections
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
//End LCD module connections

//SS connections
sbit ss1 at RA5_bit;
sbit ss1_Direction at TRISA5_bit;
sbit ss2 at RA4_bit;
sbit ss2_Direction at TRISA4_bit;
sbit ss3 at RA3_bit;
sbit ss3_Direction at TRISA3_bit;
//End SS connections

unsigned short buffer;
char temp_txt[4];  //temperature value converted to text

unsigned short tempSensor1;
unsigned short tempSensor2;

void spiInit(){
     TRISC.b3 = 0;    //set RC3/SCK/SCL as output
     TRISC.b4 = 1;    //set RC4/SDI/SDA as input
     TRISC.b5 = 0;    //set RC5/SDO as output

     SSPSTAT = 0b00000000;   //Status register
     SSPCON = 0b00110001;    //Control register
}

unsigned short Read_Sensor_Temperature(){
     unsigned short temperature;
     SPI1_Write(0x02);
     temperature = SPI1_Read(buffer);
     return temperature;
}

void main(){
  //Select Slave Directions
  ss1_Direction = 0;  //set RA5 as output
  ss2_Direction = 0;  //set RA4 as output
  ss3_Direction = 0;  //set RA3 as output
 
  //Deselect slaves
  ss1 = 0;
  ss2 = 0;
  ss3 = 0;

  Lcd_Init();
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF);

  spiInit();
 
  ss1 = 1;    //select slave
  SPI1_Write(0x80); //write to control register
  SPI1_Write(0x04); //measure temperature every 150ms
  ss1 = 0;
  delay_ms(200);

  ss2 = 1; //select slave
  SPI1_Write(0x80); //write to control register
  SPI1_Write(0x04); //measure temperature every 150ms
  ss2 = 0;
  delay_ms(200);

  do{
     Lcd_Out(1, 1, "Temperatures:");
           
     ss1 = 1;
     tempSensor1 = Read_Sensor_Temperature();
     ByteToStr(tempSensor1, temp_txt);   //convert data received to string
     Lcd_Out(2, 1, temp_txt);
     ss1 = 0;

     ss2 = 1;
     tempSensor2 = Read_Sensor_Temperature();   
     ByteToStr(tempSensor2, temp_txt);   //convert data received to string
     Lcd_Out(2, 6, temp_txt);
     ss2 = 0;

     delay_ms(500);
     Lcd_Cmd(_LCD_CLEAR);
}while(1);
}


Attachments:
Capture1.PNG
Capture1.PNG [ 43.71 KiB | Viewed 83 times ]
Top
 Profile  
 
PostPosted: 28 Mar 2018 16:05 
Offline
User avatar

Joined: 05 Mar 2018 09:44
Posts: 84
Location: Belgrade
Hi,

Code seems ok, i really dont see anything that could be causing that issue.
Unfortunately, i dont have T72 to test if this happens outside of the simulation.


Kind regards,
Petar


Top
 Profile  
 
PostPosted: 29 Mar 2018 14:03 
Offline

Joined: 24 Aug 2014 17:55
Posts: 119
Location: Sheffield
Here's the link to the datasheet if you have not already downloaded it, note the data from the TC72 is in two's compliment form.
Bit 7 of the temperature register MSB is a sign bit.

ww1.microchip.com/downloads/en/DeviceDoc/21743a.pdf

I did write code for this a long time ago but it's dissapeared into the ether...
Regards

_________________
"When the bugs strike it's no good bashing your head against a brick wall, you might damage the wall"


Top
 Profile  
 
PostPosted: 30 Mar 2018 10:46 
Offline
User avatar

Joined: 05 Mar 2018 09:44
Posts: 84
Location: Belgrade
Hi,

I`ll look the datasheet and let you know if i find something.

Kind regards,
Petar


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: ex-egll and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: