我使用MSP430FR2033的I2C读取气压传感器HP203B的气压值,在读取前进入LPM3的话功耗为4uA,在读取后进入的话为640uA,我量了这两种情况的气压传感器各引脚状态,都是一样的,而且我用完H气压传感器后已经断电了。下面是我的I2C代码和HP203B代码。
/* * i2c_hw.c * * Created on: 2017年8月7日 * Author: johnsonzhou */ #include "i2c_hw.h" unsigned char *tx_data; unsigned char *rx_data; int tx_byte; int TXByteCtr; int rx_byte; int RXByteCtr; void i2c_Write(unsigned char addr, unsigned char *pstr, int num) { tx_data = pstr; tx_byte = num; __delay_cycles(1000); // Delay between transmissions UCB0I2CSA = addr; // configure slave address TXByteCtr = 0; // Load TX byte counter while (UCB0CTLW0 & UCTXSTP); // Ensure stop condition got sent UCB0CTLW0 |= UCTR | UCTXSTT; // I2C TX, start condition __bis_SR_register(LPM0_bits | GIE); // Enter LPM0 w/ interrupts } void i2c_read(unsigned char addr, unsigned char *pstr, int num) { rx_data = pstr; rx_byte = num; __delay_cycles(1000); // Delay between transmissions UCB0I2CSA = addr; // configure slave address RXByteCtr = 0; // Load TX byte counter while (UCB0CTLW0 & UCTXSTP); // Ensure stop condition got sent UCB0CTLW0 &= ~UCTR; // I2C RX UCB0CTLW0 |= UCTXSTT; // start condition __bis_SR_register(LPM0_bits | GIE); // Enter LPM0 w/ interrupts } void i2c_init(void) { // Configure USCI_B0 for I2C mode UCB0CTLW0 = UCSWRST; // put eUSCI_B in reset state UCB0CTLW0 |= UCMODE_3 | UCMST | UCSSEL__SMCLK; // I2C master mode, SMCLK UCB0BRW = 0x50; // baudrate = SMCLK / 80 UCB0CTLW0 &= ~UCSWRST; // clear reset register UCB0IE |= UCTXIE0 | UCRXIE0 | UCNACKIE; // transmit and NACK interrupt enable } #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector = USCI_B0_VECTOR __interrupt void USCI_B0_ISR(void) #elif defined(__GNUC__) void __attribute__ ((interrupt(USCI_B0_VECTOR))) USCI_B0_ISR (void) #else #error Compiler not supported! #endif { switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG)) { case USCI_NONE: break; // Vector 0: No interrupts case USCI_I2C_UCALIFG: break; // Vector 2: ALIFG case USCI_I2C_UCNACKIFG: // Vector 4: NACKIFG UCB0CTLW0 |= UCTXSTT; // resend start if NACK break; case USCI_I2C_UCSTTIFG: break; // Vector 6: STTIFG case USCI_I2C_UCSTPIFG: break; // Vector 8: STPIFG case USCI_I2C_UCRXIFG3: break; // Vector 10: RXIFG3 case USCI_I2C_UCTXIFG3: break; // Vector 12: TXIFG3 case USCI_I2C_UCRXIFG2: break; // Vector 14: RXIFG2 case USCI_I2C_UCTXIFG2: break; // Vector 16: TXIFG2 case USCI_I2C_UCRXIFG1: break; // Vector 18: RXIFG1 case USCI_I2C_UCTXIFG1: break; // Vector 20: TXIFG1 case USCI_I2C_UCRXIFG0: // Vector 22: RXIFG0 if ((RXByteCtr >= 0) && (RXByteCtr < rx_byte)) // Check TX byte counter { *rx_data++ = UCB0RXBUF; // Read RX buffer RXByteCtr++; // Decrement RX byte counter } else { UCB0CTLW0 |= UCTXSTP; // I2C stop condition UCB0IFG &= ~UCRXIFG; // Clear USCI_B0 TX int flag __bic_SR_register_on_exit(LPM0_bits); // Exit LPM0 } break; case USCI_I2C_UCTXIFG0: // Vector 24: TXIFG0 if ((TXByteCtr >= 0) & (TXByteCtr < tx_byte)) // Check TX byte counter { UCB0TXBUF = *tx_data++; // Load TX buffer TXByteCtr++; // Decrement TX byte counter } else { UCB0CTLW0 |= UCTXSTP; // I2C stop condition UCB0IFG &= ~UCTXIFG; // Clear USCI_B0 TX int flag __bic_SR_register_on_exit(LPM0_bits); // Exit LPM0 } break; default: break; } }
/* * sht3x.c * * Created on: 2017年8月7日 * Author: johnsonzhou */ #include "hp203b.h" #include "timer_delay.h" struct adc_cvt_struct adc_cvt = { ADC_CVT_H, OSR_1024, 0, }; struct hp203b_struct hp203b = { 0x76, 0x6, ((0x2 << 5) | (0x2 << 2) | 0x0), //OSR=1024,Temperature and Pressure Channels 0x10, //read Pressure and Temperature value 0x30, //read Pressure value 0x32, //read Temperature value {0, 0, 0}, {0, 0, 0}, 0, 0, }; unsigned char hp203b_cmd[3]; unsigned char hp203b_data[6]; unsigned char hp203b_press_data[3]; unsigned char hp203b_temp_data[3]; void hp203b_off(void) { P3OUT &= ~BIT2; // off sensor power } void hp203b_on(void) { P3OUT |= BIT2; // on sensor power } void hp203b_rst(void) { hp203b_cmd[0] = hp203b.soft_rst_cmd; i2c_Write(hp203b.Address, hp203b_cmd, 1); } void hp203b_adc_cvt(void) { hp203b.osr_ch_cmd = (adc_cvt.h << 5) | (adc_cvt.osr << 2) | (adc_cvt.chnl); hp203b_cmd[0] = hp203b.osr_ch_cmd; i2c_Write(hp203b.Address, hp203b_cmd, 1); } void hp203b_read_pt(void) { hp203b_cmd[0] = hp203b.read_pt_cmd; i2c_Write(hp203b.Address, hp203b_cmd, 1); i2c_read(hp203b.Address, hp203b_data, 6); } void hp203b_read_p(void) { hp203b_cmd[0] = hp203b.read_p_cmd; i2c_Write(hp203b.Address, hp203b_cmd, 1); i2c_read(hp203b.Address, hp203b_press_data, 3); } void hp203b_read_t(void) { hp203b_cmd[0] = hp203b.read_t_cmd; i2c_Write(hp203b.Address, hp203b_cmd, 1); i2c_read(hp203b.Address, hp203b_temp_data, 3); } void get_press_temp(void) { adc_cvt.chnl = CHNL_PT; hp203b_on(); delay_hw_ms(10); hp203b_rst(); delay_hw_ms(10); hp203b_adc_cvt(); delay_hw_ms(100); hp203b_read_pt(); hp203b.temp = ((hp203b_data[0] << 16) | (hp203b_data[1] << 8) | (hp203b_data[2] << 0)) & 0xfffff; hp203b.temp = (hp203b.temp + 50) / 100; hp203b.press = ((hp203b_data[3] << 16) | (hp203b_data[4] << 8) | (hp203b_data[5] << 0)) & 0xfffff; hp203b.press = (hp203b.press + 50) / 100; } void get_press(void) { adc_cvt.chnl = CHNL_PT; hp203b_on(); delay_hw_ms(10); hp203b_rst(); delay_hw_ms(10); hp203b_adc_cvt(); delay_hw_ms(100); hp203b_read_p(); hp203b.press = ((hp203b_press_data[0] << 16) | (hp203b_press_data[1] << 8) | (hp203b_press_data[2] << 0)) & 0xfffff; hp203b.press = (hp203b.press + 50) / 100; } void get_temp(void) { adc_cvt.chnl = CHNL_T; hp203b_on(); delay_hw_ms(10); hp203b_rst(); delay_hw_ms(10); hp203b_adc_cvt(); delay_hw_ms(100); hp203b_read_t(); hp203b.temp = ((hp203b_temp_data[0] << 16) | (hp203b_temp_data[1] << 8) | (hp203b_temp_data[2] << 0)) & 0xfffff; hp203b.temp = (hp203b.temp + 50) / 100; } void hp203b_init(void) { get_press_temp(); //hp203b_rst(); delay_hw_ms(10); hp203b_off(); }