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.

Sampling and UART

Hi, 

I am facing following problems 

1. The  debugger does not go inside the " while (ADC12BUSY & ADC12CTL1 == 1) " at all. I do not know why? Plus the debugger shows me the correct value in Register " ADC12MEM0" but does not show the correct value of variable "SavedADC12MEM0". 

2.  I want to send the ADC sampled values to Tera Terminal. I can communicate with tera terminal well but unable to see the decimal values of the samples. 

 Melissa

My code is as follows 

 

void Port_Init();
void adc_init(void);
void adc_start_Read(void);
void acc_read_with_offset();
void UART(int , int);

volatile int SavedADC12MEM0, SavedADC12MEM1 ;
unsigned int adc_index = 0;
//extern unsigned int SavedADC12MEM0[];

int main(void)
{
WDTCTL = WDTPW+WDTHOLD;

Port_Init();

while (1)
{
adc_init();
adc_start_Read();
// UART();
}
}


void Port_Init(void)
{
P3SEL = 0x30; // P3.4,5 = USCI_A0 TXD/RXD
P6SEL |= 0x80;
P6DIR |= 0x00;
P1DIR |= 0x01;

}


void adc_init(void)
{

REFCTL0 &= ~REFVSEL_3; //Page 693 of data sheet
REFCTL0 |= REFVSEL_2 + REFON; // Configure internal 2.5V reference
__delay_cycles(1900); // delay for reference to settle
ADC12CTL0 &= ~ADC12ENC; // Ensure ENC is clear
ADC12CTL0 |= ADC12ON | ADC12SHT0_15 | ADC12MSC ;//| ADC12REFON | ADC12REF2_5V;
ADC12CTL1 |= ADC12SHP | ADC12CONSEQ_2 |ADC12SSEL_0;
ADC12CTL2 = ADC12RES_2;
ADC12MCTL0 |= ADC12SREF_1 | ADC12INCH_7;
ADC12MCTL1 = ADC12SREF_1 | ADC12EOS;
__delay_cycles(1900);

}

void adc_start_Read(void)
{

REFCTL0 &= ~REFVSEL_3; //Page 693 of data sheet
REFCTL0 |= REFVSEL_2 + REFON; // Configure internal 2.5V reference
__delay_cycles(1900); // delay for reference to settle
ADC12CTL0 &= ~ADC12ENC; // Ensure ENC is clear
ADC12CTL0 |= ADC12ON | ADC12SHT0_15 ;//| ADC12REFON | ADC12REF2_5V;| ADC12MSC_0
ADC12CTL1 |= ADC12SHP | ADC12CONSEQ_1 |ADC12SSEL_0;
ADC12CTL2 = ADC12RES_2;
ADC12MCTL0 |= ADC12SREF_1 | ADC12INCH_7;
ADC12MCTL1 = ADC12SREF_1 | ADC12EOS;
__delay_cycles(1900);

P1DIR |= 0x01;
P1OUT = (uint8_t) (ADC12CTL1 & ADC12BUSY);
for(;;)
{
ADC12CTL0 |= ADC12ENC | ADC12SC;

while (ADC12BUSY & ADC12CTL1 == 0)
{
P1DIR |= 0x01;
P1OUT ^= 0x00;
SavedADC12MEM0 = ADC12MEM0;
SavedADC12MEM1 = ADC12MEM1;

}
SavedADC12MEM0 = 1234;
UART(SavedADC12MEM0, ADC12MEM1);
}

}
void UART(int a , int b)
{
volatile int buffer;
UCA0CTL1 |= UCSWRST; // **Put state matchine in reset**
UCA0CTL1 |= UCSSEL_3; // SMCLK
UCA0BR0 = 6; // 1MHz 9600 (see User's Guide)
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS_0 + UCBRF_13 + UCOS16; // Modln UCBRSx=0, UCBRFx=0,
// over sampling
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
buffer = a;

while (!(UCA0IFG&UCTXIFG)); // USCI_A0 TX buffer ready?

UCA0TXBUF = buffer; // TX -> RXed character
}

void adc_start_Read(void)
{

REFCTL0 &= ~REFVSEL_3; //Page 693 of data sheet
REFCTL0 |= REFVSEL_2 + REFON; // Configure internal 2.5V reference
__delay_cycles(1900); // delay for reference to settle
ADC12CTL0 &= ~ADC12ENC; // Ensure ENC is clear
ADC12CTL0 |= ADC12ON | ADC12SHT0_15 ;//| ADC12REFON | ADC12REF2_5V;| ADC12MSC_0
ADC12CTL1 |= ADC12SHP | ADC12CONSEQ_1 |ADC12SSEL_0;
ADC12CTL2 = ADC12RES_2;
ADC12MCTL0 |= ADC12SREF_1 | ADC12INCH_7;
ADC12MCTL1 = ADC12SREF_1 | ADC12EOS;
__delay_cycles(1900);

P1DIR |= 0x01;
P1OUT = (uint8_t) (ADC12CTL1 & ADC12BUSY);
for(;;)
{
ADC12CTL0 |= ADC12ENC | ADC12SC;

while (ADC12BUSY & ADC12CTL1 == 0)
{
P1DIR |= 0x01;
P1OUT ^= 0x00;
SavedADC12MEM0 = ADC12MEM0;
SavedADC12MEM1 = ADC12MEM1;

}
SavedADC12MEM0 = 1234;
UART(SavedADC12MEM0, ADC12MEM1);
}

}
void UART(int a , int b)
{
volatile int buffer;
UCA0CTL1 |= UCSWRST; // **Put state matchine in reset**
UCA0CTL1 |= UCSSEL_3; // SMCLK
UCA0BR0 = 6; // 1MHz 9600 (see User's Guide)
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS_0 + UCBRF_13 + UCOS16; // Modln UCBRSx=0, UCBRFx=0,
// over sampling
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
buffer = a;while (!(UCA0IFG&UCTXIFG)); // USCI_A0 TX buffer ready?

UCA0TXBUF = buffer; // TX -> RXed character
}

  • Hi Melissa,

    Can you please specify the IC which ur for the above code.

  • It’s counterproductive to configure the reference at start of adc_start_read. It is already done in adc_init and switching the reference back and forth only causes an unnecessary delay. Same for the ADC12 setup code.
    Also, adc_init should be outside the while(1) loop. It only needs to be executed once.

    P1OUT^=0x00 has no effect. This will toggle all bits in 0x00, but there are no bits set in 0x00. So you’ll never see P1.0 toggle, even if the while is entered.
    (btw: adc_init uses ADC12CONSEQ_2, which will sample one channel eternally, always being busy - the code in adc_start_read uses ADC12CONSEQ_1, which converts a sequence once.

**Attention** This is a public forum