Part Number: MSP430FR2311
Hey. I am trying to send 2 ADC Values over UART to Serial. RIght now, I have a variable called ADC_Result that stores both of the blues that I want to send. Over the TX buffer I was to first send ADC_Result[0]. then a comma and then ADC_Result[1]. How is this possible. I am using sprintf to change my int to a hex value so that it can be displayed on the serial monitor.
#include <msp430.h>
#include <stdint.h>
#include <stdio.h>
unsigned int ADC_Result[2]; // 10-bit ADC conversion result array
unsigned int i; //Variable to iterate ADC_Result Array
void init_uart(); //Initilize UART Serial Communication
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop WDT
init_uart(); //initilize UART communication
// Configure ADC A0~1 pins
P1SEL0 |= BIT0 + BIT1;
P1SEL1 |= BIT0 + BIT1;
// Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings
PM5CTL0 &= ~LOCKLPM5;
// Configure ADC
ADCCTL0 |= ADCSHT_2 | ADCMSC | ADCON; // 16ADCclks, MSC, ADC ON
ADCCTL1 |= ADCSHP | ADCCONSEQ_1 | ADCSSEL_1; // ADC clock ACLK, sampling timer, s/w trig.,single sequence
ADCCTL2 |= ADCRES_1; //10-bit conversion results
ADCMCTL0 |= ADCINCH_1 | ADCSREF_4; // A0~2(EoS); Vref=1.5V
ADCIE |= ADCIE0; // Enable ADC conv complete interrupt
// Configure reference
PMMCTL0_H = PMMPW_H; // Unlock the PMM registers
PMMCTL2 |= INTREFEN; // Enable internal reference
__delay_cycles(400); // Delay for reference settling
__no_operation();
while(1)
{
i = 1;
while(ADCCTL1 & ADCBUSY); // Wait if ADC core is active
ADCCTL0 |= ADCENC | ADCSC; // Sampling and conversion start
__bis_SR_register(LPM0_bits | GIE); // Enter LPM0 w/ interrupts
while (!(UCA0IFG & UCTXIFG)); // wait for USCI_A0 TX buffer to ready
char ADC_Char[16]; //create a char array to store the ADC_Result as the serial only reads in ASCII
sprintf(ADC_Char, "%d", ADC_Result); //converts the ADC Result int value into a char array
for (i=0; i<strlen(ADC_Char); i++) { // this loop is important to read the char array, one bit at a time, this is how the serial reads it as it cant read a 2 byte array
UCA0TXBUF = ADC_Char[i];
UCA0TXBUF = 0x2C;
UCA0TXBUF = ADC_Char[i];
// if serial is not buffered, pause to send character
}
UCA0TXBUF = 0xA; //this is just a newline character
//__no_operation(); // Only for debug, don't know what theyre here for
__delay_cycles(5000);
// __no_operation();
}
}
//Initilizes the UART on Pins 1.6 and 1.7
void init_uart()
{
// Configure UART pins
P1SEL0 |= BIT6 | BIT7; // set 2-UART pin as second function
// Configure UART
UCA0CTLW0 |= UCSWRST; //Sets softare reset enable
UCA0CTLW0 |= UCSSEL__SMCLK; // Set SMCLK as BRCLK to be used for baud rate of 115200
// Baud Rate calculation. Setting BaudRate to 115200
UCA0BR0 = 8; // 1000000/115200 = 8.68 INT(N) = 8
UCA0MCTLW = 0xD600; // 1000000/115200 - INT(1000000/115200)=0.68
UCA0BR1 = 0x00; // UCBRSx value = 0xD6
UCA0CTLW0 &= ~UCSWRST; // Initialize eUSCI
}
// ADC interrupt service routine
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=ADC_VECTOR
__interrupt void ADC_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(ADC_VECTOR))) ADC_ISR (void)
#else
#error Compiler not supported!
#endif
{
switch(__even_in_range(ADCIV,ADCIV_ADCIFG))
{
case ADCIV_NONE:
break;
case ADCIV_ADCOVIFG:
break;
case ADCIV_ADCTOVIFG:
break;
case ADCIV_ADCHIIFG:
break;
case ADCIV_ADCLOIFG:
break;
case ADCIV_ADCINIFG:
break;
case ADCIV_ADCIFG:
ADC_Result[i] = ADCMEM0;
if(i == 0)
{
__bic_SR_register_on_exit(LPM0_bits); // Exist LPM0
}
else
{
i--;
}
break;
default:
break;
}
}