temp_main.c

Example code for 16F819(8xx), which measures temperature via SMT160-30, shows it on LCD and communicates with RS232 (sends values of temperature to terminal) (by Petr Mervart).



//       ======================================================================
/*       Temperature measuring via SMT160-30(PWM output),LCD and RS232 outputs 
         ======================================================================
        
         By:
         ---
                     Petr Mervart - a501@seznam.cz 
         CPU TYPE:
         ---------
                     PIC 16F819 (also 16F87x but not tested)
         Oscilator:
         ----------
                     XT 4Mhz (or internal 4Mhz)
         RS232 mode:
         -----------
                      9600-8-N-1             

         Description:
         ------------
                     The programme measures temperature in 2s interval and shows it
                     on LCD. When PC is connected via RS232 and from some terminal programme
                     is sended char '1', PIC recieve that and sends to PC
                   actual temperature in 3bytes: 250 => 25.0C (negative values 
                   without '-' :)
                   I tested communication with Matlab 6.5 and work well indeed
                   (example in 'figure.jpg', who have an intereset, mail me).
                   The testing circuit for this programme is in 'temp.gif' picture
                     (and "real testing boards" in 'boards.jpg' - sorry for mess there :))        
                   (don't forget to convert TTL levels (from PIC) to RS232
                     using some converter (MAX232...)  
                     LCD rutines are the same as in my previous example '24th clock...'
         
         Include files:
         --------------
               #include "display.h"                --> LCD rutines
                               |
                               |-> "display.c"
               #include "rs232.h"                  --> RS232 rutines
                           |
                               |->"rs232.c"

                   #include "smt160.c"                 --> SMT160/30 measuring rutine

        ======================================================================       
               -- ALL rutines listed here are FOR FREE using --
                                  24.7.2003 
        ======================================================================                           
                                  
         Btw:   Sorry for my terrible English ! :]                                         
*/

#pragma CLOCK_FREQ 4000000 

asm __config 3F29h  ;            //'3F38' for INT OSC, '3F29' FOR XT OSC 
asm list p=PIC16F819
#include "p16f819.h"

#include "display.h"
#include "smt160.c" 
#include "rs232.h" 

char recieve;                       // recieved data from RS232

void interrupt()
{
   recieve=Get_Byte();
   if (recieve=='1')                // if recieved '1' then measure actual temp. and send back
   {                                // as 3 bytes (here max 99.5 C)    
       temperature();               // measuring... result in TEMP_D and TEMP_J
       Send_Int2(TEMP_D);        // sends result - char by char via RS232 
       Send_Byte('0'+TEMP_J);    // sends 0 or 5 (0.5C resolutions)
   }
   if (recieve=='2')                // if recieved '2' - " - 
   {                                // this rutine is suitable for Matlab postprocessing   
       temperature();               // measuring... result in stridaH and stridaL
       Send_Byte(stridaH);          // sends raw data - only 2bytes, stridaH and stridaH 
       delay_us(100);
       Send_Byte(stridaL);        // ex. 187 => (187-136)/2=25.5C 
   }                                // stridaL isn't necessary,only for more accurately result  
                                    // ex. stridaH=187 stridaL=056 => 
                                    //  => t = (187-136)/2 + 56/256 => 25.72C 

    clear_bit(INTCON,INTF);
}

void main()
{
    clear_bit(STATUS,RP0);       // BANK 0 //////// BANK 0 ////
    clear_bit(STATUS,RP1);       // BANK 0 //////// BANK 0 ////
    //-----BANK 0 ------------
    adcon0=01000000b;
    PORTA=11110b;
    PORTB=10000011b;
    //-----BANK 1 ------------
    clear_bit(STATUS,RP1);       // BANK 1 //////// BANK 1 //// 
    set_bit(STATUS,RP0);            // BANK 1 //////// BANK 1 ////

  //  set_bit(osccon,IRCF2);     
  //  set_bit(osccon,IRCF1);        // 4Mhz internal oscilator configuration
  //  clear_bit(osccon,IRCF0);   

    adcon1=00001110b;            // RA0 A/D input; RA1-RA4 digital IO
    TRISA=10001b;
    TRISB=10000101b;             // RB0-RX IN,RB1-TX OUT,RB2-sensor IN,RB3-RB6-LCD OUT
    OPTION_REG=10000100b;        // interrupt on falling edge for RB0 - IMPORTANT for RS232!!
     //-----BANK 0 ------------
    clear_bit(STATUS,RP0);       //// BANK 0 //////// BANK 0 ////
    //-----banka 0 ------------

    LCD_Setup();
    Lprintf("Temp [C]: ");

    set_bit(INTCON,INTE);        // interrupt from INTE (RB0-RxD input)
    clear_bit(INTCON,INTF);      // flag from INTF 
    set_bit(INTCON,GIE);         // global interrupt allowed

   while(1)
    {
      SetPos(set_dd_line1+9);
      delay_s(2);

      clear_bit(INTCON,GIE);        // disable all interrupts when measuring is in progress..
      temperature();
      set_bit(INTCON,GIE);

      if (minus) Putch('-');        // if minus flag is set -> write '-' on LCD
      else Putch('+');

      WriteInt2(TEMP_D);            // write ones,tens and hundreds of temp. on LCD 
      Putch('.');
      WriteInt1(TEMP_J);            // write tenths .0 or .5 

      SetPos(set_dd_line2);
      WriteInt3(stridaH);           // write stridaH (L) value (only for control)   
      Putch(':');
      WriteInt3(stridaL);
    }
}



http://www.sourceboost.com/home.html

Copyright © 2002-2006 SourceBoost Technologies