Thông báo

Collapse
No announcement yet.

8051 voi ds18b20 vs LCD

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

  • 8051 voi ds18b20 vs LCD

    các anh xem hộ em chương trình của em sai ở đâu mà nó không đọc đươc nhiệt độ từ ds18b20.em dùng thạch anh 12M.

    #include <REGX52.H>
    #include <string.h>
    #include <stdio.h>
    sfr LCDdata=0xA0;
    sbit BF=P2^7;
    sbit RS=P1^0;
    sbit RW=P1^1;
    sbit EN=P1^2;
    sbit DQ=P3^7;
    void delay(unsigned int time)
    {
    while(time--);
    }
    void wait(void)
    {
    unsigned char n=0;
    EN=1;
    RS=0;
    RW=1;
    LCDdata=0xFF;
    while(BF) {n++;if(n>100) break;}
    EN=0;
    RW=0;
    }
    void LCDcontrol(unsigned char x)
    {
    EN=1;
    RS=0;
    RW=0;
    LCDdata=x;
    EN=0;
    wait();
    }
    void LCDwrite(unsigned char c)
    {
    EN=1;
    RS=1;
    RW=0;
    LCDdata=c;
    EN=0;
    wait();
    }
    void LCDinit(void)
    {
    LCDcontrol(0x30);
    LCDcontrol(0x30);
    LCDcontrol(0x30);
    LCDcontrol(0x38);
    LCDcontrol(0x0C);
    LCDcontrol(0x06);
    LCDcontrol(0x01);
    }
    void LCDgotoxy(unsigned char cot,unsigned char hang)
    {
    char d;
    if(hang==0) LCDcontrol(0x80);
    else LCDcontrol(0xC0);
    for(d=0;d<cot;d++)
    {
    LCDcontrol(0x14);
    }
    }
    void LCDclear()
    {
    LCDcontrol(0x01);
    }
    void LCDputs(unsigned char *s)
    {
    unsigned char len;
    len=strlen(s);
    while(len!=0)
    {
    LCDwrite(*s);
    s++;
    len--;
    }
    }
    //-------------------------------------------------
    // Ham khoi tao DS18b20
    //-------------------------------------------------
    void Init_DS18B20()
    {
    unsigned char x=0;
    DQ = 1;
    delay(8);
    DQ = 0;
    delay(80);
    DQ = 1;
    delay(14);
    delay(20);
    x=DQ;
    }
    //=DOC MOT BYTE
    unsigned char ds18b20_readbyte(void)
    {
    unsigned char i=0;
    unsigned char dat = 0;
    for (i=8;i>0;i--)
    {
    DQ = 0; //CHO DUONG DQ XUONG MUC THAP
    dat>>=1;
    DQ = 1; //CHO DUONG DQ LEN MUC CAO
    if(DQ)
    dat|=0x80;
    delay(4);
    }
    return(dat);
    }
    //========================
    //=CHUONG TRINH GHI 1BYTE
    void ds18b20_writebyte(unsigned char dat)
    {
    unsigned char i=0;
    for (i=8; i>0; i--)
    {
    DQ = 0;
    DQ = dat&0x01;
    delay(5);
    DQ = 1;
    dat>>=1;
    }
    }
    //-------------------------------------------------
    // Ham chuyen doi va tra ve gia tri nhiet do do duoc
    //-------------------------------------------------
    float ds18b20_readtemp(void)
    {
    float temp;
    unsigned char a,b;
    Init_DS18B20; // 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
    Init_DS18B20 ;
    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 a&0xf0)>>4)|((b&0x0f)<<4) ; // Lay phan nguyen cua gia tri nhiet do
    temp=temp+a;
    return temp; // Tra ve gia tri nhiet do
    }
    void main()
    {
    float tep;
    unsigned char Lcd_Buff[15];
    LCDinit();
    LCDgotoxy(4,0);
    LCDputs("WELCOME");
    delay(40000);
    LCDclear();
    while(1)
    {
    LCDgotoxy(0,0);
    LCDputs(" ");
    LCDgotoxy(0,0);
    LCDputs("cam bien nhiet");
    tep = ds18b20_readtemp();
    sprintf(Lcd_Buff,"TEMP: %2.2f^C",tep);
    LCDgotoxy(0,1);
    LCDputs(Lcd_Buff);
    delay(300);
    }
    }

    em mô phỏng nó cứ hiện nhiệt độ là cố định TEMP:255.94^C.
    các anh giúp em với.

  • #2
    sao không ai giúp em vậy.

    Comment


    • #3
      hàm LCD ok chưa?nếu đúng rồi thì mình có code ds18b20 luôn.....chạy thực tế luôn rồi...

      Comment


      • #4
        Nguyên văn bởi voiviqua Xem bài viết
        sao không ai giúp em vậy.
        trước hết bạn cho hiển thị ký tự lên LCD em thử OK chưa. nếu đã hiển thị đc ký tự thì đọc nhiệt độ về, chuyển đổi và cho hiển thị bất kỳ vị trí nào bạn muốn trên LCD. LCD cũng ko khó lắm.
        Click image for larger version

Name:	RTC-LCD.JPG
Views:	3
Size:	56.7 KB
ID:	1375723
        Chúc bạn thành công.
        Email:
        Phone: 0969 226 539

        Comment


        • #5
          tui nghĩ bạn reset ds18b20 chưa đúng

          Comment


          • #6
            Nguyên văn bởi vantrinh0902 Xem bài viết
            trước hết bạn cho hiển thị ký tự lên LCD em thử OK chưa. nếu đã hiển thị đc ký tự thì đọc nhiệt độ về, chuyển đổi và cho hiển thị bất kỳ vị trí nào bạn muốn trên LCD. LCD cũng ko khó lắm.
            [ATTACH=CONFIG]62415[/ATTACH]
            Chúc bạn thành công.
            bạn cho mình cái code sử dụng trong cái hình bạn gửi lên diễn đàn đi

            Comment


            • #7
              Nguyên văn bởi kimman3 Xem bài viết
              bạn cho mình cái code sử dụng trong cái hình bạn gửi lên diễn đàn đi
              Code đây bạn:
              /* --------------------- DONG A UNIVERSITY ------------------------------
              ------------------------ ELECTRICAL TECHONOLOGY -------------------------
              *********
              ------------------------ DO AN MON HOC VDK2 -----------------------------
              ---- DE TAI : DONG HO THOI GIAN THUC DUNG RTC DS1307 ----------------
              ---- DO NHIET DO MOI TRUONG DUNG DS18B20 ----------------
              ---- UNG DUNG VDK HIEN THI LCD ----------------
              ---- AUTHOR: NGUYEN VAN TRINH -----------------------------------*/

              #include <AT89X51.H>
              #include <INTRINS.H>

              // DINH NGHIA 1 SO CONG

              ///////////////////////////////////////////////////
              #define rs P1_5
              #define rw P1_6

              #define en P1_7
              #define menu P3_2
              #define tang P3_5
              #define giam P3_4
              #define congLCD P2
              #define D7 P2_7
              // KHAI BAO DS18B20
              sbit DQ = P1^4;
              sbit l1 = P1^3;
              sbit l2 = P1^2;
              sbit l3 = P1^1;
              sbit l4 = P1^0;
              unsigned char so1[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9 0};
              int nhiet_thuc,h,d,y;
              unsigned char trt,cht,dvt;

              unsigned char *rtc[]={"0","1","2","3","4","5","6","7","8","9"};
              unsigned char *day[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
              unsigned char giatrikhoitao_rtc[8];



              void delay_ms(unsigned int ms)
              {
              unsigned int x,y;
              for(x=0;x<ms;x++)
              {
              for(y=0;y<500;y++){};
              }
              }
              /************************************************** ********************************/
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              // TAO THOI GIAN TRE DUNG TIMER 0 //
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              void delay(long time)//tre time ms
              {
              while(time--)
              {
              TMOD=0x01;// che do 16 bit khong tu nap lai
              TH0=0xFC;TL0=0x67; // 6*( 65536 - FC67+1)*(1/12000)=1000us =1ms
              TR0=1;
              while(TF0!=1){};
              TF0=0;
              TR0=0;}
              }



              /************************************************** ****************************/
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              // CAC CHUONG TRINH CON GIAO TIEP VOI LCD //
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~//

              void busy()
              {
              D7=1;
              rs=0;
              rw=1;
              while(D7!=0)
              {
              en=0;
              en=1;
              }
              }
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              void ghilenhLCD(unsigned char x)
              {
              busy();
              congLCD=x; // gia tri x
              rs=0; // chon thanh ghi lenh
              rw=0; // ghi len lcd
              en=1; // cho phep muc cao
              en=0; //xung cao xuong thap
              }
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              void khoitaoLCD(void)
              {
              ghilenhLCD(0X38); // hai dong va ma tran 5x7
              ghilenhLCD(0X0C); //bat man hinh , tat con tro
              ghilenhLCD(0X01); //xoa man hinh
              ghilenhLCD(0X06); // dich hien thi sang phai(tang con tro sang phai)
              }
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              void ghi_kytu(unsigned char value)
              {
              busy();
              congLCD=value;
              rs=1;
              rw=0;
              en=1;
              en=0;
              }
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              void ghi_chuoi(unsigned char *string)
              {
              unsigned char i;
              for(i=0;string[i]!='\0';i++)
              ghi_kytu(string[i]);
              }
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

              void ghiso(unsigned char a)
              {
              unsigned char i;
              i=a/10;
              ghi_chuoi(rtc[i]);
              i=a%10;
              ghi_chuoi(rtc[i]);
              }
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              void hienthi_dulieu_rtc(unsigned char x)
              {
              unsigned char temp;
              temp = x/16;
              ghi_chuoi(rtc[temp]);
              temp = x%16;
              ghi_chuoi(rtc[temp]);
              }

              void delay_18B20(unsigned int i)
              {
              while(i--);
              }
              void Init_DS18B20(void)
              {
              unsigned char x=0;
              DQ = 1;
              delay_18B20(8);
              DQ = 0;
              delay_18B20(80);
              DQ = 1;
              delay_18B20(14);
              x=DQ;
              delay_18B20(20);
              }



              unsigned char ReadOneChar(void)
              {
              unsigned char i=0;
              unsigned char dat = 0;
              for (i=8;i>0;i--)
              {
              DQ = 0;
              dat>>=1;
              DQ = 1;
              if(DQ)
              dat|=0x80;
              delay_18B20(4);
              }
              return(dat);
              }


              void WriteOneChar(unsigned char dat)
              {
              unsigned char i=0;
              for (i=8; i>0; i--)
              {
              DQ = 0;
              DQ = dat&0x01;
              delay_18B20(5);
              DQ = 1;
              dat>>=1;
              }
              }



              void ReadTemp(void)
              {
              unsigned char a=0;
              unsigned char b=0;
              unsigned char t=0;

              Init_DS18B20();
              WriteOneChar(0xCC);
              WriteOneChar(0x44);

              delay_18B20(100);

              Init_DS18B20();
              WriteOneChar(0xCC);
              WriteOneChar(0xBE);

              delay_18B20(100);

              a=ReadOneChar();
              b=ReadOneChar();
              nhiet_thuc=b<<4;
              nhiet_thuc+=(a&0xf0)>>4;
              }
              /* ============ Chuyen doi gia tri nhiet do doc duoc DS18B20 ============*/
              void chuyen_doi_DS18B20()
              {
              unsigned char cht,dvt;
              ghilenhLCD(0xCB);


              cht=(nhiet_thuc/10)%10;
              ghi_chuoi(rtc[cht]);

              dvt=nhiet_thuc % 10;
              ghi_chuoi(rtc[dvt]);
              ghi_kytu('o');
              ghi_kytu('C');
              //trt=(nhiet_thuc/100)%10;
              }
              /* ================= Hien thi nhiet do tu DS18B20 len LCD ==================
              void hien_thi_DS18B20()
              {

              // vi tri 15 hang thu 1


              }
              /*
              void ghiso(unsigned char a)
              {
              unsigned char i;
              i=a/10;
              ghi_chuoi(rtc[i]);
              i=a%10;
              ghi_chuoi(rtc[i]);
              }
              */

              void hien_thi()
              {if(nhiet_thuc>99)
              nhiet_thuc=99;
              if(nhiet_thuc<1)
              nhiet_thuc=0;
              for(h=0;h<100;h++)
              {
              P0=0xff;
              l4=0;
              P0=so1[nhiet_thuc/10];
              delay_ms(0);
              l4=1;
              P0=0xff;
              //

              l3=0;
              P0=so1[nhiet_thuc%10];
              delay_ms(0);
              l3=1;
              P0=0xff;
              //
              l2=0;
              P0=0x9c;
              delay_ms(0);
              l2=1;
              P0=0xff;
              //
              l1=0;
              P0=0xc6;
              delay_ms(0);
              l1=1;
              P0=0xff;
              }
              }

              /************************************************** ********************************/
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              // CAC CHUONG TRINH CON GIAO TIEP VOI RTC //
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              // ca 2 dk start va stop dc tao ra boi thiet bi chu
              void start_rtc(void) //dk start: 1 su cdoi tthai tu cao xuong thap tren duong sda trong khi
              { //duong scl dang o muc cao
              scl=1;
              sda=1;
              _nop_();
              _nop_();
              sda=0;
              scl=0;
              }
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              void stop_rtc(void) //dk stop: 1 su cdoi trang thai tu muc thap len cao tren duong sda trong khi duong
              { // slc dang o muc cao
              sda=0;
              scl=1;
              _nop_();
              _nop_();
              sda=1;
              }
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              void gui_rtc(unsigned char x)
              {
              unsigned char i;
              for(i=0;i<8;i++)
              {
              sda=(x&0x80)? 1:0; //dua bit du lieu ra chan SDA
              scl=1;
              _nop_();
              _nop_();
              scl=0;
              x<<=1; //bit co trong so lon hon dc truyen truoc
              }
              scl=1; //nhan bit ACK tu SLAVER bao hieu ket thuc mot byte du lieu
              _nop_();
              _nop_();
              scl=0;
              }
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              unsigned char nhan_rtc(void)
              {
              unsigned char Data,i;
              for(i=0;i<8;i++) // nhan vao 8 bit
              {
              scl=1;
              Data<<=1;
              Data=Data|sda;
              scl=0;
              }
              sda=1; // trong qua trinh doc mot chuoi byte tu slaver master gui bit ACK=> sda=1
              scl=1; //master nhan/gui bit du lieu(sda) khi scl o muc cao
              _nop_();
              scl=0;
              _nop_();//du lieu(sda) thay doi khi scl muc thap/
              return Data;//tra gia tri cho ham
              }
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              char docdulieu(unsigned char diachi)
              {
              unsigned char Data;
              start_rtc();
              gui_rtc(0xd0);
              gui_rtc(diachi);
              start_rtc();
              gui_rtc(0xd1); //0xd0 + 0xd1 la dia chi cua ds107 va che do doc du lieu
              Data=nhan_rtc();
              stop_rtc();//Stop I2C
              return Data;
              }
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              void ghivaoDS1307(void)
              {
              unsigned char t;
              for(t=0;t<9;t++)
              {
              start_rtc();
              gui_rtc(0xd0);
              gui_rtc(t);
              gui_rtc(giatrikhoitao_rtc[t]);
              stop_rtc();
              }

              }


              /************************************************** ********************************/
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              // HIEN THI GIO , NGAY THANG TREN LCD //
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              void hienthi_rtc(void)

              {
              ghilenhLCD(0xC7); // ep con tro den vi tri thu 12 dong thu 2
              hienthi_dulieu_rtc(docdulieu(0)); //hien thi giay , du lieu doc ra la ma bcd nhung ham hienthi_dulieu_rtc da chuyen sang dec roi
              ghilenhLCD(0xc4); // ep con tro den vi tri thu 9 dong thu 2
              hienthi_dulieu_rtc(docdulieu(1)); // hien thi phut
              ghi_kytu(':');
              ghilenhLCD(0xc1); // ep con tro den vi tri thu 6 dong thu 2
              hienthi_dulieu_rtc(docdulieu(2)); //hien thi gio
              ghi_kytu(':');


              ghilenhLCD(0x8C); //
              ghi_chuoi(day[docdulieu(3)-1]); //hien thi thu
              ghi_kytu(' '); // KI TU CACH LY
              ghilenhLCD(0x81); //ep con tro den vi tri thu 6 dong thu 1
              hienthi_dulieu_rtc(docdulieu(4));//hien thi ngay
              ghi_kytu('/');
              hienthi_dulieu_rtc(docdulieu(5));//hien thi thang
              ghi_chuoi("/20");
              hienthi_dulieu_rtc(docdulieu(6)); //nam
              }







              /************************************************** ********************************/
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              // CHUONG TRINH CON THUC HIEN VIEC CAI DAT NGAY GIO, THANG NAM //
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~//

              unsigned char bcd_dec(unsigned char bcd)//chuyen tu bcd sang decima
              {
              return((bcd/16)*10+(bcd%16)); //vd bcd=66 -> dec =42
              }

              unsigned char dec_bcd(unsigned char dec)// chuyen tu decima sang bcd
              {
              return((dec/10)*16+(dec%10));
              }
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              void caidat_rtc()
              {
              unsigned char giay,phut,gio,thu,ngay,thang,nam;
              giay = bcd_dec(docdulieu(0)&0x7f); // de bit 7 (bit clock halt) cua thanh ghi giay = 0 => ko bi treo)
              phut = bcd_dec(docdulieu(1));
              gio = bcd_dec(docdulieu(2)& 0x3f); //che do 24 h
              thu = bcd_dec(docdulieu(3));
              ngay = bcd_dec(docdulieu(4));
              thang = bcd_dec(docdulieu(5));
              nam = bcd_dec(docdulieu(6));
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              // CAI DAT GIO
              ghilenhLCD(1);
              ghi_chuoi("CHINH");
              ghi_chuoi(" GIO :");
              ghilenhLCD(0x0e);
              ghilenhLCD(0xc4);
              ghiso(gio);
              ghi_kytu(':');
              ghiso(phut);
              ghi_kytu(':');
              ghiso(giay);
              /////////////////////////////////////////
              ghilenhLCD(0xC5);
              while(menu==0);
              caidatgio:while(tang!=0 && giam!=0 && menu!=0);
              if(tang==0)
              {
              gio++;
              if(gio==24) gio=0;
              ghilenhLCD(0xC4);
              ghiso(gio);
              ghilenhLCD(0xC5);
              delay(300);
              goto caidatgio; }
              if(giam==0)
              {
              gio--;
              if(gio==0xff) gio=23;
              ghilenhLCD(0xC4);
              ghiso(gio);
              ghilenhLCD(0xC5);
              delay(300);
              goto caidatgio;}

              ////////////////////////////////////////////
              while(menu==0);
              ghilenhLCD(0xC8);
              caidatphut: while(tang!=0 && giam!=0 && menu!=0);
              if(tang==0)
              {
              phut++;
              if(phut==60) phut=0;
              ghilenhLCD(0xC7);
              ghiso(phut);
              ghilenhLCD(0x10);
              delay(300);
              goto caidatphut;}
              if(giam==0)
              {
              phut--;
              if(phut==0xff) phut=59;
              ghilenhLCD(0xC7);
              ghiso(phut);
              ghilenhLCD(0x10);
              delay(300);
              goto caidatphut;
              }

              //////////////////////////////////////////////
              while(menu==0);
              ghilenhLCD(0xCB);
              caidatgiay: while(tang!=0 && giam!=0 && menu!=0);
              if(tang==0)
              {
              giay++;
              if(giay==60) giay=0;
              ghilenhLCD(0xCA);
              ghiso(giay);
              ghilenhLCD(0xCB);
              delay(100);
              goto caidatgiay;
              }
              if(giam==0)
              {
              giay--;
              if(giay==0xff) giay=59;
              ghilenhLCD(0xCA);
              ghiso(giay);
              ghilenhLCD(0xCB);
              delay(100);
              goto caidatgiay;
              }

              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              // CAI DAT NGAY - THANG - NAM

              while(menu==0);
              ghilenhLCD(1);
              ghi_chuoi("CHINH NGAY :");
              ghilenhLCD(0xc4);
              ghiso(ngay);
              ghi_kytu('-');
              ghiso(thang);
              ghi_chuoi("-20");
              ghiso(nam);
              ///////////////////////////////////////////////////
              ghilenhLCD(0xc5);
              caidatngay:while (tang!=0 && giam!=0 && menu!=0);
              if(tang==0)
              {
              ngay++;
              if(ngay==32) ngay=1;
              ghilenhLCD(0xC4);
              ghiso(ngay);
              ghilenhLCD(0x10);
              delay(300);
              goto caidatngay;
              }

              if(giam==0)
              {
              ngay--;
              if(ngay==0) ngay=31;
              ghilenhLCD(0xC4);
              ghiso(ngay);
              ghilenhLCD(0x10);
              delay(300);
              goto caidatngay;
              }

              ////////////////////////////////////////////////////
              while(menu==0);
              ghilenhLCD(0xC8);
              caidatthang:while(tang!=0 && giam!=0 && menu!=0);
              if(tang==0)
              {
              thang++;
              if(thang==13) thang=1;
              ghilenhLCD(0xC7);
              ghiso(thang);
              ghilenhLCD(0x10);
              delay(300);
              goto caidatthang;
              }
              if(giam==0)
              {
              thang--;
              if(thang==0) thang=12;
              ghilenhLCD(0xC7);
              ghiso(thang);
              ghilenhLCD(0x10);
              delay(300);
              goto caidatthang;
              }

              ////////////////////////////////////////////////////////
              while(menu==0);
              ghilenhLCD(0xCD);
              caidatnam:while(tang!=0 && giam!=0 && menu!=0);
              if(tang==0)
              {
              nam++;
              if(nam==100) nam=0;
              ghilenhLCD(0xCC);
              ghiso(nam);
              ghilenhLCD(0xCD);
              delay(300);
              goto caidatnam;
              }

              if(giam==0)
              {
              nam--;
              if(nam==0xff) nam=99;
              ghilenhLCD(0xCC);
              ghiso(nam);
              ghilenhLCD(0xCD);
              delay(300);
              goto caidatnam;
              }


              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              // CAI DAT THU
              while(menu==0);
              ghilenhLCD(1);
              ghi_chuoi("CHINH THU :");
              ghilenhLCD(0xc4);
              ghi_chuoi(day[thu-1]);
              caidatthu:while(tang!=0 && giam!=0 && menu!=0);
              if(tang==0)
              { thu++;
              if(thu==8) thu=1;
              ghilenhLCD(0xC4);
              ghi_chuoi(day[thu-1]);
              delay(300);
              goto caidatthu;
              }

              if(giam==0)
              { thu--;
              if(thu==0) thu=7;
              ghilenhLCD(0xC4);
              ghi_chuoi(day[thu-1]);
              delay(300);
              goto caidatthu;
              }

              while(menu==0); // ket thuc cai dat
              ghilenhLCD(1); //thi thuc hien xoa man hinh
              ghilenhLCD(0x0C); //bat hien thi tat con tro
              //CAP NHAT THOI GIAN VAO RTC
              giatrikhoitao_rtc[0] = dec_bcd(giay);
              giatrikhoitao_rtc[1] = dec_bcd(phut);
              giatrikhoitao_rtc[2] = dec_bcd(gio);
              giatrikhoitao_rtc[3] = dec_bcd(thu);
              giatrikhoitao_rtc[4] = dec_bcd(ngay);
              giatrikhoitao_rtc[5] = dec_bcd(thang);
              giatrikhoitao_rtc[6] = dec_bcd(nam);
              giatrikhoitao_rtc[7] = 0x00;
              giatrikhoitao_rtc[8] = 'c'; //ghi vao vi tri dau tien cua ram 1 co (flag)

              ghivaoDS1307();
              ghilenhLCD(1);


              }



              /************************************************** ********************************/
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              // CHUONG TRINH CON KIEM TRA DU LIEU TU DS1307 KHI KHOI DONG //
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              void kiemtra_rtc(void)
              {
              unsigned char temp;
              start_rtc();
              gui_rtc(0xd0);
              gui_rtc(0x08);//doc du lieu tu thanh ghi co dia chi 0x08 (vi tri ram dau tien)
              start_rtc();
              gui_rtc(0xd1);
              temp=nhan_rtc();//con tro dang tro toi dia chi 0x08 chua du lieu flag (co?`) xem da cai dat hay chua
              stop_rtc();
              if(temp!='c') // neu gia tri thu 8 nhan duoc khac voi co 'c' cap nhat khi cai dat gio
              { // thi tuc la lan dau duoc khoi tao, ta se chon gia tri ghi vao ban dau theo y minh
              giatrikhoitao_rtc[0]=0x50;
              giatrikhoitao_rtc[1]=0x59;
              giatrikhoitao_rtc[2]=0x09;
              giatrikhoitao_rtc[3]=0x04;
              giatrikhoitao_rtc[4]=0x04;
              giatrikhoitao_rtc[5]=0x05;
              giatrikhoitao_rtc[6]=0x11;
              giatrikhoitao_rtc[7]=0x00;
              giatrikhoitao_rtc[8]='c';
              ghivaoDS1307();
              }
              }
              ///////////////////////////////////////////////////////////////////////////////
              void doccacdulieu(void){ int i;
              for(i=0;i<=6;i++){docdulieu(i);} }

              //////////////////////////////////////////////////////////////////////////////

              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              // CHUONG TRINH CHINH //
              //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~//
              bit co=0;
              void caidatco() interrupt 0 //ngat ngoai 0 thi co =1
              {
              co=1;
              }
              void main()
              {
              //khoitaoLCD();
              ghilenhLCD(1);
              ///////////////////////////////////////////////////////////////////////////////////////////////
              ghilenhLCD(0x80);
              ghi_chuoi(" DONG HO SO RTC ");
              ghilenhLCD(0xc0);
              ghi_chuoi("NHIET DO DS18B20");
              delay(2000);
              ghilenhLCD(0x80);
              ghi_chuoi("NGUYEN VAN TRINH");
              ghilenhLCD(0xc0);
              ghi_chuoi(" 0973376290 ");
              delay(2000);

              ///////////////////////////////////////////////////////////////////////////////////////////////
              ghilenhLCD(1);
              kiemtra_rtc();
              IE=0x81;// EA_ET2.ES.ET1.EX1.ET0.EX0 => cho phep ngat ngoai 0 ta co 1(0)00 0001 = 81
              doccacdulieu();

              // Hien thi DS18B20
              Init_DS18B20();
              ReadTemp();
              delay(1);
              hien_thi();

              chuyen_doi_DS18B20();//Hien thi nhiet do len LCD

              while(1)
              {
              doccacdulieu();
              hienthi_rtc();
              if(co==1) //co =1 ngat 0 xay ra => cai dat gio
              {
              caidat_rtc();
              co=0;
              }
              // Hien thi DS18B20
              Init_DS18B20();
              ReadTemp();
              delay(1);
              hien_thi();

              chuyen_doi_DS18B20();


              }
              }
              Email:
              Phone: 0969 226 539

              Comment

              Về tác giả

              Collapse

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

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

              Collapse

              Đang tải...
              X