text: /**
    ADC Generated Driver File
  
    @Company
      Microchip Technology Inc.
  
    @File Name
      adc.c
  
    @Summary
      This is the generated driver implementation file for the ADC driver using MPLAB� Code Configurator
  
    @Description
      This source file provides implementations for driver APIs for ADC.
      Generation Information :
          Product Revision  :  MPLAB� Code Configurator - v2.25.2
          Device            :  PIC16F1703
          Driver Version    :  2.00
      The generated drivers are tested against the following:
          Compiler          :  XC8 v1.34
          MPLAB             :  MPLAB X v2.35 or v3.00
   */
  
  /*
  Copyright (c) 2013 - 2015 released Microchip Technology Inc.  All rights reserved.
  
  Microchip licenses to you the right to use, modify, copy and distribute
  Software only when embedded on a Microchip microcontroller or digital signal
  controller that is integrated into your product or third party product
  (pursuant to the sublicense terms in the accompanying license agreement).
  
  You should refer to the license agreement accompanying this Software for
  additional information regarding your rights and obligations.
  
  SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
  IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
  CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
  OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
  CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
  SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
   */
  
  /*
  
  This license is compatible with above license, when using this code with any
  Microchip microcontoller/signal processor. This project is designed for using it
  with Microchip controller product. If you want to change microcontrollers in
  your project, you can use all files except controller source code (with
  "Generated" keyword) - please write your own code for your own platform, and
  don't use code generator code (by the way - will Microchip code run on
  STM? ;> ).
  
  The MIT License (MIT)
  
  Copyright (c) 2015 S?awomir Kozok
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
  
  */
  
  /**
    Section: Included Files
   */
  
  #include <xc.h>
  #include "adc.h"
  #include "mcc.h"
  
  /**
    Section: Macro Declarations
   */
  
  #define ACQ_US_DELAY 5
  
  /**
    Section: ADC Module APIs
   */
  
  void ADC_Initialize(void) {
      // set the ADC to the options selected in the User Interface
  
      // GO_nDONE stop; ADON enabled; CHS AN0; 
      ADCON0 = 0x01;
  
      // ADPREF FVR; ADFM right; ADCS FOSC/2; 
      ADCON1 = 0x83;
  
      // TRIGSEL no_auto_trigger; 
      ADCON2 = 0x00;
  
      // ADRESL 0x0; 
      ADRESL = 0x00;
  
      // ADRESH 0x0; 
      ADRESH = 0x00;
  
  }
  
  void ADC_StartConversion(adc_channel_t channel) {
      // select the A/D channel
      ADCON0bits.CHS = channel;
  
      // Turn on the ADC module
      ADCON0bits.ADON = 1;
  
      // Acquisition time delay
      __delay_us(ACQ_US_DELAY);
  
      // Start the conversion
      ADCON0bits.GO_nDONE = 1;
  }
  
  bool ADC_IsConversionDone() {
      // Start the conversion
      return (!ADCON0bits.GO_nDONE);
  }
  
  adc_result_t ADC_GetConversionResult(void) {
      // Conversion finished, return the result
      return ((ADRESH << 8) + ADRESL);
  }
  
  adc_result_t ADC_GetConversion(adc_channel_t channel) {
      // Select the A/D channel
      ADCON0bits.CHS = channel;
  
      // Turn on the ADC module
      ADCON0bits.ADON = 1;
  
      // Acquisition time delay
      __delay_us(ACQ_US_DELAY);
  
      // Start the conversion
      ADCON0bits.GO_nDONE = 1;
  
      // Wait for the conversion to finish
      while (ADCON0bits.GO_nDONE) {
      }
  
      // Conversion finished, return the result
      return ((ADRESH << 8) + ADRESL);
  }
  /**
   End of File
   */