aboutsummaryrefslogtreecommitdiffstats
path: root/Lab6.X/lab6_main.c
blob: 7d46cd0ba0e3a5a79b4cbcccf9f9b85d5af7e3f7 (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
#include "xc.h"
#include <stdio.h>
#include "../Lab5.X/lab5_lcd.h"


#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))

void setup(void) {
    CLKDIVbits.RCDIV = 0;
    AD1PCFG = 0x9FFE; //AN1 analog
    TRISA |= 1;;
    
    // -- I2C STUFF --
    I2C2BRG = 0x9D;
    I2C2CONbits.I2CEN = 1;
    _I2CSIDL = 0;
    IFS3bits.MI2C2IF=0;
    lcd_init();
    
    // -- ADC STUFF --
    AD1CON1 = 0;    // set all control to 0.
    AD1CON1bits.SSRC = 0b010;  //Rollover to conversion
    AD1CON1bits.ASAM = 1; // auto-sample
    AD1CON1bits.FORM = 0b00;
    AD1CON2 = 0;
    AD1CON2bits.CSCNA = 0; // no scan
    AD1CON2bits.SMPI = 0b0; // every conversion
    AD1CON2bits.BUFM = 0;  // two 8-word buffers
    AD1CON3bits.ADCS = 0b1;
    AD1CON3bits.ADRC = 0;
    AD1CON3bits.SAMC = 0b1;
    AD1CON1bits.ADON = 1; // ton
    AD1CHS = 0;
    AD1CHSbits.CH0NB = 0;
    AD1CHSbits.CH0SB = 0;
    AD1CHSbits.CH0NA = 0;
    AD1CHSbits.CH0SA = 0;
    
    // -- TIMER 2 STUFF -- 
    T2CON = 0;
    TMR2 = 0;
    T2CONbits.TCKPS = 0b10;
    PR2 = 25000;
    T2CONbits.TON = 1;
    
    // -- TIMER 3 STUFF --
    T3CON = 0;
    TMR3 = 0;
    T3CONbits.TCKPS = 0b10;
    PR3 = 1562;
    T3CONbits.TON = 1;
    
    // -- INTERRUPT ENABLES --
    IEC0bits.AD1IE = 1;
    IFS0bits.AD1IF = 0;
    //ADC Interrupt (Enabled)
    IEC0bits.T2IE = 1;
    IFS0bits.T2IF = 0;
    // T2 Interrupt (Enabled)
}

void __attribute__((__interrupt__, __auto_psv__)) _ADC1Interrupt(void) {
    IFS0bits.AD1IF = 0;
    //This interrupt only serves to allow the ADC to work as intended.
    
}
unsigned char flag = 0;
void __attribute__((__interrupt__, __auto_psv__)) _T2Interrupt(void) {
    _T2IF = 0; TMR2 = 0;
    char adStr[20];
    lcd_setCursor(0,0);
    sprintf(adStr, "%6.4f V", (3.3/1024)*ADC1BUF0);
    lcd_printStr(adStr);
}

int main(void) {
    setup();
    while (1);
    return -1;
}