aboutsummaryrefslogtreecommitdiffstats
path: root/PICCode.X/main.c
diff options
context:
space:
mode:
authorMatt Strapp <strap012@umn.edu>2021-03-28 10:12:10 -0500
committerMatt Strapp <strap012@umn.edu>2021-03-28 10:12:10 -0500
commitf3f8cbd69e290ccbd60f9cbc0b1d9b63645127bb (patch)
treedd3ab9128eee3ca38e86f83d705526d596c0f732 /PICCode.X/main.c
parentAdded intial PIC24 Firmware (diff)
downloadee3102-f3f8cbd69e290ccbd60f9cbc0b1d9b63645127bb.tar
ee3102-f3f8cbd69e290ccbd60f9cbc0b1d9b63645127bb.tar.gz
ee3102-f3f8cbd69e290ccbd60f9cbc0b1d9b63645127bb.tar.bz2
ee3102-f3f8cbd69e290ccbd60f9cbc0b1d9b63645127bb.tar.lz
ee3102-f3f8cbd69e290ccbd60f9cbc0b1d9b63645127bb.tar.xz
ee3102-f3f8cbd69e290ccbd60f9cbc0b1d9b63645127bb.tar.zst
ee3102-f3f8cbd69e290ccbd60f9cbc0b1d9b63645127bb.zip
Add a basic VtoI function
Also rearrange things and commit iconoclasm
Diffstat (limited to 'PICCode.X/main.c')
-rw-r--r--PICCode.X/main.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/PICCode.X/main.c b/PICCode.X/main.c
new file mode 100644
index 0000000..d9a7126
--- /dev/null
+++ b/PICCode.X/main.c
@@ -0,0 +1,95 @@
+#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); //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 = 2;
+ 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;
+ _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 = 0; //Interrupt after completion of every conversion
+
+ 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.196) / 0.0335);
+}
+
+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
+ sprintf(adStr,"%6.4f A",VtoI((3.3/1024)*adValue)); //Formatting the string that will be written
+
+ lcd_printStr(adStr); //Writing the entire string to the display
+ }
+
+
+ return 0;
+}