picspalsandgals

University of Washington Electrical Engineering Blog

Radio time!

 

The last two posts were really just getting up to speed on how the MSP is coded.  Now its time to get it to do something interesting!  We’re going to use the MSP’s radio to control the onboard LED.  This application is loaded on to two separate MSPs and once they’re powered up pressing the button on one of the MSPs will toggle its own red LED and the other’s green LED.   Let’s take a look at the code.

 

#include “mrfi.h”
int main(void)
{
BSP_Init();                  //Disable WD, Set MCLK to 8MHz, set LED to out, and BTN to inttrupt
P2DIR |= 0x08;                    //Set Pin six as out
P1REN |= 0x04;                  //Enable BTN resistor
P1IE |= 0x04;                      //Enable interrupts on button press
MRFI_Init();                         //Enable radio
MRFI_WakeUp();                //Power up radio, connect 26Mhz Oscilator
MRFI_RxOn();                    //Enter Rx mode
__bis_SR_register(GIE+LPM4_bits);   //Enable global interrupts and enter low power mode 4
}
void MRFI_RxCompleteISR()             //
{
P1OUT ^= 0x02;                      //Toggle Recivers Green LED
}
#pragma vector=PORT1_VECTOR           //Declaration of function to call on interrupt
__interrupt void Port_1 (void)
{
P1IFG &= ~0x04;                     //Dismiss interrupt flag

P1OUT ^= 0x01;                      //Togle Sender Red LED
mrfiPacket_t packet;                //Create a message packet
packet.frame[0]=8+20;
MRFI_Transmit(&packet, MRFI_TX_TYPE_FORCED);
}

 

Again the code is pretty messy, so I’ve done my best to comment it.  In main we see the same code as previously discussed to enable the LEDs, pull up resistors, and interrupts.  We also see some new stuff like the BSP and MRFI commands.  These are just start up methods to set up the radio.  The important sections here are the “__interrupt void Port_1 (void)” and the “void MRFI_RxCompleteISR()”.  The Port_1(void) method is run every time we get an interrupt from a button press.  This method first dismisses the interrupt flag and toggles the green LED on board.  Next it puts together a bunch of packet information and transmits it to the paired MSP.  This is where the RxCompleteISR() method comes in.  When ever an MSP receives a packet this method is called, and in this case toggles the green LED.

The thing that I really like about this code is the symmetry.  The exact same code is loaded on to two separate MSPs, and the interrupts deal with making sure the correct action happens on each board.

Leave a comment