Upload
duongkiet
View
218
Download
4
Embed Size (px)
Citation preview
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 1
Developing a uMAVRK Wireless Sensing
System with CCS
ChaiYong Lim
March 30th
, 2012
ECE480 Design Team 2
Summary
This application note is intended to guide the reader through the process of using the code
composer studio (CCS) to develop a one to one wireless sensing system on UR-CC430F5137-
MVK module. It includes detail information about how the module functions and how to develop
the wireless sensing system based on TI example code. Figures and Schematics are also included
in this application notes.
Keywords: MAVRK, wireless sensing, MSP430, RF communication, micro-controller, CCS
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 2
Table of Contents
Introduction ................................................................................................................................................... 3
Objective ....................................................................................................................................................... 3
Getting Started .............................................................................................................................................. 4
Create a CCS Project .................................................................................................................................... 4
Modify the RF example code ........................................................................................................................ 6
Program the UR-CC430F5137-MVK modules ............................................................................................ 7
Include the UART function ......................................................................................................................... 8
Include the ADC function ............................................................................................................................. 9
Include the WDT function .......................................................................................................................... 10
Final Integration ......................................................................................................................................... 10
Complete Code............................................................................................................................................ 11
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 3
Introduction
The UR-CC430F5137-MVK contains a 16-bit ultra low power CC430F5137 MCU with a
high performance sub 1 GHZ RF transceiver core. This makes it an excellent choice in wireless
monitoring sensor system, self designed sensor board may be attached on this device. It is
designed to be used in end-product and run independently of the Modular and Versatile
Reference Kit (MAVRK) motherboard but it may be also connected to the motherboard via a
RF-UDEBUG-MVK MAVRK module.
Objective
It is assumed the reader has a basic knowledge of micro-controller and C programming
language but has no experience developing application on UR-CC430F5137-MVK module. By
the end of the document the reader should have an understanding of how to:
Program the UR-CC430F5137-MVK module
Implement RF communication
Implement ADC conversion
Implement UART communication
Implement Watch-Dog Timer
Display acquisitioned data on HyperTerminal
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 4
Getting Started
Hardware Requirements:
1. Two UR-CC430F5137-MVK modules.
2. One RF-UDEBUG-MVK module.
3. Power supply and battery packs.
4. DMM multi meter.
5. MSP-FET430UIF development tool.
6. EZ430-RF2500 development tool.
Software Requirement:
1. Code Composer Studio v5
2. HyperTerminal
Download the Code Composer Studio v5 from the TI Wiki page and install it on your computer,
you may need a MyTI account to download it. To save your hard drive space, choose custom
install and check only the MSP430. Download the CC430 RF Example codes from the TI
CC430F5137 web page under the application notes section; this is the code we are trying to build
on top of it. Download the CC430F513x C example codes from the TI CC430F5137 web page
under the software and development tools section; these example codes are very useful for
learning how to use different CC430F5137 functionality.
Create a CCS Project
Open the Code Composer Studio v5, set your own workplace path and choose evaluate for
license setup. By choosing evaluate option can use the full features of CCS for 90 days which is
more than enough for your design project. Click on the Project tab and click New CCS Project,
enter your project name, select CC430x5xx Family from your Variant tab and select
CC430F5137 next to it, shown as below figure.
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 5
Open the folder CC430x513x RF examples that you download from TI CC430F5137 web page
and open the sub folder RF_Examples_CCS, open the sub folder Fixed_LT_FIFO and copy the
file RF_Toggle_LED_Demo.h and all the files from HAL folder into your CCS Project folder.
Next, copy the entire content from RF_Toggle_LED_Demo.c to main.c under your CCS Project
folder. The resulting window should be the same as the figure below.
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 6
Modify the RF example code
To make this example code work on the UR-CC430F5137-MVK module, some modifications
need to be done on the example code. First, download the UR-CC430F5137-MVK schematic
from TI wiki page. The below figure is the partial schematic where extracted from the full
schematic, note that the green LED is connected to the port PJ.0 and the yellow LED is
connected to the port PJ.1. Also, there are two switch buttons on the UR-CC430F5137-MVK
module where one served as a reset button and another one is served as an input button which is
connected to the port P2.7. However, the example code assumed the input button is connected to
port P1.7 and the two LED’s are connected to P3.6 and P1.0. Therefore, we need to change all
the corresponding I/O port to the correct Port number at line 73, line 75, line 82, the entire
InitButtonLeds sub function, line 207 and line 212. We define the green LED as transmit signal
and yellow LED as receive signal.
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 7
Program the UR-CC430F5137 modules
After the modifications are done, attach the UR-CC430F5137-MVK on the RF-UDEBUG-MVK
module, plug in the JTAG connector and click the debug button (green bug) on the CCS. The
compilation should build successfully and program the UR-CC430F5137-MVK module with no
error. Similarly, program another UR-CC430F5137-MVK module by the same procedures. After
the both UR-CC430F5137-MVK modules are programmed successfully, try connected both
modules with battery pack and press the input button of either one module. The green LED on
the module which the input button is pressed should blink and the yellow LED on the other
module should blink next, this implies that the packet is successfully sent and received
wirelessly.
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 8
Include the UART function
Define void InitUART(void) in RF_Toggle_LED_Demo.h, look into the CC430x513x code
examples folder, there are 4 different UART example codes, to understand what is the different,
look at chapter 19 of the CC430F5137 user guide is recommended, this application note will use
the third mode of UART, open the file cc430x513x_uscia0_uart_03.c, note that this example
map the UART Tx/Rx port to P2.7 and P2.6. We need to use the default UART port P1.6 and
P1.5, in the InitUART sub function, set P1.6 as output and P1.5 as input by set the bit 6 of
P1DIR to 1 and set the bit 5 of P1DIR to 0. Copy the rest of the UART register setting code to
your InitUART sub function. The result should look similar as the figure shown below.
To output a character through UART, you need to check whether the Tx Buffer is ready or not, if
it is not ready, UCA0IFG&UCTXIFG will give a result 1. You should wait until the flag is clear
to store the character you want to send into the buffer UCA0TXBUF. Connect the UR-
CC430F5137-MVK receiver to the ez430 connector and plug it into your PC USB port. You may
need to install the ez430 driver. Open your HyperTerminal and set the baud rate to 9600, 8 bit,
none parity check and no flow control. The HyperTerminal should be ready to receive data from
the receiver.
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 9
Include the ADC function
Define void InitADC(void) in RF_Toggle_LED_Demo.h, look into the CC430x513x code
examples folder, there are 10 different UART example codes, to understand what is the different,
look at chapter 17 of the CC430F5137 user guide is recommended, this application note will
adapt from the cc430x513x_adc12_09.c example code and build on top of it, open the file
cc430x513x_adc12_09.c, note that this example shows how to perform A/D conversions on a
sequence of channels. A single sequence of conversions is performed - one conversion each on
channels A0, A1, A2, and A3. Each conversion uses AVcc and AVss for the references. The
conversion results are stored in ADC12MEM0, ADC12MEM1, ADC12MEM2, and
ADC12MEM3 respectively and are moved to 'results[]' upon completion of the sequence. In this
application note, we use channel A2 and A4. Similarly, copy the initialize setting code into the
sub function InitADC(void) and the interrupt service routine as well. The result should look
similar as the figure shown below.
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 10
Include the WDT function
Although you have already included the ADC function, you need to include a timer for sampling
your analog input. To create a timer, watch dog timer is used in this application note, define void
InitWDT(void) in your header file exactly as previous section. Copy the following code into
your sub function as the figure shown below, this setting set the watch dog timer interval to be
1000ms. Also, copy the watch dog timer service routine
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 11
Final Integration
Now you have all the components you need for this application, there are several ways to
integrate all the components, depends on your designed flow chart. The following code is the
complete code we use in our application. Details explanations for the register setting of
CC430F5137 are out of the scoop in this application note, further reading in the CC430F5137
user guide is recommended.
The Complete Code
#include "RF_Toggle_LED_Demo.h"
#include "stdio.h"
volatile int results[4]; // Needs to be global in this example
// Otherwise, the compiler removes it
// because it is not used for anything.
#define PACKET_LEN (5) // PACKET_LEN <= 61
#define RSSI_IDX (PACKET_LEN) // Index of appended RSSI
#define CRC_LQI_IDX (PACKET_LEN+1) // Index of appended LQI, checksum
#define CRC_OK (BIT7) // CRC_OK bit
#define PATABLE_VAL (0x51) // 0 dBm output
extern RF_SETTINGS rfSettings;
unsigned char packetReceived;
unsigned char packetTransmit;
unsigned char RxBuffer[PACKET_LEN+2];
unsigned char RxBufferLength = 0;
unsigned char TxBuffer[PACKET_LEN];
unsigned char buttonPressed = 0;
unsigned int i = 0;
unsigned char transmitting = 0;
unsigned char receiving = 0;
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
// Increase PMMCOREV level to 2 for proper radio operation
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 12
SetVCore(2);
ResetRadioCore();
InitRadio();
InitButtonLeds();
InitUART();
InitADC();
ReceiveOn();
receiving = 1;
while (1)
{
__bis_SR_register( LPM3_bits + GIE );
__no_operation();
if (buttonPressed) // Process a button press->transmit
{
buttonPressed = 0;
P2IFG = 0;
ReceiveOff();
receiving = 0;
transmitting = 1;
InitWDT();
}
else if(!transmitting)
{
ReceiveOn();
receiving = 1;
}
}
}
void InitWDT(void)
{
WDTCTL = WDT_ADLY_1000; // WDT 250ms, ACLK, interval timer
SFRIE1 |= WDTIE; // Enable WDT interrupt
}
void InitButtonLeds(void)
{
// Set up the button as interruptible
P2DIR &= ~BIT7;
P2REN |= BIT7;
P2IES &= BIT7;
P2IFG = 0;
P2OUT |= BIT7;
P2IE |= BIT7;
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 13
// Initialize Port J
PJOUT = 0x00;
PJDIR = 0xFF;
// Set up LEDs
PJOUT &= ~BIT0;
PJOUT &= ~BIT1;
}
void InitUART (void)
{
P1DIR |= BIT6; // Set P1.6 as TX output
P1DIR &= ~BIT5; // Set P1.5 as RX input
P1SEL |= BIT6 + BIT5; // Select P1.5 & P1.6 to UART function
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL1 |= UCSSEL_1; // CLK = ACLK
UCA0BR0 = 0x03; // 32kHz/9600=3.41 (see User's Guide)
UCA0BR1 = 0x00; //
UCA0MCTL = UCBRS_3+UCBRF_0; // Modulation UCBRSx=3, UCBRFx=0
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
}
void InitADC (void)
{
P2SEL = BIT2+BIT4; // Enable A/D channel inputs
ADC12CTL0 = ADC12ON+ADC12MSC+ADC12SHT0_2; // Turn on ADC12, set sampling time
ADC12CTL1 = ADC12SHP+ADC12CONSEQ_1; // Use sampling timer, single sequence
ADC12MCTL2 = ADC12INCH_2; // ref+=Vacc+, channel = A2
ADC12MCTL4 = ADC12INCH_4+ADC12EOS; // ref+=Vacc+, channel = A4, end seq.
ADC12IE = ADC12IE4; // Enable ADC12IFG.3
ADC12CTL0 |= ADC12ENC; // Enable conversions
}
void output(unsigned char ch)
{
while (!(UCA0IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = ch; // Output ch
}
/* sufficient for decimal rep of 16-bit number + newline + '\0' termination */
void print()
{
RxBuffer[PACKET_LEN] = '\n';
RxBuffer[PACKET_LEN+1] = '\r';
for (i = 0;i < sizeof(RxBuffer);i++)
{
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 14
output(RxBuffer[i]);
}
}
void InitRadio(void)
{
// Set the High-Power Mode Request Enable bit so LPM3 can be entered
// with active radio enabled
PMMCTL0_H = 0xA5;
PMMCTL0_L |= PMMHPMRE_L;
PMMCTL0_H = 0x00;
WriteRfSettings(&rfSettings);
WriteSinglePATable(PATABLE_VAL);
}
void Transmit(unsigned char *buffer, unsigned char length)
{
RF1AIES |= BIT9;
RF1AIFG &= ~BIT9; // Clear pending interrupts
RF1AIE |= BIT9; // Enable TX end-of-packet interrupt
WriteBurstReg(RF_TXFIFOWR, buffer, length);
Strobe( RF_STX ); // Strobe STX
}
void ReceiveOn(void)
{
RF1AIES |= BIT9; // Falling edge of RFIFG9
RF1AIFG &= ~BIT9; // Clear a pending interrupt
RF1AIE |= BIT9; // Enable the interrupt
// Radio is in IDLE following a TX, so strobe SRX to enter Receive Mode
Strobe( RF_SRX );
}
void ReceiveOff(void)
{
RF1AIE &= ~BIT9; // Disable RX interrupts
RF1AIFG &= ~BIT9; // Clear pending IFG
// It is possible that ReceiveOff is called while radio is receiving a packet.
// Therefore, it is necessary to flush the RX FIFO after issuing IDLE strobe
// such that the RXFIFO is empty prior to receiving a packet.
Strobe( RF_SIDLE );
Strobe( RF_SFRX );
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 15
}
#pragma vector=PORT2_VECTOR
__interrupt void PORT2_ISR(void)
{
switch(__even_in_range(P2IV, 16))
{
case 0: break;
case 2: break; // P1.0 IFG
case 4: break; // P1.1 IFG
case 6: break; // P1.2 IFG
case 8: break; // P1.3 IFG
case 10: break; // P1.4 IFG
case 12: break; // P1.5 IFG
case 14: break; // P1.6 IFG
case 16: // P1.7 IFG
P2IE = 0; // Debounce by disabling buttons
buttonPressed = 1;
__bic_SR_register_on_exit(LPM3_bits); // Exit active
break;
}
}
// Watchdog Timer interrupt service routine
#pragma vector = WDT_VECTOR
__interrupt void WDT_ISR(void)
{
PJOUT |= BIT0; // Pulse LED during Transmit
ADC12CTL0 |= ADC12SC;
}
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
switch(__even_in_range(ADC12IV,34))
{
case 0: break; // Vector 0: No interrupt
case 2: break; // Vector 2: ADC overflow
case 4: break; // Vector 4: ADC timing overflow
case 6: break; // Vector 6: ADC12IFG0
case 8: break; // Vector 8: ADC12IFG1
case 10: break; // Vector 10: ADC12IFG2
case 12: break; // Vector 12: ADC12IFG3
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 16
case 14:
results[0] = ADC12MEM2; // Move results, IFG is cleared
results[1] = ADC12MEM4; // Move results, IFG is cleared
sprintf((char*)TxBuffer,"A%d",results[0]);
Transmit( (unsigned char*)TxBuffer, sizeof TxBuffer);
__delay_cycles(10000);
sprintf((char*)TxBuffer,"B%d",results[1]);
Transmit( (unsigned char*)TxBuffer, sizeof TxBuffer);
break; // Vector 14: ADC12IFG4
case 16: break; // Vector 16: ADC12IFG5
case 18: break; // Vector 18: ADC12IFG6
case 20: break; // Vector 20: ADC12IFG7
case 22: break; // Vector 22: ADC12IFG8
case 24: break; // Vector 24: ADC12IFG9
case 26: break; // Vector 26: ADC12IFG10
case 28: break; // Vector 28: ADC12IFG11
case 30: break; // Vector 30: ADC12IFG12
case 32: break; // Vector 32: ADC12IFG13
case 34: break; // Vector 34: ADC12IFG14
default: break;
}
}
#pragma vector=CC1101_VECTOR
__interrupt void CC1101_ISR(void)
{
switch(__even_in_range(RF1AIV,32)) // Prioritizing Radio Core Interrupt
{
case 0: break; // No RF core interrupt pending
case 2: break; // RFIFG0
case 4: break; // RFIFG1
case 6: break; // RFIFG2
case 8: break; // RFIFG3
case 10: break; // RFIFG4
case 12: break; // RFIFG5
case 14: break; // RFIFG6
case 16: break; // RFIFG7
case 18: break; // RFIFG8
case 20: // RFIFG9
if(receiving) // RX end of packet
{
// Read the length byte from the FIFO
RxBufferLength = ReadSingleReg( RXBYTES );
Application Note [DEVELOP UMAVRK WIRELESS SENSING SYSTEM WITH CCS]
ChaiYong Lim | ECE480 Spring 2012 17
ReadBurstReg(RF_RXFIFORD, RxBuffer, RxBufferLength);
// Stop here to see contents of RxBuffer
__no_operation();
// Check the CRC results
if(RxBuffer[CRC_LQI_IDX] & CRC_OK)
{
PJOUT |= BIT1; // Turn on LED1
print();
PJOUT &= ~BIT1; // Turn off LED1
}
ReceiveOn();
}
else if(transmitting) // TX end of packet
{
RF1AIE &= ~BIT9; // Disable TX end-of-packet interrupt
PJOUT &= ~BIT0; // Turn off LED after Transmit
}
else while(1); // trap
break;
case 22: break; // RFIFG10
case 24: break; // RFIFG11
case 26: break; // RFIFG12
case 28: break; // RFIFG13
case 30: break; // RFIFG14
case 32: break; // RFIFG15
}
}