Skip to content
Snippets Groups Projects
Commit 8216257c authored by Carlo Alessandro Nicolau's avatar Carlo Alessandro Nicolau
Browse files

Under development (backup commit)

parent 2d75620b
No related branches found
No related tags found
No related merge requests found
Showing
with 3671 additions and 0 deletions
This diff is collapsed.
// Auto-generated file: variables.h
// Generation timestamp: 2021-05-28 16:22:48.444169
#ifndef _AUTOGENERATED_VARIABLES_H
#define _AUTOGENERATED_VARIABLES_H
#include "../../typedefs.h"
// analog reading info structure
typedef struct {
analog_channel_t channel; // ADC channel from which the variable can be read
analog_value_t value; // last acquired value
analog_variable_sum_t sum_curr; // current sum (to compute average)
analog_variable_sum_t sum_last; // last computed sum
analog_variable_sum_counter_t sum_counter; // current count for sum computation
analog_value_t offset; // offset value
analog_value_t value_max; // max value acquired
analogalarm_dual_info_t alarms; // structure maintaining all the info related to the slow and fast alarms
} analog_variable_t;
// number of defined analog variables
#define ANALOG_VARIABLES_COUNT 11
#define ANALOG_VARIABLE_INDEX_MON_DUL_BOARDTEMP 0 // index of the analog variable MON_DUL_BOARDTEMP inside analog_variables array
#define ANALOG_VARIABLE_INDEX_MON_TEMP2 1 // index of the analog variable MON_TEMP2 inside analog_variables array
#define ANALOG_VARIABLE_INDEX_MON_TEMP1 2 // index of the analog variable MON_TEMP1 inside analog_variables array
#define ANALOG_VARIABLE_INDEX_MON_VEOC_RTN_I 3 // index of the analog variable MON_VEOC_RTN_I inside analog_variables array
#define ANALOG_VARIABLE_INDEX_MON_VEOC_FWR_I 4 // index of the analog variable MON_VEOC_FWR_I inside analog_variables array
#define ANALOG_VARIABLE_INDEX_MON_HYDRO_I 5 // index of the analog variable MON_HYDRO_I inside analog_variables array
#define ANALOG_VARIABLE_INDEX_MON_INPUT_V 6 // index of the analog variable MON_INPUT_V inside analog_variables array
#define ANALOG_VARIABLE_INDEX_MON_LBL_I 7 // index of the analog variable MON_LBL_I inside analog_variables array
#define ANALOG_VARIABLE_INDEX_MON_GLRA_I 8 // index of the analog variable MON_GLRA_I inside analog_variables array
#define ANALOG_VARIABLE_INDEX_MON_GLRB_I 9 // index of the analog variable MON_GLRB_I inside analog_variables array
#define ANALOG_VARIABLE_INDEX_MON_PWB_I 10 // index of the analog variable MON_PWB_I inside analog_variables array
// make analog variable list available to anyone including this file
extern analog_variable_t analog_variables[ANALOG_VARIABLES_COUNT];
// Callback function for alarm on analog variable MON_DUL_BOARDTEMP (DUL board temperature sensor)
void alarm_MON_DUL_BOARDTEMP();
// Callback function for alarm on analog variable MON_TEMP2 (VICOR2 Heatsink (connector J12) temperature sensor signal acquisition)
void alarm_MON_TEMP2();
// Callback function for alarm on analog variable MON_TEMP1 (VICOR1 Heatsink (connector J1) temperature sensor signal acquisition )
void alarm_MON_TEMP1();
// Callback function for alarm on analog variable MON_VEOC_RTN_I (DU backbone return current sensor signal acquisition )
void alarm_MON_VEOC_RTN_I();
// Callback function for alarm on analog variable MON_VEOC_FWR_I (DU backbone forward current sensor signal acquisition )
void alarm_MON_VEOC_FWR_I();
// Callback function for alarm on analog variable MON_HYDRO_I (HYDRO current sensor signal acquisition )
void alarm_MON_HYDRO_I();
// Callback function for alarm on analog variable MON_INPUT_V (Main Input Line voltage sensor signal acquisition )
void alarm_MON_INPUT_V();
// Callback function for alarm on analog variable MON_LBL_I (LBL current sensor signal acquisition )
void alarm_MON_LBL_I();
// Callback function for alarm on analog variable MON_GLRA_I (GLENAIR-A current sensor signal acquisition)
void alarm_MON_GLRA_I();
// Callback function for alarm on analog variable MON_GLRB_I (GLENAIR-B current sensor signal acquisition)
void alarm_MON_GLRB_I();
// Callback function for alarm on analog variable MON_PWB_I (POWER BOARD current sensor signal acquisition)
void alarm_MON_PWB_I();
// digital reading info structure
typedef struct {
digital_value_readfunc_t readfunc; // function to read the value (must be defined by user)
digital_value_t value; // last acquired value
digital_value_t value_max; // max value acquired (...)
digitalalarm_info_t alarm; // structure maintaining all the info related to the alarm
} digital_variable_t;
// number of defined analog variables
#define DIGITAL_VARIABLES_COUNT 12
#define DIGITAL_VARIABLE_INDEX_FLAG_DUL_ALARMPOS1 0 // index of the analog variable FLAG_DUL_ALARMPOS1 inside analog_variables array
#define DIGITAL_VARIABLE_INDEX_FLAG_DUL_ALARMPOS2 1 // index of the analog variable FLAG_DUL_ALARMPOS2 inside analog_variables array
#define DIGITAL_VARIABLE_INDEX_FLAG_DUL_ALARMNEG1 2 // index of the analog variable FLAG_DUL_ALARMNEG1 inside analog_variables array
#define DIGITAL_VARIABLE_INDEX_FLAG_DUL_ALARMNEG2 3 // index of the analog variable FLAG_DUL_ALARMNEG2 inside analog_variables array
#define DIGITAL_VARIABLE_INDEX_FLAG_HYDRO_PWR_FAULT 4 // index of the analog variable FLAG_HYDRO_PWR_FAULT inside analog_variables array
#define DIGITAL_VARIABLE_INDEX_FLAG_LBL_PWR_FAULT 5 // index of the analog variable FLAG_LBL_PWR_FAULT inside analog_variables array
#define DIGITAL_VARIABLE_INDEX_FLAG_GLRA_FAULT 6 // index of the analog variable FLAG_GLRA_FAULT inside analog_variables array
#define DIGITAL_VARIABLE_INDEX_FLAG_GLRB_FAULT 7 // index of the analog variable FLAG_GLRB_FAULT inside analog_variables array
#define DIGITAL_VARIABLE_INDEX_FLAG_POWERBOARD_FAULT 8 // index of the analog variable FLAG_POWERBOARD_FAULT inside analog_variables array
#define DIGITAL_VARIABLE_INDEX_FLAG_GLRA_GOOD 9 // index of the analog variable FLAG_GLRA_GOOD inside analog_variables array
#define DIGITAL_VARIABLE_INDEX_FLAG_GLRB_GOOD 10 // index of the analog variable FLAG_GLRB_GOOD inside analog_variables array
#define DIGITAL_VARIABLE_INDEX_FLAG_POWERBOARD_GOOD 11 // index of the analog variable FLAG_POWERBOARD_GOOD inside analog_variables array
// make digital variable list available to anyone including this file
extern digital_variable_t digital_variables[DIGITAL_VARIABLES_COUNT];
// IO read function for variable FLAG_DUL_ALARMPOS1
digital_value_t ioread_DIN_ALRMPOS1(void);
// IO read function for variable FLAG_DUL_ALARMPOS2
digital_value_t ioread_DIN_ALRMPOS2(void);
// IO read function for variable FLAG_DUL_ALARMNEG1
digital_value_t ioread_DIN_ALRMNEG1(void);
// IO read function for variable FLAG_DUL_ALARMNEG2
digital_value_t ioread_DIN_ALRMNEG2(void);
// IO read function for variable FLAG_HYDRO_PWR_FAULT
digital_value_t ioread_DIN_HYDRO_PWR_FLT(void);
// IO read function for variable FLAG_LBL_PWR_FAULT
digital_value_t ioread_DIN_LBL_PWR_FLT(void);
// IO read function for variable FLAG_GLRA_FAULT
digital_value_t ioread_DIN_GLR_A_FLT(void);
// IO read function for variable FLAG_GLRB_FAULT
digital_value_t ioread_DIN_GLR_B_FLT(void);
// IO read function for variable FLAG_POWERBOARD_FAULT
digital_value_t ioread_DIN_PWB_FLT(void);
// IO read function for variable FLAG_GLRA_GOOD
digital_value_t ioread_DIN_GLR_A_PG(void);
// IO read function for variable FLAG_GLRB_GOOD
digital_value_t ioread_DIN_GLR_B_PG(void);
// IO read function for variable FLAG_POWERBOARD_GOOD
digital_value_t ioread_DIN_PWB_PG(void);
// Callback function for alarm on digital variable FLAG_DUL_ALARMPOS1 (DUL current limiting on VEOC direct alarm 1)
void alarm_FLAG_DUL_ALARMPOS1();
// Callback function for alarm on digital variable FLAG_DUL_ALARMPOS2 (DUL current limiting on VEOC direct alarm 2)
void alarm_FLAG_DUL_ALARMPOS2();
// Callback function for alarm on digital variable FLAG_DUL_ALARMNEG1 (DUL current limiting on VEOC return alarm 1)
void alarm_FLAG_DUL_ALARMNEG1();
// Callback function for alarm on digital variable FLAG_DUL_ALARMNEG2 (DUL current limiting on VEOC return alarm 2)
void alarm_FLAG_DUL_ALARMNEG2();
// Callback function for alarm on digital variable FLAG_HYDRO_PWR_FAULT (Fault status acquisition of 12V ISO HYDRO power line)
void alarm_FLAG_HYDRO_PWR_FAULT();
// Callback function for alarm on digital variable FLAG_LBL_PWR_FAULT (Fault status acquisition of 12V ISO LBL power line)
void alarm_FLAG_LBL_PWR_FAULT();
// Callback function for alarm on digital variable FLAG_GLRA_FAULT (Fault status acquisition of 12V GLENAIR-A power line)
void alarm_FLAG_GLRA_FAULT();
// Callback function for alarm on digital variable FLAG_GLRB_FAULT (Fault status acquisition of 12V GLENAIR-B power line)
void alarm_FLAG_GLRB_FAULT();
// Callback function for alarm on digital variable FLAG_POWERBOARD_FAULT (Fault status acquisition of 12V POWER BOARD power line)
void alarm_FLAG_POWERBOARD_FAULT();
// Callback function for alarm on digital variable FLAG_GLRA_GOOD (Power good status acquisition of 12V GLENAIR-A power line)
void alarm_FLAG_GLRA_GOOD();
// Callback function for alarm on digital variable FLAG_GLRB_GOOD (Power good status acquisition of 12V GLENAIR-B power line)
void alarm_FLAG_GLRB_GOOD();
// Callback function for alarm on digital variable FLAG_POWERBOARD_GOOD (Power good status acquisition of 12V POWER BOARD power line)
void alarm_FLAG_POWERBOARD_GOOD();
#include "../../errors.h"
typedef struct {
enum_switchstate_t state_SWITCH_VEOC_RETURN; // status of switch SWITCH_VEOC_RETURN, DU backbone ON/OFF return line control (high=ON)
enum_switchstate_t state_SWITCH_VEOC_DIRECT; // status of switch SWITCH_VEOC_DIRECT, DU backbone ON/OFF forward line control (high=ON)
enum_switchstate_t state_SWITCH_12V; // status of switch SWITCH_12V, DC-DC TDK 5/12V ISO ON/OFF control (high=ON)
enum_switchstate_t state_SWITCH_LBL; // status of switch SWITCH_LBL, 12V ISO LBL power ON/OFF control (high=ON)
enum_switchstate_t state_SWITCH_HYDRO; // status of switch SWITCH_HYDRO, 12V ISO HYDRO power ON/OFF control (high=ON)
enum_switchstate_t state_SWITCH_GLRA; // status of switch SWITCH_GLRA, 12V GLENAIR-A power ON/OFF control (high=ON)
enum_switchstate_t state_SWITCH_GLRB; // status of switch SWITCH_GLRB, 12V GLENAIR-B power ON/OFF control (high=ON)
enum_switchstate_t state_SWITCH_PWB; // status of switch SWITCH_PWB, 12V POWER BOARD power ON/OFF control (high=ON)
} switches_t;
extern switches_t switches;
// control functions for switch SWITCH_VEOC_RETURN, DU backbone ON/OFF return line control (high=ON)
void switch_SWITCH_VEOC_RETURN_set_closed(void);
void switch_SWITCH_VEOC_RETURN_set_open(void);
void switch_SWITCH_VEOC_RETURN_set(enum_switchstate_t _state);
enum_switchstate_t switch_SWITCH_VEOC_RETURN_get(void);
// control functions for switch SWITCH_VEOC_DIRECT, DU backbone ON/OFF forward line control (high=ON)
void switch_SWITCH_VEOC_DIRECT_set_closed(void);
void switch_SWITCH_VEOC_DIRECT_set_open(void);
void switch_SWITCH_VEOC_DIRECT_set(enum_switchstate_t _state);
enum_switchstate_t switch_SWITCH_VEOC_DIRECT_get(void);
// control functions for switch SWITCH_12V, DC-DC TDK 5/12V ISO ON/OFF control (high=ON)
void switch_SWITCH_12V_set_closed(void);
void switch_SWITCH_12V_set_open(void);
void switch_SWITCH_12V_set(enum_switchstate_t _state);
enum_switchstate_t switch_SWITCH_12V_get(void);
// control functions for switch SWITCH_LBL, 12V ISO LBL power ON/OFF control (high=ON)
void switch_SWITCH_LBL_set_closed(void);
void switch_SWITCH_LBL_set_open(void);
void switch_SWITCH_LBL_set(enum_switchstate_t _state);
enum_switchstate_t switch_SWITCH_LBL_get(void);
// control functions for switch SWITCH_HYDRO, 12V ISO HYDRO power ON/OFF control (high=ON)
void switch_SWITCH_HYDRO_set_closed(void);
void switch_SWITCH_HYDRO_set_open(void);
void switch_SWITCH_HYDRO_set(enum_switchstate_t _state);
enum_switchstate_t switch_SWITCH_HYDRO_get(void);
// control functions for switch SWITCH_GLRA, 12V GLENAIR-A power ON/OFF control (high=ON)
void switch_SWITCH_GLRA_set_closed(void);
void switch_SWITCH_GLRA_set_open(void);
void switch_SWITCH_GLRA_set(enum_switchstate_t _state);
enum_switchstate_t switch_SWITCH_GLRA_get(void);
// control functions for switch SWITCH_GLRB, 12V GLENAIR-B power ON/OFF control (high=ON)
void switch_SWITCH_GLRB_set_closed(void);
void switch_SWITCH_GLRB_set_open(void);
void switch_SWITCH_GLRB_set(enum_switchstate_t _state);
enum_switchstate_t switch_SWITCH_GLRB_get(void);
// control functions for switch SWITCH_PWB, 12V POWER BOARD power ON/OFF control (high=ON)
void switch_SWITCH_PWB_set_closed(void);
void switch_SWITCH_PWB_set_open(void);
void switch_SWITCH_PWB_set(enum_switchstate_t _state);
enum_switchstate_t switch_SWITCH_PWB_get(void);
// return the switch state set function by switch enum
typedef void (*switch_state_set_func_t)(enum_switchstate_t _state);
switch_state_set_func_t switch_get_function_set_by_enum(enum_switchnum_t _switch_index);
// return the switch state get function by switch enum
typedef enum_switchstate_t (*switch_state_get_func_t)(void);
switch_state_get_func_t switch_get_function_get_by_enum(enum_switchnum_t _switch_index);
typedef struct {
enum_pinstate_t state_SWITCH_LED_D9; // status of usr pin SWITCH_LED_D9, Line IN/OUT not assigned. Free line available on JP1 jumper. Usable in output to turn on the LED
enum_pinstate_t state_SWITCH_LED_D10; // status of usr pin SWITCH_LED_D10, Line IN/OUT not assigned. Free line available on JP2 jumper. Usable in output to turn on the LED
} userpins_t;
extern userpins_t userpins;
// control functions for user pin SWITCH_LED_D9, Line IN/OUT not assigned. Free line available on JP1 jumper. Usable in output to turn on the LED
void userpin_SWITCH_LED_D9_set_high(void);
void userpin_SWITCH_LED_D9_set_low(void);
void userpin_SWITCH_LED_D9_set(enum_pinstate_t _state);
enum_pinstate_t userpin_SWITCH_LED_D9_get(void);
// control functions for user pin SWITCH_LED_D10, Line IN/OUT not assigned. Free line available on JP2 jumper. Usable in output to turn on the LED
void userpin_SWITCH_LED_D10_set_high(void);
void userpin_SWITCH_LED_D10_set_low(void);
void userpin_SWITCH_LED_D10_set(enum_pinstate_t _state);
enum_pinstate_t userpin_SWITCH_LED_D10_get(void);
// return the user pin state set function by user pin enum
typedef void (*userpin_state_set_func_t)(enum_pinstate_t _state);
userpin_state_set_func_t userpin_get_function_set_by_enum(enum_pinnum_t _userpin_index);
// return the user pin state get function by user pin enum
typedef enum_pinstate_t (*userpin_state_get_func_t)(void);
userpin_state_get_func_t userpin_get_function_get_by_enum(enum_pinnum_t _userpin_index);
#endif // _AUTOGENERATED_VARIABLES_H
/*
* File: irqs.c
* Author: Carlo Alessandro Nicolau @ INFN
*
* Firmware for BPD board.
* Interrupt service routines.
*
*/
STO RIVEDENDO QUESTO QUI...
#include "irqs.h"
#include "adcreader.h"
#include "generated/sources/variables.h"
#include "timing.h"
// local functions
void _manage_digital_variables(void);
void _manage_digital_variable(digital_variable_t* _p_variable);
// "slow" timer interrupt service routine
void irq_timer_slow(void) {
// update the adcreader state machine
adcreader_tick_irq();
// update timing register
timing_tick_irq();
}
// "fast" timer interrupt service routine
void irq_timer_fast(void) {
// manage digital alarms
_manage_digital_variables();
}
void _manage_digital_variables(void) {
for(uint8_t idx = 0; idx < DIGITAL_VARIABLES_COUNT; idx++) {
_manage_digital_variable(&(digital_variables[idx]));
}
}
void _manage_digital_variable(digital_variable_t* _p_variable) {
// read digital variables
_p_variable->value = _p_variable->readfunc();
// and update value_max
if(_p_variable->value > _p_variable->value_max) {
_p_variable->value_max = _p_variable->value;
}
// check if timout counter has to be incremented
if(_p_variable->value > 0) {
// increment counter
_p_variable->alarm.timeout_counter += 1;
// avoid wrapping on increment (not very elegant...)
if(_p_variable->alarm.timeout_counter == 0) {
_p_variable->alarm.timeout_counter -= 1;
}
// update alarm counter stats
if(_p_variable->alarm.timeout_counter_max < _p_variable->alarm.timeout_counter) {
_p_variable->alarm.timeout_counter_max = _p_variable->alarm.timeout_counter;
}
}else{
// or reset the counter
_p_variable->alarm.timeout_counter = 0;
}
// check if alarm has fired
if(_p_variable->alarm.enabled) {
// check if timeout was reached
if(_p_variable->alarm.timeout_counter > _p_variable->alarm.timeout) {
// perform the alarm action
_p_variable->alarm.callback();
// increment the fire counter
_p_variable->alarm.firecount += 1;
// avoid wrapping on increment (not very elegant...)
if(_p_variable->alarm.firecount == 0) {
_p_variable->alarm.firecount -= 1;
}
// reset alarm timeout counter
_p_variable->alarm.timeout_counter = 0;
}
}
}
/*
* File: irqs.c
* Author: Carlo Alessandro Nicolau @ INFN
*
* Firmware for BPD board.
* Interrupt service routines.
*
*/
#ifndef __IRQS_H
#define __IRQS_H
// "slow" timer interrupt service routine
void irq_timer_slow(void);
// "fast" timer interrupt service routine
void irq_timer_fast(void);
#endif // __IRQS_H
\ No newline at end of file
/*
* File: main.c
* Author: Carlo Alessandro Nicolau @ INFN
*
* Created on May 28, 2021, 4:29 PM
*
* Firmware for BPD-V01 board.
* Main.
*
*/
#include <pic18.h>
#include "irqs.h"
#include "communication.h"
#include "errorcodes.h"
#include "errors.h"
#include "hardware.h"
#include "adcreader.h"
#include "timing.h"
#include "rescue.h"
void init_mcu(void);
void init_comm(void);
void init_adcreader(void);
void start_irqs(void);
void sensors_acquire_offsets(void);
void alarms_enable(void);
void manage_incoming_data(void);
void _delay_seconds(uint8_t _seconds);
void main(void) {
return;
}
void init_mcu(void) {
// initializations (generated by MCC): IRQ Manager, PINs, Oscillator, Peripherals
SYSTEM_Initialize();
// set the timer 0 and 2 interrupt service routine
// assuming interrupts are not currently enabled
TMR0_SetInterruptHandler(irq_timer_slow); // "slow" timer (208us period)
TMR2_SetInterruptHandler(irq_timer_fast); // "fast" timer (20us period)
// safety measure: 10ms delay
__delay_ms(10);
}
void init_comm(void) {
// initialize the communication parser
communication_parser_reset();
}
void init_adcreader(void) {
adcreader_reset();
adcreader_start();
}
void start_irqs(void){
// activate interrupts
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
// clean the serial port input buffer
while(serialport_dataready) serialport_read;
// start timer0
TMR0_StartTimer();
// start timer2
TMR2_StartTimer();
// at this point:
// - The TMR0 interrupt is constantly firing
// - The serial port receive buffer is clean
}
void sensors_acquire_offsets(void) {
// ADC reader must have already started.
// We only need to read acquired values and wait properly.
// Note that the ADC is 10 bit, but data are left aligned to 16 bit, we thus
// have an headroom of 6 bits = 32 counts.
// Note also that stored offset values are initialized to zero.
userpin_SWITCH_LED_D10_set_high();
userpin_SWITCH_LED_D9_set_high();
_delay_seconds(5); // wait 5 seconds to make signals steady
userpin_SWITCH_LED_D9_set_low();
for(uint8_t k = 0; k < 32; k++) {
__delay_ms(30); // 2.2 * 30 millis are (much) more than sufficient for ADC reader to acquire from all ADC channels
for(uint8_t varidx = 0; varidx < ANALOG_VARIABLES_COUNT; varidx++) {
analog_variables[varidx].offset += analog_variables[varidx].value/32;
}
}
userpin_SWITCH_LED_D10_set_low();
}
void alarms_enable(void) {
// not necessary since the alarms are already enabled by
// initializing the variables analog_variables and digital_variables
// (see variables.h in generated/sources)
}
void manage_incoming_data(void) {
// check if a new byte is present in serial port receive buffer
if(serialport_dataready) {
// a byte is present, read it and parse it
communication_parser_appendbyte(serialport_read());
}
// check if a complete command was received
if(communication_parser_commandready()) {
// a new complete command was received
// execute the received command and verify result
err_t retval = communication_parser_executecommand();
if(NOERR == retval){
// command correctly executed
// reset second counter since last received command
// (this function is called after the command was executed to be able to read its value)
timing_reset_seconds_since_last_command();
// send the response to the host
// (it was created by the [...]_executecommand function)
communication_transmitter_sendresponse();
}else{
// do nothing
error_notify(retval);
}
// reset the parser
communication_parser_reset();
}
}
void _delay_seconds(uint8_t _seconds) {
while(_seconds--) {
__delay_ms(447); // value trimmed, takes into account the time used under irq
}
}
/**
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 PIC10 / PIC12 / PIC16 / PIC18 MCUs
@Description
This source file provides implementations for driver APIs for ADC.
Generation Information :
Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.81.7
Device : PIC18F46K22
Driver Version : 2.02
The generated drivers are tested against the following:
Compiler : XC8 2.31 and above
MPLAB : MPLAB X 5.45
*/
/*
(c) 2018 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. It is your responsibility to comply with third party
license terms applicable to your use of third party software (including open source software) that
may accompany Microchip software.
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND 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 IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
SOFTWARE.
*/
/**
Section: Included Files
*/
#include <xc.h>
#include "adc.h"
#include "device_config.h"
/**
Section: ADC Module APIs
*/
void (*ADC_InterruptHandler)(void);
void ADC_Initialize(void)
{
// set the ADC to the options selected in the User Interface
// TRIGSEL CCP5; NVCFG VSS; PVCFG FVRbuf2;
ADCON1 = 0x08;
// ADFM left; ACQT 20; ADCS FOSC/16;
ADCON2 = 0x3D;
// ADRESL 0;
ADRESL = 0x00;
// ADRESH 0;
ADRESH = 0x00;
// GO_nDONE stop; ADON enabled; CHS AN0;
ADCON0 = 0x01;
}
void ADC_SelectChannel(adc_channel_t channel)
{
// select the A/D channel
ADCON0bits.CHS = channel;
// Turn on the ADC module
ADCON0bits.ADON = 1;
}
void ADC_StartConversion(void)
{
// Start the conversion
ADCON0bits.GO_nDONE = 1;
}
bool ADC_IsConversionDone(void)
{
// Start the conversion
return ((bool)(!ADCON0bits.GO_nDONE));
}
adc_result_t ADC_GetConversionResult(void)
{
// Conversion finished, return the result
return ((adc_result_t)((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;
// Start the conversion
ADCON0bits.GO_nDONE = 1;
// Wait for the conversion to finish
while (ADCON0bits.GO_nDONE)
{
}
// Conversion finished, return the result
return ((adc_result_t)((ADRESH << 8) + ADRESL));
}
void ADC_TemperatureAcquisitionDelay(void)
{
__delay_us(200);
}
/**
End of File
*/
\ No newline at end of file
/**
ADC Generated Driver API Header File
@Company
Microchip Technology Inc.
@File Name
adc.h
@Summary
This is the generated header file for the ADC driver using PIC10 / PIC12 / PIC16 / PIC18 MCUs
@Description
This header file provides APIs for driver for ADC.
Generation Information :
Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.81.7
Device : PIC18F46K22
Driver Version : 2.02
The generated drivers are tested against the following:
Compiler : XC8 2.31 and above
MPLAB : MPLAB X 5.45
*/
/*
(c) 2018 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. It is your responsibility to comply with third party
license terms applicable to your use of third party software (including open source software) that
may accompany Microchip software.
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND 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 IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
SOFTWARE.
*/
#ifndef ADC_H
#define ADC_H
/**
Section: Included Files
*/
#include <xc.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus // Provide C++ Compatibility
extern "C" {
#endif
/**
Section: Data Types Definitions
*/
/**
* result size of an A/D conversion
*/
typedef uint16_t adc_result_t;
/**
* result type of a Double ADC conversion
*/
typedef struct
{
adc_result_t adcResult1;
adc_result_t adcResult2;
} adc_sync_double_result_t;
/** ADC Channel Definition
@Summary
Defines the channels available for conversion.
@Description
This routine defines the channels that are available for the module to use.
Remarks:
None
*/
typedef enum
{
AIN_DUL_BRD_TEMP = 0x0,
AIN_VICOR2_TEMP = 0x1,
AIN_VICOR1_TEMP = 0x2,
AIN_DU_RTN_I_MON = 0x3,
AIN_DU_FWR_I_MON = 0x4,
AIN_HYDRO_I_MON = 0x5,
AIN_INPUT_V_MON = 0x6,
AIN_LBL_I_MON = 0x7,
AIN_GLR_A_I_MON = 0x8,
AIN_GLR_B_I_MON = 0x9,
AIN_PWB_I_MON = 0xA,
channel_CTMU = 0x1D,
channel_DAC = 0x1E,
channel_FVRBuf2 = 0x1F
} adc_channel_t;
/**
Section: ADC Module APIs
*/
/**
@Summary
Initializes the ADC
@Description
This routine initializes the Initializes the ADC.
This routine must be called before any other ADC routine is called.
This routine should only be called once during system initialization.
@Preconditions
None
@Param
None
@Returns
None
@Comment
@Example
<code>
uint16_t convertedValue;
ADC_Initialize();
convertedValue = ADC_GetConversionResult();
</code>
*/
void ADC_Initialize(void);
/**
@Summary
Allows selection of a channel for conversion
@Description
This routine is used to select desired channel for conversion.
available
@Preconditions
ADC_Initialize() function should have been called before calling this function.
@Returns
None
@Param
Pass in required channel number
"For available channel refer to enum under adc.h file"
@Example
<code>
uint16_t convertedValue;
ADC_Initialize();
ADC_SelectChannel(AN1_Channel);
ADC_StartConversion();
convertedValue = ADC_GetConversionResult();
</code>
*/
void ADC_SelectChannel(adc_channel_t channel);
/**
@Summary
Starts conversion
@Description
This routine is used to start conversion of desired channel.
@Preconditions
ADC_Initialize() function should have been called before calling this function.
@Returns
None
@Param
None
@Example
<code>
uint16_t convertedValue;
ADC_Initialize();
ADC_StartConversion();
convertedValue = ADC_GetConversionResult();
</code>
*/
void ADC_StartConversion(void);
/**
@Summary
Returns true when the conversion is completed otherwise false.
@Description
This routine is used to determine if conversion is completed.
When conversion is complete routine returns true. It returns false otherwise.
@Preconditions
ADC_Initialize() and ADC_StartConversion(void)
function should have been called before calling this function.
@Returns
true - If conversion is complete
false - If conversion is not completed
@Param
None
@Example
<code>
uint16_t convertedValue;
ADC_Initialize();
ADC_StartConversion();
while(!ADC_IsConversionDone());
convertedValue = ADC_GetConversionResult();
</code>
*/
bool ADC_IsConversionDone(void);
/**
@Summary
Returns the ADC conversion value.
@Description
This routine is used to get the analog to digital converted value. This
routine gets converted values from the channel specified.
@Preconditions
This routine returns the conversion value only after the conversion is complete.
Completion status can be checked using
ADC_IsConversionDone() routine.
@Returns
Returns the converted value.
@Param
None
@Example
<code>
uint16_t convertedValue;
ADC_Initialize();
ADC_StartConversion();
while(ADC_IsConversionDone());
convertedValue = ADC_GetConversionResult();
</code>
*/
adc_result_t ADC_GetConversionResult(void);
/**
@Summary
Returns the ADC conversion value
also allows selection of a channel for conversion.
@Description
This routine is used to select desired channel for conversion
and to get the analog to digital converted value.
@Preconditions
ADC_Initialize() function should have been called before calling this function.
@Returns
Returns the converted value.
@Param
Pass in required channel number.
"For available channel refer to enum under adc.h file"
@Example
<code>
uint16_t convertedValue;
ADC_Initialize();
conversion = ADC_GetConversion(AN1_Channel);
</code>
*/
adc_result_t ADC_GetConversion(adc_channel_t channel);
/**
@Summary
Acquisition Delay for temperature sensor
@Description
This routine should be called when temperature sensor is used.
@Preconditions
ADC_Initialize() function should have been called before calling this function.
@Returns
None
@Param
None
@Example
<code>
uint16_t convertedValue;
ADC_Initialize();
ADC_StartConversion();
ADC_temperatureAcquisitionDelay();
convertedValue = ADC_GetConversionResult();
</code>
*/
void ADC_TemperatureAcquisitionDelay(void);
#ifdef __cplusplus // Provide C++ Compatibility
}
#endif
#endif //ADC_H
/**
End of File
*/
/**
CCP5 Generated Driver File
@Company
Microchip Technology Inc.
@File Name
ccp5.c
@Summary
This is the generated driver implementation file for the CCP5 driver using PIC10 / PIC12 / PIC16 / PIC18 MCUs
@Description
This source file provides implementations for driver APIs for CCP5.
Generation Information :
Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.81.7
Device : PIC18F46K22
Driver Version : 2.01
The generated drivers are tested against the following:
Compiler : XC8 2.31 and above
MPLAB : MPLAB X 5.45
*/
/*
(c) 2018 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. It is your responsibility to comply with third party
license terms applicable to your use of third party software (including open source software) that
may accompany Microchip software.
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND 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 IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
SOFTWARE.
*/
#include <xc.h>
#include "ccp5.h"
void CCP5_Initialize(void)
{
// CCP5M off/reset; DC5B 0;
CCP5CON = 0x00;
// CCPR5L 0;
CCPR5L = 0x00;
// CCPR5H 0;
CCPR5H = 0x00;
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment