top of page

/****************************************************************************

*Sample analog

******************************************************************************/

 

/******************************************************************************

 WARNING

 Copyright 2011 this website owner. This document is the property of  this website owner. You may not possess, use, copy or disclose  this document or any information in it, for any purpose, including without  limitation, to design, manufacture or repair parts, or obtain any  government approval to do so, without this website owner express written permission.

 Neither receipt nor possession of this document alone,from any source, constitutes such permission. Possession, use, copying or disclosure by  anyone without website owner express written permission is not authorized and may  result in criminal and/or civil liability.

*******************************************************************************

 

*******************************************************************************

* Module: analog.c

*******************************************************************************

* Author(s):

*******************************************************************************

* Description: This module is responsible for analog software

*******************************************************************************

* Revision history:

* Revision   date       Initials   Comments

* -----------------------------------------------------------------------------

 

*******************************************************************************

*$Header$

*******************************************************************************

*$Log$

*******************************************************************************

*$NoKeywords$

*/

 

 

#define AD_OFF              0x00

#define AD_COMPARATORS_OFF  0x07

#define AD_CONV_REF1  0x3A

#define AD_CONV_REF2  0x0A

#define AD_CONV_CLK   0x82

#define AD_ON               0x01

#define AD_CHAN_MASK     0xC3

#define BYTE_WIDTH          0x08

 

/* 5 analog input channels */

enum adc_channel_num

{

  ADC_CH4 = 0x04,

  ADC_CH12= 0x0C,

  ADC_CH0 = 0x00,

  ADC_CH10 = 0xA,

  ADC_CH11=  0xB,

  ADC_CH1=  0x1,

  ADC_CH2=  0x2,

  ADC_CH3=  0x3,

  ADC_CH4=  0x4,

  ADC_CH5=  0x5,

  ADC_CH6=  0x6,

  ADC_CH7=  0x7,

  ADC_CH8=  0x8,

  ADC_CH9=  0x9,

  ADC_CH13=  0x1,

  ADC_CH14=  0x14,

  ADC_CH15=  0x15,

 

};

 

U8 channelnum;

 

void adc_init(void)

{

              ADCON0 = AD_OFF;

                            

              CMCON  = AD_COMPARATORS_OFF;

           

              /* Set AN0-4 are analog inputs, AN5-11 are Digital I/O  */

              ADCON1 = AD_CONV_REF1;

              /* conversion clock = Fosc/32 (at least 1.8 us)  */

              ADCON2 = AD_CONV_CLK;

           

              configure_adc_channel( channelnum);

           

              /* Set A/D on */

              ADON = AD_ON;

           

              return;

}

 

 

void configure_adc_channel( enum adc_channel_num  )

{

              if( channel == ADC_MEASUREMENT_CH )

              {

             

                ADCON1 = AD_CONV_REF1; 

              }              

              else

              {

                

                ADCON1 = AD_CONV_REF2; 

              }

           

              ADCON0 = (ADCON0 & AD_CHAN_MASK) | channel;

           

              return;

}

 

void kick_adc_conversion(void)

{

              ADIF    = FALSE; 

           

              /* starts a conversion */

              GO_DONE = TRUE; 

           

              return;

            }

           

           

            bool is_adc_conversion_bit_set(void)

            {

              bool  result;

              /* the A/D conversion complete interrupt  bit */

              if(ADIF == FALSE)

              {

              

                result = FALSE;

              }

              else

              {

               

                ADIF = FALSE;

                result = TRUE;

              } 

           

              return result;

            }

           

            u16 adc_result(void)

            {

              u16 result;

              result = (ADRESH <<0x8)|ADRESL;

              return result;   

            }

           

            /*configure_adc_channel() for each channel  & adc_init() is done only once after boot*/

           

            void adc_data_acquisition(void)

            {

           

           

           

            kick_adc_conversion();

           

            if(is_adc_conversion_bit_set())

            {

           

           

           

           

            }

 

 

 

}

bottom of page