This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
您好!
我对 MSP432上的 I2C 通信协议的基本内容有一些疑问。 我正在 CCS 中运行一个用于基本主控模式写入函数的代码。 这是我目前的代码:
main.C
#include <I2CComm.h> #include "msp.h" #include <stdio.h> void delayMs(int t); void main(void){ I2C1_init(); while (1){ I2C1_Write(0x21, 0x14, 0x08); delayMs(1000); } } void delayMs(int t) { int i, j; for (j = 0; j < t; j++) for (i = 750; i > 0; i--); /* Delay */ } I2CComm.c #include <I2CComm.h> #include "msp.h" #include <stdio.h> /* initialize I2C to 100 kHz at 3 MHz system clock */ void I2C1_init() { EUSCI_B0->CTLW0 = 0x0001; // disable UCB1 for configuration EUSCI_B0->CTLW0 = 0x0F81; /* Bit15 : Own Address of 7-bit (0) Bit 14 : Slave Address of 7-bit (0) Bit 13: Single Master Environment (0) Bit 12 : Reserved (0) Bit 11 : Master Mode Select (1) Bit 10-9 : EUSCI Mode (11) <- I2C Mode Bit 8 : Synchronous Mode Enable (1) Bit 7-6 : EUSCI Clock Source (10) Bit 5 : Transmit ACK (0) Bit 4 : Transmitter / Receiver (0) Bit 3 : Transmit NACK (0) Bit 2 : Transmit STOP (0) Bit 1 : Transmit START (0) Bit 0 : Software Reset Enable (1) In hex is represented by "0FC1" P1.7 -> SCL | P1.6 -> SDA*/ EUSCI_B0->BRW = 30; // 3 MHz / 30 = 100 kHz P1->SEL0 |= 0xC0; /* P1.7 SCLK / P1.6 SDA */ P1->SEL1 &= ~0xC0; EUSCI_B0->CTLW0 &= ~0x01; // enable UCB1 after configuration printf("I2C Initialized.\n"); } /* Write One Byte of Data */ void I2C1_Write(int slaveAddr, unsigned char reg, unsigned char data) { EUSCI_B0->I2CSA = slaveAddr; // setup slave address EUSCI_B0->CTLW0 |= 0x0012; // Enable Transmitter and Generate START bit while(!(EUSCI_B0->IFG & 0x02)); // Wait until it is ready to transmit <--- It gets stuck in this while loop EUSCI_B0->TXBUF = reg; // Send Data to slave while(!(EUSCI_B0->IFG & 0x02)); // Wait until it is ready to transmit EUSCI_B0->TXBUF = data; // Send Data to slave while(!(EUSCI_B0->IFG & 0x02)); // Wait until it is ready to transmit EUSCI_B0->CTLW0 |= 0x04; // Generate STOP bit while(EUSCI_B0->CTLW0 & 4); // Wait until STOP bit is sent }
当我运行代码时、它会像代码中所示那样停留在中断标志 while 循环上。 如果我将该行替换为:
while (EUSCI_B0->CTLW0 & 2); //等待直到发送停止位
它还会卡在该特定的 while 环路上。 我将示波器连接到引脚1.6和1.7、当代码运行时、我看不到任何变化(它在~0V 时是平坦的)。 我出了什么问题? 谢谢你
您好 Hugo、
将代码发布到论坛时、请单击"Insert"->"、使用语法格式器 代码"、以便可读。 我已经编辑过您的帖子以反映此更改。
另请查看以下有关 eUSCI 的应用手册、第5节介绍了 I2C。 标题为 MSP430、但 MSP432P4共享相同的 eUSCI 模块。 执行这些调试步骤可以帮助您缩小问题的范围。
I2C 最常见的问题是总线上的 I2C 上拉值不正确。 这也可以解释为什么您只看到0V。