Other Parts Discussed in Thread: MSP430G2231
Hi,
I use the 2231 on the launchpad to read 2 A/D ch. on P1.4 and P1.5 and set PWM on P1.6 (green LED).
When the A/D reads the ports the PWM goes on and off with no reason.
How can we separate the PWM and the AD operations?
I use the following code:
Init code:
// init system ports
void init_sys(void){
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= BIT0 + BIT6; // Set P1.0 and P1.6 to output direction
}
// init A/D global parameters
void init_AD(void){
ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE;
}
// init PWM global parameters
void init_PWM(){
P1SEL |= BIT6; // P1.6 to TA0.1 PWM
CCR0 = period; // PWM period
CCTL1 = OUTMOD_3; // CCR1 reset/set
CCR1 = period - 1; // CCR1 duty cycle
TACTL = TASSEL_2 + MC_1; // SMCLK, up mode
}
Run code:
// set PWM duty cycle
void set_PWM(int duty_cycle){
CCR1 = duty_cycle; // set new on time
TACTL = TASSEL_2 + MC_1; // SMCLK, up mode
__delay_cycles (10); // delay program execution
}
// get A/D reading from channel ch after avergng 2^avg counts, return result as integer
int get_ad_ch(int ch, int avg)
{
long int VolSum;
int i, count, ad_ch;
switch(ch){ // input channel
case 0: {ADC10CTL1 = INCH_0; break;}
case 1: {ADC10CTL1 = INCH_1; break;}
case 2: {ADC10CTL1 = INCH_2; break;}
case 3: {ADC10CTL1 = INCH_3; break;}
case 4: {ADC10CTL1 = INCH_4; break;}
case 5: {ADC10CTL1 = INCH_5; break;}
case 6: {ADC10CTL1 = INCH_6; break;}
case 7: {ADC10CTL1 = INCH_7; break;}
case 8: {ADC10CTL1 = INCH_8; break;}
case 9: {ADC10CTL1 = INCH_9; break;}
case 10: {ADC10CTL1 = INCH_10; break;}
case 11: {ADC10CTL1 = INCH_11; break;}
case 12: {ADC10CTL1 = INCH_12; break;}
case 13: {ADC10CTL1 = INCH_13; break;}
case 14: {ADC10CTL1 = INCH_14; break;}
case 15: {ADC10CTL1 = INCH_15; break;}
} // switch.
ad_ch = 0x01 << ch;
ADC10AE0 |= ad_ch; // PA.1 ADC option select
count = 0x01 << avg;
VolSum = 0;
for (i=0; i < count; i++){
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
VolSum += ADC10MEM;
} // for i
// VolSum = VolSum >> avg; // divide by 2^avg
return VolSum >> avg;
}