工具与软件:
大家好!
我一直在研究这个。 长话短说、我是在 MSP430FR5969上教自己的 I2C。 我制造了以下电路、其中包含一个 Arduino UNO 和一个 MSP430FR5969 Launchpad。 将 LaunchPad 上的 P1.6 (SDA)和 P1.7 (SCL)连接到 Arduino UNO 上的 SDA 和 SCL 引脚。 我还将 Launchpad 设置为主发送器、将 Arduino Uno 设置为从接收器。 我还通过两个4.7k Ω 电阻将 SDA 和 SCL 连接到5V 电压轨。
我注意到的是、Launchpad 最初会通过 I2C 进行通信、因为我可以看到 Arduino Uno 通过串行监视器接收的字符。 但在第二次操作中、Launchpad 将不会运行 ISR。 我添加了一些断点、并注意到 UCSCLLOW 不会改变。 它保持为0。 但当我复位 Arduino UNO 并再次使用断点运行 Launchpad 时、在我将 UCTXSTT 设为 UCB0CTLW0寄存器后、UCSCLLOW 将立即更改为1。
我真的很困惑。 非常感谢您提供一些帮助。 我附上了下面的两个代码。 Arduino UNO 的地址设置为4。
#include <msp430.h>
#include <stdbool.h>
#include <stdint.h>
static inline
void launchpad_clk_config(void) {
// The SMCLK will be used for both the SPI communication and the timers.
// Due to the FRAM, we need to add in a wait condition so that higher frequencies can be used.
CSCTL0_H = CSKEY >> 8; // Unlock CS registers for modification.
CSCTL1 = ~DCORSEL & ~DCOFSEL_2; // Set DCO to 1 MHz
CSCTL2 = SELA__VLOCLK | SELS__DCOCLK | SELM__DCOCLK; //
CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // Set all dividers
CSCTL0_H = 0; // Lock CS registers back.
}
#define LP_SDA BIT6
#define LP_SCL BIT7
static inline
void launchpad_env_config(void) {
P1SEL1 |= LP_SDA;
P1SEL0 &= ~LP_SDA;
P1SEL1 |= LP_SCL;
P1SEL0 &= ~LP_SCL;
UCB0CTL1 |= UCSWRST;
UCB0CTLW0 |= UCMODE_3 + UCSYNC + UCMST + UCSSEL_2; // Using the SMCLK, and transmit.
UCB0BRW = 10; // SMCLK will generate a clock of 12 MHz, which will be divided by 30 to yield 400 KHz
UCB0CTLW1 = UCASTP_2; // automatic STOP assertion
UCB0CTL1 &= ~UCSWRST; // eUSCI_B in operational state
UCB0IFG = 0;
}
static inline
void launchpad_env_write(const char addr, const char bytes) {
UCB0CTL1 |= UCSWRST;
UCB0TBCNT = bytes;
UCB0I2CSA = addr;
UCB0CTL1 &= ~UCSWRST;
UCB0CTLW0 |= UCTR;
UCB0IE |= UCTXIE0 + UCSTTIE + UCNACKIE;
UCB0CTLW0 |= UCTXSTT;
}
char num = '9';
void main(void) {
WDTCTL = WDTPW | WDTHOLD;
PM5CTL0 &= ~LOCKLPM5;
launchpad_clk_config();
launchpad_env_config();
__enable_interrupt();
launchpad_env_write(4, 1);
launchpad_env_write(4, 1);
}
#pragma vector=USCI_B0_VECTOR
__interrupt void USCI_B0_ISR(void) {
switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG)) {
case USCI_NONE:
__no_operation();
break;
case USCI_I2C_UCNACKIFG:
__no_operation();
break;
case USCI_I2C_UCTXIFG0:
UCB0TXBUF = num++;
break;
case USCI_I2C_UCRXIFG0:
__no_operation();
break;
default:
break;
}
return;
}
'
// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(1);
}
void receiveEvent(int howMany) {
while (Wire.available()) {
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
}