8 may 2011

SENSOR DE TEMPERATURA TC77 CON MÓDULO SPI



La finalidad de este ejercicio es la medición de la temperatura con el sensor TC77, utilizando el bus SPI para la comunicación entre el sensor y el PIC18F4550.

Lo primero que hay que hacer es mirar en el datasheet para saber la configuración del sensor. Una vez estudiado la configuración y el funcionamiento del TC77 comenzaremos a  realizar el código para la visualización de la temperatura en el LCD.


////////////////////////////////////////////////////////////////////////////////////
//   AUTOR: Clara Alonso Fernández                      Abril/2011
////////////////////////////////////////////////////////////////////////////////////
//   PROGRAMA:   LEER Tº DEL TC77                       VERSIÓN:    1.0
//   DISPOSITIVO: PIC18F4550                            COMPILADOR: CCS vs4.088
//   Entorno IDE: MPLAB 8.56                            SIMULADOR:  Proteus 7.7 sp2
//   TARJETA DE APLICACIÓN: virtual                     DEBUGGER: virtual 
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
//                       LEER LA Tº DEL SENSOR TC77 
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// CABECERA ////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
#include <18F4550.h>
#fuses INTHS            //Selecciona el oscilador interno
#use delay(internal=8Mhz)   // Selecciona la velocidad del oscilador interno
#include <LCD.c>
////////////////////////////////////////////////////////////////////////////////////
// VARIABLES GLOBALES //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// FUNCIONES ///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// PRINCIPAL ///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
void main()
{
long int parte_entera;
long int parte_decimal;
int temp_H;
int temp_L;
setup_spi(spi_MASTER | spi_L_to_H | spi_clk_div_16| SPI_XMIT_L_TO_H);
 lcd_init();
 lcd_putc('\f'); //Borra pantalla
 lcd_gotoxy(1,1);
//Secuencia para leer el Fabricante-----------------------------------------
//1º.- CS goes low to initiate the communication cycle. 
output_low(PIN_A0);
//2º.- Read 16 bits of temperature data from the Temperature register
spi_read(0); 
spi_read(0);
//3º.- Write 16 bits of data (i.e. XXFF hex) to the Configuration register to enter Shutdown mode. 
spi_write(0xff); 
spi_write(0xff);
//4º.- Read the 16 bits from the Manufacturer's ID register   (C15:C8 = 54 hex) 
//     to verify that the sensor is a Microchip device.
spi_read(0); 
spi_read(0); 
//5º.- Write 8 to 16 bits of data (00 or 0000 hex) to enter Continuous Conversion Mode.
spi_write(0x0);
spi_write(0x0); 
//6º.- Return CS high to terminate the communication cycle.
output_high(PIN_A0);
delay_ms(500); //Retardo para que al sensor le de tiempo a actualizar su registro de temperatura
 for (;;)
{
parte_entera=0;
parte_decimal=0;
output_low(PIN_A0);
//Leo el byte alto y bajo del registro de temperatura y sumo el peso de cada bit. El resultado
//lo guardo en las variables "parte_entera" y "parte_decimal"
temp_H=spi_read(0x00);
 if(bit_test(temp_H,6)){parte_entera=parte_entera+128.0;} 
if(bit_test(temp_H,5)){parte_entera=parte_entera+64.0;} 
if(bit_test(temp_H,4)){parte_entera=parte_entera+32.0;} 
if(bit_test(temp_H,3)){parte_entera=parte_entera+16.0;} 
if(bit_test(temp_H,2)){parte_entera=parte_entera+8.0;} 
if(bit_test(temp_H,1)){parte_entera=parte_entera+4.0;} 
if(bit_test(temp_H,0)){parte_entera=parte_entera+2.0;} 
temp_L=spi_read(0x00); //En proteus, esta lectura sale como ?? porque los
  //dos bits de menor peso quedan indefinidos
if(bit_test(temp_L,7)){parte_entera=parte_entera+1.0;} 
if(bit_test(temp_L,6)){parte_decimal=parte_decimal+500;} 
if(bit_test(temp_L,5)){parte_decimal=parte_decimal+250;} 
if(bit_test(temp_L,4)){parte_decimal=parte_decimal+125;} 
output_high(PIN_A0);
delay_ms(100);
lcd_gotoxy(1,1);
//Si el nº es negativo
//le resto 2^9 para encontrar el nº que representa la parte entera.
//(nº+)-(2^n)=(nº-)El nº positivo menos 2^n es igual al nº negativo, en C2, que representa
//La variable de la parte decimal contiene el complemento de las 3 cifras. Es decir, si la temperatura es 4,625ºC
//la variable contiene 275. Por ello, restamos 1000-parte_decimal y obtenemos el valor real.
if(bit_test(temp_H,7)) 
   {
   parte_entera=511-(parte_entera+256);                  
   printf(lcd_putc,"VOLTIOS=-%3lu",parte_entera);
   parte_decimal=1000-parte_decimal;   
   printf(lcd_putc,".%3luC",parte_decimal);
   }
//Si el nº es positivo no hago nada y muestro el contenido de las variables   
else{
   printf(lcd_putc,"VOLTIOS=%3lu",parte_entera);
   printf(lcd_putc,".%3luC",parte_decimal);   
   }   
 }
}

Una vez hecho el código pasaremos a realizar el esquemático con la ayuda del simulador de PROTEUS. El esquemático consta del PIC18F4550, de el sensor TC77, de una pantalla LCD y de un módulo SPI.
Os voy a mostrar cómo quedaría el esquemático.


El siguiente paso es realizar la PCB en el ares. Yo utilizo el "autoruter" para cablear más rápido.


Y si queremos también podemos ver la PCB en 3D con la herramienta "output 3D", que nos ayudará a hacernos una idea de como es la PCB de forma real.




Aquí os dejo un enlace con  el código, el esquemático y el datasheet: http://cid-64f7131e50857260.office.live.com/self.aspx/MEDIDOR%20TEMPERATURA%20TC77/TC77.rar


0 comentarios:

Publicar un comentario

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes Powered by Blogger | DSW printable coupons