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.

CCS/MSP430G2553: Interfacing with a Heartbeat Sensor and Accelerometer (MPU-6050)

Part Number: MSP430G2553


Tool/software: Code Composer Studio

Hi!

So I am trying to interface with MSP430G2553 with a heartbeat sensor and an accelerometer (MPU-6050).

I would like to merge the two but I am having trouble understanding the code. I looked up the variable names and the description just tells me the full name of the variable. Is there somewhere I can get documentation to understand this stuff?

The accelerometer has to be P1.6 and P1.7

The Heartbeat sensor must be P1.0 to P1.5


The code we have for the heartbeat sensor is the following:

#include "msp430g2253.h"

// Variables
int adc[50] = {0}; //Sets up an array of 10 integers and zero's the values
int avg_adc = 0;

// Function prototypes
void adc_Setup();
void adc_Sam10();

void main()
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
adc_Setup(); // Fucntion call for adc_setup

while(1)
{
long adctot = 0;
adc_Sam10(); // Function call for adc_samp
// Add all the sampled data and divide by 10 to find average
int i = 0;
for(i = 0; i <= 49; i++){
adctot = adctot + adc[i];
}
avg_adc = adctot/50;
}
}

// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
__bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR)
}

// ADC set-up function
void adc_Setup()
{
ADC10CTL1 = CONSEQ_2 + INCH_0; // Repeat single channel, A0
ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE; // Sample & Hold Time + ADC10 ON + Interrupt Enable
ADC10DTC1 = 0x32; // 1000 conversions
ADC10AE0 |= 0x01; // P1.0 ADC option select
}

// ADC sample conversion function
void adc_Sam10()
{
ADC10CTL0 &= ~ENC; // Disable Conversion
while (ADC10CTL1 & BUSY); // Wait if ADC10 busy
ADC10SA = (int)adc; // Transfers data to next array (DTC auto increments address)
ADC10CTL0 |= ENC + ADC10SC; // Enable Conversion and conversion start
__bis_SR_register(CPUOFF + GIE);// Low Power Mode 0, ADC10_ISR
}

this works so far. The code we have for the accelerometer is the following:

#ifndef I2C_USCI_H
#define I2C_USCI_H

// Address
#define MPU6050_ADDRESS 0x68
#define BQ32000_ADDRESS 0x68
#define DS1307_ADDRESS 0x68
#define LM92_ADDRESS 0x48
/******************************************************************************\
* Prototype *
\******************************************************************************/
void I2C_USCI_Init(unsigned char addr); //Init I2C
void I2C_USCI_Set_Address(unsigned char addr); //Change Slave's Address
unsigned char I2C_USCI_Read_Byte(unsigned char address); //Read 1 byte
//Read many Byte
unsigned char I2C_USCI_Read_Word(unsigned char Addr_Data,unsigned char *Data, unsigned char Length);
//Write 1 Byte
unsigned char I2C_USCI_Write_Byte(unsigned char address, unsigned char Data);
/******************************************************************************\
* Function *
\******************************************************************************/


void I2C_USCI_Init(unsigned char addr)
{
P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0
P1SEL2|= BIT6 + BIT7; // 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 = 40; // fSCL = SMCLK/40 = ~400kHz
UCB0BR1 = 0;
UCB0I2CSA = addr; // Set slave address
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
}

void I2C_USCI_Set_Address(unsigned char addr)
{
UCB0CTL1 |= UCSWRST;
UCB0I2CSA = addr; // Set slave address
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
}

unsigned char I2C_USCI_Read_Byte(unsigned char address)
{
while (UCB0CTL1 & UCTXSTP);
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX,START

while (!(IFG2&UCB0TXIFG));
UCB0TXBUF = address;

while (!(IFG2&UCB0TXIFG));

UCB0CTL1 &= ~UCTR; // I2C RX
UCB0CTL1 |= UCTXSTT; // I2C RESTART
IFG2 &= ~UCB0TXIFG;

while (UCB0CTL1 & UCTXSTT);
UCB0CTL1 |= UCTXSTP;
return UCB0RXBUF;
}

unsigned char I2C_USCI_Read_Word(unsigned char Addr_Data,unsigned char *Data, unsigned char Length)
{
unsigned char i=0;
while (UCB0CTL1 & UCTXSTP); // Loop until I2C STT is sent
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition

while (!(IFG2&UCB0TXIFG));
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
if(UCB0STAT & UCNACKIFG) return UCB0STAT;
UCB0TXBUF = Addr_Data;

while (!(IFG2&UCB0TXIFG));
if(UCB0STAT & UCNACKIFG) return UCB0STAT;

UCB0CTL1 &= ~UCTR; // I2C RX
UCB0CTL1 |= UCTXSTT; // I2C start condition
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
while (UCB0CTL1 & UCTXSTT); // Loop until I2C STT is sent
for(i=0;i<(Length-1);i++)
{
while (!(IFG2&UCB0RXIFG));
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
Data[i] = UCB0RXBUF;
}
while (!(IFG2&UCB0RXIFG));
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
UCB0CTL1 |= UCTXSTP; // I2C stop condition after 1st TX
Data[Length-1] = UCB0RXBUF;
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
return 0;
}

unsigned char I2C_USCI_Write_Byte(unsigned char address, unsigned char data)
{
while (UCB0CTL1 & UCTXSTP);
UCB0CTL1 |= UCTR + UCTXSTT;

while (!(IFG2&UCB0TXIFG));
if(UCB0STAT & UCNACKIFG) return UCB0STAT;
UCB0TXBUF = address;


while (!(IFG2&UCB0TXIFG));
if(UCB0STAT & UCNACKIFG) return UCB0STAT;
UCB0TXBUF = data;

while (!(IFG2&UCB0TXIFG));
if(UCB0STAT & UCNACKIFG) return UCB0STAT;
UCB0CTL1 |= UCTXSTP;
IFG2 &= ~UCB0TXIFG;
return 0;
}
#endif /* I2C_USCI */

I would like to merge the two but I am having trouble understanding the code. I looked up the variable names and the description just tells me the full name of the variable. Is there somewhere I can get documentation to understand this stuff?

Thank you,
Vidhu Appalaraju

  • Hello,

    When posting code to the forum, please use the advanced menu by clicking the Insert Code, Attach Files and more... link. From this menu click the </> button and insert your code this way so it will be formatted for readability . I have already manually edited your post to do this currently.

    As for your question, it seems one sensor is done by ADC and the other is by I2C interface. I do not see an issue when combining these. There are some variables used in both, but you would have to read the documentation of where you got this code for more information on this. Most of the code you are looking at are MSP430 register definitions and flag masking for specific bits in the register. to find out more about those, you will need to look into the User Guide for you MSP430.

**Attention** This is a public forum