This code example demonstrates how to take a reading from a one wire temperature sensor connected to a GHI Electronics FEZ board.
This code is written in C# and assumes the development workstation has all required software installed.
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
public class Program
{
public static void Main()
{
Cpu.Pin myPin = (Cpu.Pin)FEZ_Pin.Digital.Di4;
OneWire ow = new OneWire(myPin);
ushort temperature;
while (true)
{
if (ow.Reset())
{
ow.WriteByte(0xCC); // Skip ROM, we only have one device
ow.WriteByte(0x44); // Start temperature conversion
while (ow.ReadByte() == 0) ; // wait while busy
ow.Reset();
ow.WriteByte(0xCC); // skip ROM
ow.WriteByte(0xBE); // Read Scratchpad
temperature = ow.ReadByte(); // LSB
temperature |= (ushort)(ow.ReadByte() << 8); // MSB
Debug.Print("Temperature: " + temperature / 16);
Thread.Sleep(1000);
}
else
{
Debug.Print("Device is not detected.");
}
Thread.Sleep(1000);
}
}
}
For more information on specific namespaces please refer to the following resources: