工具与软件:
大家好、
我正在尝试在基于 I2C 的传感器和 MSP430开发板之间建立通信。 请查看下面的代码。 传感器不会发送 ACK。 我尚未使用默认1MHz 更改系统的时钟。 并且 I2C 通信速度为100KHz。
如图所示、我已将4.7k Ω 上拉电阻连接到 I2C 线路。 

#include "driverlib.h"
#include <msp430.h>
#include "inc/hw_memmap.h"
#include "uart_debug.h"
#define BAUD_RATE 115200
//******************************************************************************
//!
//! Empty Project that includes driverlib
//!
//******************************************************************************
//******************************************************************************
//!
//! Local Function Prototypes
//!
//******************************************************************************
static void watchdog_stop (void);
static void init_i2c(void);
static void check_i2c_slave(void);
//******************************************************************************
//!
//! Local Variables
//!
//******************************************************************************
int main (void)
{
watchdog_stop();
init_debug_uart();
init_i2c();
//check_i2c_slave();
while(1)
{
debug_print("Debugging Working\n\r");
check_i2c_slave();
__delay_cycles(3000000); //3sn
}
}
//******************************************************************************
//!
//! Local Function Definitions
//!
//******************************************************************************
static void watchdog_stop (void)
{
WDTCTL = WDTPW + WDTHOLD;
}
static void init_i2c(void)
{
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P4, GPIO_PIN2 | GPIO_PIN1);//P4.1 SDA, P4.2 SCL
UCB1CTL1 |= UCSWRST; // Enable SW reset
UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC; // Master, I2C, Synchronous mode
UCB1CTL1 = UCSSEL_2 + UCSWRST + UCTR; // Use SMCLK, keep SW reset
UCB1BR0 = 10; // set prescaler, since SMCLK is 1MHz, /10 will give 100KHz
UCB1BR1 = 0;
UCB1I2CSA = 0x74; // Set slave address
UCB1CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
while (UCB1STAT & UCBBUSY); // Wait for the I2C Bus to get free.
}
static void check_i2c_slave (void)
{
// Send start condition (this happens when the first byte is sent)
UCB1CTL1 |= UCTR + UCTXSTT; // Transmit mode, send start bit
while (UCB1CTL1 & UCTXSTT); // Wait for start bit to be transmitted
debug_print("Start Bit Transmission Complete\r\n");
// Now, you can send data or address
//UCB1TXBUF = (0x74) << 1; // Send slave address with write operation
// Wait for data transmission to complete
while (UCB1CTL1 & UCTXSTT); // Wait for start condition to be sent
debug_print("Data Transmission Complete\r\n");
// For now, let's just stop the transmission.
UCB1CTL1 |= UCTXSTP; // Send stop condition
// Wait for stop condition
while (UCB1CTL1 & UCTXSTP);
debug_print("Stop Bit Transmission Complete\r\n");
}