Inital working code

This commit is contained in:
2025-01-30 20:02:06 -05:00
commit ea24f650d1
49 changed files with 6617 additions and 0 deletions

View File

@@ -0,0 +1,139 @@
/**
* ADC Generated Driver API Header File
*
* @file adc.h
*
* @defgroup adc ADC
*
* @brief This file contains the API prototypes and data types for the ADC driver.
*
* @version ADC Driver Version 2.1.3
*/
/*
<EFBFBD> [2025] Microchip Technology Inc. and its subsidiaries.
Subject to your compliance with these terms, you may use Microchip
software and any derivatives exclusively with Microchip products.
You are responsible for complying with 3rd party license terms
applicable to your use of 3rd party software (including open source
software) that may accompany Microchip software. SOFTWARE IS ?AS IS.?
NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS
SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT,
MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY
KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S
TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT
EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR
THIS SOFTWARE.
*/
#ifndef ADC_H
#define ADC_H
#include <xc.h>
#include <stdint.h>
#include <stdbool.h>
/**
@ingroup adc
@typedef adc_result_t
@brief Used for the result of the Analog-to-Digital (A/D) conversion.
*/
typedef uint16_t adc_result_t;
/**
* @ingroup adc
* @typedef adc_sync_double_result_t
* @struct Used for the result for a Double ADC conversion value.
*/
typedef struct
{
adc_result_t adcResult1;
adc_result_t adcResult2;
} adc_sync_double_result_t;
/**
* @ingroup adc
* @enum adc_channel_t
* @brief Contains the available ADC channels.
*/
typedef enum
{
channel_AVSS = 0x3b,
channel_Temp = 0x3c,
channel_DAC1 = 0x3d,
channel_FVR_BUF1 = 0x3e,
channel_FVR_BUF2 = 0x3f,
channel_ANC0 = 0x10
} adc_channel_t;
/**
Section: ADC Module APIs
*/
/**
* @ingroup adc
* @brief Initializes the ADC module. This routine is called before any other ADC routine.
* @param None.
* @return None.
*/
void ADC_Initialize(void);
/**
* @ingroup adc
* @brief Selects the channel for the A/D conversion.
* @param channel - Analog channel number on which the A/D conversion will be applied.
* Refer to adc_channel_t for the available channels.
* @return None.
*/
void ADC_SelectChannel(adc_channel_t channel);
/**
* @ingroup adc
* @brief Starts A/D conversion.
* @param None.
* @return None.
*/
void ADC_StartConversion(void);
/**
* @ingroup adc
* @brief Checks if ongoing A/D conversion is complete.
* @param None.
* @retval True - A/D conversion is complete.
* @retval False - A/D conversion is ongoing.
*/
bool ADC_IsConversionDone(void);
/**
* @ingroup adc
* @brief Retrieves the result of the latest A/D conversion.
* @param None.
* @return The result of A/D conversion. Refer to the adc_result_t.
*/
adc_result_t ADC_GetConversionResult(void);
/**
* @ingroup adc
* @brief Retrieves the result of a single A/D conversion on any given channel.
* @param channel - Analog channel number on which the A/D conversion will be applied.
* Refer to adc_channel_t for the available channels.
* @return The result of A/D conversion. Refer to the adc_result_t.
*/
adc_result_t ADC_GetConversion(adc_channel_t channel);
/**
* @ingroup adc
* @brief Adds the acquisition delay for the temperature sensor.
* @pre This function is called when temperature sensor is used.
* @param None.
* @return None.
*/
void ADC_TemperatureAcquisitionDelay(void);
#endif //ADC_H

View File

@@ -0,0 +1,125 @@
/**
* ADC Generated Driver File
*
* @file adc.c
*
* @ingroup adc
*
* @brief This file contains the API implementations for the ADC module.
*
* @version ADC Driver Version 2.1.3
*/
/*
<EFBFBD> [2025] Microchip Technology Inc. and its subsidiaries.
Subject to your compliance with these terms, you may use Microchip
software and any derivatives exclusively with Microchip products.
You are responsible for complying with 3rd party license terms
applicable to your use of 3rd party software (including open source
software) that may accompany Microchip software. SOFTWARE IS ?AS IS.?
NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS
SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT,
MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY
KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S
TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT
EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR
THIS SOFTWARE.
*/
/**
Section: Included Files
*/
#include <xc.h>
#include "../adc.h"
#include "../../system/clock.h"
/**
Section: Macro Declarations
*/
#define ACQ_US_DELAY 5
/**
Section: ADC Module APIs
*/
void ADC_Initialize(void)
{
//ADPREF VDD; ADCS FOSC/64; ADFM right;
ADCON1 = 0xE0;
//ADRESL 0x0;
ADRESL = 0x0;
//ADRESH 0x0;
ADRESH = 0x0;
//ADACT disabled;
ADACT = 0x0;
//ADON enabled; GO_nDONE stop; CHS ANC0;
ADCON0 = 0x41;
//Clear the ADC interrupt flag
PIR1bits.ADIF = 0;
}
void ADC_SelectChannel(adc_channel_t channel)
{
//Selects the A/D channel
ADCON0bits.CHS = channel;
}
void ADC_StartConversion(void)
{
//Starts the conversion
ADCON0bits.GOnDONE = 1;
}
bool ADC_IsConversionDone(void)
{
//Returns the conversion status
return ((bool)(!ADCON0bits.GOnDONE));
}
adc_result_t ADC_GetConversionResult(void)
{
//Conversion finished, returns the result
return ((adc_result_t)((ADRESH << 8) + ADRESL));
}
adc_result_t ADC_GetConversion(adc_channel_t channel)
{
//Selects the A/D channel
ADCON0bits.CHS = channel;
//Turns on the ADC module
ADCON0bits.ADON = 1;
//Acquisition time delay
__delay_us(ACQ_US_DELAY);
//Starts the conversion
ADCON0bits.GOnDONE = 1;
//Waits for the conversion to finish
while (ADCON0bits.GOnDONE)
{
}
//Conversion finished, returns the result
return ((adc_result_t)((ADRESH << 8) + ADRESL));
}
void ADC_TemperatureAcquisitionDelay(void)
{
__delay_us(200);
}