主题中讨论的其他器件: MSP430WARE
可能涉及的人员、
我尝试使用 MSP430BT5190上的 I2C 功能、由于某种原因、它无法正常工作。
我尝试使用 Arduino 读取 I2C 线路和示波器、但 P1.0上的 LED 确实会打开和关闭、但我不会通过 SDA 或 SCL 传输任何内容。
我重复使用 MSP430F5418AIPNR 上用于 I2C 初始化和发送的代码。
我通过每个寄存器来确保这个迭代和最后一个迭代具有相同的 I2C 初始化和标志值。
我不确定在哪里取得我的成果、如果有任何帮助、我将不胜感激。
因此、
Andres Choy Buentello
#include <msp430.h>
#include "stdbool.h"
#define i2c_address 0x28
/**
* blink.c
*/
unsigned char TxByteCtr;
unsigned char RxByteCtr;
int sendCounter;
_Bool UCB0_Done;
unsigned char *PTxData;
unsigned char i2c_info;
void i2c_send(unsigned char slave_address, unsigned char *DataBuffer, unsigned char ByteCtr)
{
UCB0I2CSA = slave_address;
UCB0IFG &= ~(UCTXIFG + UCRXIFG); // Clear any pending interrupts
UCB0IE &= ~UCRXIE; // Disable RX interrupt
UCB0IE |= UCTXIE; // Enable TX interrupt
PTxData = DataBuffer;
TxByteCtr = ByteCtr;
sendCounter= 0;
UCB0_Done = false;
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
// wait for USB to finish
while(!UCB0_Done)
{ asm(" nop"); }
asm(" nop");
}
void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
P1DIR |= 0x01; // configure P1.0 as output
//Timer Specific lines
TA1CCTL0 = CCIE; // CCR0 interrupt enabled
TA1CCR0 = ((14745600ul/8ul) / 100ul) -1;
TA1CTL = ID__8 + TASSEL__SMCLK + MC_1 + TACLR; // SMCLK, contmode, clear TAR
//I2C Specific Init lines
P3SEL |= BIT1 | BIT2; // P3.0,1 option select
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
// TI example uses 12
UCB0BR0 = 36; // fSCL = SMCLK/10 = ~100kHz with SMCLK = 1MHz
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
UCB0IE |= UCNACKIE; // Enable TX interrup
volatile unsigned int i; // volatile to prevent optimization
int send_cnt = 0;
while(1)
{
// Prepping data to send
if (send_cnt == 0)
{
i2c_info = 0x00;
send_cnt = send_cnt + 1;
} else if (send_cnt == 1){
i2c_info = 0x01;
send_cnt = send_cnt + 1;
} else if (send_cnt == 2){
i2c_info = 0x02;
send_cnt = send_cnt + 1;
} else if (send_cnt == 3){
i2c_info = 0x03;
send_cnt = 0;
}
// Send data
i2c_send(i2c_address, (unsigned char *)i2c_info, 2);
__bis_SR_register(GIE); // Enter LPM3, enable interrupts
__no_operation(); // For debugger
// Visual verify that indo is being sent
P1OUT ^= 0x01; // toggle P1.0
for(i=10000; i>0; i--); // delay
}
}
#pragma vector=USCI_B0_VECTOR
__interrupt void USCIAB0TX_ISR(void) {
switch(__even_in_range(UCB0IV,12))
{
case 0:
break; // Vector 0: No interrupts
case 2:
asm(" nop");
break; // Vector 2: ALIFG
case 4:
asm(" nop");
break; // Vector 4: NACKIFG
case 6: // Vector 6: STTIFG
asm(" nop");
break;
case 8: // Vector 8: STPIFG
asm(" nop");
break;
case 10: // Vector 10: RXIFG
asm(" nop");
break;
case 12: // Vector 12: TXIFG
if (TxByteCtr) { // Check TX byte counter
UCB0TXBUF = *PTxData++; // Load TX buffer
TxByteCtr--; // Decrement TX byte counter
sendCounter++;
} else {
UCB0CTL1 |= UCTXSTP; // I2C stop condition
UCB0IE &= ~UCTXIE;
UCB0_Done = true;
}
break;
default:
asm(" nop");
break;
}
}