a
Dependencies: mbed mcp3208 HeptaTemp
main.cpp@0:709d290281a1, 2022-06-14 (annotated)
- Committer:
- goro56
- Date:
- Tue Jun 14 09:22:13 2022 +0000
- Revision:
- 0:709d290281a1
a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
goro56 | 0:709d290281a1 | 1 | #include "mbed.h" |
goro56 | 0:709d290281a1 | 2 | SPI spi(p11,p12,p13); |
goro56 | 0:709d290281a1 | 3 | DigitalOut cs(p14); |
goro56 | 0:709d290281a1 | 4 | Serial pc(USBTX, USBRX); |
goro56 | 0:709d290281a1 | 5 | int main(void) |
goro56 | 0:709d290281a1 | 6 | { |
goro56 | 0:709d290281a1 | 7 | int bit = 8; |
goro56 | 0:709d290281a1 | 8 | spi.format(bit,0); |
goro56 | 0:709d290281a1 | 9 | spi.frequency(2000000); //VDD=5Vの場合は2MHz. |
goro56 | 0:709d290281a1 | 10 | printf("\r\n"); |
goro56 | 0:709d290281a1 | 11 | printf("Starting MCP3208\r\n"); |
goro56 | 0:709d290281a1 | 12 | float Vref = 5.08; |
goro56 | 0:709d290281a1 | 13 | while(1) |
goro56 | 0:709d290281a1 | 14 | { |
goro56 | 0:709d290281a1 | 15 | cs = 0; |
goro56 | 0:709d290281a1 | 16 | uint8_t d0 = spi.write(0x06); |
goro56 | 0:709d290281a1 | 17 | uint8_t d1 = spi.write(0x40); |
goro56 | 0:709d290281a1 | 18 | uint8_t d2 = spi.write(0x00); |
goro56 | 0:709d290281a1 | 19 | printf("%d,%d,%d \r\n",d0,d1,d2); |
goro56 | 0:709d290281a1 | 20 | uint8_t d3 = d1 << 4; |
goro56 | 0:709d290281a1 | 21 | uint16_t d = (d3 << 4) | d2; // 1文にまとめると uint16_t d = (d1 << 8) | d2; |
goro56 | 0:709d290281a1 | 22 | //float volt = Vref * (float)d / 4096; |
goro56 | 0:709d290281a1 | 23 | //float amp = Vref * (float)d / 4096; |
goro56 | 0:709d290281a1 | 24 | //printf("d=%d volt=%5.2f\n\r", d, volt); |
goro56 | 0:709d290281a1 | 25 | //printf("d=%d CDH_Amp =%f [A]\n\r", d, amp); |
goro56 | 0:709d290281a1 | 26 | |
goro56 | 0:709d290281a1 | 27 | //tem |
goro56 | 0:709d290281a1 | 28 | |
goro56 | 0:709d290281a1 | 29 | float R3; |
goro56 | 0:709d290281a1 | 30 | float R4; |
goro56 | 0:709d290281a1 | 31 | float R5; |
goro56 | 0:709d290281a1 | 32 | float Pt; |
goro56 | 0:709d290281a1 | 33 | float R_1; |
goro56 | 0:709d290281a1 | 34 | float R_2; |
goro56 | 0:709d290281a1 | 35 | //current |
goro56 | 0:709d290281a1 | 36 | float I; |
goro56 | 0:709d290281a1 | 37 | //voltage |
goro56 | 0:709d290281a1 | 38 | float Vreft; |
goro56 | 0:709d290281a1 | 39 | |
goro56 | 0:709d290281a1 | 40 | //temperature coefficient |
goro56 | 0:709d290281a1 | 41 | float ce; |
goro56 | 0:709d290281a1 | 42 | //resistance |
goro56 | 0:709d290281a1 | 43 | R3 = 110;//[Ω] |
goro56 | 0:709d290281a1 | 44 | R4 = 1000;//[Ω] |
goro56 | 0:709d290281a1 | 45 | R5 = 68000;//[Ω] |
goro56 | 0:709d290281a1 | 46 | Pt = 100;//[Ω] |
goro56 | 0:709d290281a1 | 47 | R_1 = 3;//[kΩ] |
goro56 | 0:709d290281a1 | 48 | R_2 = 2;//[kΩ] |
goro56 | 0:709d290281a1 | 49 | |
goro56 | 0:709d290281a1 | 50 | //current |
goro56 | 0:709d290281a1 | 51 | I = 0.001;//[A] |
goro56 | 0:709d290281a1 | 52 | |
goro56 | 0:709d290281a1 | 53 | //voltage |
goro56 | 0:709d290281a1 | 54 | Vreft = 2.5;//[V] |
goro56 | 0:709d290281a1 | 55 | |
goro56 | 0:709d290281a1 | 56 | //Gain&Offset |
goro56 | 0:709d290281a1 | 57 | float gain = -R5*I/R4; |
goro56 | 0:709d290281a1 | 58 | float off = Vreft+I*R3; |
goro56 | 0:709d290281a1 | 59 | |
goro56 | 0:709d290281a1 | 60 | //temperature coefficient |
goro56 | 0:709d290281a1 | 61 | ce = 0.003851; |
goro56 | 0:709d290281a1 | 62 | |
goro56 | 0:709d290281a1 | 63 | float volt = Vref*(float)d / 4096*(R_1 + R_2)/R_1; |
goro56 | 0:709d290281a1 | 64 | float Rth = (volt-off)/gain+R3; |
goro56 | 0:709d290281a1 | 65 | float temp = (Rth-Pt)/(ce*Pt); |
goro56 | 0:709d290281a1 | 66 | printf("d=%d EPS_Temp =%f [℃]\n\r", d, temp); |
goro56 | 0:709d290281a1 | 67 | |
goro56 | 0:709d290281a1 | 68 | cs = 1; |
goro56 | 0:709d290281a1 | 69 | wait(1.0); |
goro56 | 0:709d290281a1 | 70 | } |
goro56 | 0:709d290281a1 | 71 | } |