1 |
/** |
---|
2 |
ADC1 Generated Driver File |
---|
3 |
|
---|
4 |
@Company |
---|
5 |
Microchip Technology Inc. |
---|
6 |
|
---|
7 |
@File Name |
---|
8 |
adc1.c |
---|
9 |
|
---|
10 |
@Summary |
---|
11 |
This is the generated driver implementation file for the ADC1 driver using PIC10 / PIC12 / PIC16 / PIC18 MCUs |
---|
12 |
|
---|
13 |
@Description |
---|
14 |
This source file provides implementations for driver APIs for ADC1. |
---|
15 |
Generation Information : |
---|
16 |
Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.65.2 |
---|
17 |
Device : PIC16F1579 |
---|
18 |
Driver Version : 2.01 |
---|
19 |
The generated drivers are tested against the following: |
---|
20 |
Compiler : XC8 1.45 |
---|
21 |
MPLAB : MPLAB X 4.15 |
---|
22 |
*/ |
---|
23 |
|
---|
24 |
/* |
---|
25 |
(c) 2018 Microchip Technology Inc. and its subsidiaries. |
---|
26 |
|
---|
27 |
Subject to your compliance with these terms, you may use Microchip software and any |
---|
28 |
derivatives exclusively with Microchip products. It is your responsibility to comply with third party |
---|
29 |
license terms applicable to your use of third party software (including open source software) that |
---|
30 |
may accompany Microchip software. |
---|
31 |
|
---|
32 |
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER |
---|
33 |
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY |
---|
34 |
IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS |
---|
35 |
FOR A PARTICULAR PURPOSE. |
---|
36 |
|
---|
37 |
IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, |
---|
38 |
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND |
---|
39 |
WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP |
---|
40 |
HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO |
---|
41 |
THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL |
---|
42 |
CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT |
---|
43 |
OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS |
---|
44 |
SOFTWARE. |
---|
45 |
*/ |
---|
46 |
|
---|
47 |
/** |
---|
48 |
Section: Included Files |
---|
49 |
*/ |
---|
50 |
|
---|
51 |
#include <xc.h> |
---|
52 |
#include "adc1.h" |
---|
53 |
#include "device_config.h" |
---|
54 |
|
---|
55 |
/** |
---|
56 |
Section: Macro Declarations |
---|
57 |
*/ |
---|
58 |
|
---|
59 |
#define ACQ_US_DELAY 5 |
---|
60 |
|
---|
61 |
/** |
---|
62 |
Section: ADC Module APIs |
---|
63 |
*/ |
---|
64 |
|
---|
65 |
void ADC1_Initialize(void) |
---|
66 |
{ |
---|
67 |
// set the ADC1 to the options selected in the User Interface |
---|
68 |
|
---|
69 |
// GO_nDONE stop; ADON enabled; CHS AN0; |
---|
70 |
ADCON0 = 0x01; |
---|
71 |
|
---|
72 |
// ADFM right; ADPREF VDD; ADCS FOSC/8; |
---|
73 |
ADCON1 = 0x90; |
---|
74 |
|
---|
75 |
// TRIGSEL no_auto_trigger; |
---|
76 |
ADCON2 = 0x00; |
---|
77 |
|
---|
78 |
// ADRESL 0; |
---|
79 |
ADRESL = 0x00; |
---|
80 |
|
---|
81 |
// ADRESH 0; |
---|
82 |
ADRESH = 0x00; |
---|
83 |
|
---|
84 |
} |
---|
85 |
|
---|
86 |
void ADC1_SelectChannel(adc_channel_t channel) |
---|
87 |
{ |
---|
88 |
// select the A/D channel |
---|
89 |
ADCON0bits.CHS = channel; |
---|
90 |
// Turn on the ADC module |
---|
91 |
ADCON0bits.ADON = 1; |
---|
92 |
} |
---|
93 |
|
---|
94 |
void ADC1_StartConversion() |
---|
95 |
{ |
---|
96 |
// Start the conversion |
---|
97 |
ADCON0bits.GO_nDONE = 1; |
---|
98 |
} |
---|
99 |
|
---|
100 |
|
---|
101 |
bool ADC1_IsConversionDone() |
---|
102 |
{ |
---|
103 |
// Start the conversion |
---|
104 |
return ((bool)(!ADCON0bits.GO_nDONE)); |
---|
105 |
} |
---|
106 |
|
---|
107 |
adc_result_t ADC1_GetConversionResult(void) |
---|
108 |
{ |
---|
109 |
// Conversion finished, return the result |
---|
110 |
return ((adc_result_t)((ADRESH << 8) + ADRESL)); |
---|
111 |
} |
---|
112 |
|
---|
113 |
adc_result_t ADC1_GetConversion(adc_channel_t channel) |
---|
114 |
{ |
---|
115 |
// select the A/D channel |
---|
116 |
ADCON0bits.CHS = channel; |
---|
117 |
|
---|
118 |
// Turn on the ADC module |
---|
119 |
ADCON0bits.ADON = 1; |
---|
120 |
|
---|
121 |
// Acquisition time delay |
---|
122 |
__delay_us(ACQ_US_DELAY); |
---|
123 |
|
---|
124 |
// Start the conversion |
---|
125 |
ADCON0bits.GO_nDONE = 1; |
---|
126 |
|
---|
127 |
// Wait for the conversion to finish |
---|
128 |
while (ADCON0bits.GO_nDONE) |
---|
129 |
{ |
---|
130 |
} |
---|
131 |
|
---|
132 |
//GdS: turn ADC off |
---|
133 |
ADCON0bits.ADON = 0; |
---|
134 |
|
---|
135 |
// Conversion finished, return the result |
---|
136 |
return ((adc_result_t)((ADRESH << 8) + ADRESL)); |
---|
137 |
} |
---|
138 |
|
---|
139 |
void ADC1_TemperatureAcquisitionDelay(void) |
---|
140 |
{ |
---|
141 |
__delay_us(200); |
---|
142 |
} |
---|
143 |
/** |
---|
144 |
End of File |
---|
145 |
*/ |
---|