#include #include uint16_t getAdc(uint16_t channel); void print(char *text); void printNumber(unsigned int num); volatile long Ic=0; volatile long Vc=0; volatile int startFlag=0; int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer if (CALBC1_16MHZ==0xFF) // Check if calibration constant erased { while(1); // do not load program } DCOCTL = 0; // Select lowest DCO settings BCSCTL1 = CALBC1_16MHZ; // Set DCO to 8 MHz DCOCTL = CALDCO_16MHZ; //PWM MODULE int z,y,x; P2DIR |= BIT1+BIT4; // Set Pins as Timer Output P2SEL |= BIT1+BIT4; // Set Pins as Timer Output TA1CCTL1 |= OUTMOD_7; // Set Waveform Mode - Clear on Compare, Set on Overflow TA1CCTL2 |= OUTMOD_7; // Set Waveform Mode - Clear on Compare, Set on Overflow TA1CTL |= TASSEL_2 + ID_0 + MC_1; // Timer Clock -> SMCLK, Divider -> /1, Mode -> Up Count TA1CCR0 |= 100; //ADC MODULE ADC10CTL1 = ADC10SSEL_0 | ADC10DIV_4; ADC10CTL0 |= REFON + REF2_5V + ADC10SHT_3 + ADC10ON; __delay_cycles(10000); //UART MODULE P1SEL = BIT1 + BIT2 ; // Select UART RX/TX function on P1.1,P1.2 P1SEL2 = BIT1 + BIT2; UCA0CTL1 |= UCSSEL_2; // UART Clock -> SMCLK UCA0BR0 = 138; // Baud Rate Setting for 1MHz 9600 UCA0BR1 = 0; // Baud Rate Setting for 1MHz 9600 UCA0MCTL = UCBRS_7; // Modulation Setting for 1MHz 9600 UCA0CTL1 &= ~UCSWRST; IE2 |= UCA0RXIE; // Enable RX interrupt __bis_SR_register(GIE); // Enter LPM0, Enable Interrupt while(1) { if(startFlag){ TA1CCR1 = 0; TA1CCR2 = 0; __delay_cycles(100000); z=0; for (y=0;y<8;y++){ TA1CCR2 = z; for (x=0;x<100;x++){ TA1CCR1 = x; __delay_cycles(1000); Vc = 0; Ic = 0; int v; for(v = 0; v < 10; v++){ Vc += getAdc(6); Ic += getAdc(4); } Vc /= 10; Ic /= 10; printNumber(Ic); // Print values on UART print(","); printNumber(Vc); print("\r\n"); // Print newline } z = z + 12; } startFlag = 0; } else { z=0; for (y=0;y<8;y++){ TA1CCR2 = z; for (x=0;x<100;x++){ TA1CCR1 = x; __delay_cycles(1000); } z = z + 12; } } } return 0; } #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR (void) { __bic_SR_register_on_exit(CPUOFF); // return to active mode } uint16_t getAdc(uint16_t channel) { ADC10CTL0 &= ~ENC; ADC10CTL1 = ADC10SSEL_0 | ADC10DIV_4; ADC10CTL0 = REFON | REF2_5V | ADC10ON | ADC10SHT_3 | ADC10IE; ADC10CTL1 |= (channel << 12); ADC10AE0 = (1 << channel); __delay_cycles(128); ADC10CTL0 |= ENC | ADC10SC; // enable ADC and start conversion while (ADC10CTL1 & ADC10BUSY) { // sleep and wait for completion __bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled } ADC10CTL0 &= ~(ENC); ADC10CTL0 &= ~(ADC10ON | REFON); return ADC10MEM; } #pragma vector=USCIAB0RX_VECTOR // UART RX Interrupt Vector __interrupt void USCI0RX_ISR(void) { if(UCA0RXBUF == 'S') { startFlag = 1; __bic_SR_register_on_exit(CPUOFF); } } void print(char *text) { unsigned int i = 0; while(text[i] != '\0') { while (!(IFG2&UCA0TXIFG)); // Check if TX is ongoing UCA0TXBUF = text[i]; // TX -> Received Char + 1 i++; } } void printNumber(unsigned int num) { char buf[6]; char *str = &buf[5]; *str = '\0'; do { unsigned long m = num; num /= 10; char c = (m - 10 * num) + '0'; *--str = c; } while(num); print(str); }