Thursday, July 23, 2020

PIC16F818 interfaces to TC72 SPI Digital Temperature Sensor using CCS PICC

TC72 SPI Temperature Sensor At Brief

TC72 is a temperature-to-digital converter with serial peripheral interface (SPI). It could read the temperature from -55 to +125 degree Celsius. The temperature data is 10-bit format in which the upper 8-bit is the signed decimal value. The remaining two lower bits is the fraction value. The fraction is 0.25 degree Celsius step.


Sample program of TC72 temperature reading sensor negative Proteus CCS PICC PIC PIC16F display LCD microcontroller Microchip digital thermometer
A program screen shot reading the temperature of -16.25 degree Celsius.

The device comes with 8-pin SMD package.


TC72 SMD Package
Pins diagram of this device lists below.

TC72 Pins Diagram

The supply voltage is between 2.65 V to 5.5 V DC. All pins description are list below.

  1. NC - No Connection
  2. CE - Chip Enable (active high)
  3. SCK - Serial Clock Input
  4. GND - Ground
  5. SDO - Serial Data Out
  6. SDI - Serial Data In
  7. NC - No Connection
  8. VDD - Positive Supply 

For data communication, there are two read and write operations- SPI single byte and SPI multiple byte. But here, I implement only the SPI single byte implementation.


TC72 SPI Read/Write Operation
Ax Denotes the address of register. Dx is the data written or reading from the corresponding address.

Reading and writing need one 8-bit address and one 8-bit data. There are four addresses registers- Control, LSB Temperature, MSB Temperature and Manufacturer ID Register. Control register is read/write while others are read-only.

TC72 Address

Control register is for initialize the operation of this device. There are three modes- one-shot, continuous conversion and shut down. At power on reset or brown out reset, it is in shut down mode. Control register setting lists below.

  1. One-shot = 0 and Shut-Down = 0 - Continuous temperature conversion
  2. One-shot = 0 and Shut-Down = 1 - Shut Down
  3. One-shot = 1 and Shut-Down = 0 - Continuous temperature conversion
  4. One-shot = 1 and Shut-Down = 1 - One Shot

LSB temperature is the two-bit fraction number. It is 0.25 degree Celsius per bit, and 0.75 degree Celsius maximum value. We can ignore this fraction LSB temperature number with 0.75 degree Celsius error.

MSB temperature is the signed decimal temperature data. Manufacturer ID identifies this device.

Programming with CCS PICC

PIC16F818 comes with and SPI communication module. The program memory sizes up to 2 kB, sufficient for this programming example.

In this example, the SPI port commands to read the temperature from TC72 in continuous mode. An 16x2 character LCD display the result with full formatting. 

The MCU clocks at 4 MHz, yielding a 1 micro second instruction speed. SPI clock is divided by 64 to make a steady serial data reading. 


Schematic for this program
Schematic diagram

CCS PICC program lists below.


#include<16F818.h>
#use delay(clock=4M)
#fuses NOWDT,INTRC_IO

#define LCD_ENABLE_PIN  PIN_A2                                        
#define LCD_RS_PIN      PIN_A0 
#define LCD_RW_PIN      PIN_A1    
#define LCD_DATA4       PIN_A4    
#define LCD_DATA5       PIN_A3    
#define LCD_DATA6       PIN_A6  
#define LCD_DATA7       PIN_A7

/*use a built-in LCD driver*/
#include<lcd.c>

void main(){
   int sspL,sspH,fraction;
   /*223 is custom code for degree*/
   char c1=223,signing;
   int fractionN[3]={75,50,25};
   /*SPI master mode, clock low to high divided by 64*/
   setup_spi(SPI_MASTER | SPI_L_TO_H |SPI_CLK_DIV_64);

   output_b(0x00);
   set_tris_b(0x02);
   setup_adc_ports(no_analogs);
   lcd_init();
   lcd_gotoxy(1,1);
   
   printf(LCD_PUTC,"Temperature");
   /*Configure the TC72, Set the control
   register to continuous temperature reading*/
   output_high(pin_b0);
   /*0x80 is the control register address*/
   spi_write(0x80);
   /*0x04 means continuous temperature conversion*/
   spi_write(0x04);
   output_low(pin_b0);
   /*Wait for device to be ready*/
   delay_ms(250);
   
   while(1){
      
      /*Reading the LSB fraction number*/
      output_high(pin_b0);
      /*0x01 is the LSB temperature fraction number*/
      spi_write(0x01);
      /*output one more clock frame*/
      spi_write(0x00);
      output_low(pin_b0);    
      if( spi_data_is_in() ) sspL=spi_read();   
      
      /*Reading the MSB decimal number*/
      output_high(pin_b0);
      /*0x02 is MSB temperature address*/
      spi_write(0x02);
      /*output one more clock frame*/
      spi_write(0x00);    
      output_low(pin_b0);  
      if( spi_data_is_in() ) sspH=spi_read();
      
      lcd_gotoxy(1,2);
      
      /*Processing the fraction number*/
      sspL>>=6;
      fraction=sspL*25;
      
      /*Check wether temperature is negative*/
      if(sspH&0x80){
         sspH=~sspH;
         signing='-';
         fraction=fractionN[sspL];
      }
      else {
         signing=' ';
      }
      
      printf(LCD_PUTC,"%c%d.%d %cC   ",signing,sspH,fraction,c1);    
      delay_ms(1000);
   }
}

The overall program resource usage needs 86% of flash memory and 24% of program memory.

CCS PICC completed compilation


If you want a standard PCB for ATMega32 micro-controller, you can order my AVR Microcontroller project from PCBWay with a reasonable price. Click here to get a free $5 credit for new account.

Interfacing ATMega32 to 74HC595 shift register
ATMega16 ATMega32 Experiment Board PCB from PCBWay

 

 Watch it on YouTube


No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)