์ผ๋จ SenseHAT๋ ์ด๋ ๊ฒ ๋ผ์ฆ๋ฒ ๋ฆฌํ์ด์ ์น์ด์ ์ฌ์ฉํ ์ ์๋ ์น๊ตฌ๋ค.
์ถ ๋์ด๊ฐ ์ผ์ ํ๊ฒ ๋ฑ ๋ง์์ ์์ ์ ์ผ๋ก ์ด์ฉํ ์ ์๋ค.
์ ์์ ์ธ๊ฐํ๋ ์๊ฐ LED๋ค์ด ๋ฐ์ง ์ผ์ ธ์ ์ฌ๊ดํ์ ์ฐ์์ผํ์ง๋ง ์ดํ์ ์์์ฅํ๊ฒ ๋ค์ด์จ๋ค.
HTS221๋ก๋ถํฐ ์จ์ต๋๋ฅผ ์ฝ์ด๋ด๊ธฐ ์ํด์ ๋ฐ์ดํฐ์ํธ๊ฐ ํ์ํ๋ค.
control register๋ก ์ ์ดํ๊ณ ๋ฐ์ดํฐ๊ฐ๋ I2C๋ฅผ ํตํด register๋ฅผ ์ฝ์ด๋ด๋ ๋ฐฉ์์ผ๋ก ํ๋ค.
์จ๋์ ์ต๋๋ฅผ ์ฝ์ด๋ด๋ ๋ฐฉ์์ ์๋ด์ ๊ฐ๋ค. ๋ ์ฌ์ง ๋ชจ๋ HTS221 datasheet์์ ์ผ๋ถ ๋ฐ์ทํ ์ฌ์ง์ด๋ค.
๊ฐ์ ๊ทธ๋ฅ ์ฝ์ด๋ด๋ ๊ฒ์ด ์๋๋ผ x์ขํ๊ฐ ๋๊ฐ, ๊ทธ์ ์์ํ๋ y์ขํ๊ฐ ๋๊ฐ๋ฅผ ์ฝ์ ํ, ๋ ์ ์ ์ง๋๋ ์ง์ ์ ๋ํ ๋ฐฉ์ ์์ ํตํด ๊ฐ์ ์ค์ ์ผ์๊ฐ ์ฝ์ ๊ฐ์ ๊ณ์ฐํ๋ ๋ฐฉ์์ด๋ค.
๋ฌธ์ ๋ ์ด ๊ณ์ฐ์ ์ ๋ถ ์ฝ๋ฉ์ผ๋ก ๊ฐ์ ๋ฃ์ด์ผํ๋ค๋ ์ ์ด๋ค.
๋ ์ง์คํฐ์ ์ ์ฅ๋ x,y ์ขํ๊ฐ ๋์ธํธ๋ ํ์ฌ ์จ์ต๋์ ๊ด๋ จ์์ด ์์๊ฐ์ ์ง๋๋ ๋ฏ ๋ณด์ธ๋ค.
์๋ง ์ผ์ ๋ด์ ๋ด์ฅ๋ ์ฐ์ ์ฐ์ฐ์ฅ์น๊ฐ ์์ด๊ธฐ์ ์ด๋ ๊ฒ ์ํํธ์จ์ด์ ์ผ๋ก ์ฐ์ ์ ๋ณด๋ฅผ ์ฒ๋ฆฌํ๋๋ก ํ๋ ๋ฏ ํ๋ค.
์ผ์์ ๊ฐ ๋ ์ง์คํฐ๋ I2Cํต์ ์ผ๋ก ํ๊ธฐ์, ์ฝ์ด๋ด๋ ๋ฐ์ดํฐ๋ฅผ 8-bit์ฉ ๋์ด ์ฝ์ด๋ด์ผ ํ๋๋ฐ, ์ค์ ๋ก ์ผ์๊ฐ ๊ฐ์ง ๊ฐ์ 8-bit๋ก ํํํ ์ ์๋ค. ๋ฐ๋ผ์ 16bit์ ๊ฒฝ์ฐ์๋ ๋ ์ง์คํฐ๋ฅผ ๋๋ก ๋๋์ด ๋ฐ์ดํฐ์ low๋ถ๋ถ๊ณผ high๋ถ๋ถ์ unsigned๊ฐ์ผ๋ก ์ฝ์ด๋ด ๋ ๋ฐ์ดํธ๋ฅผ ํฉ์ณ doubleํ์ผ๋ก ์ ์ฅํ๋ค. ์ค์ ๊ฐ์ 2's complement์ด๋ฏ๋ก ๋ถํธ๊ฐ ์คํ๊ธฐ์ unsiigned๋ก ๋ฐ์ ํฉ์น๋ ๊ฒ์ด๋ค.
์ฝ๋ ์ ๋ฌธ์ ์๋์ ๊ฐ๋ค.
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <sys/ioctl.h>
static const char* I2C_DEV = "/dev/i2c-1";
static const int I2C_SLAVE = 0x0703;
static const int HTS221_ID = 0x5F;
static const int CTRL1 = 0x20;
static const int CTRL2 = 0x21;
static const int humi_x_0l = 0x36;
static const int humi_x_0h = 0x37;
static const int humi_x_1l = 0x3A;
static const int humi_x_1h = 0x3B;
static const int humi_y_0 = 0x30;
static const int humi_y_1 = 0x31;
static const int temp_x_0l = 0x3C;
static const int temp_x_0h = 0x3D;
static const int temp_x_1l = 0x3E;
static const int temp_x_1h = 0x3F;
static const int temp_y_0l = 0x32;
static const int temp_y_1l = 0x33;
static const int temp_y_01h = 0x35;
static const int temp_out_l = 0x2A;
static const int temp_out_h = 0x2B;
static const int humi_out_l = 0x28;
static const int humi_out_h = 0x29;
void getdata(int fd, double *temperature, double *humidity);
int main() {
int i2c_fd;
double temperature, humidity; //2 bytes
i2c_fd = open(I2C_DEV, O_RDWR);
ioctl(i2c_fd, I2C_SLAVE, HTS221_ID);
for(int i = 0 ; i < 10 ; i++ ) {
wiringPiI2CWriteReg8(i2c_fd, CTRL1, 0x00); //powerdown
wiringPiI2CWriteReg8(i2c_fd, CTRL1, 0x84); //active mode & register update resist
wiringPiI2CWriteReg8(i2c_fd, CTRL2, 0x01); //oneshot enable
getdata(i2c_fd, &temperature, &humidity);
printf("Temp : %.2f C\n", temperature);
printf("Humi : %.0f%% rH\n", humidity);
delay(1000);
}
wiringPiI2CWriteReg8(i2c_fd, CTRL1, 0x00); //powerdown HTS221
close(i2c_fd);
return 0;
}
void getdata(int fd, double *temperature, double *humidity) {
int result;
do {
delay(100);
result = wiringPiI2CReadReg8(fd, CTRL2);
} while (result != 0); //run untill ctrl_reg goes to zero.
//humi x & y
unsigned char HUMI_x_0l = wiringPiI2CReadReg8(fd, humi_x_0l);
unsigned char HUMI_x_0h = wiringPiI2CReadReg8(fd, humi_x_0h);
unsigned char HUMI_x_1l = wiringPiI2CReadReg8(fd, humi_x_1l);
unsigned char HUMI_x_1h = wiringPiI2CReadReg8(fd, humi_x_1h);
unsigned char HUMI_y_0_raw = wiringPiI2CReadReg8(fd, humi_y_0);
unsigned char HUMI_y_1_raw = wiringPiI2CReadReg8(fd, humi_y_1);
//temp x & y
unsigned char TEMP_x_0l = wiringPiI2CReadReg8(fd, temp_x_0l);
unsigned char TEMP_x_0h = wiringPiI2CReadReg8(fd, temp_x_0h);
unsigned char TEMP_x_1l = wiringPiI2CReadReg8(fd, temp_x_1l);
unsigned char TEMP_x_1h = wiringPiI2CReadReg8(fd, temp_x_1h);
unsigned char TEMP_y_0l = wiringPiI2CReadReg8(fd, temp_y_0l);
unsigned char TEMP_y_1l = wiringPiI2CReadReg8(fd, temp_y_1l);
unsigned char TEMP_y_01h = wiringPiI2CReadReg8(fd, temp_y_01h);
//use unsinged char. becuase of 2's complement.
//and A register has only 8-bit.
//if use just char, 2 byte date short = (2's comp)(2's comp) is not correct
//in code, 2byte datas are 2's complement.
//humi-y, msb-data, temp-y are unsinged type.
unsigned char TEMP_out_l = wiringPiI2CReadReg8(fd, temp_out_l);
unsigned char TEMP_out_h = wiringPiI2CReadReg8(fd, temp_out_h);
unsigned char HUMI_out_l = wiringPiI2CReadReg8(fd, humi_out_l);
unsigned char HUMI_out_h = wiringPiI2CReadReg8(fd, humi_out_h);
//bit setting - cartesian-X
short HUMI_x_0 = HUMI_x_0h << 8 | HUMI_x_0l;
short HUMI_x_1 = HUMI_x_1h << 8 | HUMI_x_1l;
short TEMP_x_0 = TEMP_x_0h << 8 | TEMP_x_0l;
short TEMP_x_1 = TEMP_x_1h << 8 | TEMP_x_1l;
//bit setting - cartesian-Y
double HUMI_y_0 = HUMI_y_0_raw / 2.0;
double HUMI_y_1 = HUMI_y_1_raw / 2.0;
double TEMP_y_0 = ((TEMP_y_01h & 0b11) << 8 | TEMP_y_0l) / 8.0;
double TEMP_y_1 = (((TEMP_y_01h & 0b1100) >> 2 ) << 8 | TEMP_y_1l) / 8.0;
//bit setting output data
short TEMP_out = TEMP_out_h << 8 | TEMP_out_l;
short HUMI_out = HUMI_out_h << 8 | HUMI_out_l;
//gradient & central data by cartesian
double gradient_temp = (TEMP_y_1 - TEMP_y_0) / (TEMP_x_1 - TEMP_x_0);
double gradient_humi = (HUMI_y_1 - HUMI_y_0) / (HUMI_x_1 - HUMI_x_0);
*temperature = gradient_temp * (TEMP_out - TEMP_x_1) + TEMP_y_1;
*humidity = gradient_humi * (HUMI_out - HUMI_x_1) + HUMI_y_1;
}
gradient๋ก ๊ธฐ์ธ๊ธฐ๋ฅผ ์ฝ๊ณ , ํ ์ ์ ์ง๋๊ณ ๊ธฐ์ธ๊ธฐ๋ฅผ ์๋ ์ง์ ์ ๋ฐฉ์ ์์ ์ด์ฉํด ํ์ฌ ๊ฐ์ ์ฝ์ด๋ธ๋ค.
'SW > Jetson nano & Raspberry Pi' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Jetson Nano ์ธํ ํ๊ธฐ (0) | 2023.07.21 |
---|---|
๋ผ์ฆ๋ฒ ๋ฆฌํ์ด4 model B (Raspberrypi4 model B) (0) | 2023.01.22 |