nothing9099
Newbie level 4
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?
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;
}
}