;=============================================================================== ; Title: Soldering Station Analog to Digital Converter ; ; Author: Rob Jansen, Copyright (c) 2021..2021, all rights reserved. ; ; Revisions ; --------- ; 2021-04-10 : Initial version. ; ; Description: Reads the temperature sensor and the potentiometer and converts ; the analog signals to a digital value. ; ; Sources: - ; ; --------------------------------- Pins --------------------------------------- ; The temperature sensor is connected to AN11 (RB5) and the potentiometer ; to AN10 (RB4). ; -------------------------- Constant declarations ----------------------------- ; Selection of the ADC inputs. const byte ADC_INPUT_POTENTIOMETER = 0b01010 ; Sensor on RB4 (AN10). const byte ADC_INPUT_SENSOR = 0b01011 ; Sensor on RB5 (AN11). ; -------------------------- Variable declarations ----------------------------- var word adc_capture_value var bit adc_data_ready ; ------------------------- Functions and Procedures---------------------------- ; Init the ADC and the GPIO pins (AN0). We follow the steps from the datasheet. ; Note that we also need to set the refence voltage so we need to start at least ; one ADC conversion. This procedure must be called after the power supply is ; powerd on and the supply voltage is set. procedure adc_init() Is adc_data_ready = FALSE ; Set the right ports to analog input. ANSELA = 0b0000_0000 ; All digital I/O. ANSELB = 0b0011_0000 ; RB4 and RB5 analog input. ANSELC = 0b0000_0000 ; All digital I/O. ; Select the corrsponsing adc input. ADCON0_CHS = ADC_INPUT_POTENTIOMETER ADCON0_ADON = TRUE ; Set right justified and Fosc/64, negative reference voltage to ground and ; positive reference voltage to VDD. ADCON1 = 0b1110_0000 ; ADC conversion takes about 2 us. ; Enable the ADC interrupts. PIR1_ADIF = FALSE PIE1_ADIE = TRUE end procedure ; Handle the ADC interrupt. procedure adc_interrupt() is pragma interrupt if PIR1_ADIF & PIE1_ADIE then adc_capture_value = (word(ADRESH) * 256) + word(ADRESL) adc_data_ready = TRUE PIR1_ADIF = FALSE end if end procedure ; Return TRUE if the adc is ready. function adc_ready() return bit is return !ADCON0_GO_NDONE end function ; Return TRUE if the new ADC data is available. function adc_data_available() return bit is return adc_data_ready end function ; Return the captured ADC value. After this call no data is available. function adc_get_value() return word is var word adc_new_value var bit adc_enable ; Disable ADC interrupt as to prevent data change because of ADC interrupt. adc_enable = PIE1_ADIE PIE1_ADIE = FALSE adc_new_value = adc_capture_value adc_data_ready = FALSE PIE1_ADIE = adc_enable return adc_new_value end function ; Start an Analog to Digital Conversion for capaturing the potentiometer data. ; Note: Some time must have been taken into account after setting the correct ; ADC input and starting a new AD conversion. procedure adc_start_conversion() is var bit adc_enable adc_enable = PIE1_ADIE PIE1_ADIE = FALSE if adc_ready() then adc_data_ready = FALSE ADCON0_GO_NDONE = TRUE end if PIE1_ADIE = adc_enable end procedure ; Select the sensor as input for the ADC. Note: Some delay must ; be taken into account for the input to settle before starting ; a new AD conversion. procedure adc_select_sensor_input() is ADCON0_CHS = ADC_INPUT_SENSOR end procedure ; Select the potentiometer as input for the ADC. Note: Some delay must ; be taken into account for the input to settle before starting ; a new AD conversion. procedure adc_select_potentiometer_input() is ADCON0_CHS = ADC_INPUT_POTENTIOMETER end procedure