-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.c
100 lines (81 loc) · 2.82 KB
/
user.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
/******************************************************************************/
/* Files to Include */
/******************************************************************************/
/* Device header file */
#if defined(__XC16__)
#include <xc.h>
#elif defined(__C30__)
#if defined(__dsPIC33E__)
#include <p33Exxxx.h>
#elif defined(__dsPIC33F__)
#include <p33Fxxxx.h>
#endif
#endif
#include <stdint.h> /* For uint16_t definition */
#include <stdbool.h> /* For true/false definition */
#include <delay.h>
#include "user.h" /* variables/params used by user.c */
/******************************************************************************/
/* User Functions */
/******************************************************************************/
/* <Initialize variables in user.h and insert code for user algorithms.> */
void InitApp(void)
{
/* TODO Initialize User Ports/Peripherals/Project here */
/* Set port configuration */
//TRISA = 0x0000;
//PORTA = 0xFFFF;
ANSELA = ANSELB = 0x0000;
ANSELBbits.ANSB0 = 1; // Ensure AN0/RB0 is analog
ANSELBbits.ANSB1 = 1; // Ensure AN1/RB1 is analog
ANSELBbits.ANSB2 = 1; // Ensure AN2/RB2 is analog
ANSELAbits.ANSA0 = 1; // Ensure AN5/RB5 is analog
/* Initialize and enable ADC module */
AD1CON1 = 0x000C; // Enable simultaneous sampling and auto-sample
AD1CON2 = 0x0300; // Sample 4 channels
AD1CON3 = 0x000F;
AD1CON4 = 0x0000;
AD1CSSH = 0x0000;
AD1CSSL = 0x0000;
AD1CHS0bits.CH0SA = 5; // Select AN5 for CH0 +ve input
AD1CHS0bits.CH0NA = 0; // Select Vref- for CH0 -ve input
AD1CHS123bits.CH123SA = 0; // Select AN0 for CH1 +ve input
// Select AN1 for CH2 +ve input
// Select AN2 for CH3 +ve input
AD1CHS123bits.CH123NA = 0; // Select Vref- for CH1/CH2/CH3 -ve inputs
AD1CON1bits.ADON = 1;
/* Setup analog functionality and port direction */
/* Initialize peripherals */
}
void initADC(void)
{
ANSELA = 0x0000;
ANSELAbits.ANSA0 = 1; // Ensure AN0/RA0 is analog
/* Initialize and enable ADC module */
//AD1CON1 = 0x0704; //Signed fractional
AD1CON1 = 0x0404; //Unsigned integer
AD1CON2 = 0x0000;
AD1CON3 = 0x000F;
AD1CON4 = 0x0000;
AD1CHS123 = 0x0000;
AD1CHS0 = 0x0000; //Select AN0
AD1CSSH = 0x0000;
AD1CSSL = 0x0000;
AD1CON1bits.ADON = 1;
__delay_us(20);
}
void __delay_us(unsigned int delay)
{
int i;
for (i = 0; i < delay; i++)
{
__asm__ volatile ("repeat #39");
__asm__ volatile ("nop");
}
}
void __delay_ms(unsigned int delay)
{
int i;
for (i = 0; i < 1000; i++)
__delay_us(delay);
}