Anda di halaman 1dari 2

#include <16F877A.

h> void comandwrt(void)


#include <stdlib.h> {
#include <string.h>  RS = 0;
#fuses XT,NOWDT,NOPROTECT,NOLVP  // set  RW = 0;
config for PIC  E  = 1;
#use delay(clock=4000000)  E  = 0;
#use fast_io(A)                  // use with comand set_tris  delay_ms(1);
#use fast_io(B) }
#use fast_io(D) // Function send data to LCD
#byte   PORTB    = 0x06    // PortB address void datawrt(void)
#define LCD PORTB {
#bit RA2         = 0x05.2  RS = 1;
#bit RA1         = 0x05.1  RW = 0;
#bit RA0         = 0x05.0  E  = 1;
#define RS  RA0                // RS =0 comand,RST =1  E  = 0;
Data  delay_ms(1);
#define RW  RA1               // RW=1 read, RW =0 }
write // Function move pointer xy(colum,line) on LCD
#define E   RA2                 // H-to-L enable LCD display
#bit RD1         = 0x08.1     // data pin's DS1620 void LCD_gotoxy(int x, int y)
#bit RD2         = 0x08.2     // clock pin {  int address;
#bit RD3         = 0x08.3     // reset pin     switch(y)
#define DQ     RD1            // =0 output, =1 input    {
#define CLK    RD2          case 1:  address=0;
#define RST    RD3                   address+=x-1;
// Functions used in program                   LCD = (0x80 + address);    // line 1,
void LCD_init(void); colum1: 80h
void comandwrt(void);                   break;
void datawrt(void);          case 2:  address=0;
void LCD_gotoxy(byte x, byte y);                   address+=x-1;
void DS1620_InitTemp(void);                   LCD = (0xC0 + address);    // line 2,
unsigned int8 DS1620_GetTemp(void); colum1: C0h
void DS1620_Write(unsigned char Data);                   break;
unsigned int8 DS1620_Read(void);          default : break;
void display(int8 temp);    }
void main(void); }
// Global variable //xxxxxxxxxxxxxxxxxxxxxxx DS1620 Program
const unsigned char dongchu[]="Temp:";      // global xxxxxxxxxxxxxxxxxxxxx
const
int i = 0, sign = 0;                                      // void DS1620_InitTemp(void)
//xxxxxxxxxxxxxxxxxxxxxxxx LCD Program {
xxxxxxxxxxxxxxxxxxxxxxx       RST = 1;                       // setup 3-wire mode.
      DS1620_Write(0x0C);    // enable write config
void LCD_init()       DS1620_Write(0x00);    // clear config, setup
{ continuous converse
   delay_ms(50);       // delay at least 30ms       RST = 0;
   LCD = 0x38;         // 2line, dot matrix 5*8, 8 bits       RST = 1;                      // setup 3-wire mode
interface       DS1620_Write(0xEE);    // start converse
   comandwrt();          RST =0;
   delay_us(50);       // delay at least 39us }
   LCD = 0x0C;         // display on, cusor off and blink
off void DS1620_Write(unsigned char Data)
   comandwrt();    {
   delay_us(50);       // delay at least 39us    unsigned char i;
   LCD = 0x01;         // clear display    set_tris_D(0);             // Port D : ouput
   comandwrt();       CLK = 1;                    // set clock pulse
   delay_ms(3);        // delay at least 1.53ms    for (i=1; i<=8; ++i)
   LCD = 0x06;         // increase cusor    {
   comandwrt();        // and blink off        DQ = (Data & 1);   
   delay_ms(1);        CLK = 0;
}        CLK = 1;                // set clock
// Function send comand to LCD        Data = Data >> 1;  // shift Data
   }          datawrt();
}    }
      LCD = '.';
unsigned int8 DS1620_Read(void)       datawrt();
{       LCD = '5';
   unsigned int8 Data, temp;       datawrt();
   set_tris_D(0x02);            // DQ = 1 input, others:       LCD = ' ';
ouput       datawrt();
   Data = 0;       LCD = 'd';
   temp = 1;       datawrt();
   CLK = 1;       LCD = 'o';
   for (i=1; i<=8; ++i)       datawrt();
   {       LCD = 'C';
       CLK = 0;                     // CLK = 0: convert       datawrt();
       if (DQ == 1) Data += Temp;    }
       Temp = Temp << 1;           else
       CLK = 1;    {
   }       for(i=0;i<leng;i++)
   CLK = 0;    {
   if (DQ == 1) Sign = 1;      // get sign bit, the 9th bit          LCD = string[i];
   else Sign = 0;          datawrt();
   CLK = 1;    }
   return(Data);       LCD = '.';
}       datawrt();
      LCD = '0';
unsigned int8 DS1620_GetTemp(void)       datawrt();
{       LCD = ' ';
   unsigned int8 Data;       datawrt();
   RST =1;                        LCD = 'd';
   DS1620_Write(0xAA);       // enable read       datawrt();
temperature       LCD = 'o';
   data = DS1620_Read();    // read temp       datawrt();
   RST =0;                          // end 3wire mode       LCD = 'C';
   return(data);                  datawrt();
}    }
}
void display(int8 temp)
{ //xxxxxxxxxxxxxxxxxxxxxxxx Main function
   int8 Data, leng; xxxxxxxxxxxxxxxxxxxxxxx/
   unsigned char string[3]; void main(void)
   if (sign == 1)                        // minus degree {
   {    int8 data;
      LCD_gotoxy(5,2);    set_tris_B(0);                       // output
      comandwrt();    set_tris_A(0);                       // output
      LCD = '-';    LCD_init();                           // set LCD mode
      datawrt();    delay_us(2);
      Data = ~(temp - 1) / 2;    DS1620_InitTemp();
       delay_ms(2);
   }    LCD_gotoxy(5,1);                 // colum 5, line 1
   else  {  LCD_gotoxy(5,2);      // positive    comandwrt();
            comandwrt();    for (i=0;i<=8;i++)
            LCD = '+';    {
            datawrt();       LCD = dongchu[i];             // put line words to
            Data = temp / 2; LCD
         }       datawrt();
   itoa(Data,10,string);    }
   leng = strlen(string);    while(1)
   if (bit_test(temp,0))    {
   {       data  = DS1620_GetTemp();     // get data
      for(i=0;i<leng;i++)       display(data);
   {    }
         LCD = string[i];               // put temp }

Anda mungkin juga menyukai