forked from jcannon6032/ME_330-TSwift_Technicians
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoalSensor
87 lines (64 loc) · 2.1 KB
/
GoalSensor
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
/*
* File: Lab_7.c
* Author: svoss
*
* Created on October 24, 2017, 8:58 AM
*/
//This lab is for IR detection
#include "xc.h"
void config_ad()
{
/*** Configure Desired Port Pins as Analog Inputs ***/
_TRISB2 = 1; // TRISA/B, pg. 45 datasheet
_ANSB2 = 1; // ANSA/B, pg. 136-137
/*** Select Voltage Reference Source ***/
// use AVdd for positive reference
_PVCFG = 00; // AD1CON2<15:14>, pg. 212-213 datasheet
// use AVss for negative reference
_NVCFG = 0; // AD1CON2<13>
/*** Select Analog Conversion Clock Rate ***/
// make sure Tad is at least 600ns, see Table 29-41 datasheet
_ADCS = 00000000; // AD1CON3<7:0>, pg. 213 datasheet
/*** Select Sample/Conversion Sequence ***/
// use auto-convert
_SSRC = 0b0111; // AD1CON1<7:4>, pg. 211 datasheet
// use auto-sample
_ASAM = 1; // AD1CON1<2>
// choose a sample time >= 1 Tad, see Table 29-41 datasheet
_SAMC = 00001; // AD1CON3<12:8>
/*** Choose Analog Channels to be Used ***/
// scan inputs
_CSCNA = 1; // AD1CON2<10>
// choose which channels to scan, e.g. for ch AN12, set _CSS12 = 1;
_CSS4 = 1; // AD1CSSH/L, pg. 217
/*** Select How Results are Presented in Buffer ***/
// set 12-bit resolution
_MODE12 = 1; // AD1CON1<10>
// use absolute decimal format
_FORM = 00; // AD1CON1<9:8>
// load results into buffer determined by converted channel, e.g. ch AN12
// results appear in ADC1BUF12
_BUFREGEN = 1; // AD1CON2<11>
/*** Select Interrupt Rate ***/
// interrupt rate should reflect number of analog channels used, e.g. if
// 5 channels, interrupt every 5th sample
_SMPI = 00000; // AD1CON2<6:2> Change it according to # of channels
/*** Turn on A/D Module ***/
_ADON = 1; // AD1CON1<15>
}
int main(void) {
_TRISB8=0; //Set Desired pin as output
config_ad(); //Call A/D function
while(1)
{
if (((3.3/4096.0)*ADC1BUF4) > 2) //If voltage stored in Buffer is higher than desired threshold
{
_LATB8 = 1; //Set desired pin high
}
else
{
_LATB8 = 0; //If not, set it low
}
}
return 0;
}