blob: 98b5203ca5e088c1eb2c7ac37e9fe07d10c84e39 (
plain) (
blame)
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
|
#include "xc.h"
#include <stdio.h>
#include "bufferlib.h"
#include "lcd.h"
// CW1: FLASH CONFIGURATION WORD 1 (see PIC24 Family Reference Manual 24.1)
#pragma config ICS = PGx1 // Comm Channel Select (Emulator EMUC1/EMUD1 pins are shared with PGC1/PGD1)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog Timer is disabled)
#pragma config GWRP = OFF // General Code Segment Write Protect (Writes to program memory are allowed)
#pragma config GCP = OFF // General Code Segment Code Protect (Code protection is disabled)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG port is disabled)
// CW2: FLASH CONFIGURATION WORD 2 (see PIC24 Family Reference Manual 24.1)
#pragma config I2C1SEL = PRI // I2C1 Pin Location Select (Use default SCL1/SDA1 pins)
#pragma config IOL1WAY = OFF // IOLOCK Protection (IOLOCK may be changed via unlocking seq)
#pragma config OSCIOFNC = ON // Primary Oscillator I/O Function (CLKO/RC15 functions as I/O pin)
#pragma config FCKSM = CSECME // Clock Switching and Monitor (Clock switching is enabled,
// Fail-Safe Clock Monitor is enabled)
#pragma config FNOSC = FRCPLL // Oscillator Select (Fast RC Oscillator with PLL module (FRCPLL))
volatile int adValue;
void __attribute__((__interrupt__,__auto_psv__)) _ADC1Interrupt(void) {
_AD1IF = 0;
putVal(ADC1BUF0, ADC1BUF1); //Grab latest sampled value after conversion and place it in buffer
}
void __attribute__((__interrupt__,__auto_psv__)) _T2Interrupt(void) {
_T2IF = 0;
adValue = getAvg(); //Grab average buffer value every 100ms
}
void setup (void) {
AD1PCFG = 0x9FFC; //Set pin RA0, RA1 as analog
TRISA = 0x0003; //Set pin RA0, RA1 as an input
T2CON = 0; //Set up timer 2 to have a delay of 100ms
PR2 = 24999; //and enable the interrupt for timer 2
T2CONbits.TCKPS = 3;
T2CONbits.TON = 1;
_T2IE = 1;
_T2IF = 0;
I2C1CONbits.I2CEN = 0; //Disable I2C2 to safely change Baud Rate
I2C1BRG = 0x9D; //Set Baud Rate to 100kHz
I2C1CONbits.I2CEN = 1; //Enable I2C2 for project use
_MI2C1IF = 0; //clear I2C2 flag
T3CON = 0; //Set up timer 3 with a delay of 62.5ms for
PR3 = 15625; //use with ADC conversions on pin RA0
T3CONbits.TCKPS = 2;
T3CONbits.TON = 1;
AD1CON1 = 0, AD1CON2 = 0, AD1CON3 = 0;
AD1CSSL = 0x0003; //Set ADC1 to obtain both RA0 and RA1
_AD1IE = 1;
_AD1IF = 0;
AD1CON1bits.ASAM = 1; //Turn on automatic sampling
AD1CON1bits.SSRC = 2; //Use timer 3 to end sampling time
AD1CON2bits.VCFG = 0; //Configure reference voltages to be Vdd and GND
AD1CON2bits.SMPI = 1; //Interrupt after completion of every 2nd conversion
AD1CON2bits.CSCNA = 1; //Enable sequential scanning on bits specified in AD1CSSL register above
AD1CON3bits.SAMC = 1; //Set Tsmp = 1*Tad
AD1CON3bits.ADCS = 1; //Set Tad = 2*Tcy
AD1CON1bits.ADON = 1; //Turn on ADC1 Peripheral
}
double VtoI(float value) {
//TODO: Verify this. I've done a few and the error seems to be no more than 0.5 mA
//MAKE SURE THE ERROR IS NEVER >=1.0mA (as per project description)
return (double) ((value + 0.202) / 0.036);
}
int main(void) {
setup(); //Initializing pins and setting up peripherals
initBuffer(); //Clearing the circular buffer
lcd_init(); //Running through LCD initialization sequence
char adStr[20]; //The string printed to the LCD
while(1) {
lcd_setCursor(0,0); //Setting the cursor to the top left corner of the display
int curr =VtoI((3.3/1024)*adValue);
if (curr < 0)
curr =0;
sprintf(adStr,"%6.4fmA",curr); //Formatting the string that will be written
lcd_printStr(adStr); //Writing the entire string to the display
}
return 0;
}
|