-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADC.c
140 lines (128 loc) · 3.46 KB
/
ADC.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* File: ADC.c
* Author: Mario
*
*/
#include "ADC.h"
/******************************************************************************
* @fn - ADC_setup
* @brief - This function configure the ADC peripheral
*
*
* @param[in] - ADC_CLK macros
* @param[in] - ADC_PVREF macros
* @param[in] - ADC_NVREF macros
*
* @return - none
*
* @note - check TAD parameter for don't viloate it with ADC_CLK
*****************************************************************************/
void ADC_setup(uint8_t ADC_CLK,uint8_t ADC_PVREF,uint8_t ADC_NVREF)
{
ADCON1bits.ADCS = ADC_CLK;
ADCON1bits.ADPREF = ADC_PVREF;
ADCON1bits.ADNREF = ADC_NVREF;
}
/******************************************************************************
* @fn - ADC_Format
* @brief - This function configure the fustified format of the
* result
*
*
* @param[in] - ADC_FORMAT_LEFT or ADC_FORMAT_RIGHT
*
* @return - none
*
* @note - none
*****************************************************************************/
void ADC_Format(uint8_t ADC_FORMAT)
{
ADCON1bits.ADFM = ADC_FORMAT;
}
void ADC_Init_CH(uint8_t ADC_CH)
{
switch (ADC_CH)
{
case ADC_CH_AN0:
WPUAbits.WPUA0 = 0;
ANSELAbits.ANSA0 = 1;
TRISAbits.TRISA0 = 1;
break;
case ADC_CH_AN1:
WPUAbits.WPUA1 = 0;
ANSELAbits.ANSA1 = 1;
TRISAbits.TRISA1 = 1;
break;
case ADC_CH_AN2:
WPUAbits.WPUA2 = 0;
ANSELAbits.ANSA2 = 1;
TRISAbits.TRISA2 = 1;
break;
case ADC_CH_AN3:
WPUAbits.WPUA4 = 0;
ANSELAbits.ANSA4 = 1;
TRISAbits.TRISA4 = 1;
break;
case ADC_CH_AN4:
WPUCbits.WPUC0 = 0;
ANSELCbits.ANSC0 = 1;
TRISCbits.TRISC0 = 1;
break;
case ADC_CH_AN5:
WPUCbits.WPUC1 = 0;
ANSELCbits.ANSC1 = 1;
TRISCbits.TRISC1 = 1;
break;
case ADC_CH_AN6:
WPUCbits.WPUC2 = 0;
ANSELCbits.ANSC2 = 1;
TRISCbits.TRISC2 = 1;
break;
case ADC_CH_AN7:
WPUCbits.WPUC3 = 0;
ANSELCbits.ANSC3 = 1;
TRISCbits.TRISC3 = 1;
break;
case ADC_CH_AN8:
WPUCbits.WPUC6 = 0;
ANSELCbits.ANSC6 = 1;
TRISCbits.TRISC6 = 1;
break;
case ADC_CH_AN9:
WPUCbits.WPUC7 = 0;
ANSELCbits.ANSC7 = 1;
TRISCbits.TRISC7 = 1;
break;
case ADC_CH_AN10:
WPUBbits.WPUB4 = 0;
ANSELBbits.ANSB4 = 1;
TRISBbits.TRISB4 = 1;
break;
default:
WPUBbits.WPUB5 = 0;
ANSELBbits.ANSB5 = 1;
TRISBbits.TRISB5 = 1;
break;
}
}
/******************************************************************************
* @fn - ADC_Read
* @brief - This function read the given channel
*
*
*
* @param[in] - ADC_CH macros
*
* @return - reading value
*
* @note - none
*****************************************************************************/
uint16_t ADC_Read(uint8_t ADC_CH)
{
ADCON0bits.CHS = ADC_CH;
ADCON0bits.ADON = 1;
ADCON0bits.GO_nDONE = 1;
while(ADCON0bits.GO_nDONE);
ADCON0bits.ADON = 0;
return ADRES;
}