#include #include #include int UART_printf(const char *format, ...); //unsigned int ADC[4]; unsigned int ADC[4]; unsigned int Pos_ADC; unsigned int Vel_ADC; char newprint = 0; long timeint = 0; int freq = 4800; // Print to UART every .5 seconds void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT DCOCTL = CALDCO_16MHZ; // Set uC to run at approximately 16 Mhz BCSCTL1 = CALBC1_16MHZ; P1DIR |= 0xFF; // P1.0 and P1.7 outputs CCTL0 = CCIE; // CCR0 interrupt enabled CCR0 = 1653; // 1653 is 1/9600 seconds sets up timer for output only UART baud rate of 9600. TACTL = TASSEL_2 + MC_2; // SMCLK, contmode ADC10CTL1 = INCH_1 + ADC10SSEL_3 + CONSEQ_1; //INCH_1: block convert will capture channels 1 and 0 in that order. ADC10CTL0 = ADC10ON + MSC + ADC10IE; ADC10AE0 |= 0x03; // P1.1,0 ADC10 option select. ADC10DTC1 = 2; // Two conversions. ADC10SA = (short)&ADC[0]; // ADC10 data transfer starting address. ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start. //_BIS_SR(LPM0_bits + GIE); // LPM0, ADC10_ISR will force exit. _BIS_SR(GIE); // Enable global interrupt while(1) { if (newprint == 1) { // newprint set to one in timer ISR // P1OUT ^= 0x01; // toggle P1.0 on and off Pos_ADC = ADC[1]; Vel_ADC = ADC[0]; UART_printf("Digi %d %d\n\r",Pos_ADC,Vel_ADC); // Print to UART //UART_printf("YESa \n\r "); // Print to UART newprint = 0; // reset flag back to zero } } } //ADC10 interrupt Service Routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR (void) { ADC10CTL0 &= ~ADC10IFG; ADC10SA = (short)&ADC[0]; // ADC10 data transfer starting address. ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start. } // only 15 characters can be sent at a time #define UART_PRINTF_SIZE 15 char txbuff[UART_PRINTF_SIZE]; // global character buffer set in the function UART_printf and the output in the timer ISR function Timer_A signed char txcount = 0; // variables used to "Bit-Bang" the UART output transmission. signed char currentbit = -1; signed char currentindex = 0; signed char senddone = 1; // This function assumes txbuff has already been filled with the characters to send. // It initializes txcount to the number of chars to send and then sets senddone = 0 so that txbuff is sent out P1.7 in the function Timer_A int sendchars(int size) { if (senddone == 1) { // Only setup txcount if previous transmission complete if (size < UART_PRINTF_SIZE) { txcount = size; } else { txcount = UART_PRINTF_SIZE; } currentbit = -1; // initialize indexes currentindex = 0; senddone = 0; // signal that a new transmission should occur. return(0); } else { return(-1); // error } } int UART_printf(const char *format, ...) { // the "va" and "v" functions handle the variable argument ... in the function parameters va_list ap; int error; va_start(ap, format); /* Variable argument begin */ error = sendchars(vsprintf(txbuff, format, ap)); // fill txbuff with the format string and the pass the size of txbuff to the sendchars function va_end(ap); /* Variable argument end */ return error; } // Timer A0 interrupt service routine #pragma vector=TIMERA0_VECTOR __interrupt void Timer_A (void) { CCR0 += 1653; // Add Offset to CCR0 because timer 0 has been told to continuously count up. if (senddone == 0) { if (currentbit == -1) { P1OUT &= 0x7F;// start bit currentbit++; } else if (currentbit == 8) { P1OUT |= 0x80; // stop bit currentbit = -1; currentindex++; if (currentindex == txcount) { senddone = 1; } } else { if ((txbuff[currentindex]>>currentbit)&0x1) { P1OUT |= 0x80; // char bit is HI } else { P1OUT &= 0x7F; // char bit is LOW } currentbit++; } } timeint++; // Keep track of time for main while loop. This is incrementing even when characters are not being sent out if ((timeint%freq) == 0) { newprint = 1; // flag main while loop that "freq: number of ticks have gone by. } }