MSP430(ADC, timer and UART) - Page 1
Forum section: electronics / general

author: Lidiya Thomas
author_rank: Newbie
timestamp: May 27, 2016, 04:51:39 am
content: Hi all,I am a beginner in MSP coding. I am trying to integrate adc, timer and UART. I need 10 ADC values after running the code. But it is showing only one value. Can someone help me in solving this.#include <msp430g2553.h>#define TXD BIT2 // TXD on P1.2#define RXD BIT1 // RXD on P1.1#define LED_0 BIT0#define LED_1 BIT6#define LED_OUT P1OUT#define LED_DIR P1DIR#define TIMER_COUNT 12000unsigned int value,n=0,i;unsigned int adc;void serial_init(void);void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop WDT// config clockBCSCTL1 = CALBC1_1MHZ; // Set rangeDCOCTL = CALDCO_1MHZ; // SMCLK = DCO = 1MHz// config timerTACCTL0 |= CCIE;TACTL |= TASSEL_2 + MC_2; // Set the timer A to SMCLCK, Continuous//config adcADC10CTL0 &= ~ENC; // Disable conversionADC10CTL1 = INCH_5 + ADC10SSEL_3 + ADC10DIV_3 + CONSEQ_2; // p1.5 analog signal, Clock = ADC10CLK/4ADC10CTL0 = SREF_1 + ADC10SHT_0 + REFON + ADC10ON + ADC10IE;ADC10AE0 |= BIT5;//Configure Hardware UART */P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXDP1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXDUCA0CTL1|=UCSWRST;UCA0CTL0=0x00;UCA0CTL1 |= UCSSEL_2; // SMCLKUCA0BR0 = 104; // Set Baud rate (1MHz CPU clock)/(9600 baud) (lower byte)UCA0BR1 = 0; // Set Baud rate to (1MHz CPU clock)/(9600 baud) (upper byte)UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1IE2 |= UCA0TXIE;UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**while(n<10){__enable_interrupt();__bis_SR_register(LPM0 + GIE); // LPM0 with interrupts enabledn++;}void serial_charTx(int adc){//adc=ADC10MEM;value[n-1]=adc;while (!(IFG2&UCA0TXIFG)); // Wait until TX buffer ready?UCA0TXBUF = adc >> 8; // Send MSB firstwhile (!(IFG2&UCA0TXIFG)); // Wait until TX buffer ready?UCA0TXBUF = (adc & 0x00FF); // Send LSB}//}#pragma vector=TIMER0_A0_VECTOR__interrupt void Timer_A (void){P1OUT ^= 0x01;ADC10CTL0 |= ENC + ADC10SC;__bic_SR_register_on_exit(CPUOFF); // Return to active mode}#pragma vector=ADC10_VECTOR__interrupt void ADC10_ISR (void){adc = ADC10MEM;serial_charTx(value);//value[n-1]=adc;P1OUT ^= BIT6;TACCR0 += TIMER_COUNT;ADC10CTL0&=~ADC10IFG;//Set timer count__bic_SR_register_on_exit(CPUOFF); // Return to active mode// ADC10CTL0&=~ADC10IFG;}

author: AndreasF
author_rank: Frequent Contributor
timestamp: May 27, 2016, 09:32:46 am
content: Welcome to the forum!There are a couple of issues with the code, but perhaps some are there because you didn't copy all of it. As is, it shouldn't compile.One of the most glaring issue is maybe this:Code:unsigned int value; ......void serial_charTx(int adc) {...}...#pragma vector=ADC10_VECTOR__interrupt void ADC10_ISR (void){adc = ADC10MEM;serial_charTx(value);...}Instead of passing the recorded ADC value to the serial_charTx function, you're passing the name of the array. Without the [ ] an array name is the memory address of its first element, so what you're printing is the memory location of value, not its value. That memory address doesn't change, of course. Is that what you mean by "But it is showing only one value."?You probably also don't want to end main with the while loop you have. What happens after n is no longer less than 10?(BTW, please try to use the "code" tags:http://wiki.simplemachines.org/smf/Code)

author: Lidiya Thomas
author_rank: Newbie
timestamp: May 28, 2016, 03:50:58 am
content: Thanks for the reply..but my issue is different, rather than getting 10 values, only one value is getting printed..that might be the value. For n=0, everything is running smooth. But for n=1,it is not at al entering to the second pragma(i.e, adc pragma)

author: casinada
author_rank: Frequent Contributor
timestamp: May 28, 2016, 06:21:15 am
content: I know that you're a beginner but there is a dedicated section of the forum for Micro-controllers.There is a dedicated site for MSP430: 43oh.comThere are dedicated forums for TI support:https://e2e.ti.com/support/microcontrollers/msp430/f/166I hope this helps to post the question on a more appropriate place

author: AndreasF
author_rank: Frequent Contributor
timestamp: May 28, 2016, 10:40:18 am
content: Like I said in my previous post, there are multiple issues, and the code you posted shouldn't even compile. Judging by the various commented lines in the code, you probably have tried all kinds of things already, so perhaps the code you posted wasn't the version that compiled and ran. If you post the full code that actually compiled and if possible any compiler warnings, I'm happy to have another look.Please use the "code" feature(button with the # in it) this time, though!However, having looked at the "intent" of your code more closely now (and after chasing a few dead ends), there is nothing obvious (to me) why you only get 1 value printed. There are a few non-ideal ways of coding this, with potential timing issues, but some values should still be transmitted. Are you getting a full 2-byte (16-bit) value, or only a single byte? If you pause the execution in debug mode after "only one value is getting printed", which line of the code does it stop? Does the timer continue to trigger its interrupt (i.e. does P1.0 continue to toggle)?

author: mayor
author_rank: Regular Contributor
timestamp: May 29, 2016, 12:20:01 pm
content: Without digging any deeper in your code, it appears you are not specifying the number you want. You need to set ADC10DTC1 = 10. That's a starting point.