// /|\ /|\ // MSP430F261x/241x 10k 10k // master | | // ----------------- -------- | | --------------------- // -|XIN P3.1/UCB0SDA|<-|-------- --> |SDA | // | | | | | // -|XOUT | | | BH1750fvi | // | P3.2/UCB0SCL |<-----------> | SCL | // | | | ------------------ | //
上面是传感器与单片机的连接方式,只画了串行时钟与数据引脚,下面是用TI官网例程改的。最后出的数据不正确,就算不接传感器也有数据,而且总是8080.哪位大侠帮我看一下,不胜感激。
#include "msp430x26x.h"
//传感器的操作命令 #define POWER 0X01 //SHANG DIAN #define RESET 0X07 //CHONG ZHI #define CHMODE 0X10 //连续H分辨率
unsigned char RXData;
void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT P3SEL |= 0x06; // Assign I2C pins to USCI_B0 UCB0CTL1 |= UCSWRST; // Enable SW reset UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz UCB0BR1 = 0; UCB0I2CSA = 0x5c; // Slave Address is 05ch UCB0TXBUF=POWER; UCB0RXBUFCHMODE; UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation IE2 |= UCB0RXIE; // Enable RX interrupt RXCompare = 0x0; // Used to check incoming data
while (1) { while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent UCB0CTL1 |= UCTXSTT; // I2C start condition while (UCB0CTL1 & UCTXSTT); // Start condition sent? UCB0CTL1 |= UCTXSTP; // I2C stop condition __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts } }
// USCI_B0 Data ISR #pragma vector = USCIAB0TX_VECTOR __interrupt void USCIAB0TX_ISR(void) { RXData = UCB0RXBUF; // Get RX data __bic_SR_register_on_exit(CPUOFF); // Exit LPM0 }
|