====== AN41004 - 모아콘과 온습도센서 연결하기 (SHT7X 시리즈) ====== SHT7X 시리즈는 I2C 통신으로 온/습도를 읽어올 수 있는 센서입니다. {{ :cublocapp:an41004:sht71.png?nolink |}} 모아콘에는 CSG (세븐세그먼트 모듈)을 위한 I2C포트가 있습니다. 이 포트를 활용해서 SH17XX를 연결해 보았습니다. {{ :cublocapp:an41004:sht71_모아콘_결선도.png?nolink |}} 다음은 동작 동영상입니다.
다음은 모아콘 소스코드입니다. [[http://www.comfile.co.kr/download/moacon/SH17XX_moacon.zip|소스 다운로드]] #include "moacon500.h" //#include "csg.h" #define __dataPin 83 #define __clockPin 82 #define csg_delay_clock 26 #define LSBFIRST 0 #define MSBFIRST 1 //adr command r/w #define STATUS_REG_W 0x06 //000 0011 0 #define STATUS_REG_R 0x07 //000 0011 1 #define MEASURE_TEMP 0x03 //000 0001 1 #define MEASURE_HUMI 0x05 //000 0010 1 #define RESET 0x1e //000 1111 0 void __SCK(u8 val){ portMode(__clockPin,__OUTOD); if(val)portHigh(__clockPin); else portLow(__clockPin); } void __DATA(u8 val){ portMode(__dataPin,__OUTOD); if(val)portHigh(__dataPin); else portLow(__dataPin); } u8 __RDATA(void){ u8 i; portMode(__dataPin,__IN); i = portIn(__dataPin); return i; } void _nop_(void){ asm ("nop"); //delaymicro(csg_delay_clock); } void s_transstart(void) //---------------------------------------------------------------------------------- // generates a transmission start // _____ ________ // DATA: |_______| // ___ ___ // SCK : ___| |___| |______ { __DATA(1); __SCK(0); //Initial state _nop_(); __SCK(1); _nop_(); __DATA(0); _nop_(); __SCK(0); _nop_();_nop_();_nop_(); __SCK(1); _nop_(); __DATA(1); _nop_(); __SCK(0); } void s_connectionreset(void) //---------------------------------------------------------------------------------- // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart // _____________________________________________________ ________ // DATA: |_______| // _ _ _ _ _ _ _ _ _ ___ ___ // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______ { unsigned char i; __DATA(1); __SCK(0); //Initial state for(i=0;i<9;i++) //9 SCK cycles { __SCK(1); __SCK(0); } s_transstart(); //transmission start } void I2C_shiftOut(u8 bitOrder, u8 val) { u8 i; for (i = 0; i < 8; i++) { if (bitOrder == LSBFIRST){ if(!!(val & (1 << i))){ __DATA(1); _nop_(); } else { __DATA(0); _nop_(); } } else{ if( !!(val & (1 << (7 - i)))){ __DATA(1) ;_nop_(); } else { __DATA(0) ;_nop_(); } } __SCK(1);_nop_(); _nop_();_nop_();_nop_(); __SCK(0); _nop_(); } } u8 I2C_shiftIn(u8 bitOrder) { u8 value = 0, i; for (i = 0; i < 8; ++i) { __SCK(1);_nop_(); if (bitOrder == LSBFIRST) value |= __RDATA() << i; else{ value |= __RDATA() << (7 - i); __SCK(0);_nop_(); } } return value; } //____________________________________________________________________________________ void SHT_sendCommand(u8 command) { s_transstart(); I2C_shiftOut(MSBFIRST, command); __SCK(1); _nop_(); if (__RDATA())printf("ACK ERROR 0\r\n"); __SCK(0);_nop_(); if (!__RDATA())printf("ACK ERROR 1\r\n"); } void SHT_waitForResult() { // wait for the SHTx answer int ack; //acknowledgement int i; //need to wait up to 2 seconds for the value for ( i=0; i<1000; ++i){ delay(2); ack = __RDATA(); if (ack == 0) break; } if (ack == 1) printf("ACK ERROR 2\r\n");; } int SHT_getData() { u8 MSB = I2C_shiftIn(MSBFIRST); __DATA(1); __DATA(0); __SCK(1); __SCK(0); u8 LSB = I2C_shiftIn(MSBFIRST); return ((MSB << 8) | LSB); //combine bits } void SHT_skipCrc() { __DATA(1); __SCK(1); __SCK(0); } float getHumidity() { //Return Relative Humidity SHT_sendCommand(MEASURE_HUMI); SHT_waitForResult(); int val = SHT_getData(); SHT_skipCrc(); return -4.0 + 0.0405 * val + -0.0000028 * val * val; } float getTemperature() { //Return Temperature in Celsius SHT_sendCommand(MEASURE_TEMP); SHT_waitForResult(); int val = SHT_getData(); SHT_skipCrc(); //return (float)val * 0.01 - 40; //convert to celsius return (float)val * 0.01 - 40.1; //convert to celsius } void cmain(void) { clcdI2cInit(0); // 슬레이브 어드레스는 0 으로 합니다. clcdPower(1); // clcd 에 전원을 공급합니다. delay(100); while(1){ s_connectionreset(); float humidity = getHumidity(); float temperature = getTemperature(); debugLocate(5,5); printf("humidity = %f\r\n",humidity ); debugLocate(5,8); printf("temperature = %f\r\n",temperature ); delay(20); clcdPrint (0,0, "=MOACON SHT7x TEST="); clcdPrint (2,1, "Temp : %4.2f C",temperature); clcdPrint (2,2, "Humi : %4.2f %""%",humidity); clcdPrint (1,3, "Humi&Temp Sensor"); delay(30); } } [[cublocapp:index#MOACON 어플리케이션 노트|MOACON 어플리케이션 노트]]