Ниже приведенный код делает следующее: берет температуру с каждого из датчиков ds18B20, выводит ее на LCD экран и печатает инфу в UART.
Мог маленько накосячить, ибо из сурсов выкидывал еще обработку сервы.
Стоит отметить, что на arduino.cc есть несколько библиотек для однопроводной связи и не все из них работают с большим количеством параллельно подключенных датчиков.
lcd4bit же использован потому, что штатная библиотека по-моему не умеет работать с более чем однострочными экранами.
Код:#include <LCD4Bit.h> #include <OneWire.h> #include "WProgram.h" void setup(void); void loop(void); LCD4Bit lcd = LCD4Bit(2); // две строки. Пины описаны в LCD4Bit.cpp. OneWire ds(3); // on pin 3 void setup(void) { pinMode(13, OUTPUT); //we'll use the debug LED to output a heartbeat lcd.init(); Serial.begin(9600); } void loop(void) { byte i; byte present = 0; byte data[12]; byte addr[8]; int Temp; if ( !ds.search(addr)) { //Serial.print("No more addresses.\n"); ds.reset_search(); return; } Serial.print("R="); //R=28 Not sure what this is for( i = 0; i < 8; i++) { Serial.print(addr[i], HEX); Serial.print(" "); } if ( OneWire::crc8( addr, 7) != addr[7]) { Serial.print("CRC is not valid!\n"); return; } if ( addr[0] != 0x28) { Serial.print("Device is not a DS18S20 family device.\n"); return; } ds.reset(); ds.select(addr); ds.write(0x44,1); // start conversion, with parasite power on at the end delay(1000); // maybe 750ms is enough, maybe not // we might do a ds.depower() here, but the reset will take care of it. present = ds.reset(); ds.select(addr); ds.write(0xBE); // Read Scratchpad Serial.print("P="); Serial.print(present,HEX); Serial.print(" "); for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = ds.read(); Serial.print(data[i], HEX); Serial.print(" "); } Temp=(data[1]<<8)+data[0];//take the two bytes from the response relating to temperature Temp=Temp;//divide by 16 to get pure celcius readout Serial.print("T=");//output the temperature to serial port Serial.print(Temp/16); Serial.print("."); Serial.print(((Temp%16)*100)/16); Serial.print(" "); Serial.print(" CRC="); Serial.print( OneWire::crc8( data, 8), HEX); Serial.println(); lcd.clear(); for ( i = 0; i < 8; i++) { if (addr[i]<16) { lcd.print("0"); } lcd.print(addr[i],HEX); } lcd.cursorTo(2, 0); //line=2, x=0. lcd.print("Temp="); lcd.print(Temp/16,DEC); lcd.print("."); lcd.print(((Temp%16)*100)/16,DEC); lcd.print("S "); }




 
			
			 
					
						 
					
						 Re: Цифровой датчик температуры на Carduino.
 Re: Цифровой датчик температуры на Carduino.
				 
					
					
					
						 Ответить с цитированием
  Ответить с цитированием 
			