Quantcast
Channel: Microcontrollers
Viewing all articles
Browse latest Browse all 216409

Forum Post: RE: I2C Communication issue between MSP430FR5739 (master) and MSP430G2553 (slave)

$
0
0

Dear all,

First of all thanks for your support and I would say that with your help people can solve many problem that occur when we are develop a new code or programming a new device.

In my concret case, I have problems to make work a I2C code developed for run on msp430FR5739, I am using MSP-EXP430FR5739 FRAM Experimenter Board.

I would like use I2C peripheral to communicate with a sensor. In this case I read the datasheet and in particular USCI_B0 registers. And I wrote a code bit by bit using the USCI_0 registers.

My problem is that I am not receive ack from the slave device. Please help me... my code can be found below.

#include <msp430.h>
#include "stdbool.h"
#include "stdio.h"

unsigned int counter=0;
unsigned int test=0;
unsigned char buffer_RX;
unsigned char buffer_TX [20]={'T','E','S','T','E'};
unsigned char SPACE [] = {0x20,0x20,0x20,0x20,0x20,0x20,0x20};

void system_init()
{
WDTCTL = WDTPW | WDTHOLD;

CSCTL0_H = 0xA5; // Init SMCLK = MCLk = ACLK = 1MHz
CSCTL1 |= DCOFSEL0 + DCOFSEL1; // Set max. DCO setting = 8MHz
CSCTL2 = SELA_3 + SELS_3 + SELM_3; // set ACLK = MCLK = DCO
CSCTL3 = DIVA_3 + DIVS_3 + DIVM_3; // set all dividers to 1MHz
}


/*----------------------------------------------------- UART FUNCTIONS ----------------------------------------------------------*/
void UART_TX(char *buffer_TX,unsigned char index)
{
UCA0TXBUF=0x41; // Load The Buffer With Data To Send And clear The Flag UCTXIFG
while((UCA0IFG & UCTXIFG)!=0); // Wait while data is not complete transmitted
}

void UART_TX_Single_Caracter(char *buffer_TX)
{
unsigned int i;

unsigned int size = strlen(buffer_TX); //get length of data to be sent
for (i = 0; i < size; i++)
{
while(!(UCA0IFG & UCTXIFG)); // Wait while data is not complete transmitted
UCA0TXBUF=buffer_TX[i]; // Load The Buffer With Data To Send And clear The Flag UCTXIFG
}
}

void UART_RX()
{
while(!(UCA0IFG & UCRXIFG)); // Wait while data is not complete received
buffer_RX=UCA0RXBUF; // Buffer to send data to PC
}

void UART_Conf_init()
{
UCA0CTL1 |= UCSWRST;
UCA0CTL1 = UCSSEL_2; // Set SMCLK as UCLk

UCA0BR0 = 52 ; // 9600 baud
UCA0BR1 = 0;
UCA0MCTLW = 0x4911 ;

P2DIR |=BIT0; // UART TX SELECT AS OUTPUT
P2DIR&=~BIT1; // UART RX Pin SELECT AS INPUT

P2SEL1 |= BIT0 + BIT1; // UART Pins function
P2SEL0 &= ~(BIT0 + BIT1); // UART RX and TX Pins Function

UCA0CTL1 &= ~UCSWRST; // START UART or RELEASE FROM RESET STATE
}

/*-------------------------------------------------------------I2C FUNCTIONS-------------------------------------------------------------------*/

void I2C_Config_Master_Mode()
{
P1SEL1 |= BIT6 + BIT7; // Configure I2C pins (device specific

UCB0CTLW0 |= UCSWRST ; //Software reset enabled // put eUSCI_B in reset state before make register configurations
UCB0CTLW0 |= UCSSEL_2 + UCMODE_3 + UCMST;
UCB0BRW = 0x0008; // baud rate = SMCLK / 8

UCB0CTLW1 = UCASTP_2; // autom. STOP assertion
UCB0TBCNT = 0x07; // TX 7 bytes of data

UCB0CTLW0 &=~UCSWRST; // eUSCI_B in operational state
}

unsigned int I2C_Send_Address(unsigned char slave_address)
{
UCB0I2CSA = slave_address; // Buffer For Slave Address
UCB0CTLW0 |= UCTR + UCTXSTT; // Enable Transmitter mode and Start Condition

while((UCB0CTLW0 & UCTXSTT)==1); // Slave address was sent

if (UCB0IFG & UCNACKIFG==1) // Check if (not)Ack was sent
{
return 1;
}
else
{
return 0;
}
}

void I2C_Write_Byte(unsigned char data)
{
if((UCB0IFG & UCTXIFG0)) // Data can be write to the bus ->Or wait for Start Condition sent
UCB0TXBUF = data;
while((UCB0IFG & UCTXIFG0)==0);
}

int I2CMaster_end()
{
UCB0CTLW0 |= UCTXSTP; // set stop condition
while((UCB0CTLW0 & UCTXSTP)!=0); // wait until the end to be sure
return 0;
}

unsigned char I2C_Read(unsigned slv_w, unsigned char slv_r,unsigned device_register)
{
UCB0CTLW0 |= UCTR + UCTXSTT; // Start Condition

UCB0I2CSA = slv_w; // Device Address To Write
UCB0CTLW0 |= UCTR + UCTXSTT; // Device Address To Write

while((UCB0CTLW0 & UCTXSTT)==1);

if(UCB0IFG & UCNACKIFG==1)
{ return 1; }
else { return 0; }

if((UCB0IFG & UCTXIFG0))
UCB0TXBUF = device_register;

UCB0CTLW0 |= UCTR + UCTXSTT;

UCB0TXBUF = device_register;
}

/*
* main.c
*/
int main(void)
{
system_init();

P4DIR &=~(BIT0 +BIT1); // input Pin For Switch S1
P4OUT |= BIT0 +BIT1; // Configure pullup
P4REN |= BIT0 + BIT1; // Enable pullup resistor
P4IN=0x00;


I2C_Config_Master_Mode();

while(1)
{
if((P4IN & BIT0)==0)
{
I2C_Send_Address(0xA6); // ADXL312 address to write
I2C_Write_Byte(0x00); // Register that hold the device ID
I2C_Send_Address(0xA7); //ADXL312 address to read
I2CMaster_end(); // Stop Condition
}
}


return 0;
}


Viewing all articles
Browse latest Browse all 216409

Trending Articles