Hello all, I am using the following code to receive adc values for 4 channels, and to send some insrtuction to the uC via UART. But the problem is, untill I send anything to the controller, I get all of these adc values correctly via UART, but whenever I send data to the rx, it receives it correctly and do the job its been told to, but since then all the 1.xx pins send incorrect adc value via tx. What is causing this conflict between TX and RX?
My Code:
#include <msp430g2553.h>
#include <stdbool.h>
////////////////Defines////////////////
#define LED1 BIT6
#define LED0 BIT0
#define Error BIT0
#define GND BIT4
#define DAT BIT5
#define VCC BIT0 //port P2.0
char buffer[4];
float temp,v;
int n_temp,r_temp;
////////////////Function Protos////////////////
static char *i2a(unsigned i, char *a, unsigned r)
{
if (i/r > 0) a = i2a(i/r,a,r);
*a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
return a+1;
}
char *itoa(int i, char *a, int r)
{
if ((r < 2) || (r > 36)) r = 10;
if (i < 0)
{
*a = '-';
*i2a(-(unsigned)i,a+1,r) = 0;
}
else *i2a(i,a,r) = 0;
return a;
}
void TX(char *tx_message)
{
unsigned int i=0; //Define end of string loop int
char *message; // message variable
unsigned int message_num; // define ascii int version variable
message = tx_message; // move tx_message into message
while(1)
{
if(message[i]==0) // If end of input string is reached, break loop.
{break;}
message_num = (int)message[i]; //Cast string char into a int variable
UCA0TXBUF = message_num; // write INT to TX buffer
i++; // increase string index
__delay_cycles(10000); //transmission delay
if(i>50) //prevent infinite transmit
{
P1OUT |= Error;
break;
}
} // End TX Main While Loop
} // End TX Function
void m4302553_Init()
{
ADC10CTL0= ADC10ON + ADC10SHT_3+ADC10IE; ; //Temperature sensor supply REFON = 0, INCHx = 0Ah,
//only ISENSOR current as reference,64 clocks for sample HOLDING TIME
ADC10CTL1=INCH_10+ ADC10DIV_3; // ADC10clock/4, as 30 MicroSeconds is bare minimum
}
void lm_init()
{
/* Configure ADC Channel */
ADC10CTL0 = SREF_1+REFON+ADC10SHT_3 + ADC10ON + ADC10IE; //1.5v internal as reference
ADC10CTL1 = INCH_5 + ADC10DIV_3 ; // Channel 5, ADC10CLK/4
}
void opto101pj_init()
{
ADC10CTL0 = SREF_0 + ADC10SHT_3 + ADC10ON + ADC10IE; //Vcc & Vss as reference
ADC10CTL1 = INCH_7 + ADC10DIV_3 ; // Channel 5, ADC10CLK/4
}
void inputvcc_init()
{
ADC10CTL0 = SREF_1 +ADC10ON + REFON + REF2_5V + ADC10SHT_3+ADC10IE; // use internal ref, turn on 2.5V ref, set samp time = 64 cycles
ADC10CTL1 = INCH_11+ADC10DIV_3;
}
////////////////////////////////MAIN PROGRAM LOOP//////////////////////////////////////////////////
void main(void)
{WDTCTL = WDTPW + WDTHOLD; // Stop WDT
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
DCOCTL = CALDCO_1MHZ;
////////////////USCI setup////////////////
P1SEL = BIT1 + BIT2; // Set P1.1 to RXD and P1.2 to TXD
P1SEL2 = BIT1 + BIT2; //
UCA0CTL1 |= UCSSEL_2; // Have USCI use SMCLK
UCA0BR0 = 104; // Baud: 9600, N= CLK/Baud, N= 10^6 / 9600
UCA0MCTL = UCBRS_1; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // Start USCI
IE2 |= UCA0RXIE;//enable rx interrupt
////////////////General GPIO Defines////////////////
P1DIR |= (LED1+GND+LED0); //Define GPIOs as outputs else GPIOs are inputs
P2DIR |=VCC+BIT1;
P1OUT&=~GND;
P2OUT&=~BIT1;
////////////////Main Loop////////////////
while(1)
{
int value2,value,value1,value3,value4;
ADC10CTL0 &= ~ENC;
//vcc
inputvcc_init();
__delay_cycles(1000); // Wait for ADC Ref to settle
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
__bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled
value = ADC10MEM;
itoa( value , buffer, 10);
TX(buffer);
TX("/");
ADC10CTL0 &= ~ENC;
//LM35
lm_init();
__delay_cycles(1000);
P1OUT = LED0;
ADC10CTL0 |= ENC + ADC10SC;
__bis_SR_register(CPUOFF + GIE);
value4 = ADC10MEM;
itoa( value4 , buffer, 10);
TX(buffer);
TX("/");
ADC10CTL0&=~ENC;
//2553
m4302553_Init();
__delay_cycles(1000);
ADC10CTL0 |= ENC + ADC10SC;
__bis_SR_register(CPUOFF + GIE);
value2 = ADC10MEM;
itoa(value2, buffer, 10);
TX(buffer);
TX("/");
P1OUT &=~LED0;
ADC10CTL0&=~ENC;
//OPTO101
P1OUT =LED1;
opto101pj_init();
__delay_cycles(1000);
ADC10CTL0 |= ENC + ADC10SC;
__bis_SR_register(CPUOFF + GIE);
value3 = ADC10MEM;
itoa(value3, buffer, 10);
TX(buffer);
TX("/");
P1OUT &=~LED1;
_delay_cycles(996000);//+4*1000 DELAYS MAKE APPROX 1 SEC TOTAL
} // End While
} // End Main Program
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
__bic_SR_register_on_exit(CPUOFF); // Return to active mode
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
if(UCA0RXBUF == 'T') //Turn on 2.1
{
P2OUT = BIT1;
}
if(UCA0RXBUF == 'O') //Turn off 2.1
{
P2OUT &=~ BIT1;
}
}