Aguijón Software Libraries  1.0
Documentation for the included Libraries
 All Files Functions Variables Typedefs Enumerations Enumerator Macros
analog.c
Go to the documentation of this file.
1 /********************************************************************
2  FileName: analog.c
3  Dependencies: See INCLUDES section
4  Hardware: Aguijón rev3.0
5  Complier: Microchip XC16(for PIC24), C30(for PIC24)
6  Company: Vinagrón Digital
7 
8  Software License Agreement:
9 
10  blah blah blah
11 
12 ********************************************************************
13  File Description:
14  Includes all required analog control functions.
15 
16 
17  Change History:
18  Rev Description
19  ---- -----------------------------------------
20  1.0 Initial release
21 ********************************************************************/
22 
23 #include <p24FJ128GB106.h>
24 #include "definitions.h"
25 #include "analog.h"
26 
28 #define ADC_MAX_VALUE 1023
29 
30 #define ADC_MIN_VALUE 0
31 
43 void initADC(void)
44 {
45  AD1PCFG = 0x7FFF; // select RB15(AN15) as analog input
46  AD1CON1 = 0x00E0; // internal counter ends sampling and starts conversion
47  AD1CON2 = 0; // use AVDD, AVSS as reference pins
48  AD1CON3 = 0x1F02; // Tsamp = 32 x Tad; Tad=125ns
49  AD1CSSL = 0; // no scanning required
50  AD1CON1bits.ADON = 1; // turn on the ADC
51 }
52 
64 int readADC(void)
65 {
66  AD1CHS = 15; // Select analog input channel
67  AD1CON1bits.SAMP = 1; // Start sampling
68  while (!AD1CON1bits.DONE); // Wait for the conversion to complete
69  return ADC1BUF0; // Read the conversion result
70 }
71 
83 int normalize_ADCReading(unsigned char inferiorLimit, unsigned char superiorLimit)
84 {
85  /*normaliza el valor del ADC con las siguientes ecuaciones:
86  * x = MAX_VALUE / (superiorLimit - inferiorLimit)
87  *
88  * (LecturaADC / x) + limite inferior // hay un offset
89  *
90  */
91  float x = 0.0;
92  float preResult = 0.0;
93  int ADCreading = 0;
94  int finalResult = 0;
95 
96  ADCreading = readPOT();
97 
98  x = ADC_MAX_VALUE / (superiorLimit - inferiorLimit);
99 
100  preResult = (ADCreading / x);
101  preResult += inferiorLimit;
102 
103  finalResult = (int)(preResult);
104 
105  return finalResult;
106 }