忘備録-備忘録

技術的な備忘録

raspberryPi3で温度測定

2016-10-17 18:07:32 | raspberry ...

raspberry Pi 3に熱電対式の温度計を接続して温度を測定するプログラムです。 熱電対温度計にはMAX31855を使用したK型熱電対アンプモジュールを用いました。


回路図

回路図はFritzingを使い描きました。

プログラム(C言語)

  1. #include <stdint.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <getopt.h>
  6. #include <fcntl.h>
  7. #include <sys/ioctl.h>
  8. #include <linux/types.h>
  9. #include <linux/spi/spidev.h>
  10. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  11. static void pabort(const char *s)
  12. {
  13.     perror(s);
  14.     abort();
  15. }
  16. static const char *device = "/dev/spidev0.0";
  17. static uint8_t mode;
  18. static uint8_t bits = 8;
  19. static uint32_t speed = 500000;
  20. static uint16_t delay;
  21. static void transfer(int fd)
  22. {
  23.     int ret;
  24.     unsigned tmp = 0;
  25.     double x;
  26.     uint8_t tx[] = {
  27.         0xFF, 0xFF, 0xFF, 0xFF
  28.     };
  29.     uint8_t rx[ARRAY_SIZE(tx)] = {0, };
  30.     struct spi_ioc_transfer tr = {
  31.         .tx_buf = (unsigned long)tx,
  32.         .rx_buf = (unsigned long)rx,
  33.         .len = ARRAY_SIZE(tx),
  34.         .delay_usecs = delay,
  35.         .speed_hz = speed,
  36.         .bits_per_word = bits,
  37.     };
  38.     ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
  39.     if (ret
  40.         pabort("can't send spi message");
  41.     if(rx[3]&0x01) printf("断線\n");
  42.     if(rx[3]&0x02) printf("GNDにショート\n");
  43.     if(rx[3]&0x04) printf("VCCにショート\n");
  44.     tmp = (rx[2] << 4) | (rx[3] >> 4);
  45.     if(tmp&0x800) {
  46.         tmp = (~tmp + 1) & 0x7FF;
  47.         x = (double)tmp*(-0.0625);
  48.     } else {
  49.         x = (double)tmp * 0.0625;
  50.     }
  51.     printf("現在の室温 %lf\n",x);
  52.       tmp = (rx[0] << 8) | (rx[1]);
  53.     tmp = tmp >> 2;
  54.     if(tmp&0x2000) {
  55.         tmp = (~tmp + 1) & 0x1FFF;
  56.         x = (double)tmp*(-0.25);
  57.     } else {
  58.         x = (double)tmp * 0.25;
  59.     }
  60.     printf("現在の熱電対の温度 %lf\n",x);
  61. }
  62. int main(int argc, char *argv[])
  63. {
  64.     int ret = 0;
  65.     int fd;
  66.     fd = open(device, O_RDWR);
  67.     if (fd < 0)
  68.         pabort("can't open device");
  69.     /*
  70.      * spi mode
  71.      */
  72.     ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
  73.     if (ret == -1)
  74.         pabort("can't set spi mode");
  75.     ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
  76.     if (ret == -1)
  77.         pabort("can't get spi mode");
  78.     /*
  79.      * bits per word
  80.      */
  81.     ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  82.     if (ret == -1)
  83.         pabort("can't set bits per word");
  84.     ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  85.     if (ret == -1)
  86.         pabort("can't get bits per word");
  87.     /*
  88.      * max speed hz
  89.      */
  90.     ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  91.     if (ret == -1)
  92.         pabort("can't set max speed hz");
  93.     ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
  94.     if (ret == -1)
  95.         pabort("can't get max speed hz");
  96.     printf("spi mode: %d\n", mode);
  97.     printf("bits per word: %d\n", bits);
  98.     printf("max speed: %d Hz (%d KHz)\n\n", speed, speed/1000);
  99.     transfer(fd);
  100.     close(fd);
  101.     return ret;
  102. }


プログラムはSPIのサンプルコードを書き換えたものです。

プログラム(JavaScript)
SPIモジュールをインストールします。

  1.  $ npm install pi-spi


  1. var SPI = require('pi-spi');
  2. var spi = SPI.initialize("/dev/spidev0.0"),
  3.     test = Buffer("Hello, World!");
  4. var msg = Buffer("");
  5. // reads and writes simultaneously
  6. spi.read(4,function(e,d) {
  7.     if (e) console.error(e);
  8.     else {
  9. //--------------------
  10.     var tmp,x;
  11.     if(d[3]&0x01) console.log("断線\n");
  12.     if(d[3]&0x02) console.log("GNDにショート\n");
  13.     if(d[3]&0x04) console.log("VCCにショート\n");
  14.     tmp = (d[2] << 4) | (d[3] >> 4);
  15.     if( (tmp&0x800) != 0 ) {
  16.         tmp = (~tmp + 1) & 0x7FF;
  17.         x = tmp*(-0.0625);
  18.     } else {
  19.         x = tmp * 0.0625;
  20.     }
  21.     console.log("現在の室温 "+ x);
  22.       tmp = (d[0] << 8) | (d[1]);
  23.     tmp = tmp >> 2;
  24.     if((tmp&0x2000) != 0 ) {
  25.         tmp = (~tmp + 1) & 0x1FFF;
  26.         x = tmp*(-0.25);
  27.     } else {
  28.         x = tmp * 0.25;
  29.     }
  30.     console.log("現在の熱電対の温度 "+ x);
  31. //--------------------
  32.     }
  33. });