Мне нужно связаться с датчиком через I2 C. Датчик MMA 7455: https://www.nxp.com/docs/en/data-sheet/MMA7455L.pdf. Мне нужно прочитать регистры: $ 06, $ 07 и $ 08. Я не уверен, почему это не работает. Я вижу на линии последовательной передачи данных высокий сигнал с падением до 0 каждые 25 мс. На линии последовательной синхронизации инвертируется. У меня есть нагрузочные резисторы 10 кОм, подключенные к линиям SDA и SCL.
// Include's
//__________________________________________________________________
#include <msp430.h>
//__________________________________________________________________
// Define's & Variable
//__________________________________________________________________
typedef enum {MASTER,SLAVE} i2cMode;
typedef enum {SEVEN,TEN} AddresLength;
typedef enum {NSTART,START} Start;
#define TxBuffer UCB0TXBUF
#define RxBuffer UCB0RXBUF
unsigned int TxRegister;
unsigned int RxData;
unsigned int TxData;
//__________________________________________________________________
// Prototype's
//__________________________________________________________________
void i2c_init(unsigned char i2cmode,unsigned char addresLength); // Initializer I2C
void i2c_User_Addres(unsigned int adr); // Make a address for this µc
void i2c_Slave_Addres(unsigned int adr); // Address for the slave
void i2c_start_codon(void); // Just sending a start codon
void i2c_rx_start(void); // Receive mode with start codon
void i2c_rx(unsigned int start); // Receive mode without start codon
void i2c_tx_start(void); // Transmit mode with start codon
void i2c_tx(void); // Transmit mode without start codon
void i2c_Stop(void); // Stop codon
void i2c_NACK(void); // Not acknowlegde
void ReadAcellerometer(void); // Accellerometer reading
//__________________________________________________________________
// main.c
//__________________________________________________________________
int main(void)
{
BCSCTL1 = CALBC1_16MHZ; // Set DCO range
DCOCTL = CALDCO_16MHZ; // Set DCO step and modulation
i2c_init(MASTER, SEVEN); // Setup this µc for I2C
i2c_User_Addres(0x08); // Give this µc a I2C Addres
i2c_Slave_Addres(0x1D); // Slave addres or coversation
ReadAcellerometer();
return 0;
}
//__________________________________________________________________
// Functie's
//__________________________________________________________________
void ReadAcellerometer(void)
{
TxBuffer = 0x18; // Fill TX informatie
i2c_tx_start();
i2c_rx(START);
i2c_start_codon();
i2c_NACK();
i2c_Stop();
}
//__________________________________________________________________
// Functie's I2C
//__________________________________________________________________
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
if (~UCTR) // Recieve?
{
UCB0CTL1 |= UCTXSTP; // I2C stop condition
RxData = RxBuffer; // Get RX data
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
else if (UCTR) // Transmit?
{
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
}
//---------I2C INICIALIZATION----------------------
void i2c_init(unsigned char i2cmode, unsigned char addresLength )
{
P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0
P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = (UCA10 << addresLength) + (UCSLA10 << addresLength) + (UCMST<<i2cmode) + (UCMODE_3<< i2cmode )+ UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
if (~(i2cmode))
{
UCA0MCTL |= UCBRS_7; // CLK wave modulation see table 15-4 BRCLK = 1MHz; SCL = 100kHz;
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
}
IE2 |= UCB0RXIE + UCB0TXIE; // Enable RX en TX interrupt
}
//----------User's Addres----------
void i2c_User_Addres(unsigned int adr)
{
UCB0I2COA = (1<<adr); // Address for this µc
}
//----------CHANGE OF ADDRESS--------------
void i2c_Slave_Addres(unsigned int adr)
{
UCB0I2CSA = adr; // Address for the slave µc
}
//----------RECEIVE DATA WITH START CODON----------
void i2c_rx_start(void)
{
while (UCB0CTL1 & UCTXSTP); // Ensure stop codon got sent
UCB0CTL1 &= ~UCTR; // Receive Mode
UCB0CTL1 |= UCTXSTT; // Send start codon
while (UCB0CTL1 & UCTXSTT); // Start codon sent?
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
}
//----------RECEIVE DATA----------
void i2c_rx(unsigned int start)
{
UCB0CTL1 &= ~UCTR; // Receive Mode
if (start) UCB0CTL1 |= UCTXSTT; // Send start codon
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
}
//----------SEND DATA------
void i2c_tx(void)
{
UCB0CTL1 |= UCTR; // Transmit mode
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
}
//----------SEND DATA WITH START CODON------
void i2c_tx_start(void)
{
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTR; // Transmit mode
UCB0CTL1 |= UCTXSTT; // I2C start codon
__bis_SR_register(CPUOFF + GIE);
}
//---------------STOP-------------------------
void i2c_start_codon(void)
{
UCB0CTL1 |= UCTXSTT; // I2C start codon
}
//---------------STOP-------------------------
void i2c_Stop(void)
{
while((UCB0STAT & BUSY)!= 0); // Wait to finish the byte
UCB0CTL1 |= UCTXSTP; // I2C Stop codon
while(UCB0CTL1 & UCTXSTP); // Ensure the stop Condon is send
}
//---------------NACK---------------
void i2c_NACK(void)
{
UCB0CTL1 |= UCTXNACK; // Not Acknowlegded
}
//__________________________________________________________________
// End
//__________________________________________________________________