Thông báo

Collapse
No announcement yet.

Báo cháy sử dụng cảm biến DS18B20 và 8051 hiển thị LCD

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

  • #16
    Nguyên văn bởi Vinhanboy Xem bài viết
    Phù phù... cuối cùng cũng sửa xong được cái code
    Nhưng có một thắc mắc thế này, mong bác nào đã làm con này rồi giúp cho, khi nhiệt độ thay đổi thì hiển thị bên LCD thay đổi chậm hơn nhiều, sao vậy nhỉ ? Mong các bác tư vấn giúp
    cái đó phụ thuộc thời gian lấy mẫu, xem bảng 2 trong datasheet để biết chi tiết

    Comment


    • #17
      Tình hình là đã xong được phần đo nhiệt độ của 4 con DS18b20, làm riêng nên viết code thật dài . Nhưng bây giờ lại có theo một lỗi nữa, mà không biết tại làm sao. Trên LCD mình muốn hiển thị : Dòng 1 là số lượng xe , dòng 2 là nhiệt độ cao nhất . Nhưng mà khi chạy thì dòng 1 nó hiển thị sai ( ví dụ 50 thì nó hiển thị là y35) , trong khi dòng 2 vẫn hiển thị phần nhiệt độ đó được, mình ngắt kết nối với con DS18B20 ( trong hàm main vẫn gọi các lệnh đọc nhiệt độ bình thường ) thì dòng 1 lại hiển thị đúng. Các bác có cao kiến gì cho mình tham khảo với.

      Code đây : Hơi dài, mong các bác thông cảm
      Code:
      #include<stdio.h>
      #include<reg51.h>
      sbit DQ  =  P2^0;
      sbit DQ2 = P2^1;
      sbit DQ3 = P2^2;
      sbit DQ4 = P2^3;
      char  readdata[2];
      unsigned int t1,t2,t3,t4,nhietdo;
      int xe= 50;
      sbit xevao = P3^0;
      sbit xera = P3^1;
      	
      // Khai bao cho LCD	--
      sbit RS = P3^6;
      sbit EN = P3^7; //RW=1 => doc
      				//RS=0 => code
      				//RS=1 => data
      #define lcd_PORT P0
      
      //===========================
      void delay_ms1(int n)
      {
      	int k,j;
      	for(k=0;k<n;k++)
      	{
      		for(j=0;j<500;j++);
      	}
      
      
      }
      //==========================
      void delay_5ms(){
      int i,j;
      for(i=0;i<250;i++)
      for(j=0;j<4;j++){}
      }
      //===========================
      void delay_15ms(){
      int i,j;
      for(i=0;i<250;i++)
      for(j=0;j<100;j++){}
      }
      //============================
      void lcd_command(unsigned char c) //CT con ghi du lieu len LCD
      {
       RS=0;
       lcd_PORT=c;
       EN=1;
       EN=0;
       delay_5ms();
      }
      //==============================
      void lcd_data(unsigned char c) //CT con doc du lieu tu LCD
      {
       RS=1;
       lcd_PORT=c;
       EN=1;
       EN=0;
       delay_5ms();
      }
      
      //===============================
      void lcd_init()  // Khoi tao LCD
      {
      	delay_15ms();
      	lcd_command(0x38);
      	lcd_command(0x0C);
      	lcd_command(0x01);  // Xoa man hinh LCD
      }
      
      //=============================
      void lcd_putsf(unsigned char *s)
      {
       while (*s)
       {
        lcd_data(*s);
        s++;
       }
      }
      
      //------------------DS18b20---------------------------------------------------------------------
      void DelayUs(int us)
      {
      	int i;
      	for (i=0; i<us; i++);
      }
      
      //----------------------------------------
      // Reset DS1820
      //----------------------------------------
      bit ResetDS1820(void)
      {
      	bit presence;
      	DQ = 0; 		//pull DQ line low
      	DelayUs(29); 	// leave it low for about 490us
      	DQ = 1; 		// allow line to return high
      	DelayUs(3); 	// wait for presence 55 uS
      	presence = DQ; 	// get presence signal
      	DelayUs(25); 	// wait for end of timeslot 316 uS 
      	return(presence); // presence signal returned
      } 	// 0=presence, 1 = no part
      
      //-----------------------------------------
      // Read one bit from DS1820
      //-----------------------------------------
      unsigned char ReadBit(void)
      {
      	unsigned char i;
      	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
      }
      
      //-----------------------------------------
      // Write one bit to DS1820
      //-----------------------------------------
      void WriteBit(char bitval)
      {
      	DQ=0;	
      	if(bitval==1)
      		DQ = 1;
      	DelayUs(5); 			// delay about 39 uS
      	DQ = 1;
      }
      
      //-----------------------------------------
      // Read 1 byte from DS1820
      //-----------------------------------------
      unsigned char ReadByte(void)
      {
      	unsigned char i;
      	unsigned char value = 0;
      	for (i=0;i<8;i++)
      	{
      		if(ReadBit()) value|=0x01<<i;
      		DelayUs(6); 
      	}
      	return(value);
      }
      
      //-----------------------------------------
      // Write 1 byte
      //-----------------------------------------
      void WriteByte(char val)
      {
      	unsigned char i;
      	unsigned char temp;
      
      	for (i=0; i<8; i++) // writes byte, one bit at a time
      	{	       
      		temp = val>> i;
      		temp &=0x01;
      		WriteBit(temp);
      	}
      	DelayUs(5);
      }
      
      //-----------------------------------------
      // Read temperature
      //-----------------------------------------
      void ReadTemp(void)
      {
      	int k;
      	int thap, cao=0;
      	ResetDS1820();
          WriteByte(0xcc);  // skip ROM
          WriteByte(0x44);  // perform temperatur conversion
          while (ReadByte()==0xff); // wait for conversion complete	
          ResetDS1820();
          WriteByte(0xcc);  // skip ROM
          WriteByte(0xbe);  // read the result
          for (k=0; k<9; k++)     // read 9 bytes but, use only one byte
          {
             readdata[k]=ReadByte();  // read DS1820
          } 
      	thap = readdata[0];
      	cao = readdata[1];
      	readdata[1]=readdata[1]<<4; 
          readdata[1]=readdata[1] & 0x70; 
          thap=readdata[0]; 
          thap=thap>>4; 
          thap=thap & 0x0f; 
          readdata[1]=readdata[1] | thap; 
          thap=2; 
          t1=readdata[1];
      }
      // ---------------------------- DS18B20 --------> 2 ---------------------
      bit ResetDS18202(void)
      {
      	bit presence;
      	DQ2 = 0; 		//pull DQ line low
      	DelayUs(29); 	// leave it low for about 490us
      	DQ2 = 1; 		// allow line to return high
      	DelayUs(3); 	// wait for presence 55 uS
      	presence = DQ2; 	// get presence signal
      	DelayUs(25); 	// wait for end of timeslot 316 uS 
      	return(presence); // presence signal returned
      } 	// 0=presence, 1 = no part
      
      //-----------------------------------------
      // Read one bit from DS1820
      //-----------------------------------------
      unsigned char ReadBit2(void)
      {
      	unsigned char i;
      	DQ2 = 0; 	// pull DQ low to start timeslot
      	DQ2 = 1;
      	for (i=0; i<3; i++); // delay 17 us from start of timeslot
      	return(DQ2); // return value of DQ line
      }
      
      //-----------------------------------------
      // Write one bit to DS1820
      //-----------------------------------------
      void WriteBit2(char bitval)
      {
      	DQ2=0;	
      	if(bitval==1)
      		DQ2 = 1;
      	DelayUs(5); 			// delay about 39 uS
      	DQ2 = 1;
      }
      
      //-----------------------------------------
      // Read 1 byte from DS1820
      //-----------------------------------------
      unsigned char ReadByte2(void)
      {
      	unsigned char i;
      	unsigned char value = 0;
      	for (i=0;i<8;i++)
      	{
      		if(ReadBit2()) value|=0x01<<i;
      		DelayUs(6); 
      	}
      	return(value);
      }
      
      //-----------------------------------------
      // Write 1 byte
      //-----------------------------------------
      void WriteByte2(char val)
      {
      	unsigned char i;
      	unsigned char temp;
      
      	for (i=0; i<8; i++) // writes byte, one bit at a time
      	{	       
      		temp = val>> i;
      		temp &=0x01;
      		WriteBit2(temp);
      	}
      	DelayUs(5);
      }
      
      //-----------------------------------------
      // Read temperature
      //-----------------------------------------
      void ReadTemp2(void)
      {
      	int k;
      	int thap, cao=0;	
      	ResetDS18202();
          WriteByte2(0xcc);  // skip ROM
          WriteByte2(0x44);  // perform temperatur conversion
          while (ReadByte2()==0xff); // wait for conversion complete	
          ResetDS18202();
          WriteByte2(0xcc);  // skip ROM
          WriteByte2(0xbe);  // read the result
          for (k=0; k<9; k++)     // read 9 bytes but, use only one byte
          {
             readdata[k]=ReadByte2();  // read DS1820
          } 
      	thap = readdata[0];
      	cao = readdata[1];
      	readdata[1]=readdata[1]<<4; 
          readdata[1]=readdata[1] & 0x70; 
          thap=readdata[0]; 
          thap=thap>>4; 
          thap=thap & 0x0f; 
          readdata[1]=readdata[1] | thap; 
          thap=2; 
          t2=readdata[1];
      }
      
      // ---------------------------- DS18B20 --------> 3 ---------------------
      bit ResetDS18203(void)
      {
      	bit presence;
      	DQ3 = 0; 		//pull DQ line low
      	DelayUs(29); 	// leave it low for about 490us
      	DQ3 = 1; 		// allow line to return high
      	DelayUs(3); 	// wait for presence 55 uS
      	presence = DQ3; 	// get presence signal
      	DelayUs(25); 	// wait for end of timeslot 316 uS 
      	return(presence); // presence signal returned
      } 	// 0=presence, 1 = no part
      
      //-----------------------------------------
      // Read one bit from DS1820
      //-----------------------------------------
      unsigned char ReadBit3(void)
      {
      	unsigned char i;
      	DQ3 = 0; 	// pull DQ low to start timeslot
      	DQ3 = 1;
      	for (i=0; i<3; i++); // delay 17 us from start of timeslot
      	return(DQ3); // return value of DQ line
      }
      
      //-----------------------------------------
      // Write one bit to DS1820
      //-----------------------------------------
      void WriteBit3(char bitval)
      {
      	DQ3=0;	
      	if(bitval==1)
      		DQ3 = 1;
      	DelayUs(5); 			// delay about 39 uS
      	DQ3 = 1;
      }
      
      //-----------------------------------------
      // Read 1 byte from DS1820
      //-----------------------------------------
      unsigned char ReadByte3(void)
      {
      	unsigned char i;
      	unsigned char value = 0;
      	for (i=0;i<8;i++)
      	{
      		if(ReadBit3()) value|=0x01<<i;
      		DelayUs(6); 
      	}
      	return(value);
      }
      
      //-----------------------------------------
      // Write 1 byte
      //-----------------------------------------
      void WriteByte3(char val)
      {
      	unsigned char i;
      	unsigned char temp;
      
      	for (i=0; i<8; i++) // writes byte, one bit at a time
      	{	       
      		temp = val>> i;
      		temp &=0x01;
      		WriteBit3(temp);
      	}
      	DelayUs(5);
      }
      
      //-----------------------------------------
      // Read temperature
      //-----------------------------------------
      void ReadTemp3(void)
      {
      	int k;
      	int thap, cao=0;	
      	ResetDS18203();
          WriteByte3(0xcc);  // skip ROM
          WriteByte3(0x44);  // perform temperatur conversion
          while (ReadByte3()==0xff); // wait for conversion complete	
          ResetDS18203();
          WriteByte3(0xcc);  // skip ROM
          WriteByte3(0xbe);  // read the result
          for (k=0; k<9; k++)     // read 9 bytes but, use only one byte
          {
             readdata[k]=ReadByte3();  // read DS1820
          } 
      	thap = readdata[0];
      	cao = readdata[1];
      	readdata[1]=readdata[1]<<4; 
          readdata[1]=readdata[1] & 0x70; 
          thap=readdata[0]; 
          thap=thap>>4; 
          thap=thap & 0x0f; 
          readdata[1]=readdata[1] | thap; 
          thap=2; 
          t3=readdata[1];
      }
      // ---------------------------- DS18B20 --------> 4 ---------------------
      bit ResetDS18204(void)
      {
      	bit presence;
      	DQ4 = 0; 		//pull DQ line low
      	DelayUs(29); 	// leave it low for about 490us
      	DQ4 = 1; 		// allow line to return high
      	DelayUs(3); 	// wait for presence 55 uS
      	presence = DQ4; 	// get presence signal
      	DelayUs(25); 	// wait for end of timeslot 316 uS 
      	return(presence); // presence signal returned
      } 	// 0=presence, 1 = no part
      
      //-----------------------------------------
      // Read one bit from DS1820
      //-----------------------------------------
      unsigned char ReadBit4(void)
      {
      	unsigned char i;
      	DQ4 = 0; 	// pull DQ low to start timeslot
      	DQ4 = 1;
      	for (i=0; i<3; i++); // delay 17 us from start of timeslot
      	return(DQ4); // return value of DQ line
      }
      
      //-----------------------------------------
      // Write one bit to DS1820
      //-----------------------------------------
      void WriteBit4(char bitval)
      {
      	DQ4=0;	
      	if(bitval==1)
      		DQ4 = 1;
      	DelayUs(5); 			// delay about 39 uS
      	DQ4 = 1;
      }
      
      //-----------------------------------------
      // Read 1 byte from DS1820
      //-----------------------------------------
      unsigned char ReadByte4(void)
      {
      	unsigned char i;
      	unsigned char value = 0;
      	for (i=0;i<8;i++)
      	{
      		if(ReadBit4()) value|=0x01<<i;
      		DelayUs(6); 
      	}
      	return(value);
      }
      
      //-----------------------------------------
      // Write 1 byte
      //-----------------------------------------
      void WriteByte4(char val)
      {
      	unsigned char i;
      	unsigned char temp;
      
      	for (i=0; i<8; i++) // writes byte, one bit at a time
      	{	       
      		temp = val>> i;
      		temp &=0x01;
      		WriteBit4(temp);
      	}
      	DelayUs(5);
      }
      
      //-----------------------------------------
      // Read temperature
      //-----------------------------------------
      void ReadTemp4(void)
      {
      	int k;
      	int thap, cao=0;	
      	ResetDS18204();
          WriteByte4(0xcc);  // skip ROM
          WriteByte4(0x44);  // perform temperatur conversion
          while (ReadByte4()==0xff); // wait for conversion complete	
          ResetDS18204();
          WriteByte4(0xcc);  // skip ROM
          WriteByte4(0xbe);  // read the result
          for (k=0; k<9; k++)     // read 9 bytes but, use only one byte
          {
             readdata[k]=ReadByte4();  // read DS1820
          } 
      	thap = readdata[0];
      	cao = readdata[1];
      	readdata[1]=readdata[1]<<4; 
          readdata[1]=readdata[1] & 0x70; 
          thap=readdata[0]; 
          thap=thap>>4; 
          thap=thap & 0x0f; 
          readdata[1]=readdata[1] | thap; 
          thap=2; 
          t4=readdata[1];
      }
      
      //-------------- Ham hien thi nhiet do
      void HienThi_ADC(unsigned char so)
      {
      	
      	if ( so<100)
      	{
      	//lcd_data(t/100+48);
      	lcd_data(((so/10)%10)+48);
      	lcd_data(so%10+48);
      	}
      	else
      	{
      	lcd_data(so/100+48);
      	lcd_data(((so/10)%10)+48);
      	lcd_data(so%10+48);
      	}
      }
      void Hienthiso(unsigned int count)
      {
      	
       	lcd_data(count/100+48);
      	lcd_data(((count/10)%10)+48);
      	lcd_data(count%10+48);
      }
      //--------------------------------------------
      void main (void)
      {
         	lcd_init();
      
      	IE=0XAF;
      	IT0=1;
      	lcd_command(0x01);
          lcd_command(0x80);
      	lcd_putsf("Welcome");
      	lcd_command(0xC0);
      	lcd_putsf("Bai giu xe O");
      	delay_ms1(100);
         	while(1)
      	{
      		      	
      		lcd_command(0x80);
      		lcd_putsf("So luong xe: ");
      		Hienthiso(xe);
      		lcd_command(0xC0);
      		lcd_putsf("Nhiet do: ");
      		ReadTemp();
      		ReadTemp2();
      		ReadTemp3();
      		ReadTemp4();
      		nhietdo = t1;
      		if(nhietdo < t2 )
      			nhietdo = t2;
      		if(nhietdo < t3)
      			nhietdo = t3;
      		if(nhietdo < t4 )
      			nhietdo = t4;
      		HienThi_ADC(nhietdo);
      
      	  	lcd_command(0xc0+14);          
            	lcd_putsf("oC ");	    
      		delay_ms1(10);
      
      	}
      }
      void ngat0(void) interrupt 0
      {
      	EA=0;
      	xe++;
      	//if(x==100)x=0;
      	delay_ms1(100);
      	EA=1;
      }
      void ngat1(void) interrupt 2
      {
      	EA=0;
      	xe--;
      	//if(x==100)x=0;
      	delay_ms1(100);
      	EA=1;
      }

      Comment


      • #18
        Code:
        #include<stdio.h>
        #include<reg51.h>
        sbit DQ  =  P2^0;
        sbit DQ2 = P2^1;
        sbit DQ3 = P2^2;
        sbit DQ4 = P2^3;
        char  readdata[2];
        unsigned int t1,t2,t3,t4,nhietdo;
        int xe= 50;
        sbit xevao = P3^0;
        sbit xera = P3^1;
        	
        // Khai bao cho LCD	--
        sbit RS = P3^6;
        sbit EN = P3^7; //RW=1 => doc
        				//RS=0 => code
        				//RS=1 => data
        #define lcd_PORT P0
        
        //===========================
        void delay_ms1(int n)
        {
        	int k,j;
        	for(k=0;k<n;k++)
        	{
        		for(j=0;j<500;j++);
        	}
        
        
        }
        //==========================
        void delay_5ms(){
        int i,j;
        for(i=0;i<250;i++)
        for(j=0;j<4;j++){}
        }
        //===========================
        void delay_15ms(){
        int i,j;
        for(i=0;i<250;i++)
        for(j=0;j<100;j++){}
        }
        //============================
        void lcd_command(unsigned char c) //CT con ghi du lieu len LCD
        {
         RS=0;
         lcd_PORT=c;
         EN=1;
         EN=0;
         delay_5ms();
        }
        //==============================
        void lcd_data(unsigned char c) //CT con doc du lieu tu LCD
        {
         RS=1;
         lcd_PORT=c;
         EN=1;
         EN=0;
         delay_5ms();
        }
        
        //===============================
        void lcd_init()  // Khoi tao LCD
        {
        	delay_15ms();
        	lcd_command(0x38);
        	lcd_command(0x0C);
        	lcd_command(0x01);  // Xoa man hinh LCD
        }
        
        //=============================
        void lcd_putsf(unsigned char *s)
        {
         while (*s)
         {
          lcd_data(*s);
          s++;
         }
        }
        
        //------------------DS18b20---------------------------------------------------------------------
        void DelayUs(int us)
        {
        	int i;
        	for (i=0; i<us; i++);
        }
        
        //----------------------------------------
        // Reset DS1820
        //----------------------------------------
        bit ResetDS1820(void)
        {
        	bit presence;
        	DQ = 0; 		//pull DQ line low
        	DelayUs(29); 	// leave it low for about 490us
        	DQ = 1; 		// allow line to return high
        	DelayUs(3); 	// wait for presence 55 uS
        	presence = DQ; 	// get presence signal
        	DelayUs(25); 	// wait for end of timeslot 316 uS 
        	return(presence); // presence signal returned
        } 	// 0=presence, 1 = no part
        
        //-----------------------------------------
        // Read one bit from DS1820
        //-----------------------------------------
        unsigned char ReadBit(void)
        {
        	unsigned char i;
        	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
        }
        
        //-----------------------------------------
        // Write one bit to DS1820
        //-----------------------------------------
        void WriteBit(char bitval)
        {
        	DQ=0;	
        	if(bitval==1)
        		DQ = 1;
        	DelayUs(5); 			// delay about 39 uS
        	DQ = 1;
        }
        
        //-----------------------------------------
        // Read 1 byte from DS1820
        //-----------------------------------------
        unsigned char ReadByte(void)
        {
        	unsigned char i;
        	unsigned char value = 0;
        	for (i=0;i<8;i++)
        	{
        		if(ReadBit()) value|=0x01<<i;
        		DelayUs(6); 
        	}
        	return(value);
        }
        
        //-----------------------------------------
        // Write 1 byte
        //-----------------------------------------
        void WriteByte(char val)
        {
        	unsigned char i;
        	unsigned char temp;
        
        	for (i=0; i<8; i++) // writes byte, one bit at a time
        	{	       
        		temp = val>> i;
        		temp &=0x01;
        		WriteBit(temp);
        	}
        	DelayUs(5);
        }
        
        //-----------------------------------------
        // Read temperature
        //-----------------------------------------
        void ReadTemp(void)
        {
        	int k;
        	int thap, cao=0;
        	ResetDS1820();
            WriteByte(0xcc);  // skip ROM
            WriteByte(0x44);  // perform temperatur conversion
            while (ReadByte()==0xff); // wait for conversion complete	
            ResetDS1820();
            WriteByte(0xcc);  // skip ROM
            WriteByte(0xbe);  // read the result
            for (k=0; k<9; k++)     // read 9 bytes but, use only one byte
            {
               readdata[k]=ReadByte();  // read DS1820
            } 
        	thap = readdata[0];
        	cao = readdata[1];
        	readdata[1]=readdata[1]<<4; 
            readdata[1]=readdata[1] & 0x70; 
            thap=readdata[0]; 
            thap=thap>>4; 
            thap=thap & 0x0f; 
            readdata[1]=readdata[1] | thap; 
            thap=2; 
            t1=readdata[1];
        }
        // ---------------------------- DS18B20 --------> 2 ---------------------
        bit ResetDS18202(void)
        {
        	bit presence;
        	DQ2 = 0; 		//pull DQ line low
        	DelayUs(29); 	// leave it low for about 490us
        	DQ2 = 1; 		// allow line to return high
        	DelayUs(3); 	// wait for presence 55 uS
        	presence = DQ2; 	// get presence signal
        	DelayUs(25); 	// wait for end of timeslot 316 uS 
        	return(presence); // presence signal returned
        } 	// 0=presence, 1 = no part
        
        //-----------------------------------------
        // Read one bit from DS1820
        //-----------------------------------------
        unsigned char ReadBit2(void)
        {
        	unsigned char i;
        	DQ2 = 0; 	// pull DQ low to start timeslot
        	DQ2 = 1;
        	for (i=0; i<3; i++); // delay 17 us from start of timeslot
        	return(DQ2); // return value of DQ line
        }
        
        //-----------------------------------------
        // Write one bit to DS1820
        //-----------------------------------------
        void WriteBit2(char bitval)
        {
        	DQ2=0;	
        	if(bitval==1)
        		DQ2 = 1;
        	DelayUs(5); 			// delay about 39 uS
        	DQ2 = 1;
        }
        
        //-----------------------------------------
        // Read 1 byte from DS1820
        //-----------------------------------------
        unsigned char ReadByte2(void)
        {
        	unsigned char i;
        	unsigned char value = 0;
        	for (i=0;i<8;i++)
        	{
        		if(ReadBit2()) value|=0x01<<i;
        		DelayUs(6); 
        	}
        	return(value);
        }
        
        //-----------------------------------------
        // Write 1 byte
        //-----------------------------------------
        void WriteByte2(char val)
        {
        	unsigned char i;
        	unsigned char temp;
        
        	for (i=0; i<8; i++) // writes byte, one bit at a time
        	{	       
        		temp = val>> i;
        		temp &=0x01;
        		WriteBit2(temp);
        	}
        	DelayUs(5);
        }
        
        //-----------------------------------------
        // Read temperature
        //-----------------------------------------
        void ReadTemp2(void)
        {
        	int k;
        	int thap, cao=0;	
        	ResetDS18202();
            WriteByte2(0xcc);  // skip ROM
            WriteByte2(0x44);  // perform temperatur conversion
            while (ReadByte2()==0xff); // wait for conversion complete	
            ResetDS18202();
            WriteByte2(0xcc);  // skip ROM
            WriteByte2(0xbe);  // read the result
            for (k=0; k<9; k++)     // read 9 bytes but, use only one byte
            {
               readdata[k]=ReadByte2();  // read DS1820
            } 
        	thap = readdata[0];
        	cao = readdata[1];
        	readdata[1]=readdata[1]<<4; 
            readdata[1]=readdata[1] & 0x70; 
            thap=readdata[0]; 
            thap=thap>>4; 
            thap=thap & 0x0f; 
            readdata[1]=readdata[1] | thap; 
            thap=2; 
            t2=readdata[1];
        }
        
        // ---------------------------- DS18B20 --------> 3 ---------------------
        bit ResetDS18203(void)
        {
        	bit presence;
        	DQ3 = 0; 		//pull DQ line low
        	DelayUs(29); 	// leave it low for about 490us
        	DQ3 = 1; 		// allow line to return high
        	DelayUs(3); 	// wait for presence 55 uS
        	presence = DQ3; 	// get presence signal
        	DelayUs(25); 	// wait for end of timeslot 316 uS 
        	return(presence); // presence signal returned
        } 	// 0=presence, 1 = no part
        
        //-----------------------------------------
        // Read one bit from DS1820
        //-----------------------------------------
        unsigned char ReadBit3(void)
        {
        	unsigned char i;
        	DQ3 = 0; 	// pull DQ low to start timeslot
        	DQ3 = 1;
        	for (i=0; i<3; i++); // delay 17 us from start of timeslot
        	return(DQ3); // return value of DQ line
        }
        
        //-----------------------------------------
        // Write one bit to DS1820
        //-----------------------------------------
        void WriteBit3(char bitval)
        {
        	DQ3=0;	
        	if(bitval==1)
        		DQ3 = 1;
        	DelayUs(5); 			// delay about 39 uS
        	DQ3 = 1;
        }
        
        //-----------------------------------------
        // Read 1 byte from DS1820
        //-----------------------------------------
        unsigned char ReadByte3(void)
        {
        	unsigned char i;
        	unsigned char value = 0;
        	for (i=0;i<8;i++)
        	{
        		if(ReadBit3()) value|=0x01<<i;
        		DelayUs(6); 
        	}
        	return(value);
        }
        
        //-----------------------------------------
        // Write 1 byte
        //-----------------------------------------
        void WriteByte3(char val)
        {
        	unsigned char i;
        	unsigned char temp;
        
        	for (i=0; i<8; i++) // writes byte, one bit at a time
        	{	       
        		temp = val>> i;
        		temp &=0x01;
        		WriteBit3(temp);
        	}
        	DelayUs(5);
        }
        
        //-----------------------------------------
        // Read temperature
        //-----------------------------------------
        void ReadTemp3(void)
        {
        	int k;
        	int thap, cao=0;	
        	ResetDS18203();
            WriteByte3(0xcc);  // skip ROM
            WriteByte3(0x44);  // perform temperatur conversion
            while (ReadByte3()==0xff); // wait for conversion complete	
            ResetDS18203();
            WriteByte3(0xcc);  // skip ROM
            WriteByte3(0xbe);  // read the result
            for (k=0; k<9; k++)     // read 9 bytes but, use only one byte
            {
               readdata[k]=ReadByte3();  // read DS1820
            } 
        	thap = readdata[0];
        	cao = readdata[1];
        	readdata[1]=readdata[1]<<4; 
            readdata[1]=readdata[1] & 0x70; 
            thap=readdata[0]; 
            thap=thap>>4; 
            thap=thap & 0x0f; 
            readdata[1]=readdata[1] | thap; 
            thap=2; 
            t3=readdata[1];
        }
        // ---------------------------- DS18B20 --------> 4 ---------------------
        bit ResetDS18204(void)
        {
        	bit presence;
        	DQ4 = 0; 		//pull DQ line low
        	DelayUs(29); 	// leave it low for about 490us
        	DQ4 = 1; 		// allow line to return high
        	DelayUs(3); 	// wait for presence 55 uS
        	presence = DQ4; 	// get presence signal
        	DelayUs(25); 	// wait for end of timeslot 316 uS 
        	return(presence); // presence signal returned
        } 	// 0=presence, 1 = no part
        
        //-----------------------------------------
        // Read one bit from DS1820
        //-----------------------------------------
        unsigned char ReadBit4(void)
        {
        	unsigned char i;
        	DQ4 = 0; 	// pull DQ low to start timeslot
        	DQ4 = 1;
        	for (i=0; i<3; i++); // delay 17 us from start of timeslot
        	return(DQ4); // return value of DQ line
        }
        
        //-----------------------------------------
        // Write one bit to DS1820
        //-----------------------------------------
        void WriteBit4(char bitval)
        {
        	DQ4=0;	
        	if(bitval==1)
        		DQ4 = 1;
        	DelayUs(5); 			// delay about 39 uS
        	DQ4 = 1;
        }
        
        //-----------------------------------------
        // Read 1 byte from DS1820
        //-----------------------------------------
        unsigned char ReadByte4(void)
        {
        	unsigned char i;
        	unsigned char value = 0;
        	for (i=0;i<8;i++)
        	{
        		if(ReadBit4()) value|=0x01<<i;
        		DelayUs(6); 
        	}
        	return(value);
        }
        
        //-----------------------------------------
        // Write 1 byte
        //-----------------------------------------
        void WriteByte4(char val)
        {
        	unsigned char i;
        	unsigned char temp;
        
        	for (i=0; i<8; i++) // writes byte, one bit at a time
        	{	       
        		temp = val>> i;
        		temp &=0x01;
        		WriteBit4(temp);
        	}
        	DelayUs(5);
        }
        
        //-----------------------------------------
        // Read temperature
        //-----------------------------------------
        void ReadTemp4(void)
        {
        	int k;
        	int thap, cao=0;	
        	ResetDS18204();
            WriteByte4(0xcc);  // skip ROM
            WriteByte4(0x44);  // perform temperatur conversion
            while (ReadByte4()==0xff); // wait for conversion complete	
            ResetDS18204();
            WriteByte4(0xcc);  // skip ROM
            WriteByte4(0xbe);  // read the result
            for (k=0; k<9; k++)     // read 9 bytes but, use only one byte
            {
               readdata[k]=ReadByte4();  // read DS1820
            } 
        	thap = readdata[0];
        	cao = readdata[1];
        	readdata[1]=readdata[1]<<4; 
            readdata[1]=readdata[1] & 0x70; 
            thap=readdata[0]; 
            thap=thap>>4; 
            thap=thap & 0x0f; 
            readdata[1]=readdata[1] | thap; 
            thap=2; 
            t4=readdata[1];
        }
        
        //-------------- Ham hien thi nhiet do
        void HienThi_ADC(unsigned char so)
        {
        	
        	if ( so<100)
        	{
        	//lcd_data(t/100+48);
        	lcd_data(((so/10)%10)+48);
        	lcd_data(so%10+48);
        	}
        	else
        	{
        	lcd_data(so/100+48);
        	lcd_data(((so/10)%10)+48);
        	lcd_data(so%10+48);
        	}
        }
        void Hienthiso(unsigned int count)
        {
        	
         	lcd_data(count/100+48);
        	lcd_data(((count/10)%10)+48);
        	lcd_data(count%10+48);
        }
        //--------------------------------------------
        void main (void)
        {
           	lcd_init();
        
        	IE=0XAF;
        	IT0=1;
        	lcd_command(0x01);
            lcd_command(0x80);
        	lcd_putsf("Welcome");
        	lcd_command(0xC0);
        	lcd_putsf("Bai giu xe O");
        	delay_ms1(100);
           	while(1)
        	{
        		      	
        		lcd_command(0x80);
        		lcd_putsf("So luong xe: ");
        		Hienthiso(xe);
        		lcd_command(0xC0);
        		lcd_putsf("Nhiet do: ");
        		ReadTemp();
        		ReadTemp2();
        		ReadTemp3();
        		ReadTemp4();
        		nhietdo = t1;
        		if(nhietdo < t2 )
        			nhietdo = t2;
        		if(nhietdo < t3)
        			nhietdo = t3;
        		if(nhietdo < t4 )
        			nhietdo = t4;
        		HienThi_ADC(nhietdo);
        
        	  	lcd_command(0xc0+14);          
              	lcd_putsf("oC ");	    
        		delay_ms1(10);
        
        	}
        }
        void ngat0(void) interrupt 0
        {
        	EA=0;
        	xe++;
        	//if(x==100)x=0;
        	delay_ms1(100);
        	EA=1;
        }
        void ngat1(void) interrupt 2
        {
        	EA=0;
        	xe--;
        	//if(x==100)x=0;
        	delay_ms1(100);
        	EA=1;
        }
        Mình đã chạy mô phỏng code này trên Protues thành công, nhưng không hiểu sao khi nạp vào VĐK lại chạy bị lỗi. Không phải ko chạy được, nhưng nó chỉ chạy được 1 phần thôi. Rồi đứng luôn... cái này lỗi do VĐK phải ko? Nhưng con VĐK này mình nạp ct khác thì vẫn chạy bt ) Các cao thủ giúp với.

        Comment


        • #19
          đồ án ngon chưa cậu,cho mình xin file về hc tập đc không

          Comment

          Về tác giả

          Collapse

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

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

          Collapse

          • vi van pham
            Trả lời cho Tiếng Anh cho người Việt
            bởi vi van pham
            Cám ơn những lời chúc tốt đẹp của em.

            Việc em làm giống như chê sếp dốt, chẳng những không có miếng xôi nào để ăn mà còn chịu nhiều trù dập lên bờ , xuống ruộng.

            Hãy tránh vết xe đổ của tui đi. Dành thời gian lo cho gia đình....
            hôm nay, 02:00
          • nhathung1101
            Trả lời cho Tiếng Anh cho người Việt
            bởi nhathung1101
            Ngoại ngữ là không thể thiếu, nhất là làm việc với công nghệ.

            Nhưng học để tán gái hay để đọc datasheet, manual là chuyện cần quan tâm.

            Và đọc ở nguồn nào đáng tin cậy, chứ cứ lên tictok hay facebook học lỏm thì...
            Hôm qua, 22:18
          • dinhthuong80
            Trả lời cho Tiếng Anh cho người Việt
            bởi dinhthuong80
            Bác nói rất khách quan và chính xác. Ngoại ngữ là chìa khóa thăng tiến dù làm gì. Chả thế mà nay nước mình dạy tiếng Anh từ tiểu học.

            Nhớ hồi năm 2006 em ra Hải Phòng, Hải Dương tìm việc, thời đó ở Bình Dương lương công...
            Hôm qua, 14:51
          • dinhthuong80
            Trả lời cho Tiếng Anh cho người Việt
            bởi dinhthuong80
            Cảm ơn bạn, ĐT cũng có ý nghĩ như vậy.

            Thực ra, lỗi của hãng đó ( gọi là hãng vì không chỉ một model sản phẩm) là về phần cứng, ĐT tuy không biết về lập trình phần mềm nhưng cũng thấy rằng sẽ chẳng khó khăn gì đáng...
            Hôm qua, 14:39
          • dinhthuong80
            Trả lời cho Tiếng Anh cho người Việt
            bởi dinhthuong80
            Dạ, bác bảy mấy tuổi đời rồi cũng không kém bố cháu bao nhiêu. Tuổi nghề thì bác cũng đáng tuổi cha chú.

            Nhưng 2 điều trên thì chưa hẳn đã đáng quí và đáng tôn trọng bằng việc bác rất nhiệt tình chia sẻ kinh nghiệm chuyên...
            Hôm qua, 14:28
          • bqviet
            Trả lời cho Tiếng Anh cho người Việt
            bởi bqviet
            Đúng, nếu chú tâm thì chỉ cần mỗi tiếng Việt là đã khá đủ để làm đa số công việc thông thường, ở thời đại ngày nay khi tài liệu sách vở phương tiện thông tin liên lạc đã nhiều. Nhưng cũng chính ở thời nay giao lưu các nước nhiều...
            21-02-2025, 20:26
          • mèomướp
            Trả lời cho Tiếng Anh cho người Việt
            bởi mèomướp
            Dạ cháu nghĩ chú dinh... cứ mạnh dạn gửi thư đi ạ, chú có thể gửi bằng văn bản in chuyển phát nhanh sẽ có giá trị hơn. Vấn đề chưa hẳn là cần hãng làm gì đó, mà chỉ đơn giản là mình cảm thấy nhẹ lòng vì đã làm những việc bản...
            21-02-2025, 12:32
          • vi van pham
            Trả lời cho Tiếng Anh cho người Việt
            bởi vi van pham
            Những lần hắt hơi sổ mũi làm tôi mệt lã, phải dùng rượu uống để ngũ. Tôi cũng đang uống rượu 1 mình, viết vài dòng này cho em ( có lẽ dt chỉ bằng tuổi em tôi).

            Dinh thuong dang đi vào vết xe đổ của tôi. Càng chứng minh, có...
            20-02-2025, 21:31
          • bqviet
            Trả lời cho Hỏi về cách hàn linh kiện ( giúp tớ với )
            bởi bqviet
            Thiết nghĩ thi thoảng bác lên đây chia xẻ ít kinh nghiệm cũng vui rồi. Còn chuyện con người sinh lão bệnh tử là thường, sống cùng với quy luật đó thôi. Bqv nhớ trước đây bác từng kể về chuyện rang chì ô-xít bằng chảo để phục hồi bình điện, đấy cũng là thành quả đáng nể phục ở thời kỳ thiếu thốn đó.
            20-02-2025, 17:22
          • dinhthuong80
            Trả lời cho Tiếng Anh cho người Việt
            bởi dinhthuong80
            Tiếng Anh, Đình Thường phải nói là rất tệ, khiến việc giao tiếp đời thường thôi cũng khó chứ nói gì về kĩ thuật.

            Nhân tiện, nhờ các bạn, các bác xem giúp thư ĐT viết thế này liệu có thể gửi tới địa chỉ nhận hay chăng,...
            20-02-2025, 11:52
          Đang tải...
          X