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 #include "stdint.h"
Dance 0:de885a6da962 20 #include "USBHAL.h"
Dance 0:de885a6da962 21 #include "USBHID.h"
Dance 0:de885a6da962 22
Dance 0:de885a6da962 23
Dance 0:de885a6da962 24 USBHID::USBHID(uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect): USBDevice(vendor_id, product_id, product_release)
Dance 0:de885a6da962 25 {
Dance 0:de885a6da962 26 output_length = output_report_length;
Dance 0:de885a6da962 27 input_length = input_report_length;
Dance 0:de885a6da962 28 if(connect) {
Dance 0:de885a6da962 29 USBDevice::connect();
Dance 0:de885a6da962 30 }
Dance 0:de885a6da962 31 }
Dance 0:de885a6da962 32
Dance 0:de885a6da962 33
Dance 0:de885a6da962 34 bool USBHID::send(HID_REPORT *report)
Dance 0:de885a6da962 35 {
Dance 0:de885a6da962 36 return write(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
Dance 0:de885a6da962 37 }
Dance 0:de885a6da962 38
Dance 0:de885a6da962 39 bool USBHID::sendNB(HID_REPORT *report)
Dance 0:de885a6da962 40 {
Dance 0:de885a6da962 41 return writeNB(EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE);
Dance 0:de885a6da962 42 }
Dance 0:de885a6da962 43
Dance 0:de885a6da962 44
Dance 0:de885a6da962 45 bool USBHID::read(HID_REPORT *report)
Dance 0:de885a6da962 46 {
Dance 0:de885a6da962 47 uint32_t bytesRead = 0;
Dance 0:de885a6da962 48 bool result;
Dance 0:de885a6da962 49 result = USBDevice::readEP(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
Dance 0:de885a6da962 50 if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
Dance 0:de885a6da962 51 return false;
Dance 0:de885a6da962 52 report->length = bytesRead;
Dance 0:de885a6da962 53 return result;
Dance 0:de885a6da962 54 }
Dance 0:de885a6da962 55
Dance 0:de885a6da962 56
Dance 0:de885a6da962 57 bool USBHID::readNB(HID_REPORT *report)
Dance 0:de885a6da962 58 {
Dance 0:de885a6da962 59 uint32_t bytesRead = 0;
Dance 0:de885a6da962 60 bool result;
Dance 0:de885a6da962 61 result = USBDevice::readEP_NB(EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE);
Dance 0:de885a6da962 62 // if readEP_NB did not succeed, does not issue a readStart
Dance 0:de885a6da962 63 if (!result)
Dance 0:de885a6da962 64 return false;
Dance 0:de885a6da962 65 report->length = bytesRead;
Dance 0:de885a6da962 66 if(!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
Dance 0:de885a6da962 67 return false;
Dance 0:de885a6da962 68 return result;
Dance 0:de885a6da962 69 }
Dance 0:de885a6da962 70
Dance 0:de885a6da962 71
Dance 0:de885a6da962 72 uint16_t USBHID::reportDescLength() {
Dance 0:de885a6da962 73 reportDesc();
Dance 0:de885a6da962 74 return reportLength;
Dance 0:de885a6da962 75 }
Dance 0:de885a6da962 76
Dance 0:de885a6da962 77
Dance 0:de885a6da962 78
Dance 0:de885a6da962 79 //
Dance 0:de885a6da962 80 // Route callbacks from lower layers to class(es)
Dance 0:de885a6da962 81 //
Dance 0:de885a6da962 82
Dance 0:de885a6da962 83
Dance 0:de885a6da962 84 // Called in ISR context
Dance 0:de885a6da962 85 // Called by USBDevice on Endpoint0 request
Dance 0:de885a6da962 86 // This is used to handle extensions to standard requests
Dance 0:de885a6da962 87 // and class specific requests
Dance 0:de885a6da962 88 // Return true if class handles this request
Dance 0:de885a6da962 89 bool USBHID::USBCallback_request() {
Dance 0:de885a6da962 90 bool success = false;
Dance 0:de885a6da962 91 CONTROL_TRANSFER * transfer = getTransferPtr();
Dance 0:de885a6da962 92 uint8_t *hidDescriptor;
Dance 0:de885a6da962 93
Dance 0:de885a6da962 94 // Process additional standard requests
Dance 0:de885a6da962 95
Dance 0:de885a6da962 96 if ((transfer->setup.bmRequestType.Type == STANDARD_TYPE))
Dance 0:de885a6da962 97 {
Dance 0:de885a6da962 98 switch (transfer->setup.bRequest)
Dance 0:de885a6da962 99 {
Dance 0:de885a6da962 100 case GET_DESCRIPTOR:
Dance 0:de885a6da962 101 switch (DESCRIPTOR_TYPE(transfer->setup.wValue))
Dance 0:de885a6da962 102 {
Dance 0:de885a6da962 103 case REPORT_DESCRIPTOR:
Dance 0:de885a6da962 104 if ((reportDesc() != NULL) \
Dance 0:de885a6da962 105 && (reportDescLength() != 0))
Dance 0:de885a6da962 106 {
Dance 0:de885a6da962 107 transfer->remaining = reportDescLength();
Dance 0:de885a6da962 108 transfer->ptr = reportDesc();
Dance 0:de885a6da962 109 transfer->direction = DEVICE_TO_HOST;
Dance 0:de885a6da962 110 success = true;
Dance 0:de885a6da962 111 }
Dance 0:de885a6da962 112 break;
Dance 0:de885a6da962 113 case HID_DESCRIPTOR:
Dance 0:de885a6da962 114 // Find the HID descriptor, after the configuration descriptor
Dance 0:de885a6da962 115 hidDescriptor = findDescriptor(HID_DESCRIPTOR);
Dance 0:de885a6da962 116 if (hidDescriptor != NULL)
Dance 0:de885a6da962 117 {
Dance 0:de885a6da962 118 transfer->remaining = HID_DESCRIPTOR_LENGTH;
Dance 0:de885a6da962 119 transfer->ptr = hidDescriptor;
Dance 0:de885a6da962 120 transfer->direction = DEVICE_TO_HOST;
Dance 0:de885a6da962 121 success = true;
Dance 0:de885a6da962 122 }
Dance 0:de885a6da962 123 break;
Dance 0:de885a6da962 124
Dance 0:de885a6da962 125 default:
Dance 0:de885a6da962 126 break;
Dance 0:de885a6da962 127 }
Dance 0:de885a6da962 128 break;
Dance 0:de885a6da962 129 default:
Dance 0:de885a6da962 130 break;
Dance 0:de885a6da962 131 }
Dance 0:de885a6da962 132 }
Dance 0:de885a6da962 133
Dance 0:de885a6da962 134 // Process class-specific requests
Dance 0:de885a6da962 135
Dance 0:de885a6da962 136 if (transfer->setup.bmRequestType.Type == CLASS_TYPE)
Dance 0:de885a6da962 137 {
Dance 0:de885a6da962 138 switch (transfer->setup.bRequest)
Dance 0:de885a6da962 139 {
Dance 0:de885a6da962 140 case SET_REPORT:
Dance 0:de885a6da962 141 // First byte will be used for report ID
Dance 0:de885a6da962 142 outputReport.data[0] = transfer->setup.wValue & 0xff;
Dance 0:de885a6da962 143 outputReport.length = transfer->setup.wLength + 1;
Dance 0:de885a6da962 144
Dance 0:de885a6da962 145 transfer->remaining = sizeof(outputReport.data) - 1;
Dance 0:de885a6da962 146 transfer->ptr = &outputReport.data[1];
Dance 0:de885a6da962 147 transfer->direction = HOST_TO_DEVICE;
Dance 0:de885a6da962 148 transfer->notify = true;
Dance 0:de885a6da962 149 success = true;
Dance 0:de885a6da962 150 default:
Dance 0:de885a6da962 151 break;
Dance 0:de885a6da962 152 }
Dance 0:de885a6da962 153 }
Dance 0:de885a6da962 154
Dance 0:de885a6da962 155 return success;
Dance 0:de885a6da962 156 }
Dance 0:de885a6da962 157
Dance 0:de885a6da962 158
Dance 0:de885a6da962 159 #define DEFAULT_CONFIGURATION (1)
Dance 0:de885a6da962 160
Dance 0:de885a6da962 161
Dance 0:de885a6da962 162 // Called in ISR context
Dance 0:de885a6da962 163 // Set configuration. Return false if the
Dance 0:de885a6da962 164 // configuration is not supported
Dance 0:de885a6da962 165 bool USBHID::USBCallback_setConfiguration(uint8_t configuration) {
Dance 0:de885a6da962 166 if (configuration != DEFAULT_CONFIGURATION) {
Dance 0:de885a6da962 167 return false;
Dance 0:de885a6da962 168 }
Dance 0:de885a6da962 169
Dance 0:de885a6da962 170 // Configure endpoints > 0
Dance 0:de885a6da962 171 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
Dance 0:de885a6da962 172 addEndpoint(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
Dance 0:de885a6da962 173
Dance 0:de885a6da962 174 // We activate the endpoint to be able to recceive data
Dance 0:de885a6da962 175 readStart(EPINT_OUT, MAX_PACKET_SIZE_EPINT);
Dance 0:de885a6da962 176 return true;
Dance 0:de885a6da962 177 }
Dance 0:de885a6da962 178
Dance 0:de885a6da962 179
Dance 0:de885a6da962 180 uint8_t * USBHID::stringIinterfaceDesc() {
Dance 0:de885a6da962 181 static uint8_t stringIinterfaceDescriptor[] = {
Dance 0:de885a6da962 182 0x08, //bLength
Dance 0:de885a6da962 183 STRING_DESCRIPTOR, //bDescriptorType 0x03
Dance 0:de885a6da962 184 'H',0,'I',0,'D',0, //bString iInterface - HID
Dance 0:de885a6da962 185 };
Dance 0:de885a6da962 186 return stringIinterfaceDescriptor;
Dance 0:de885a6da962 187 }
Dance 0:de885a6da962 188
Dance 0:de885a6da962 189 uint8_t * USBHID::stringIproductDesc() {
Dance 0:de885a6da962 190 static uint8_t stringIproductDescriptor[] = {
Dance 0:de885a6da962 191 0x16, //bLength
Dance 0:de885a6da962 192 STRING_DESCRIPTOR, //bDescriptorType 0x03
Dance 0:de885a6da962 193 'H',0,'I',0,'D',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - HID device
Dance 0:de885a6da962 194 };
Dance 0:de885a6da962 195 return stringIproductDescriptor;
Dance 0:de885a6da962 196 }
Dance 0:de885a6da962 197
Dance 0:de885a6da962 198
Dance 0:de885a6da962 199
Dance 0:de885a6da962 200 uint8_t * USBHID::reportDesc() {
Dance 0:de885a6da962 201 static uint8_t reportDescriptor[] = {
Dance 0:de885a6da962 202 0x06, LSB(0xFFAB), MSB(0xFFAB),
Dance 0:de885a6da962 203 0x0A, LSB(0x0200), MSB(0x0200),
Dance 0:de885a6da962 204 0xA1, 0x01, // Collection 0x01
Dance 0:de885a6da962 205 0x75, 0x08, // report size = 8 bits
Dance 0:de885a6da962 206 0x15, 0x00, // logical minimum = 0
Dance 0:de885a6da962 207 0x26, 0xFF, 0x00, // logical maximum = 255
Dance 0:de885a6da962 208 0x95, input_length, // report count
Dance 0:de885a6da962 209 0x09, 0x01, // usage
Dance 0:de885a6da962 210 0x81, 0x02, // Input (array)
Dance 0:de885a6da962 211 0x95, output_length,// report count
Dance 0:de885a6da962 212 0x09, 0x02, // usage
Dance 0:de885a6da962 213 0x91, 0x02, // Output (array)
Dance 0:de885a6da962 214 0xC0 // end collection
Dance 0:de885a6da962 215
Dance 0:de885a6da962 216 };
Dance 0:de885a6da962 217 reportLength = sizeof(reportDescriptor);
Dance 0:de885a6da962 218 return reportDescriptor;
Dance 0:de885a6da962 219 }
Dance 0:de885a6da962 220
Dance 0:de885a6da962 221 #define DEFAULT_CONFIGURATION (1)
Dance 0:de885a6da962 222 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
Dance 0:de885a6da962 223 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
Dance 0:de885a6da962 224 + (1 * HID_DESCRIPTOR_LENGTH) \
Dance 0:de885a6da962 225 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
Dance 0:de885a6da962 226
Dance 0:de885a6da962 227 uint8_t * USBHID::configurationDesc() {
Dance 0:de885a6da962 228 static uint8_t configurationDescriptor[] = {
Dance 0:de885a6da962 229 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
Dance 0:de885a6da962 230 CONFIGURATION_DESCRIPTOR, // bDescriptorType
Dance 0:de885a6da962 231 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
Dance 0:de885a6da962 232 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
Dance 0:de885a6da962 233 0x01, // bNumInterfaces
Dance 0:de885a6da962 234 DEFAULT_CONFIGURATION, // bConfigurationValue
Dance 0:de885a6da962 235 0x00, // iConfiguration
Dance 0:de885a6da962 236 C_RESERVED | C_SELF_POWERED, // bmAttributes
Dance 0:de885a6da962 237 C_POWER(0), // bMaxPower
Dance 0:de885a6da962 238
Dance 0:de885a6da962 239 INTERFACE_DESCRIPTOR_LENGTH, // bLength
Dance 0:de885a6da962 240 INTERFACE_DESCRIPTOR, // bDescriptorType
Dance 0:de885a6da962 241 0x00, // bInterfaceNumber
Dance 0:de885a6da962 242 0x00, // bAlternateSetting
Dance 0:de885a6da962 243 0x02, // bNumEndpoints
Dance 0:de885a6da962 244 HID_CLASS, // bInterfaceClass
Dance 0:de885a6da962 245 HID_SUBCLASS_NONE, // bInterfaceSubClass
Dance 0:de885a6da962 246 HID_PROTOCOL_NONE, // bInterfaceProtocol
Dance 0:de885a6da962 247 0x00, // iInterface
Dance 0:de885a6da962 248
Dance 0:de885a6da962 249 HID_DESCRIPTOR_LENGTH, // bLength
Dance 0:de885a6da962 250 HID_DESCRIPTOR, // bDescriptorType
Dance 0:de885a6da962 251 LSB(HID_VERSION_1_11), // bcdHID (LSB)
Dance 0:de885a6da962 252 MSB(HID_VERSION_1_11), // bcdHID (MSB)
Dance 0:de885a6da962 253 0x00, // bCountryCode
Dance 0:de885a6da962 254 0x01, // bNumDescriptors
Dance 0:de885a6da962 255 REPORT_DESCRIPTOR, // bDescriptorType
Dance 0:de885a6da962 256 (uint8_t)(LSB(this->reportDescLength())), // wDescriptorLength (LSB)
Dance 0:de885a6da962 257 (uint8_t)(MSB(this->reportDescLength())), // wDescriptorLength (MSB)
Dance 0:de885a6da962 258
Dance 0:de885a6da962 259 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Dance 0:de885a6da962 260 ENDPOINT_DESCRIPTOR, // bDescriptorType
Dance 0:de885a6da962 261 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
Dance 0:de885a6da962 262 E_INTERRUPT, // bmAttributes
Dance 0:de885a6da962 263 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Dance 0:de885a6da962 264 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Dance 0:de885a6da962 265 1, // bInterval (milliseconds)
Dance 0:de885a6da962 266
Dance 0:de885a6da962 267 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Dance 0:de885a6da962 268 ENDPOINT_DESCRIPTOR, // bDescriptorType
Dance 0:de885a6da962 269 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
Dance 0:de885a6da962 270 E_INTERRUPT, // bmAttributes
Dance 0:de885a6da962 271 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Dance 0:de885a6da962 272 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Dance 0:de885a6da962 273 1, // bInterval (milliseconds)
Dance 0:de885a6da962 274 };
Dance 0:de885a6da962 275 return configurationDescriptor;
Dance 0:de885a6da962 276 }