jueves, 5 de enero de 2012

Creación de Sensor de temperatura

Hola a todos, en esta práctica lo que vamos a hacer es crear un sensor de temperatura. Para eso lo que vamos a necesitar es en primer lugar, el sensor. El TC77 es el que voy a usar yo.

Echad un vistazo a mi biblioteca, ahí encontrareis un enlace que os llevará al datasheet del mismo y donde podreis informaros sobre el componente. Como veis en las imagenes que nos proporciona la wikipedia, es muy sencillo comunicar un maestro con un esclavo, o varios de manera simultánea.

También necesitaremos una manera de saber qué temperatura está marcando nuestro TC77, para ello vamos a utilizar nuestro LCD alfanumérico para que se muestren los grados.

Como ya os he dicho que vamos a utilizar un LCD alfanumérico, vosotros en vuestra cabeza ya tendreis que tener pensado que vamos a utilizar esa librería, para poder controlarlo.
Aquí os la adjunto.




// flex_lcd.c

// These pins are for the Microchip PicDem2-Plus board,
// which is what I used to test the driver.  Change these
// pins to fit your own board.

#define LCD_DB4   PIN_D0
#define LCD_DB5   PIN_D1
#define LCD_DB6   PIN_D2
#define LCD_DB7   PIN_D3
//
#define LCD_RS    PIN_A3
#define LCD_RW    PIN_A2
#define LCD_E     PIN_A1

// If you only want a 6-pin interface to your LCD, then
// connect the R/W pin on the LCD to ground, and comment
// out the following line.

#define USE_LCD_RW   1

//========================================

#define lcd_type 2        // 0=5x7, 1=5x10, 2=2 lines
#define lcd_line_two 0x40 // LCD RAM address for the 2nd line


int8 const LCD_INIT_STRING[4] =
{
 0x20 | (lcd_type << 2), // Func set: 4-bit, 2 lines, 5x8 dots
 0xc,                    // Display on
 1,                      // Clear display
 6                       // Increment cursor
 };


//-------------------------------------
void lcd_send_nibble(int8 nibble)
{

// Note:  !! converts an integer expression
// to a boolean (1 or 0).
 output_bit(LCD_DB4, !!(nibble & 1));
 output_bit(LCD_DB5, !!(nibble & 2));
 output_bit(LCD_DB6, !!(nibble & 4));
 output_bit(LCD_DB7, !!(nibble & 8));

 delay_cycles(1);
 output_high(LCD_E);
 delay_us(2);
 output_low(LCD_E);
}

//-----------------------------------
// This sub-routine is only called by lcd_read_byte().
// It's not a stand-alone routine.  For example, the
// R/W signal is set high by lcd_read_byte() before
// this routine is called.

#ifdef USE_LCD_RW
int8 lcd_read_nibble(void)
{
int8 retval;
// Create bit variables so that we can easily set
// individual bits in the retval variable.
#bit retval_0 = retval.0
#bit retval_1 = retval.1
#bit retval_2 = retval.2
#bit retval_3 = retval.3

retval = 0;

output_high(LCD_E);
delay_cycles(1);

retval_0 = input(LCD_DB4);
retval_1 = input(LCD_DB5);
retval_2 = input(LCD_DB6);
retval_3 = input(LCD_DB7);

output_low(LCD_E);

return(retval);
}
#endif

//---------------------------------------
// Read a byte from the LCD and return it.

#ifdef USE_LCD_RW
int8 lcd_read_byte(void)
{
int8 low;
int8 high;

output_high(LCD_RW);
delay_cycles(1);

high = lcd_read_nibble();

low = lcd_read_nibble();

return( (high<<4) | low);
}
#endif

//----------------------------------------
// Send a byte to the LCD.
void lcd_send_byte(int8 address, int8 n)
{
output_low(LCD_RS);

#ifdef USE_LCD_RW
while(bit_test(lcd_read_byte(),7)) ;
#else
delay_us(60);
#endif

if(address)
   output_high(LCD_RS);
else
   output_low(LCD_RS);

 delay_cycles(1);

#ifdef USE_LCD_RW
output_low(LCD_RW);
delay_cycles(1);
#endif

output_low(LCD_E);

lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0xf);
}

//----------------------------
void lcd_init(void)
{
int8 i;

output_low(LCD_RS);

#ifdef USE_LCD_RW
output_low(LCD_RW);
#endif

output_low(LCD_E);

delay_ms(15);

for(i=0 ;i < 3; i++)
   {
    lcd_send_nibble(0x03);
    delay_ms(5);
   }

lcd_send_nibble(0x02);

for(i=0; i < sizeof(LCD_INIT_STRING); i++)
   {
    lcd_send_byte(0, LCD_INIT_STRING[i]);

    // If the R/W signal is not used, then
    // the busy bit can't be polled.  One of
    // the init commands takes longer than
    // the hard-coded delay of 60 us, so in
    // that case, lets just do a 5 ms delay
    // after all four of them.
    #ifndef USE_LCD_RW
    delay_ms(5);
    #endif
   }

}

//----------------------------

void lcd_gotoxy(int8 x, int8 y)
{
int8 address;

if(y != 1)
   address = lcd_line_two;
else
   address=0;

address += x-1;
lcd_send_byte(0, 0x80 | address);
}

//-----------------------------
void lcd_putc(char c)
{
 switch(c)
   {
    case '\f':
      lcd_send_byte(0,1);
      delay_ms(2);
      break;

    case '\n':
       lcd_gotoxy(1,2);
       break;

    case '\b':
       lcd_send_byte(0,0x10);
       break;

    default:
       lcd_send_byte(1,c);
       break;
   }
}

//------------------------------
#ifdef USE_LCD_RW
char lcd_getc(int8 x, int8 y)
{
char value;

lcd_gotoxy(x,y);

// Wait until busy flag is low.
while(bit_test(lcd_read_byte(),7));

output_high(LCD_RS);
value = lcd_read_byte();
output_low(lcd_RS);

return(value);
}
#endif

void lcd_setcursor_vb(short visible, short blink) {
  lcd_send_byte(0, 0xC|(visible<<1)|blink);
}



Tenemos que tener en cuenta una cosa en esta práctica a diferencia de las otras. Por ejemplo, en la entrada anterior, la de la RAM, utilicé una librería, que no había hecho yo, obviamente, pero que me sirvió para controlar el PCF8583, sin embargo, en esta práctica es diferente, es decir, no vamos a utlizar lo que es una librería de "control" a diferencia del LCD, por ejemplo.

No sé si me entendeis, en cualquier caso, esto puede ser algo más complicado de lo que puede parecer en un principio, puesto que vamos a tener que conocer los códigos de control y demás.


Aquí os adjunto el programa principal:


////////////////////////////////////////////////////////////////////////////////////
//   AUTOR: Gabi Allende                             Enero/2010
////////////////////////////////////////////////////////////////////////////////////
//   PROGRAMA:  Sensor TC77                        VERSIÓN:    1.0
//   DISPOSITIVO: PIC18F4550                            COMPILADOR: CCSc
//   Entorno IDE: MPLAB                        SIMULADOR:  Proteus 7.7
//   TARJETA DE APLICACIÓN: 2010/2011                   DEBUGGER:    ICD3

////////////////////////////////////////////////////////////////////////////////////
//                            SENSOR TC77
////////////////////////////////////////////////////////////////////////////////////
/*
El sensor consta de tres registros (CONFIGURACIÓN-TEMPERATURA-FABRICANTE)
Puede trabajar en dos modos:
.-SHUTDOWN (0xFFH)
.-CONTINUO (0x00H)
Para configurar los modos hace falta leer 16bit(en dos veces) y luego escribir otros 16
EJEMPLO:
    1. CS goes low to initiate the communication cycle.
    2. Read 16 bits of temperature data from the
    Temperature register.
    3. Write 16 bits of data (i.e. XXFF hex) to the Configuration
    register to enter Shutdown mode.
    4. Read the 16 bits from the Manufacturer's ID register
    (C15:C8 = 54 hex) to verify that the sensor
    is a Microchip device.
    5. Write 8 to 16 bits of data (00 or 0000 hex) to
    enter Continuous Conversion Mode.
    6. Return CS high to terminate the communication
    cycle.
------------------------------------------------------------------------------------
El registro da la temperatura en complemento a 2 y trabaja de la siguiente forma:
bit15 bit14 bit13 bit12 bit11 bit10 bit9 bit8 bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
T15   T14   T13   T12   T11   T10   T9   T8   T7   T6   T5   T4   T3   T2   T1   T0
2^8   2^7   2^6   2^5   2^4   2^3   2^2  2^1  2^0  2^-1 2^-2 2^-3 2^-4 *    X    X
EJEMPLO:
    Temperature = 85.125°C
    Temperature Register = 00101010 10010111b
    = 2^6 + 2^4 + 2^2 + 2^0 + 2^-3
    = 64 + 16 + 4 + 1 + 0.125
    = 85.125°C
*/
////////////////////////////////////////////////////////////////////////////////////
// CABECERA ////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
#include
#FUSES HS                    //External clock
#FUSES MCLR                    //Master Clear pin enabled
#fuses nowdt,noprotect,nolvp,nodebug,usbdiv,pll5,cpudiv1,novregen                     
#use fast_io(c)
#use fast_io(b)
#use delay(clock=20000000)
#define CS PIN_A0
//#fuses INTHS                //Selecciona el oscilador interno
//#use delay(internal=1Mhz)   // Selecciona la velocidad del oscilador interno
#include
#include
/*
Mientras no empleemos el bus USB, se utilizará el oscilador interno para simplificar
la configuración de reloj y reducir componentes externos.
*/
////////////////////////////////////////////////////////////////////////////////////
// VARIABLES GLOBALES //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
int Templ=0;
int Temph=0;
signed int16 Temp16Bits=0;
float decimales=0;
float enteros=0;
int1 banderanegativo=0;
float Temperatura;
char A4[7];
////////////////////////////////////////////////////////////////////////////////////
// FUNCIONES ///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
// PRINCIPAL ///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

void main()
{
glcd_init(ON);
set_tris_b(0b00000001);
set_tris_c(0X00);
setup_spi(spi_master|spi_l_to_h| spi_ss_disabled);


//configuracion del sensor en modo continuo
/*
output_low(CS);
spi_read(0);//leemos byte alto
spi_read(0);//leemos byte bajo
spi_write(0x00);
spi_write(0xFF);
spi_read(0);//leemos byte alto
spi_read(0);//leemos byte bajo
spi_write(0x00);
spi_write(0x00);
output_high(CS);
*/
    while(1)
    {
       
        output_low(CS);
        Temph=spi_read(0);//obtenemos el Temp alto del registro de temperatura
        Templ=spi_read(0);//obtenemos el Temp bajo del registro de temperatura
        output_high(CS);
        Temp16Bits=make16(Temph,Templ);//convertimos a un registro de 16Bits
        banderanegativo=0;
        if (Temp16Bits<0)
            {
                banderanegativo=1;
                Temp16Bits=~Temp16Bits+8;
       
            }//si es negativo lo complementamos a 2 y le sumamos 8
        Temp16Bits=Temp16Bits>>3;//descartamos los 3 bits menos significativos
        decimales=(float)(Temp16Bits&0b0000000000001111)*0.0625;//damos formato a los decimales
        enteros=(float)(Temp16Bits>>4); //damos formato a los enteros, solo descartando los decimales
        if (banderanegativo)
            {
                Temperatura=-1*(enteros+decimales);
               
               
           
            }
        else
            {
                Temperatura=(enteros+decimales);
               
           
            }
            glcd_line(1, 10, 8, 10, ON);
            //glcd_line(1, 10, 8, 10, OFF);
            //glcd_text57(11, 30, 2, 1, ON);
        //    glcd_fillScreen(on);
            sprintf(A4,"%.3f",Temperatura);
            glcd_text57(11, 30, A4, 1, ON);
           
            delay_ms(100);
           
    } 



Hay que tener en cuenta que debemos conectar el RA0 justo en el pin que pone (Tº) de la placa para que funcione.





Esta sería una foto del TC77 físicamente, así como la simulación en foto de mejor calidad.





Aquí te DESCARGAS EL PROGRAMA

Simulación en Proteus y la simulación real:

No hay comentarios:

Publicar un comentario