Vi Điều Khiển

Chào mừng các bạn đến với thế giới của Vi điều khiển!

--welcome to the world of microcontrollers^^ --

Điện Tử Cơ Bản

nơi khởi đầu

Lập Trình

linh hồn của phần cứng

Hiển thị các bài đăng có nhãn hiển thị lên lcd 16x2. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn hiển thị lên lcd 16x2. Hiển thị tất cả bài đăng

Thứ Năm, 12 tháng 4, 2012

Code mẫu PIC - Giao Tiếp I2C Với IC Thời gian thực DS1307

.
Tham khảo thêm: Hướng dẫn - IC thời gian thực DS1307

Chương trình thực hiện giao tiếp I2C giữa PIC 16F877A và IC DS1307 để cài đặt thời gian, đọc thời gian từ DS1307, hiển thị lên LCD, truyền qua RS232.
Với LCD, chương trình sẽ đọc dữ liệu DS1307 và cập nhật LCD liên tục, còn khi truyền lên máy tính sẽ dựa vào ngắt RB0 đưa vào từ xung 1Hz của DS1307, tức là mỗi 1s sẽ truyền dữ liệu 1 lần.
Trong chương trình có sử dụng các thư viện: lcd_lib_4bit.cds1307.c.

Sơ đồ nguyên lý mô phỏng trên Proteus:



Truyền lên hyper terminal:



Mã nguồn chương trình chính:



#include "16f877a.h"
#include "def_877a.h"


#device *=16 ADC=10
#use delay(clock=20000000)
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use i2c(Master, sda = PIN_C4, scl=PIN_C3)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


#include "lcd_lib_4bit.c"
#include "ds1307.c"


#define Slave_add 0x68
#define Read 1
#define Write 0


void send(int8 a);


int8 sec,min,hrs,day,month,yr,dow;


//ngat o chan RB0: Truyen len cong RS232
#int_EXT
void EXT_isr(void) //moi 1s truyen len may tinh 1 lan
{
   ds1307_get_date(day,month,yr,dow); 
   ds1307_get_time(hrs,min,sec);
   send(hrs);
   putc(45);
   send(min);
   putc(45);
   send(sec);
   putc(10);
   return;
}
void main()
{
   enable_interrupts(INT_EXT);//cho phep ngat RB0
   ext_int_edge(0,H_TO_L);//dat suon ngat
   enable_interrupts(GLOBAL);//cho phep ngat toan cuc
   
   LCD_init(); //Khoi tao LCD.
   delay_ms(10);
   ds1307_init();// khoi tao DS1307, tao xung 1Hz o chan 7 DS1307.
   // Set date : 12-4-2012
   // Set time : thu 5 - 12 gio, 59 phút 10 giây
   ds1307_set_date_time(12,4,12,5,12,59,10);
   


   while(1) 
   { 
      ds1307_get_date(day,month,yr,dow); 
      ds1307_get_time(hrs,min,sec);
      
      //Truyen len LCD
      //o day chi hien gio, phut, giay. cac thong tin khac thuc hien tuong tu.
      LCD_PutCmd(0x80);
      LCD_PutChar(hrs/10+48);
      LCD_PutChar(hrs%10+48);
      LCD_PutChar(45);
      LCD_PutChar(min/10+48);
      LCD_PutChar(min%10+48);
      LCD_PutChar(45);
      LCD_PutChar(sec/10+48);
      LCD_PutChar(sec%10+48);
   }
}
void send(int8 a)
{
    if(a<10)
     {
         putc(a+48);         
     }
     if(a>9&&a<100)
     {
          unsigned char c=a/10;
          unsigned char d=a%10;
          putc(c+48);
          putc(d+48);       
     }
     if(a>99)
     {
          unsigned char t=a/100;
          unsigned char c=a/10-10*t;
          unsigned char d=a%10;
          putc(t+48);
          putc(c+48);
          putc(d+48);
     }
}


Thư viện lcd_lib_4bit.c:


#include <stddef.h>


#define LCD_RS          PIN_D2
//#define LCD_RW          PIN_A1
#define LCD_EN          PIN_D3


#define LCD_D4          PIN_D4
#define LCD_D5          PIN_D5
#define LCD_D6          PIN_D6
#define LCD_D7          PIN_D7


// misc display defines-
#define Line_1          0x80
#define Line_2          0xC0
#define Clear_Scr       0x01


// prototype statements
#separate void LCD_Init ( void );// ham khoi tao LCD
#separate void LCD_SetPosition ( unsigned int cX );//Thiet lap vi tri con tro
#separate void LCD_PutChar ( unsigned int cX );// Ham viet1kitu/1chuoi len LCD
#separate void LCD_PutCmd ( unsigned int cX) ;// Ham gui lenh len LCD
#separate void LCD_PulseEnable ( void );// Xung kich hoat
#separate void LCD_SetData ( unsigned int cX );// Dat du lieu len chan Data
// D/n Cong
#use standard_io (C)
#use standard_io (D)


//khoi tao LCD**********************************************
#separate void LCD_Init ( void )
    {
    LCD_SetData ( 0x00 );
    delay_ms(200);       /* wait enough time after Vdd rise >> 15ms */
    output_low ( LCD_RS );// che do gui lenh
    LCD_SetData ( 0x03 );   /* init with specific nibbles to start 4-bit mode */
    LCD_PulseEnable();
    LCD_PulseEnable();
    LCD_PulseEnable();
    LCD_SetData ( 0x02 );   /* set 4-bit interface */
    LCD_PulseEnable();      /* send dual nibbles hereafter, MSN first */
    LCD_PutCmd ( 0x2C );    /* function set (all lines, 5x7 characters) */
    LCD_PutCmd ( 0x0C );    /* display ON, cursor off, no blink */
    LCD_PutCmd ( 0x06 );    /* entry mode set, increment & scroll left */
    LCD_PutCmd ( 0x01 );    /* clear display */
    }


#separate void LCD_SetPosition ( unsigned int cX )
    {
    /* this subroutine works specifically for 4-bit Port A */
    LCD_SetData ( swap ( cX ) | 0x08 );
    LCD_PulseEnable();
    LCD_SetData ( swap ( cX ) );
    LCD_PulseEnable();
    }


#separate void LCD_PutChar ( unsigned int cX )
    {
    /* this subroutine works specifically for 4-bit Port A */
        output_high ( LCD_RS );
        LCD_PutCmd( cX );
        output_low ( LCD_RS );
    }


#separate void LCD_PutCmd ( unsigned int cX )
    {
    /* this subroutine works specifically for 4-bit Port A */
    LCD_SetData ( swap ( cX ) );     /* send high nibble */
    LCD_PulseEnable();
    LCD_SetData ( swap ( cX ) );     /* send low nibble */
    LCD_PulseEnable();
    }
#separate void LCD_PulseEnable ( void )
    {
    output_high ( LCD_EN );
    delay_us ( 3 );         // was 10
    output_low ( LCD_EN );
    delay_ms ( 3 );         // was 5
    }


#separate void LCD_SetData ( unsigned int cX )
    {
    output_bit ( LCD_D4, cX & 0x01 );
    output_bit ( LCD_D5, cX & 0x02 );
    output_bit ( LCD_D6, cX & 0x04 );
    output_bit ( LCD_D7, cX & 0x08 );
    }

Thư viện ds1307.c:


BYTE bin2bcd(BYTE binary_value);
BYTE bcd2bin(BYTE bcd_value);


void ds1307_init(void) 

   BYTE initsec = 0;
   BYTE initmin=0;
   BYTE inithr=0;                  
   BYTE initdow=0;
   BYTE initday=0;                   
   BYTE initmth=0;
   BYTE inityear=0;
   i2c_start(); 
   i2c_write(0xD0);      // WR to RTC 
   i2c_write(0x00);      // REG 0 
   i2c_start(); 
   i2c_write(0xD1);      // RD from RTC 
   initsec  = bcd2bin(i2c_read() & 0x7f); 
   initmin  = bcd2bin(i2c_read() & 0x7f); 
   inithr   = bcd2bin(i2c_read() & 0x3f); 
   initdow  = bcd2bin(i2c_read() & 0x7f);   // REG 3 
   initday  = bcd2bin(i2c_read() & 0x3f);   // REG 4 
   initmth  = bcd2bin(i2c_read() & 0x1f);   // REG 5 
   inityear = bcd2bin(i2c_read(0));         // REG 6 
   i2c_stop(); 
   delay_us(3);


   i2c_start(); 
   i2c_write(0xD0);      // WR to RTC 
   i2c_write(0x00);      // REG 0 
   i2c_write(bin2bcd(initsec));      // Start oscillator with current "seconds value 
   i2c_write(bin2bcd(initmin));      // REG 1 
   i2c_write(bin2bcd(inithr));       // REG 2 
   i2c_write(bin2bcd(initdow));      // REG 3 
   i2c_write(bin2bcd(initday));      // REG 4 
   i2c_write(bin2bcd(initmth));      // REG 5 
   i2c_write(bin2bcd(inityear));     // REG 6 
   i2c_start(); 
   i2c_write(0xD0);      // WR to RTC 
   i2c_write(0x07);      // Control Register 
   i2c_write(0x90);      //  squarewave output pin 1Hz
   i2c_stop(); 





void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
  sec &= 0x7F;
  hr &= 0x3F;


  i2c_start();
  i2c_write(0xD0);              // I2C write address
  i2c_write(0x00);              // Start at REG 0 - Seconds
  i2c_write(bin2bcd(sec));      // REG 0
  i2c_write(bin2bcd(min));      // REG 1
  i2c_write(bin2bcd(hr));       // REG 2
  i2c_write(bin2bcd(dow));      // REG 3
  i2c_write(bin2bcd(day));      // REG 4
  i2c_write(bin2bcd(mth));      // REG 5
  i2c_write(bin2bcd(year));     // REG 6
  i2c_write(0x90);              // REG 7 - 1Hz squarewave output pin
  i2c_stop();
}


void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x03);              // Start at REG 3 - Day of week
  i2c_start();
  i2c_write(0xD1);
  dow  = bcd2bin(i2c_read() & 0x7f);   // REG 3
  day  = bcd2bin(i2c_read() & 0x3f);   // REG 4
  mth  = bcd2bin(i2c_read() & 0x1f);   // REG 5
  year = bcd2bin(i2c_read(0));         // REG 6
  i2c_stop();
}


void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x00);                     // Start at REG 0 - Seconds
  i2c_start();
  i2c_write(0xD1);
  sec = bcd2bin(i2c_read() & 0x7f);
  min = bcd2bin(i2c_read() & 0x7f);
  hr  = bcd2bin(i2c_read(0) & 0x3f);
  i2c_stop();


}


BYTE bin2bcd(BYTE binary_value)
{
  BYTE temp;
  BYTE retval;


  temp = binary_value;
  retval = 0;


  while(1)
  {
    // Get the tens digit by doing multiple subtraction
    // of 10 from the binary value.
    if(temp >= 10)
    {
      temp -= 10;
      retval += 0x10;
    }
    else // Get the ones digit by adding the remainder.
    {
      retval += temp;
      break;
    }
  }


  return(retval);
}




// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
  BYTE temp;


  temp = bcd_value;
  // Shifting upper digit right by 1 is same as multiplying by 8.
  temp >>= 1;
  // Isolate the bits for the upper digit.
  temp &= 0x78;


  // Now return: (Tens * 8) + (Tens * 2) + Ones


  return(temp + (temp >> 2) + (bcd_value & 0x0f));





//tjtanja

Thứ Bảy, 25 tháng 2, 2012

code mẫu cho 8051 - Đo nhiệt độ, hiển thị LCD, truyền dữ liệu qua RS232

Chương trình thực hiện kết hợp đo nhiệt độ, hiển thị lên LCD, và truyền dữ liệu lên máy tính qua giao tiếp RS232.

Mô phỏng trên ISIS - Proteus:

mô phỏng trên ISIS - Proteus

Thứ Sáu, 24 tháng 2, 2012

Code mẫu cho PIC - Kết hợp: ADC đo nhiệt độ, điện áp, hiển thị LCD và giao tiếp với máy tính

Chương trình thực hiện đo nhiệt độ, điện áp, hiển thị kết quả lên màn hình LCD 16x2, và truyền giá trị lên máy tính.
Mô phỏng trên Proteus:


mô phỏng trên Proteus


Sơ đồ nguyên lý:


Sơ đồ nguyên lý


Mã nguồn:


#include "16f877a.h"
#include "def_877a.h"
#device *=16 ADC=10
#use delay(clock=20000000)
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include "lcd_lib_4bit.c"
//=======================================================================
void send(int8 a);
void HIENTHI_LCD(int8 t);
void HIENTHI_LCD_float(int8 t);
int8 count=0;
int8 i,value,vl[10];
//=======================================================================
#INT_RDA //ngat khi nhan du lieu.
Receive_isr()
{
   char c;
   c=getc();
   switch(c)
   {
      case '0':   //xoa man hinh LCD
      {
               LCD_putcmd(0x01);
      }
      break;
          //o day co the su dung ma thap phan! (ngoai tru cac ky tu dieu khien)
      case '-':   //lui con tro hien thi LCD 1 don vi.
      {
               LCD_putcmd(0x10);
      }
      break;
      case '1':   //truyen len may tinh: gia tri do duoc.
      {
         Set_ADC_channel(0);  //kenh 0 chan so2
         delay_us(10);
         for(i=0;i<10;i++)
         {
               vl[i]=read_adc();
               delay_us(10);
         }
         value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/20.48;
         send(value);
      }
      break;
      case '2':   //truyen len may tinh: gia tri do duoc.
      {
         Set_ADC_channel(1);  //kenh 1 chan so3
         delay_us(10);
         if(read_adc()==0)
         {
               for(i=0;i<10;i++)
               {
                     vl[i]=read_adc();
                     delay_us(10);
               }
               value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/10;
         }
         else
         {
               value=read_adc()+6;
               for(i=0;i<10;i++)
               {
                     vl[i]=read_adc()+6;
                     delay_us(10);
               }
               value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/10;
         }
         send(value);
      }
      break;
      case '9':   //thoat khoi ham ngat, cho ADC làm viec.
      {
            count=0;   //ket thuc viec truyen ky tu len LCD
            LCD_putcmd(0x01);
      }
      break;
      default : //truyen ky tu xuong LCD ^^.
      {
               count++;
               if(count==1) LCD_putcmd(0x01);
               if(count==16) LCD_putcmd(0xC0);
               if(count==32)
               {
                  LCD_putcmd(0x01);
                  count=0;
               }
               LCD_putchar(c);
      }    
    }
}
//=======================================================================
void main()
{
   //trisD=0x0C; //D0,D1 LA CONG VAO, D2-D7 LA CONG RA.
 
   enable_interrupts(int_rda); //cho phep ngat noi tiep nhan.
   enable_interrupts(GLOBAL);

 
   LCD_init(); //Khoi tao LCD.
   LCD_putcmd(0xC0);
   Printf(LCD_putchar,"Khoi tao...");
   delay_ms(250);

   setup_adc_ports(AN0_AN1_AN3);   //Khoi tao che do cho bo ADC.
   setup_adc(ADC_CLOCK_INTERNAL);
   delay_us(10);

   while(1)
   {
      //do nhiet do
      {
         Set_ADC_channel(0);  //kenh 0 chan so2
         delay_us(10);
         value=read_adc();
         value=value/2.048;
       
         LCD_putcmd(0x80);
         LCD_putchar("Nhiet do: ");
         LCD_putcmd(0x89);
         HIENTHI_LCD(value);
         LCD_putcmd(0x8C);
         LCD_putchar("oC");
         delay_ms(0.1);
      }
      //do diep ap
      {
         Set_ADC_channel(1);  //kenh 1 chan so3
         delay_us(10);
         if(read_adc()==0)
         {
               for(i=0;i<10;i++)
               {
                     vl[i]=read_adc();
                     delay_us(10);
               }
               value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/10;
         }
         else
         {
               value=read_adc()+6;
               for(i=0;i<10;i++)
               {
                     vl[i]=read_adc()+6;
                     delay_us(10);
               }
               value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/10;
         }

         LCD_PutCmd(0xC0);
         LCD_putchar("DIEN AP : ");
         LCD_putcmd(0xC9);
         //HIENTHI_LCD(value);
         HIENTHI_LCD_float(value);
         LCD_putcmd(0xCD);
         LCD_putchar("V ");
      }
   }
}
//=======================================================================
void HIENTHI_LCD(int8 t)
{
     unsigned char v;
     if(t<10)
          LCD_putchar(t+48);
     else
     if(t<100)
     {
          LCD_putchar(t/10+48);
          LCD_putchar(t%10+48);
     }
     else
     {
         v=t/10;
         LCD_putchar(v/10+48);
         LCD_putchar(v%10+48);
         LCD_putchar(t%10+48);
     }
}
void send(int8 a)
{
    if(a<10)
     {
         putc(a+48);      
     }
     if(a>9&&a<100)
     {
          unsigned char c=a/10;
          unsigned char d=a%10;
          putc(c+48);
          putc(d+48);    
     }
     if(a>99)
     {
          unsigned char t=a/100;
          unsigned char c=a/10-10*t;
          unsigned char d=a%10;
          putc(t+48);
          putc(c+48);
          putc(d+48);
     }
}
void HIENTHI_LCD_float(int8 t)
{
     int8 v;
     if(t<10)
     {
          LCD_putchar(48);
          LCD_putchar(46);
          LCD_putchar(t+48);
          LCD_putchar(32);
     }
     else
     if(t<100)
     {
          LCD_putchar(t/10+48);
          LCD_putchar(46);
          LCD_putchar(t%10+48);
          LCD_putchar(32);
     }
     else
     {
         v=t/10;
         LCD_putchar(v/10+48);
         LCD_putchar(v%10+48);
         LCD_putchar(46);
         LCD_putchar(t%10+48);
     }
}
/*
void send_float(float a)
{
    if(a<10)
     {
         putc(a+48);      
     }
     if(a>9&&a<100)
     {
          unsigned char c=a/10;
          unsigned char d=a%10;
          putc(c+48);
          putc(d+48);    
     }
     if(a>99)
     {
          unsigned char t=a/100;
          unsigned char c=a/10-10*t;
          unsigned char d=a%10;
          putc(t+48);
          putc(c+48);
          putc(d+48);
     }
}*/
//=======================================================================


Thư Viện: lcd_lib_4bit.c
Sơ Đồ Nguyên Lý: NguyenLy.pdf

Code mẫu cho PIC - Đo nhiệt độ, Hiển thị lên LCD 16x2

Bộ chuyển đổi từ tương tự sang số là một khối mạch điện tử quan trọng, có mặt trong rất nhiều thiết kế điện tử. Các bộ ADC thực tế được đóng gói trong những IC chuyên dụng, do nhiều hãng sản xuất. Điểm quan trong cần lưu ý ở các bộ ADC này là độ phân giải và tốc độ lấy mẫu tìn hiệu. Độ phân giải của bộ ADC có thể là 8‐bít, 10‐bít, 12‐bít, 16‐bít, 24‐bít… Tốc độ lấy mẫu của ADC có thể nhanh hay chậm, tùy từng ứng dụng mà ta chọn tốc độ thích hợp.
Vi điều khiển PIC là một trong những dòng Vi điều khiển có phần giao tiếp ngoại vi mạnh và đa dạng. Bên trong PIC đã được tích hợp sẵn một bộ ADC có độ phân giải tối đa là 10‐bít (tùy chon là 8‐bit hay 10‐bit). Với bộ ADC trong PIC ta có thể làm được khá nhiều công việc, dưới đây tôi trình bày một ứng dụng của bộ ADC trong việc thiết kế mạch đo nhiệt độ sử dụng sensor nhiệt LM335.

Dưới đây là phần code mạch đo nhiệt dộ, hiển thị trên LCD.


//=================================================  =======
// Ten chuong trinh  : Mach do nhiet do
// Mo ta phan cung  : Dung PIC16F877A ‐ thach anh 20MHz
//   : LCD giao tiep voi PORTD
//   : Dau ra LM335 dua vao chan AN0
//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
// Chu thich : hoac cac chu thich khac
//   : dung che do Power On Reset
//   : chuong trinh viet cho PIC Tutorial
//=================================================  =======
#include "16F877A.h"
#include "def_877a.h"
#device *=16 adc=10
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT,
NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=9)
#include <lcd_lib_4bit.c>  // Thu vien ham cho LCD
int8 low,high,key,mode,min,max,mode1,i;
int1 do_F;
void convert_bcd(int8 x);
void bao_dong();
void test();
//‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
void main()
{

 float value;
  on_off =1;
  min    =15;   //nhiet do min default
  max    =35;   //nhiet do max default
  do_F   =0 ;
  i = 50 ;
  mode   =0 ;
  mode1 = 0 ;
   trisa = 0xFF;
   trisb = 0x01;
   trisd = 0x00;
      LCD_init();
      Printf(LCD_putchar,ʺLop DT8 ‐ BKHNʺ);
      LCD_putcmd(0xC0);
      Printf(LCD_putchar,ʺKhoi tao...ʺ);
// Khoi tao cho ngat ngoai
      enable_interrupts (INT_EXT);
      ext_int_edge(H_TO_L);
      enable_interrupts (GLOBAL);
// Khoi tao che do cho bo ADC
      setup_adc_ports(AN0);
      setup_adc(ADC_CLOCK_INTERNAL);
      delay_us(10);
// Lay mau nhiet do lan dau tien
      value=(float)read_adc();
      value = (value ‐ 558.5)/2.048;        // For 5V supply
      //    value = (value ‐ 754.8)/2.048;  // For 3.7V Supply
      //    value = (value ‐ 698.2)/2.048;  // For 4V supply
      convert_bcd((int8)value);  // Tach so tram, chuc, donvi de hien thi len LED 7
      delay_ms(1000);
      LCD_putcmd(0xC0);
      Printf(LCD_putchar,ʺKhoi tao xongʺ);
      while(1)
      {
       if (i==50)
          {
            value = read_adc();

 value=(value‐558.5)/2.048;
            if (do_F==1) value=1.8*value+32;
            convert_bcd((int8)value);
            printf(ʺ\n\rNhiet do phong: %uʺ,value);// Gui gia tri len may tinh
            LCD_putcmd(0xC0);
            printf(LCD_putchar,ʺ  Temp = ʺ);
            LCD_putchar(high); LCD_putchar(low);
            if (do_F==0) printf(LCD_putchar,ʺ Cʺ);
            else printf(LCD_putchar,ʺ Fʺ);
            i=0;
          }
       i++;
       if(((int8)value > 40) || ((int8)value < 15)) on_off=1;
       else
        {
         on_off = 0;
         LCD_Putcmd(0xCF);
         LCD_putchar(ʺ ʺ);
         blink=0;
        }
       if (on_off==1)
       {
       if (blink==0) {
LCD_Putcmd(0xCF);LCD_putchar(ʺ!ʺ);blink=1;delay_ms(250);}
       else          {LCD_Putcmd(0xCF);LCD_putchar(ʺ ʺ);blink=0;delay_ms(250);}
       }
     }
   }//end main‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
#INT_EXT
void test()
{
   if (do_F == 1) do_F=0;
   else          do_F=1;
}
void convert_bcd(int8 x)
  {
   low=x%10;  //chia lay phan du, so hang don vi

 high=x/10;   //tach hang tram va hang chuc
   low = low + 0x30;
   high = high + 0x30;
  }
void bao_dong(){
int8 i;
if (blink == 0) blink = 1;
else            blink=0;
     for(i=0;i<50;i++)
     {
      LCD_Putcmd(0xCF);
      if (blink==0) LCD_putchar(ʺ!ʺ);
      else         LCD_putchar(ʺ ʺ);
     }
}

Dưới đây là một sơ đồ dùng PIC và LM335 để đo nhiệt độ, hiển thị trên LCD. Trong chương trình bạn thấy có hàm chuyển đổi nhiệt độ từ ía trị độ K về độ C. Nguyên nhân có hàm đó là do con LM335 thay đổi 10mV/K, ta cần hiển thị là độ C. Nhận thấy 0oC = 273K, như vậy tại 0oC con LM335 sẽ xuất ra một điện áp là 2.73V và với điện áp này, ADC trong PIC sẽ cho giá trị số là:  (2.73*1023)/5 = 558 . 5585 . Như vậy khi tính toán giá trị nhiệt độ ta cần trừ đi giá trị 558.558 này. Công thức đầy đủ là:
 Do_C= [(adc_value)- 558.558] / 2.048
Giá trị 2.048 có là do ta dùng ADC 10‐bit, điện áp lấy mẫu là 5V, như vậy mỗi mức lượng tử sẽ tương ứng với  5V/1024 = 4.883mV.
LM335 thay dổi 10mV/K do đó ứng với sụ thay đổi 1 độ C sẽ thay đổi 2.048 mức lượng tử (10mV/4.883mV = 2.048). Công thức trên là cho ADC 10‐bit, với các bộ ADC 8‐bit hay 12‐bit việc tính toán chuyển đổi giá trị cũng tương tự.
 Mạch đo nhiệt độ LM335 hiển thị trên LCD 16x2