lpcware

ADC Read giving me 0!

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by kamhagh on Fri Sep 26 06:52:20 MST 2014
this is my library!
:
 #include "LPC17xx.H"

void adc_init()
{
  LPC_SC->PCONP |= (1 << 12); //set PCADC for power on ADC ;
  LPC_ADC->ADCR |= (1 << 21);  // start adc
  LPC_PINCON->PINSEL1 &= ~0x003FC000; /* P0.23~26, A0.0~3, function 01 */
  LPC_PINCON->PINSEL1 |= 0x00154000;
  LPC_PINCON->PINMODE1 &= ~0x003FC000;
  LPC_PINCON->PINMODE1 |= 0x002A8000;
}

int ADCRead(char channelNum) {
int data;
  LPC_ADC->ADCR &= 0xFFFFFF00;  // clear channels
  LPC_ADC->ADCR |= (1 << 24) | (1 << channelNum);  // set chanel and start convert
  while (!(LPC_ADC->ADGDR & (1<<31)));    //wait to done
  data = LPC_ADC->ADDR7;
  LPC_ADC->ADCR &= 0xF8FFFFFF;  /* stop ADC now */

  data = ( data >> 4 ) & 0xFFF;
  return ( data );  /* return A/D conversion value */
}


the code is not good but its just a testing library !

here's my main.c

/*-------------------------------------------------------------------------------
www.ECA.ir
   LPC1768 Dev Board Rev.A
--------------------------------------------------------------------------------*/
#include <LPC17xx.h>
#include <stdio.h>
#include "uart.h"
#include "adc.h"


void Delay (uint32_t Time)
{
    uint32_t i;
   
    i = 0;
    while (Time--) {
        for (i = 0; i < 5000; i++);
    }
}

int main(void)
{
volatile int i=0;
unsigned char lcd_buf[32];

SystemInit();
adc_init();
UART0_Init();

    while (1)
  {
i++;
sprintf(lcd_buf,"AnalogRead(%d)(A0): %d\r\n\n",i,ADCRead(0));
UART0_SendString(lcd_buf);
    Delay(2000);
    }
}


i get this:

AnalogRead(numer of AR)(A0) : 0, its always 0

why i keep getting 0?

Outcomes