AnsweredAssumed Answered

urgent (read 3 ADC channel)

Question asked by sy.see on Aug 22, 2013
first thanks for spenting your time to me. I need read 3 channel for AIN0,AIN2 and AIN3. But i cant get the analog reading. it always show me constant value even my analog input changed. I'll post related about ADC part..Please help me. thanks in advance

 // Initialisation of ADC
  
  ADC1_ConversionConfig(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_3, ADC1_ALIGN_LEFT);
  ADC1_PrescalerConfig(ADC1_PRESSEL_FCPU_D18);
  ADC1_Cmd(ENABLE);
  ADC1_ITConfig(ADC1_IT_EOCIE, ENABLE);
  ADC1_StartConversion();


 INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
    //  ADC Conversion completed interrupt handler.
   
    u16 ADC_Raw_Value;
    static u16 ADC_dIntegrator=0;
    static u16 ADC_itIntegrator=0;
    static u8 ADC_mCount=0;
    
    ADC1->CSR &= ~ADC1_CSR_EOC;            //  Indicate that ADC conversion is complete.       
    ADC_Raw_Value = ADC1->DRH;            //  Read Most Significant bits first 
    ADC_Raw_Value <<= 2;                  //  Switch 2 bit to left(D9-D2)
    ADC_Raw_Value |= (ADC1->DRL & 0x03);    //  Add most and least significant bits 
    ADC_mCount++;
    ADC_itIntegrator+= ADC_Raw_Value;
    if(ADC_mCount == 20)
    {
      ADC_dIntegrator=ADC_itIntegrator ;
      callbackADC_Integrator(ADC_dIntegrator);
      DALIslave_SetAdcDataReadyFlag(TRUE);
      ADC_dIntegrator = 0;
      ADC_mCount = 0;           


    }


 }

void callbackADC_Integrator(u16 itADC_Integrator)
{
  ADC_Integrator = itADC_Integrator;
}


void DALIslave_SetAdcDataReadyFlag(bool state)
{
  AdcDataReadyFlag = state;
}


bool DALIslave_GetAdcDataReadyFlag(void)
{
  return AdcDataReadyFlag;
}


void DALIslave_AvgADCValue()
{
  static u8 AdcChannel = 0;
  u16 ADC_final;
  u8 ADC_low;
  u8 ADC_high;
  
  if (AdcChannel == 0)
  {
    ADC1->CR1 &= ~ADC1_CR1_ADON;                          //disable ADC conversion channel-0
    ADC1->CSR |= ADC1_CHANNEL_2;
    ADC1->CR1 |= ADC1_CR1_ADON;                           //start ADC conversion channel-2
    AdcChannel++;                                            //set to channel-2(current)
   
    ADC_temperature = ADC_Integrator / 20;                //get temperature average    
    ADC_final = (ADC_temperature );//* 33) / 10;
    ADC_low = (u8)(ADC_final & 0xff);           
    ADC_high =(u8)((ADC_final >> 8 )& 0xff);
    DALIP_SetTemperature(ADC_high, ADC_low);              //set temperature to RAM
  }
  
  else if (AdcChannel == 1)
  {
    ADC1->CR1 &= ~ADC1_CR1_ADON;                          //disable ADC conversion channel-2
    ADC1->CSR |= ADC1_CHANNEL_3;                          //set to channel-3(voltage)
    ADC1->CR1 |= ADC1_CR1_ADON;                           //start ADC conversion channel-3
    AdcChannel++;
    
    ADC_current = ADC_Integrator / 20;                    //get current average   
    ADC_final = (ADC_current );//* 33) / 10;
    ADC_low = (u8)(ADC_final & 0xff);
    ADC_high = (u8)((ADC_final >> 8 )& 0xff);
    DALIP_SetCurrent(ADC_high, ADC_low);                  //set current to RAM 
  }
  
  else
  {
    ADC1->CR1 &= ~ADC1_CR1_ADON;                          //disable ADC conversion channel-3    
    ADC1->CSR |= ADC1_CHANNEL_0;
    ADC1->CR1 |= ADC1_CR1_ADON;                           //set to channel-0(temperature)
    AdcChannel = 0;
    
    ADC_voltage = ADC_Integrator / 20;                    //get voltage average
    ADC_final = (ADC_voltage );//* 33) / 10;
    ADC_low = (u8)(ADC_final & 0xff);
    ADC_high =(u8)((ADC_final >> 8 )& 0xff);
    DALIP_SetVoltage(ADC_high, ADC_low);                  //set temperature to RAM
  }
  AdcDataReadyFlag = FALSE;
}




void main(void)
{
  while(1)

     if (DALIslave_GetAdcDataReadyFlag() == TRUE)
     {
       DALIslave_AvgADCValue();
     }  

}


  

Outcomes