Cara Setting ADC LPC 1768

Untuk setting adc LPC1768, ada beberapa register yg perlu di setting.

Berdasarkan datasheet LPC1768 "(UM10360).

Berikut contoh fungsi programnya untuk setting ADC0.7

Parameter: 
ADC_Clk : kecepatan conversi adc

uint32_t ADCInit( uint32_t ADC_Clk )
{
  uint32_t pclkdiv, pclk;
  /* Enable CLOCK into ADC controller */
  LPC_SC->PCONP |= (1 << 12);                                           //mengaktifkan adc

  
  pclkdiv = (LPC_SC->PCLKSEL0 >> 24) & 0x03;              //mengambil clock dari adc
  

  switch ( pclkdiv )
  {
    case 0x00:
    default:
       pclk = SystemCoreClock/4;
    break;
    case 0x01:
      pclk = SystemCoreClock;
    break;
    case 0x02:
      pclk = SystemCoreClock/2;
    break;
    case 0x03:
      pclk = SystemCoreClock/8;
    break;
  }
  LPC_ADC->ADCR = ( 1 << 7 ) |     /* SEL=1,select channel 0 on ADC.7 */       //aktifkan pin
  ( ( pclk  / ADC_Clk - 1 ) << 8 ) |  /* CLKDIV = Fpclk / ADC_Clk - 1 */
  ( 0 << 16 ) |         /* BURST = 0, no BURST, software controlled */
  ( 0 << 17 ) |          /* CLKS = 0, 11 clocks/10 bits */
  ( 1 << 21 ) |          /* PDN = 1, normal operation */
  ( 0 << 24 ) |          /* START = 0 A/D conversion stops */   
  ( 0 << 27 );        /* EDGE = 0 (CAP/MAT singal falling,trigger A/D conversion) */
   
  LPC_PINCON->PINSEL0 |= (1<<5);       
  LPC_PINCON->PINMODE0 |= (1<<5);
  return (1);
}



Berikut contoh fungsi programnya untuk ambil data ADC0.7
Parameter: channelNum: nomer ADC 

uint32_t ADC0Read( uint8_t channelNum )
{
  #if !ADC_INTERRUPT_FLAG
    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->ADCR &= 0xFFFFFF00;
  LPC_ADC->ADCR |= (1 << 24) | (1 << channelNum);
  

/* switch channel,start A/D convert */
  #if !ADC_INTERRUPT_FLAG
  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->ADCR &= 0xF8FFFFFF;    /* stop ADC now */
  if ( regVal & ADC_OVERRUN )    /* save data when it's not overrun, otherwise, return zero */
  {
    return ( 0 );
  }
  ADC_Data = ( regVal >> 4 ) & 0xFFF;
  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
}



Komentar

Postingan populer dari blog ini

Cara Menambahkan Pack Installer ke Keil uVision, Menambahkan Board / Device

Mencoba STM32-L476RG Nucleo Board dengan Keil uVision 5

System Requirments untuk Instal Keil uVision v5