■ ADT7410 디지털 온도 센서를 사용하는 방법을 보여준다.
▶ 부품 내역
1 2 3 4 5 6 7 |
──────────────────── 구분 모델 수량 비고 ───────── ──── ── ── SENSOR/TEMPERATURE ADT7410 1 ──────────────────── |
[회로 구성도]
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<Page x:Class="TestApplication.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestApplication" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <TextBlock x:Name="textBlock" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="48" TextWrapping="Wrap" Text="TextBlock" /> </Grid> </Page> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
using System; using System.Diagnostics; using System.Threading; using Windows.Devices.Enumeration; using Windows.Devices.I2c; using Windows.Foundation; using Windows.UI.Xaml.Controls; namespace TestApplication { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// I2C 장치 /// </summary> private I2cDevice i2cDevice; /// <summary> /// 타이머 /// </summary> private Timer timer; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); InitializeADT7410(); Unloaded += Page_Unloaded; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 페이지 언로드시 처리하기 - Page_Unloaded(sender, e) /// <summary> /// 페이지 언로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Page_Unloaded(object sender, object e) { if(this.i2cDevice != null) { this.i2cDevice.Dispose(); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 온도 계산하기 - CalculateTemperature(highValue, lowValue) /// <summary> /// 온도 계산하기 /// </summary> /// <param name="highValue">상위 값</param> /// <param name="lowValue">하위 값</param> /// <returns>온도</returns> private static double CalculateTemperature(int highValue, int lowValue) { int value = (highValue << 5) + (lowValue >> 3); if(value > 4096) // 온도가 영하인 경우 { value = -1 * (8192 - value); } double temperature = value * 0.0625; return temperature; } #endregion #region 타이머 콜백 처리하기 - TimerCallback(state) /// <summary> /// 타이머 콜백 처리하기 /// </summary> /// <param name="state">상태</param> private void TimerCallback(object state) { string temperatureString; byte[] byteArray = new byte[2]; try { this.i2cDevice.Read(byteArray); } catch(Exception exception) { Debug.WriteLine("예외 : " + exception.Message); } int highValue = ((int)byteArray[0]); int lowValue = ((int)byteArray[1]); double temperature = CalculateTemperature(highValue, lowValue); Debug.WriteLine(temperature); temperatureString = String.Format("온도 : {0:F4} ℃", temperature); IAsyncAction task = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { this.textBlock.Text = temperatureString; }); } #endregion #region ADT7410 초기화하기 - InitializeADT7410() /// <summary> /// ADT7410 초기화하기 /// </summary> private async void InitializeADT7410() { try { string advancedQuerySyntax = I2cDevice.GetDeviceSelector(); DeviceInformationCollection deviceInformationCollection = await DeviceInformation.FindAllAsync(advancedQuerySyntax); I2cConnectionSettings i2cConnectionSettings = new I2cConnectionSettings(0x48); i2cConnectionSettings.BusSpeed = I2cBusSpeed.FastMode; this.i2cDevice = await I2cDevice.FromIdAsync(deviceInformationCollection[0].Id, i2cConnectionSettings); } catch(Exception exception) { Debug.WriteLine("예외 : " + exception.Message); } this.timer = new Timer(TimerCallback, null, 0, 1000); } #endregion } } |