/* * main.cpp * * Created on: 2014/05/29 * Author: sunaga */ #include "I2C.h" #include #include #include #include int InitTextLcd(CI2C* pI2c) { pI2c->Set8bits(0x50, 0x00, 0x00);//clear display usleep(100000); pI2c->Set8bits(0x50, 0x00, 0x38);//function set: 8bit, 2line usleep(100000); pI2c->Set8bits(0x50, 0x00, 0x0c);//display on, no cursor usleep(100000); pI2c->Set8bits(0x50, 0x00, 0x06);//direction=INC, no shift usleep(100000); return 0; } //秋月電子ーーキャラクタLCDモジュール(ACM1602NI-FLW-FBW-M01) //100kHzでは動きません。50kHzで動作しました。 //i2cgetdevice をやると固まります //これを使うときはi2cgetdeviceをやらないでください int SetTextLcd(CI2C* pI2c, char* lpszText) { int i; int len = strlen(lpszText); for(i = 0; i < len; i++) { pI2c->Set8bits(0x50, 0x80, *lpszText); lpszText++; } return 0; } void DrawTemparature(CI2C* pI2c) { i2cVal val; char buf[128]; double temp1, temp2; pI2c->Set8bits(0x50, 0x00, 0x80);//clear display usleep(1000); val = pI2c->Get16bits(0x48, 0); //13bits int data = ((val.val << 8) & 0xff00) + (val.val >> 8); if((data & 0x8000) == 0) { data = data >> 3; temp1 = (double)data / 16.0; } else { data = data >> 3; temp1 = ((double)data - 8192.0) / 16.0; } //temp = ((double)data / 8.0) * 0.0625; pI2c->Set8bits(0x39, 0x03, 0x0c);//12bits resolutuon val = pI2c->Get8bits(0x39, 0x00); temp2 = val.val; val = pI2c->Get8bits(0x39, 0x02); val.val >>= 4; temp2 += (val.val * 0.0625); sprintf(buf, "%.1f %cC %.1f %cC", temp1, 0xdf, temp2, 0xdf); SetTextLcd(pI2c, buf); printf(buf); printf("\n"); } void DrawPressure(CI2C* pI2c) { i2cVal val; char buf[128]; pI2c->Set8bits(0x50, 0x00, 0xc0); val = pI2c->Get8bits(0x5d, 0x28); if(val.enable == false) { printf("error\n"); return; } int l = val.val; val = pI2c->Get8bits(0x5d, 0x29); int m = val.val; val = pI2c->Get8bits(0x5d, 0x2a); int h = val.val; int data = (h << 16) + (m << 8) + l; int p = data / 4096; sprintf(buf, "%d hPa", p ); SetTextLcd(pI2c, buf); printf(buf); printf("\n"); val = pI2c->Get8bits(0x5d, 0x2b); l = val.val; val = pI2c->Get8bits(0x5d, 0x2c); h = val.val; short t = (short)((h << 8) + l); double temp = t / 480.0 + 42.5; sprintf(buf, " %.1f %cC", temp, 0xdf ); SetTextLcd(pI2c, buf); printf(buf); printf("\n"); } int main() { int i; i2cVal val; char buf[128]; CI2C i2c( "/dev/i2c-1"); int br = i2c.GetBaudrate(); i2c.SetBaudrate(50000); br = i2c.GetBaudrate(); InitTextLcd(&i2c); i2c.Set8bits(0x5d, 0x20, 0x90); for(i = 0; i < 100; i++) { DrawTemparature(&i2c); DrawPressure(&i2c); sleep(1); } return 0; }