Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

MSP430 I2C communication

Status
Not open for further replies.

nothing9099

Newbie level 4
Joined
Oct 25, 2017
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
78
This is a very simple coding question as I'm very new to programming. I'm just testing communicating two MSP430s using I2C and I need to simply blink an LED when the slave receives the transmitted data.

The code for the slave is shown below. In the ISR, in case 10, I put P1OUT ^= BIT0;
Should this not toggle the LED every time a byte is received? When I run this code, the LED will just toggle once and that it all.

This code was given to me, I didn't write it, I just have this task to do. I'm sure I understand the operation of the code, but I'm just not writing it properly to blink the LED. Can someone show me what I should be writing to blink the LED?


Code:
#include "msp430.h"

unsigned char *PRxData;                     // Pointer to RX data
unsigned char RXByteCtr;
volatile unsigned char RxBuffer[128];       // Allocate 128 byte of RAM

void main(void)
{
    P1SEL &= ~BIT0;
    P1DIR |= BIT0;

  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

  P3SEL |= 0x06;                            // Assign I2C pins to USCI_B0
  UCB0CTL1 |= UCSWRST;                      // Enable SW reset
  UCB0CTL0 = UCMODE_3 + UCSYNC;             // I2C Slave, synchronous mode
  UCB0I2COA = 0x48;                         // Own Address is set to 048h
  UCB0CTL1 &= ~UCSWRST;                     // Clear SW reset, resume operation
  UCB0IE |= UCSTPIE + UCSTTIE + UCRXIE;     // Enable STT, STP & RX interrupt

  while (1)
  {
    PRxData = (unsigned char *)RxBuffer;    // Start of RX buffer
    RXByteCtr = 0;                          // Clear RX byte count
    __bis_SR_register(LPM0_bits + GIE);     // Enter LPM0, enable interrupts
                                            // Remain in LPM0 until master
    __no_operation();                       // Set breakpoint >>here<< and read
  }                                        // read out the RxBuffer
}

//------------------------------------------------------------------------------
// The USCI_B0 data ISR RX vector is used to move received data from the I2C
// master to the MSP430 memory.
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// The USCI_B0 state ISR TX vector is used to wake up the CPU from LPM0 in order
// to process the received data in the main program. LPM0 is only exit in case
// of a (re-)start or stop condition when actual data was received.
//------------------------------------------------------------------------------
#pragma vector = USCI_B0_VECTOR
__interrupt void USCI_B0_ISR(void)
{
  switch(__even_in_range(UCB0IV,12))
  {
  case  0: break;                           // Vector  0: No interrupts
  case  2: break;                           // Vector  2: ALIFG
  case  4: break;                           // Vector  4: NACKIFG
  case  6:                                  // Vector  6: STTIFG
    UCB0IFG &= ~UCSTTIFG;
    break;
  case  8:                                  // Vector  8: STPIFG
    UCB0IFG &= ~UCSTPIFG;
    if (RXByteCtr)                          // Check RX byte counter
        __bic_SR_register_on_exit(LPM0_bits); // keep CPU active after the ISR
    break;
  case 10:                                  // Vector 10: RXIFG
     P1OUT ^= BIT0;
   *PRxData++ = UCB0RXBUF;                 // Get RX'd byte into buffer
    RXByteCtr++;
    break;
  case 12: break;                           // Vector 12: TXIFG
  default: break;
  }
}
 

I'm not familiar with the MSP430, but have you tried running a debugger on this code (if one exists)? Or a simulator? Can you actually read the output port (P1OUT). You might need to use a 'shadow register' to keep track of the state of P1OUT.

e.g.

Code:
P1out_shadow ^= BIT0;
P1OUT= P1OUT_shadow;
 
Last edited:

i've tested the code and the LED will toggle once, but it won't toggle every time a transmission is received as i want it to.

theoretically should that code work? have i written it properly?
 

i've tested the code and the LED will toggle once, but it won't toggle every time a transmission is received as i want it to.

theoretically should that code work? have i written it properly?

If you keep asking the same question, you're going to get the same answer.

DID YOU USE A DEBUGGER OR A SIMULATOR?
IS IT VALID TO READ AN OUPUT AS AN INPUT (as you are doing)?
DID YOU TRY WHAT I SUGGESTED ABOUT USING SHADOW REGISTER?
 

For a long time I have not used MSP430, but as far as I can remember it does not even need an external debugger, since it has J-tag, besides a powerful tool, C-spy with which you can monitor the real value of the variables of the microcontroller, so you can easily get some answers to what you are looking for in the circuit itself.
 

i'm asked to same question because it wasn't answer, sorry.

i don't have the equipment right now to debug or use a simulator, but i'm asking you just by looking at the code, does it look written properly. i'm really very very new to programming so I don't understand if i'm even writing it properly. so thats my question.
 

You don't need any extra equipment, the same tool you're using to download code into microcontroller (supposedly based on JTAG) is all you need, and C-spy is already integrated with the IDE. The fact is: At first sight your code does not seem to have any errors, but people can overlook some trivial thing, and it would expected you make experiments in order to elliminate possible causes of the problem.

I'm assuming you're using IAR, otherwise forget about the tool tips.
 

Hello!

i'm asked to same question because it wasn't answer, sorry.

A kind of chicken and egg problem. If you are asked a question, it's usually because
it would help answering you. If you disregard this question and ask the same one, then
you il enter in an infinite loop...

i don't have the equipment right now to debug or use a simulator, but i'm asking you just by
looking at the code, does it look written properly. i'm really very very new to programming so
I don't understand if i'm even writing it properly. so thats my question.

There are 2 kinds of engineers: those who read the docs first, and those who program first.
I belong to the second category, and therefore I would advice to do exactly as I would: find
some sample code from TI, which is known to work. Verify first that it works, then when it does,
add your LED code somewhere and test.

Dora.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top