Hello all,
I've been trying to blink onboard LEDs using timerA upmode, but I want to control the TACCR0 value using ADC. I setup the following circuit and connected it to P1.1. What I want to do is to read the value from ADC10MEM and set TACCR0 accordingly.
I debugged the code, ADC10 is working. CCR0 value is set correctly. But whenever I run the code, It starts to blink aperiodically, then after a few seconds blinking turns to normal. However, turning the potentiometer changes nothing, LED just blinks in a constant speed.
/*
* Controlling timer intervals using adc, and blink LED1 accordingly
*/
#include <msp430g2231.h>
// Timer Config
void configure_timer(x){
unsigned int i;
TACCTL0 = CCIE; // CCR0 interrupt enabled
CCR0 = 0; // CCR0 initalized
//x -> ADC10MEM
//since ADC10MEM <= 1024 I multiplied it by 60 to get visible blinking
for(i=0;i<60;i++) //For loop addition to avoid multipication
CCR0 += x;
TACTL = TASSEL_2 + MC_1 + ID_1 + TAIE; // SMCLK, upmode, Clk/2, Interrupt enabled
}
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled
ADC10CTL1 = INCH_1; // input A1
ADC10AE0 |= 0x02; // PA.1 ADC option select
P1DIR |= 0x01; // Set P1.0 to output direction
while(1)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit
}
}
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
__bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR)
TACTL = MC_0; // Close timer to configuration
configure_timer(ADC10MEM);
}
// Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
P1OUT ^= 0x01; // Toggle P1.0
}
Any help on the problem is appreciated.
Thanks a lot in advance.
Günay
