Part Number: MSP-EXP430FR2355
Other Parts Discussed in Thread: MSP430FR2355
Hi All
I am doing a very simple project (so I thought) where I read the value in on P1.4 ADC from a 10k pot connected to 3.3V.
The ADC reads the value with no issues what so every. However, when I try to transmit the HEX value out on the UART I get meaningless characters which says to me it is a baud rate configuration issue. But I followed all the user guides and the formula and check it against several online AI tools and tey all come up with the same answer.
I suspect it is something to do with how I have set up my UART interrupts
I am using SMCLK = 1MHz ad the desired baud rate is 9600
The code is below and ay help would be much appreciated.
#include <msp430.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
//Variables
volatile unsigned int ADC_value = 0;
void UART_init(void)
{
/* Configure UART pins (P4.2 = RXD, P4.3 = TXD) */
P4SEL0 |= BIT2 | BIT3; // Set P4.2 and P4.3 to UART mode
P4SEL1 &= ~(BIT2 | BIT3);
UCA1CTLW0 |= UCSWRST; // Put eUSCI_A1 in reset
UCA1CTLW0 |= UCSSEL__SMCLK; // Choose SMCLK clock source
/* Baud Rate Calculation for 9600 baud with 1,000,000 Hz SMCLK:
1000000 / 9600 = 104.166 -> UCBRx = 104, fractional portion = 0.166
Using Table 22-4 from User's Guide: UCBRSx = 0x11*/
UCA1BR0 = 0x68; // 1MHz 9600 baud
UCA1BR1 = 0;
UCA0MCTLW = 0x1100;
UCA1CTLW0 &= ~UCSWRST; // Initialize eUSCI state machine
UCA1IE |= UCTXIE; // Enable USCI_A1 TX interrupt
__bis_SR_register(GIE);
__no_operation();
}
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
/* Disable the GPIO power-on default high-impedance mode to activate
* previously configured port settings*/
PM5CTL0 &= ~LOCKLPM5;
UART_init();
/* Configure ADC Channel Pins*/
P1SEL1 |= BIT4; // Configure P1.4 for ADC
P1SEL0 |= BIT4;
/* Configure ADC12 */
ADCCTL0 = ADCSHT_2 | ADCON; // ADCON, S&H=16 ADC clks
ADCCTL1 = ADCSHP; // ADCCLK = MODOSC; sampling timer; ADC conversion start source is ADCSC bit
ADCCTL2 = ADCRES_2; // Set ADCRES_2 to binary 10 for 12 bit resolution
ADCIE |= ADCIE0; // Enable ADC conv complete interrupt
ADCMCTL0 |= ADCINCH_4 | ADCSREF_0; // ADC channel A4 selected as input and internal reference selected 000b = {VR+ = AVCC and VR- = AVSS}
/* Configure voltage reference for ADC conversion */
PMMCTL0_H = PMMPW_H; // Unlock the PMM registers
PMMCTL2 |= INTREFEN; // Enable internal reference 1.5V
__delay_cycles(400); // Delay for reference settling
while(1)
{
ADCCTL0 |= ADCENC | ADCSC; // Sampling and conversion start
__bis_SR_register(LPM0_bits | GIE); // LPM0, ADC_ISR will force exit
__no_operation();
}
}
#pragma vector = ADC_VECTOR
__interrupt void ADC_ISR(void)
{
switch (__even_in_range(ADCIV, ADCIV_ADCIFG))
{
case ADCIV_ADCIFG: // ADC12MEM1 Interrupt
ADC_value = ADCMEM0; // Save MEM0
__bic_SR_register_on_exit(LPM0_bits | GIE); // Exit CPU, clear interrupts
break;
default:
break;
}
}
#pragma vector = USCI_A1_VECTOR // Vector depends on specific MSP430 family variant
__interrupt void USCI_A1_ISR(void)
{
switch(__even_in_range(UCA1IV, USCI_UART_UCTXCPTIFG))
{
case USCI_NONE:
break;
case USCI_UART_UCRXIFG:
break;
case USCI_UART_UCTXIFG:
UCA1TXBUF = (ADC_value);
break;
case USCI_UART_UCSTTIFG:
break;
case USCI_UART_UCTXCPTIFG:
break;
default:
break;
}
}