Transistor Gijutsu, October 2014, Special Features Chapter 8,Software of the thermistor thermometer of 0.001 ° resolution, トランジスタ技術2014年10月号 特集第8章のソフトウェア 0.001℃分解能で気配もキャッチ「超敏感肌温度計」

Dependencies:   USBDevice mbed

Information

tg_201410s8_AD7714 トランジスタ技術 2014年 10月号 第8章のソフトウェア

Program for Section 8 in October. 2014 issue of the Transistor Gijutsu
(Japanese electronics magazine)

概要

このプログラムは、サーミスタの抵抗値変化をAD7714(24bitADC)で測定し、抵抗値を温度値に変換することで、0.001℃程度の分解能で温度変化を測定します。

ファイル

このソフトウエアは、次のファイルから構成されています。

  • AD7714.cpp - AD7714の内部レジスタを設定
  • Thermistor.cpp - サーミスタの抵抗値から温度値に変換
  • ExpAvr.cpp - 指数平均によるソフトウエアLPF
  • main.cpp - main()関数

詳細については、10月号の記事および上記ファイル中のコメントを参照してください。

Committer:
Dance
Date:
Fri Aug 29 08:38:36 2014 +0000
Revision:
0:de885a6da962
Transistor Gijutsu, October 2014, Special Features Chapter 8; ????????2014?10??????8????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Dance 0:de885a6da962 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Dance 0:de885a6da962 2 *
Dance 0:de885a6da962 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Dance 0:de885a6da962 4 * and associated documentation files (the "Software"), to deal in the Software without
Dance 0:de885a6da962 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Dance 0:de885a6da962 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Dance 0:de885a6da962 7 * Software is furnished to do so, subject to the following conditions:
Dance 0:de885a6da962 8 *
Dance 0:de885a6da962 9 * The above copyright notice and this permission notice shall be included in all copies or
Dance 0:de885a6da962 10 * substantial portions of the Software.
Dance 0:de885a6da962 11 *
Dance 0:de885a6da962 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Dance 0:de885a6da962 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Dance 0:de885a6da962 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Dance 0:de885a6da962 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Dance 0:de885a6da962 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Dance 0:de885a6da962 17 */
Dance 0:de885a6da962 18
Dance 0:de885a6da962 19 #ifndef USBAudio_H
Dance 0:de885a6da962 20 #define USBAudio_H
Dance 0:de885a6da962 21
Dance 0:de885a6da962 22 /* These headers are included for child class. */
Dance 0:de885a6da962 23 #include "USBEndpoints.h"
Dance 0:de885a6da962 24 #include "USBDescriptor.h"
Dance 0:de885a6da962 25 #include "USBDevice_Types.h"
Dance 0:de885a6da962 26
Dance 0:de885a6da962 27 #include "USBDevice.h"
Dance 0:de885a6da962 28
Dance 0:de885a6da962 29
Dance 0:de885a6da962 30 /**
Dance 0:de885a6da962 31 * USBAudio example
Dance 0:de885a6da962 32 *
Dance 0:de885a6da962 33 * @code
Dance 0:de885a6da962 34 * #include "mbed.h"
Dance 0:de885a6da962 35 * #include "USBAudio.h"
Dance 0:de885a6da962 36 *
Dance 0:de885a6da962 37 * Serial pc(USBTX, USBRX);
Dance 0:de885a6da962 38 *
Dance 0:de885a6da962 39 * // frequency: 48 kHz
Dance 0:de885a6da962 40 * #define FREQ 48000
Dance 0:de885a6da962 41 *
Dance 0:de885a6da962 42 * // 1 channel: mono
Dance 0:de885a6da962 43 * #define NB_CHA 1
Dance 0:de885a6da962 44 *
Dance 0:de885a6da962 45 * // length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1
Dance 0:de885a6da962 46 * #define AUDIO_LENGTH_PACKET 48 * 2 * 1
Dance 0:de885a6da962 47 *
Dance 0:de885a6da962 48 * // USBAudio
Dance 0:de885a6da962 49 * USBAudio audio(FREQ, NB_CHA);
Dance 0:de885a6da962 50 *
Dance 0:de885a6da962 51 * int main() {
Dance 0:de885a6da962 52 * int16_t buf[AUDIO_LENGTH_PACKET/2];
Dance 0:de885a6da962 53 *
Dance 0:de885a6da962 54 * while (1) {
Dance 0:de885a6da962 55 * // read an audio packet
Dance 0:de885a6da962 56 * audio.read((uint8_t *)buf);
Dance 0:de885a6da962 57 *
Dance 0:de885a6da962 58 *
Dance 0:de885a6da962 59 * // print packet received
Dance 0:de885a6da962 60 * pc.printf("recv: ");
Dance 0:de885a6da962 61 * for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
Dance 0:de885a6da962 62 * pc.printf("%d ", buf[i]);
Dance 0:de885a6da962 63 * }
Dance 0:de885a6da962 64 * pc.printf("\r\n");
Dance 0:de885a6da962 65 * }
Dance 0:de885a6da962 66 * }
Dance 0:de885a6da962 67 * @endcode
Dance 0:de885a6da962 68 */
Dance 0:de885a6da962 69 class USBAudio: public USBDevice {
Dance 0:de885a6da962 70 public:
Dance 0:de885a6da962 71
Dance 0:de885a6da962 72 /**
Dance 0:de885a6da962 73 * Constructor
Dance 0:de885a6da962 74 *
Dance 0:de885a6da962 75 * @param frequency_in frequency in Hz (default: 48000)
Dance 0:de885a6da962 76 * @param channel_nb_in channel number (1 or 2) (default: 1)
Dance 0:de885a6da962 77 * @param frequency_out frequency in Hz (default: 8000)
Dance 0:de885a6da962 78 * @param channel_nb_out_in channel number (1 or 2) (default: 1)
Dance 0:de885a6da962 79 * @param vendor_id Your vendor_id
Dance 0:de885a6da962 80 * @param product_id Your product_id
Dance 0:de885a6da962 81 * @param product_release Your preoduct_release
Dance 0:de885a6da962 82 */
Dance 0:de885a6da962 83 USBAudio(uint32_t frequency_in = 48000, uint8_t channel_nb_in = 1, uint32_t frequency_out = 8000, uint8_t channel_nb_out = 1, uint16_t vendor_id = 0x7bb8, uint16_t product_id = 0x1111, uint16_t product_release = 0x0100);
Dance 0:de885a6da962 84
Dance 0:de885a6da962 85 /**
Dance 0:de885a6da962 86 * Get current volume between 0.0 and 1.0
Dance 0:de885a6da962 87 *
Dance 0:de885a6da962 88 * @returns volume
Dance 0:de885a6da962 89 */
Dance 0:de885a6da962 90 float getVolume();
Dance 0:de885a6da962 91
Dance 0:de885a6da962 92 /**
Dance 0:de885a6da962 93 * Read an audio packet. During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Blocking
Dance 0:de885a6da962 94 *
Dance 0:de885a6da962 95 * @param buf pointer on a buffer which will be filled with an audio packet
Dance 0:de885a6da962 96 *
Dance 0:de885a6da962 97 * @returns true if successfull
Dance 0:de885a6da962 98 */
Dance 0:de885a6da962 99 bool read(uint8_t * buf);
Dance 0:de885a6da962 100
Dance 0:de885a6da962 101 /**
Dance 0:de885a6da962 102 * Try to read an audio packet. During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Non Blocking
Dance 0:de885a6da962 103 *
Dance 0:de885a6da962 104 * @param buf pointer on a buffer which will be filled if an audio packet is available
Dance 0:de885a6da962 105 *
Dance 0:de885a6da962 106 * @returns true if successfull
Dance 0:de885a6da962 107 */
Dance 0:de885a6da962 108 bool readNB(uint8_t * buf);
Dance 0:de885a6da962 109
Dance 0:de885a6da962 110 /**
Dance 0:de885a6da962 111 * Write an audio packet. During a frame, only a single writing (you can't write and read an audio packet during the same frame)can be done using this method.
Dance 0:de885a6da962 112 *
Dance 0:de885a6da962 113 * @param buf pointer on the audio packet which will be sent
Dance 0:de885a6da962 114 * @returns true if successful
Dance 0:de885a6da962 115 */
Dance 0:de885a6da962 116 bool write(uint8_t * buf);
Dance 0:de885a6da962 117
Dance 0:de885a6da962 118 /**
Dance 0:de885a6da962 119 * Write and read an audio packet at the same time (on the same frame)
Dance 0:de885a6da962 120 *
Dance 0:de885a6da962 121 * @param buf_read pointer on a buffer which will be filled with an audio packet
Dance 0:de885a6da962 122 * @param buf_write pointer on the audio packet which will be sent
Dance 0:de885a6da962 123 * @returns true if successful
Dance 0:de885a6da962 124 */
Dance 0:de885a6da962 125 bool readWrite(uint8_t * buf_read, uint8_t * buf_write);
Dance 0:de885a6da962 126
Dance 0:de885a6da962 127
Dance 0:de885a6da962 128 /** attach a handler to update the volume
Dance 0:de885a6da962 129 *
Dance 0:de885a6da962 130 * @param function Function to attach
Dance 0:de885a6da962 131 *
Dance 0:de885a6da962 132 */
Dance 0:de885a6da962 133 void attach(void(*fptr)(void)) {
Dance 0:de885a6da962 134 updateVol.attach(fptr);
Dance 0:de885a6da962 135 }
Dance 0:de885a6da962 136
Dance 0:de885a6da962 137 /** Attach a nonstatic void/void member function to update the volume
Dance 0:de885a6da962 138 *
Dance 0:de885a6da962 139 * @param tptr Object pointer
Dance 0:de885a6da962 140 * @param mptr Member function pointer
Dance 0:de885a6da962 141 *
Dance 0:de885a6da962 142 */
Dance 0:de885a6da962 143 template<typename T>
Dance 0:de885a6da962 144 void attach(T *tptr, void(T::*mptr)(void)) {
Dance 0:de885a6da962 145 updateVol.attach(tptr, mptr);
Dance 0:de885a6da962 146 }
Dance 0:de885a6da962 147
Dance 0:de885a6da962 148
Dance 0:de885a6da962 149 protected:
Dance 0:de885a6da962 150
Dance 0:de885a6da962 151 /*
Dance 0:de885a6da962 152 * Called by USBDevice layer. Set configuration of the device.
Dance 0:de885a6da962 153 * For instance, you can add all endpoints that you need on this function.
Dance 0:de885a6da962 154 *
Dance 0:de885a6da962 155 * @param configuration Number of the configuration
Dance 0:de885a6da962 156 * @returns true if class handles this request
Dance 0:de885a6da962 157 */
Dance 0:de885a6da962 158 virtual bool USBCallback_setConfiguration(uint8_t configuration);
Dance 0:de885a6da962 159
Dance 0:de885a6da962 160 /*
Dance 0:de885a6da962 161 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
Dance 0:de885a6da962 162 * This is used to handle extensions to standard requests
Dance 0:de885a6da962 163 * and class specific requests
Dance 0:de885a6da962 164 *
Dance 0:de885a6da962 165 * @returns true if class handles this request
Dance 0:de885a6da962 166 */
Dance 0:de885a6da962 167 virtual bool USBCallback_request();
Dance 0:de885a6da962 168
Dance 0:de885a6da962 169 /*
Dance 0:de885a6da962 170 * Get string product descriptor
Dance 0:de885a6da962 171 *
Dance 0:de885a6da962 172 * @returns pointer to the string product descriptor
Dance 0:de885a6da962 173 */
Dance 0:de885a6da962 174 virtual uint8_t * stringIproductDesc();
Dance 0:de885a6da962 175
Dance 0:de885a6da962 176 /*
Dance 0:de885a6da962 177 * Get string interface descriptor
Dance 0:de885a6da962 178 *
Dance 0:de885a6da962 179 * @returns pointer to the string interface descriptor
Dance 0:de885a6da962 180 */
Dance 0:de885a6da962 181 virtual uint8_t * stringIinterfaceDesc();
Dance 0:de885a6da962 182
Dance 0:de885a6da962 183 /*
Dance 0:de885a6da962 184 * Get configuration descriptor
Dance 0:de885a6da962 185 *
Dance 0:de885a6da962 186 * @returns pointer to the configuration descriptor
Dance 0:de885a6da962 187 */
Dance 0:de885a6da962 188 virtual uint8_t * configurationDesc();
Dance 0:de885a6da962 189
Dance 0:de885a6da962 190 /*
Dance 0:de885a6da962 191 * Called by USBDevice layer. Set interface/alternate of the device.
Dance 0:de885a6da962 192 *
Dance 0:de885a6da962 193 * @param interface Number of the interface to be configured
Dance 0:de885a6da962 194 * @param alternate Number of the alternate to be configured
Dance 0:de885a6da962 195 * @returns true if class handles this request
Dance 0:de885a6da962 196 */
Dance 0:de885a6da962 197 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);
Dance 0:de885a6da962 198
Dance 0:de885a6da962 199 /*
Dance 0:de885a6da962 200 * Called by USBDevice on Endpoint0 request completion
Dance 0:de885a6da962 201 * if the 'notify' flag has been set to true. Warning: Called in ISR context
Dance 0:de885a6da962 202 *
Dance 0:de885a6da962 203 * In this case it is used to indicate that a HID report has
Dance 0:de885a6da962 204 * been received from the host on endpoint 0
Dance 0:de885a6da962 205 *
Dance 0:de885a6da962 206 * @param buf buffer received on endpoint 0
Dance 0:de885a6da962 207 * @param length length of this buffer
Dance 0:de885a6da962 208 */
Dance 0:de885a6da962 209 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
Dance 0:de885a6da962 210
Dance 0:de885a6da962 211 /*
Dance 0:de885a6da962 212 * Callback called on each Start of Frame event
Dance 0:de885a6da962 213 */
Dance 0:de885a6da962 214 virtual void SOF(int frameNumber);
Dance 0:de885a6da962 215
Dance 0:de885a6da962 216 /*
Dance 0:de885a6da962 217 * Callback called when a packet is received
Dance 0:de885a6da962 218 */
Dance 0:de885a6da962 219 virtual bool EP3_OUT_callback();
Dance 0:de885a6da962 220
Dance 0:de885a6da962 221 /*
Dance 0:de885a6da962 222 * Callback called when a packet has been sent
Dance 0:de885a6da962 223 */
Dance 0:de885a6da962 224 virtual bool EP3_IN_callback();
Dance 0:de885a6da962 225
Dance 0:de885a6da962 226 private:
Dance 0:de885a6da962 227
Dance 0:de885a6da962 228 // stream available ?
Dance 0:de885a6da962 229 volatile bool available;
Dance 0:de885a6da962 230
Dance 0:de885a6da962 231 // interrupt OUT has been received
Dance 0:de885a6da962 232 volatile bool interruptOUT;
Dance 0:de885a6da962 233
Dance 0:de885a6da962 234 // interrupt IN has been received
Dance 0:de885a6da962 235 volatile bool interruptIN;
Dance 0:de885a6da962 236
Dance 0:de885a6da962 237 // audio packet has been written
Dance 0:de885a6da962 238 volatile bool writeIN;
Dance 0:de885a6da962 239
Dance 0:de885a6da962 240 // FREQ
Dance 0:de885a6da962 241 uint32_t FREQ_OUT;
Dance 0:de885a6da962 242 uint32_t FREQ_IN;
Dance 0:de885a6da962 243
Dance 0:de885a6da962 244 // size of the maximum packet for the isochronous endpoint
Dance 0:de885a6da962 245 uint32_t PACKET_SIZE_ISO_IN;
Dance 0:de885a6da962 246 uint32_t PACKET_SIZE_ISO_OUT;
Dance 0:de885a6da962 247
Dance 0:de885a6da962 248 // mono, stereo,...
Dance 0:de885a6da962 249 uint8_t channel_nb_in;
Dance 0:de885a6da962 250 uint8_t channel_nb_out;
Dance 0:de885a6da962 251
Dance 0:de885a6da962 252 // channel config: master, left, right
Dance 0:de885a6da962 253 uint8_t channel_config_in;
Dance 0:de885a6da962 254 uint8_t channel_config_out;
Dance 0:de885a6da962 255
Dance 0:de885a6da962 256 // mute state
Dance 0:de885a6da962 257 uint8_t mute;
Dance 0:de885a6da962 258
Dance 0:de885a6da962 259 // Volume Current Value
Dance 0:de885a6da962 260 uint16_t volCur;
Dance 0:de885a6da962 261
Dance 0:de885a6da962 262 // Volume Minimum Value
Dance 0:de885a6da962 263 uint16_t volMin;
Dance 0:de885a6da962 264
Dance 0:de885a6da962 265 // Volume Maximum Value
Dance 0:de885a6da962 266 uint16_t volMax;
Dance 0:de885a6da962 267
Dance 0:de885a6da962 268 // Volume Resolution
Dance 0:de885a6da962 269 uint16_t volRes;
Dance 0:de885a6da962 270
Dance 0:de885a6da962 271 // Buffer containing one audio packet (to be read)
Dance 0:de885a6da962 272 volatile uint8_t * buf_stream_in;
Dance 0:de885a6da962 273
Dance 0:de885a6da962 274 // Buffer containing one audio packet (to be written)
Dance 0:de885a6da962 275 volatile uint8_t * buf_stream_out;
Dance 0:de885a6da962 276
Dance 0:de885a6da962 277 // callback to update volume
Dance 0:de885a6da962 278 FunctionPointer updateVol;
Dance 0:de885a6da962 279
Dance 0:de885a6da962 280 // boolean showing that the SOF handler has been called. Useful for readNB.
Dance 0:de885a6da962 281 volatile bool SOF_handler;
Dance 0:de885a6da962 282
Dance 0:de885a6da962 283 volatile float volume;
Dance 0:de885a6da962 284
Dance 0:de885a6da962 285 };
Dance 0:de885a6da962 286
Dance 0:de885a6da962 287 #endif