public class Program { static OutputPort owPort = new OutputPort(Pins.GPIO_PIN_D0, false); static OneWire DS1821 = new OneWire(owPort); public static void Main() { lcdDisplay.Clear(); while (true) { string tmp = GetTemperature().ToString("F2"); lcdDisplay.SetCursorPosition(0, 0); lcdDisplay.Write(tmp); Thread.Sleep(100); } } public static float GetTemperature() { ushort temp_read = 0; ushort count_remain = 0; ushort count_per_c = 0; ushort configuration_register = 0; ushort whileCount = 0; // One -Shot Convert Temp DS1821.TouchReset(); DS1821.WriteByte(0xEE); // Loop and sleep a bit until it is done converting the Temperature data. do { Thread.Sleep(10); configuration_register = 0; DS1821.TouchReset(); DS1821.WriteByte(0xAC); // Read the configuration Register from the DS1821 configuration_register = ReadBytes(1); whileCount++; } while (((configuration_register & (1 << 7)) == 0) && whileCount < 100); // If Bit #8 is 1 then we are finished converting the temp if (whileCount >= 100) return -1; // Get Temp DS1821.TouchReset(); DS1821.WriteByte(0xAA); temp_read = ReadBytes(1); ; // Get Count Remaining DS1821.TouchReset(); DS1821.WriteByte(0xA0); count_remain = ReadBytes(2); // Load The Counter to populate the slope accumulator DS1821.TouchReset(); DS1821.WriteByte(0x41); // Read Count Per Deg DS1821.TouchReset(); DS1821.WriteByte(0xA0); count_per_c = ReadBytes(2); // If we are reading above the 200 mark then we are below 0 and need to compensate the calculation if (temp_read >= 200) temp_read -= 256; var highResTemp = (float)temp_read - .5 + (((float)count_per_c - (float)count_remain) / (float)count_per_c); return (float)((1.80 * highResTemp) + 32.00); } static ushort ReadBytes(ushort count) { ushort val = 0; for (ushort i = 0; i < count; i++) { val |= (ushort)(DS1821.ReadByte() << i * 8); } return val; } }
- Netduino Forums
- → Viewing Profile: Topics: Thomas Rankin
The Netduino forums have been replaced by new forums at community.wildernesslabs.co.
This site has been preserved for archival purposes only
and the ability to make new accounts or posts has been turned off.
Community Stats
- Group Members
- Active Posts 26
- Profile Views 8748
- Member Title Member
- Age Age Unknown
- Birthday Birthday Unknown
-
Gender
Not Telling
2
Neutral
User Tools
Friends
Thomas Rankin hasn't added any friends yet.
Topics I've Started
DS1821 temperature sensor over OneWire on Netduino Plus 2
19 December 2012 - 02:35 AM
I've ported my DS1821 code over to use Microsoft.SPOT.Hardware.OneWire. Thought I would share it out in case anyone else had these devices.
- Netduino Forums
- → Viewing Profile: Topics: Thomas Rankin
- Privacy Policy