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.

I2C Receiving data.

Other Parts Discussed in Thread: MSP430F2252

Hi guys, 

 

I am trying to transmitt a data to slave address and trying to read it back. I can see the value in AC1 when i step through but not when I run the program and set a break point 

 

 

#define true 1

#include "msp430f2252.h"

volatile unsigned char receive = 0;

volatile unsigned char ready = 0;

volatile short ac1 = 0;

 

void main(void)

{

WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

 

BCSCTL1 = CALBC1_1MHZ;

DCOCTL = CALDCO_1MHZ;

 

// Initialise i2c

P3SEL |= 0x06;                             // Assign I2C pins to USCI_B0

UCB0CTL1 |= UCSWRST;                       // Enable SW reset

UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;     // I2C Master, synchronous mode

   UCB0CTL1 = UCSSEL_2 + UCSWRST;             // Use SMCLK, keep SW reset

   UCB0BR0 = 6;                             // fSCL = SMCLK/12 = ~100kHz

   UCB0BR1 = 0;

IE2 |= UCB0RXIE;                           // Enable RX interrupt

__delay_cycles(5);

 

    __bis_SR_register(CPUOFF + GIE);        // Enter LPM0 w/ interrupts

 

 

   // Set the slave address

   UCB0I2CSA = 0x77; //01110111

   UCB0CTL1 &= ~UCSWRST;  // software reset disable. 

 

   // Start transmit condition

   UCB0CTL1 |= UCTR; // Enable Transmissoin mode

   UCB0CTL1 |= UCTXSTT; // generating start condition by setting UCTXSTT

 

   // Reading the calibration data

   while(!(IFG2 & UCB0TXIFG)); //   USCI_B0 RX buffer ready?

   UCB0TXBUF =  0xAC; // Transmit address to buffer

 

   // Send restart condition

   UCB0CTL1 &= ~UCTR; //Disable Transmission Mode

   UCB0CTL1 |= UCTXSTT; // gererate start condition 

 

   // Should receive two bytes from the slave

   //while(!(IFG2 & UCB0RXIFG));

ac1 = (short) (UCB0RXBUF << 8); //  16-bit 

 

//UCB0CTL1 |= UCTXSTP; 

 

//while(!(IFG2 & UCB0RXIFG));

ac1 = ac1 | UCB0RXBUF; 

 

// Respond with a NACK after receiving second byte

UCB0CTL1 |= UCTXSTP; // generate stop condition after the next acknowledge from the slave

 

I have a break Point here.  I shout get ac1 value in my watch window, when I run my program but I do not get anything. Can anyone suggest why I am unable to do that. If this is possible through interrupts please advise. Thanks. 

 

I am using Code Composer studio. Any help would be appreciatable. Thanks. 

  • Muhammad Imran Khan said:

    UCB0CTL1 |= UCTXSTT; // gererate start condition
       // Should receive two bytes from the slave
       //while(!(IFG2 & UCB0RXIFG));
    ac1 = (short) (UCB0RXBUF << 8); //  16-bit

    teh reason is the timing.
    Aboe you tell the USCI to send a start condition, transmit the slave address, wait for acknowledgement and receive a byte. Best case, this requires 18 UCLK cycles. Yet only two or three MCLK cycles you try to read the result. At this moment, the USCi has not even sent the start condition, let alone sent the slave address, waited for acknowledgement and then read a value.
    When single-stepping, the CPU is halted for each step, but the USCI hardware continues working. So between two manually triggered CPU steps, teh USCI might have transmitted and received a whole newspaper - your desired answer had moe than enoguh time to come in.

    However, if for some reason the slave does not resopond, waiting for RXIFG results in an endless loop.

    The proper sequence is:
    Set UCTXSTT
    Wait for UCTXSTT clear again (then the start conditionand slave address haven been sent and the slave has either responded or not)
    Check UCNACKIFG (if it is set, the slave didn't respond, so exit here)
    Wait for UXRXIFG. Once it is set, one byte has been received. If UCNACKIFG was set, this will never happen
    Read UCRXBUF
    Wait again for UCRXIFG.
    Since this is the last byte you want to receive, set UCTXSTP now. The USCI will transmit a NACK to the slave, then a stop condition, and receive no more bytes (alternatively, you can set UCTXSTT if you want to append another transfer
    Read UCRXBUF
    To be sure the transfer has ended, wait for UCTXSTP clear again (to avoid that the USCI is still busy when you try to start another transfer)

     

     

**Attention** This is a public forum