- //TWI通用操作
- #define Wait() while(!(TWCR & _BV(TWINT)))
- #define START() {TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);Wait();}
- #define STOP() {TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);}
- #define WriteByte(x) {TWDR =(x);TWCR = _BV(TWINT) | _BV(TWEN);Wait();}
- #define ACK() (TWCR |= _BV(TWEA))
- #define NACK() (TWCR &= ~_BV(TWEA))
- #define TWI() {TWCR = _BV(TWINT) | _BV(TWEN) ;Wait();}
- unsigned char get_lm75(void)
- {
- init_twi();
- START(); if(TW_STATUS != TW_START) return 2;
- WriteByte(0x92 ); if(TW_STATUS !=TW_MT_SLA_ACK) return 3;
- WriteByte(0x00 ); if(TW_STATUS != TW_MT_DATA_ACK) return 4;
- START(); if(TW_STATUS != TW_REP_START) return 5;
- WriteByte(0x93); if(TW_STATUS != TW_MR_SLA_ACK) return 6;
- TWI(); // if(TW_STATUS != TW_MR_DATA_ACK) return 7;
- lm75_tem_h = TWDR;
- TWI(); // if(TW_STATUS != TW_MR_DATA_NACK)return 8;
- lm75_tem_l = TWDR;
- STOP();
- return 1;
- }
[color=rgb(51, 102, 153) !important]复制代码
1:实测准确,我用DHT11和他对比测试,一样。对了,我就是纳闷,17和19行为什么必须注释才能过的去,否则不给你返回值。我不知道哪里错了。
2:这东西经常比实际温度低几摄氏度,不知道为何。但这次很准确。主板自己设计的,怀疑主板帮他散温?但室内温度30摄氏度 |
|