Thông báo

Collapse
No announcement yet.

Thiết kế bộ đo và điều khiển nhiệt độ dùng DS18B20 Lên LCD Sử dụng AT89C51

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • Thiết kế bộ đo và điều khiển nhiệt độ dùng DS18B20 Lên LCD Sử dụng AT89C51

    CODE Chuẩn:
    #include "REGX51.H"
    #include "stdio.H"
    sbit LCD_RS = P0^0;
    sbit LCD_RW = P0^1;
    sbit LCD_E = P0^2;
    sbit DQ = P3^7;
    sbit LCD_D4= P2^4;
    sbit LCD_D5= P2^5;
    sbit LCD_D6= P2^6;
    sbit LCD_D7= P2^7;
    #define LINE_1 0x80
    #define LINE_2 0xC0
    #define CLEAR_LCD 0x01
    void delay_ms(unsigned int Time);
    void delay_us(unsigned int Time);
    // Ham khoi tao DS18b20
    //-------------------------------------------------
    bit DS18B20_Init()
    {
    bit presence;
    DQ = 0 ;
    delay_us(29);
    DQ = 1 ;
    delay_us(3) ;
    presence = DQ ;
    delay_us(25) ;
    return(presence) ;
    }
    //=====================================
    //========= read bit=================
    bit ReadBit(void)
    {
    unsigned char i=0;
    DQ = 0; // pull DQ low to start timeslot
    DQ=1;
    for (i=0; i<3; i++); // delay 17 us from start of timeslot
    return(DQ); // return value of DQ line
    }
    //==============================================
    void WriteBit(bit Dbit)
    {
    unsigned char i=0;
    DQ=0;
    DQ = Dbit ? 1:0;
    delay_us(5); // delay about 39 uS
    DQ = 1;
    }
    //-------------------------------------------------
    // Ham doc 1 byte tu DS18b20 ra ngoai
    //-------------------------------------------------
    unsigned char ds18b20_readbyte(void)
    {
    unsigned char i = 0 ;
    unsigned char Data = 0 ;
    for (i = 0 ; i < 8 ; i++)
    {
    Data|=ReadBit()? 0x01<<iata;
    delay_us(6);
    }
    return (Data) ;
    }

    //-------------------------------------------------
    // Ham viet 1 byte vao DS18b20
    //-------------------------------------------------
    void ds18b20_writebyte(unsigned char Data)
    {
    unsigned char i = 0 ;
    for (i = 0 ; i < 8 ; i++)
    {
    WriteBit((bit)(Data & 0x1));
    Data = Data >> 1;
    }
    delay_us(5);
    }

    //-------------------------------------------------
    // Ham chuyen doi va tra ve gia tri nhiet do do duoc
    //-------------------------------------------------
    float ds18b20_readtemp(void)
    {
    float temp;
    unsigned char a,b;
    DS18B20_Init(); // Khoi tao DS18b20
    ds18b20_writebyte(0xCC) ; // Cho phep VDK truy cap thang den cac lenh bo nho cua DS18b20
    ds18b20_writebyte(0x44) ; // Khoi dong qua trinh do va chuyen doi nhiet do ra so nhi phan
    while (ds18b20_readbyte()==0xff);
    DS18B20_Init() ;
    ds18b20_writebyte(0xCC) ;
    ds18b20_writebyte(0xBE) ; // Cho phep doc du lieu tu bo nho DS18b20 ra ngoai

    a = ds18b20_readbyte();
    tempfloat)(a&0x0f))/16; // Lay phan thuc cua gia tri nhiet do
    b = ds18b20_readbyte();
    //a = (int)(a&0xff)*0x100+(b&0xff);
    a a&0xf0)>>4)|((b&0x0f)<<4) ; // Lay phan nguyen cua gia tri nhiet do
    temp=(float)a;
    return temp; // Tra ve gia tri nhiet do
    }
    // Ham tao xung ENABLE
    void lcd1602_enable(void)
    {
    LCD_E=1;
    delay_us(3);
    LCD_E=0;
    delay_us(5);
    }

    // --------------------------------------------------
    // Ham Gui 4 Bit Du Lieu Ra LCD
    void lcd1602_send_4bit_data ( unsigned char cX )
    {
    LCD_D4 = cX & 0x01;
    LCD_D5 = (cX>>1)&1;
    LCD_D6 = (cX>>2)&1;
    LCD_D7 = (cX>>3)&1;
    }

    // --------------------------------------------------
    // Ham Gui 1 Lenh Cho LCD
    void lcd1602_send_command (unsigned char cX )
    {
    lcd1602_send_4bit_data ( cX >>4 ); // gui 4 bit cao
    lcd1602_enable() ;
    lcd1602_send_4bit_data ( cX ); // gui 4 bit thap
    lcd1602_enable() ;
    }

    // --------------------------------------------------
    // Ham Khoi Tao LCD
    void lcd1602_init ( void )
    {
    lcd1602_send_4bit_data ( 0x00 );
    delay_ms(200);

    LCD_RS=0;
    LCD_RW=0;
    LCD_E=0; // che do gui lenh

    lcd1602_send_4bit_data ( 0x03 ); // ket noi 8 bit
    lcd1602_enable() ;
    lcd1602_enable () ;
    lcd1602_enable () ;
    lcd1602_send_4bit_data ( 0x02 ); // ket noi 4 bit
    lcd1602_enable() ;

    lcd1602_send_command( 0x2C ); // giao thuc 4 bit, hien thi 2 hang, ki tu 5x8
    lcd1602_send_command( 0x80);
    lcd1602_send_command( 0x0C); // cho phep hien thi man hinh
    lcd1602_send_command( 0x06 ); // tang ID, khong dich khung hinh
    lcd1602_send_command( CLEAR_LCD ); // xoa toan bo khung hinh
    }

    // --------------------------------------------------
    // Ham Thiet Lap Vi Tri Con Tro
    void lcd1602_gotoxy(unsigned char x, unsigned char y)
    {
    unsigned char address;
    if(!y)
    address = (LINE_1+x);
    else
    address = (LINE_2+x);
    delay_ms(3);
    lcd1602_send_command(address);
    delay_ms(1);
    }

    // --------------------------------------------------
    // Ham Xoa Man Hinh LCD
    void lcd1602_clear(void)
    {
    lcd1602_send_command( CLEAR_LCD );
    //delay_us(300);
    }

    // --------------------------------------------------
    // Ham Gui 1 Ky Tu Len LCD
    void lcd1602_putchar ( unsigned int cX )
    {
    LCD_RS=1;
    lcd1602_send_command( cX );
    LCD_RS=0;
    }

    // --------------------------------------------------
    // Ham Gui 1 Chuoi Ky Tu Len LCD
    void lcd1602_puts(char *s)
    {
    while (*s)
    {
    lcd1602_putchar(*s);
    s++;
    }
    }

    void delay_ms(unsigned int count)
    {
    unsigned int i;
    while(count) {
    i = 115;
    while(i>0) i--;
    count--;
    }
    }


    void delay_us(int us)
    {
    int i;
    for (i=0; i<us; i++);
    }

    void main()
    {
    float Nhiet_Do = 30.00;
    unsigned char Lcd_Buff[15];

    lcd1602_init();
    lcd1602_clear();
    lcd1602_gotoxy(0,0);
    lcd1602_puts(" WELLCOM TO..! ");
    lcd1602_gotoxy(0,1);
    lcd1602_puts(" HVKTQS ");
    delay_ms(3000);
    lcd1602_clear();
    lcd1602_gotoxy(0,0);
    lcd1602_puts("Temperature: ");

    while(1)
    {
    Nhiet_Do = ds18b20_readtemp();
    sprintf(Lcd_Buff," TEMP: %2.2f Celcius",Nhiet_Do);
    lcd1602_gotoxy(0,1);
    lcd1602_puts(Lcd_Buff);
    delay_ms(300);
    }

    }
    Click image for larger version

Name:	6-15-2013 10-12-53 PM.png
Views:	1
Size:	22.7 KB
ID:	1417593

  • #2
    hoan hô hay quá
    đam mê là không sợ khó khăn

    Comment


    • #3
      code bi loi ban oi

      Comment

      Về tác giả

      Collapse

      thaiban Tìm hiểu thêm về thaiban

      Bài viết mới nhất

      Collapse

      Đang tải...
      X