[SOLVED] Read an Analog Input - strange values as output

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

[SOLVED] Read an Analog Input - strange values as output

1,556 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Mondo90 on Thu Aug 21 11:31:40 MST 2014
Hello, as In subject I have some difficulties reading analog input on my LPC1114. I use example program called "adc_main" which use function "ADC_Read" to read voltage on input channel. The problem is even when no voltage is applied to the channel I am getting non zero voltage and what is more interesting the output is changing in time o.0. On the other hand when voltage is applied the output is constant and is equal to "1023", no matter how big the voltage is !

What is wrong ? ;(

Fragemnt of main function:
 ADCInit( ADC_CLK );

  while(1)
  {

#if CONFIG_ADC_ENABLE_BURST_MODE==1/* Interrupt driven only */
ADCBurstRead();
while ( !ADC0IntDone );
ADC0IntDone = 0;
#else/* Not burst mode */
#if CONFIG_ADC_ENABLE_ADC_IRQHANDLER==1/* Interrupt driven */
for ( i = 0; i < ADC_NUM; i++ )
{
  ADCRead( i );
  while ( !ADCIntDone );
  ADCIntDone = 0;
}
#else  /* Polling */
for ( i = 0; i < ADC_NUM; i++ )
{
  ADCValue = ADCRead( i );
}
#endif/* Endif interrupt driven */
#endif/* Endif BURST mode */
#ifdef SEMIHOSTED_ADC_OUTPUT
#ifndef OUTPUT_ONLY_CH0
for ( i = 0; i < ADC_NUM; i++ )
{
ADCBar(i, ADCValue);
}
#else
debug_printf("Voltage on AD0 => %d |\n",ADCRead(0));
//ADCBar(0, ADCValue[0]);

#endif
#endif
}
}

ADCInit function
void ADCInit( uint32_t ADC_Clk )
{
  uint32_t i;

  /* Disable Power down bit to the ADC block. */  
  LPC_SYSCON->PDRUNCFG &= ~(0x1<<4);

  /* Enable AHB clock to the ADC. */
  LPC_SYSCON->SYSAHBCLKCTRL |= (1<<13);

  for ( i = 0; i < ADC_NUM; i++ )
  {
ADCValue = 0x0;
  }
  /* Unlike some other pings, for ADC test, all the pins need
  to set to analog mode. Bit 7 needs to be cleared according 
  to design team. */
  LPC_IOCON->R_PIO0_11 &= ~0x8F; /*  ADC I/O config */
  LPC_IOCON->R_PIO0_11 |= 0x02;  /* ADC IN0 */
  LPC_IOCON->R_PIO1_0  &= ~0x8F;
  LPC_IOCON->R_PIO1_0  |= 0x02;  /* ADC IN1 */
  LPC_IOCON->R_PIO1_1  &= ~0x8F;
  LPC_IOCON->R_PIO1_1  |= 0x02;  /* ADC IN2 */
  LPC_IOCON->R_PIO1_2 &= ~0x8F;
  LPC_IOCON->R_PIO1_2 |= 0x02; /* ADC IN3 */
#ifdef __SWD_DISABLED
  LPC_IOCON->SWDIO_PIO1_3   &= ~0x8F;
  LPC_IOCON->SWDIO_PIO1_3   |= 0x02;  /* ADC IN4 */
#endif
  LPC_IOCON->R_PIO0_11   = 0x02;// Select AD0 pin function
  LPC_IOCON->R_PIO1_0    = 0x02;// Select AD1 pin function
  LPC_IOCON->R_PIO1_1    = 0x02;// Select AD2 pin function
  LPC_IOCON->R_PIO1_2    = 0x02;// Select AD3 pin function
//  LPC_IOCON->ARM_SWDIO_PIO1_3    = 0x02;// Select AD4 pin function
  LPC_IOCON->PIO1_4    = 0x01;// Select AD5 pin function
  LPC_IOCON->PIO1_10   = 0x01;// Select AD6 pin function
  LPC_IOCON->PIO1_11   = 0x01;// Select AD7 pin function

  LPC_ADC->CR = ((SystemCoreClock/LPC_SYSCON->SYSAHBCLKDIV)/ADC_Clk-1)<<8;

  /* If POLLING, no need to do the following */
#if CONFIG_ADC_ENABLE_ADC_IRQHANDLER==1
  NVIC_EnableIRQ(ADC_IRQn);
  LPC_ADC->INTEN = 0x1FF;/* Enable all interrupts */
#endif
  return;
}

ADCRead function:
uint32_t ADCRead( uint8_t channelNum )
{
#if CONFIG_ADC_ENABLE_ADC_IRQHANDLER!=1
  uint32_t regVal, ADC_Data;
#endif

  /* channel number is 0 through 7 */
  if ( channelNum >= ADC_NUM )
  {
channelNum = 0;/* reset channel number to 0 */
  }
  LPC_ADC->CR &= 0xFFFFFF00;
  LPC_ADC->CR |= (1 << 24) | (1 << channelNum);
/* switch channel,start A/D convert */
#if CONFIG_ADC_ENABLE_ADC_IRQHANDLER!=1
  while ( 1 )/* wait until end of A/D convert */
  {
regVal = *(volatile unsigned long *)(LPC_ADC_BASE 
+ ADC_OFFSET + ADC_INDEX * channelNum);
/* read result of A/D conversion */
if ( regVal & ADC_DONE )
{
  break;
}
  }
        
  LPC_ADC->CR &= 0xF8FFFFFF;/* stop ADC now */    
  if ( regVal & ADC_OVERRUN )/* save data when it's not overrun, otherwise, return zero */
  {
return ( 0 );
  }
  ADC_Data = ( regVal >> 6 ) & 0x3FF;
  return ( ADC_Data );/* return A/D conversion value */
#else
  return ( channelNum );/* if it's interrupt driven, the ADC reading is 
done inside the handler. so, return channel number */
#endif
}

Voltage output when no volatge is applied:
Voltage on AD0 => 465 |
Voltage on AD0 => 464 |
Voltage on AD0 => 492 |
Voltage on AD0 => 936 |
Voltage on AD0 => 682 |
Voltage on AD0 => 867 |
Voltage on AD0 => 465 |
Voltage on AD0 => 118 | 

UPDATE! Problem solved, these strange output results comes from no ground reference. My mistake.

Thanks for help.
0 Kudos
Reply
2 Replies

1,213 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Mondo90 on Fri Aug 22 08:23:43 MST 2014
starblue, I update my first post and now you can see my ADCInit function where I configure input pins. Anyway it is example code so it should work just fine and eveything should be configured well ;/

UPDATE! Problem solved, these strange output results comes from no ground reference. My mistake.

Thanks for help.
0 Kudos
Reply

1,213 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by starblue on Fri Aug 22 00:46:54 MST 2014
Did you configure the I/O-pins?
0 Kudos
Reply