K22F Power Conditioner

Dependencies:   NetworkServices W5500Interface

Revision:
0:9d0945119ba4
Child:
1:f857c10c3cd2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Feb 13 19:50:38 2017 +0000
@@ -0,0 +1,184 @@
+#include "mbed.h"
+#include "Oled_C.h"
+#include "AS3935.h"
+
+Serial pc(USBTX, USBRX);
+InterruptIn AS3935(PTB16); // AS3935 INT pin
+DigitalIn sw2(SW2);
+DigitalIn sw3(SW3);
+//InterruptIn AS3935(PTC1); //SW2
+
+char inOutSetting = 0;
+char detectFlag = 0;
+
+void check_sw3(void)
+{
+    if (sw3 == 0) {
+        wait(1);
+        if (inOutSetting == 0) {
+            AS3935setOutdoors();
+            AS3935checkInOut();
+            inOutSetting = 1;
+        } else if (inOutSetting == 1) {
+            AS3935setIndoors();
+            AS3935checkInOut();
+            inOutSetting = 0;
+        }
+    }
+}
+
+void AS3935IntSrv(void)     //Interrupt handler for the AS3935
+{
+    AS3935.disable_irq();
+    detectFlag = 1;
+}
+
+int main()
+{
+    char returnVal;
+    char outStringTemp[256];
+    char intVal = 0;
+    char display = 0;
+    char distance;
+    long int energy;
+    float LCOfreq;
+    int i = 1;
+
+    InitMCU();
+    pc.baud(115200);
+    pc.printf("\n\nFRDM-K22F board AS3935 Test.\n");
+
+    OLED_C_Init();
+    Black_Background();
+    OLED_C_Image(lightning,0,0,94,94);
+    wait(1);
+    White_Background();
+    OLED_C_Image(logo,0,80,95,14);
+    strConstRamCpy(CRam1,"Powervar");
+    Draw_Text(CRam1,1,77,0x0418,1);
+    Draw_line(0, 65, 95, 65, 0x8410);
+
+    AS3935spiInit();
+    LCOfreq = AS3935MeasureLCOFreq();
+    pc.printf("LCO freq: %.0f Hz\n",LCOfreq);
+    if (LCOfreq > 500000) {
+        while (LCOfreq > 500000) {
+            returnVal = AS3935setTuneCap(i);
+            LCOfreq = AS3935MeasureLCOFreq();
+            pc.printf("Capacitor Value: %dpF\n",i*8);
+            pc.printf("LCO freq: %.0f Hz\n\n",LCOfreq);
+            i++;
+        }
+        } else {
+            while (LCOfreq < 500000) {
+                returnVal = AS3935setTuneCap(i);
+                LCOfreq = AS3935MeasureLCOFreq();
+                pc.printf("Capacitor Value: %dpF\n",i*8);
+                pc.printf("LCO freq: %.0f Hz\n\n",LCOfreq);
+                i++;
+            }
+        }
+    wait(1);
+    AS3935powerUp();
+    AS3935enableDisturbers();
+    AS3935setIndoors();
+    //AS3935setOutdoors();    
+    returnVal = AS3935setNoiseFloor(2);
+    returnVal = AS3935setSpikeRejection(2);
+    returnVal = AS3935setWatchdogThreshold(4);
+    returnVal = AS3935getMinimumLightnings();
+    AS3935checkInOut();
+    pc.printf("Minimum Lightnings: %x\n",returnVal);
+    returnVal = AS3935getNoiseFloor();
+    pc.printf("Noise Floor: %x\n",returnVal);
+    returnVal = AS3935getSpikeRejection();
+    pc.printf("Spike Rejection: %x\n",returnVal);
+    returnVal = AS3935getWatchdogThreshold();
+    pc.printf("Watchdog Threshold: %x\n",returnVal);
+    AS3935IntSrv(); //make sure the the int bit is cleared
+    returnVal = AS3935getTuneCap();
+    pc.printf("Capacitor Value: %dpF\n",returnVal*8);
+    
+    wait(1);
+//    DDRAM_access();
+    
+    
+    //wait(1);
+    //AS3935registerWrite(AS3935_LCO_FDIV,0);
+    //AS3935registerWrite(AS3935_DISP_LCO,1);
+    //pc.printf("\nLCO on INT pin\n");
+    //wait(5);
+    //while (sw3 == 1) {
+    //           
+    //    }
+    //AS3935registerWrite(AS3935_DISP_LCO,0);
+    AS3935.rise(&AS3935IntSrv);
+    AS3935.enable_irq();
+    pc.printf("\nNormal Operation Started\n");
+    while (true) {
+        intVal = AS3935.read();
+        if (detectFlag == 1) {
+            detectFlag = 0;
+            wait(0.005);
+            source = AS3935interruptSource();
+            source = source & 0x0F;
+            pc.printf("Source: 0x%02X\n",source);
+            if (source == 0x08) {
+                display = 3;
+                distance = AS3935lightningDistanceKm();
+                energy = Thunder_Read_Energy();
+                pc.printf("Lightning Detected!!!\n");
+                pc.printf("Distance: %d\n",distance);
+                pc.printf("Energy: %lu\n",energy);
+                OLED_C_Image(strike,0,25,19,19);
+                strConstRamCpy(CRam1,"Lightning");
+                Draw_Text(CRam1,20,45,0x0000,1);
+                snprintf( outStringTemp, 256, "@ %d km      ", distance);
+                strConstRamCpy(CRam1,outStringTemp);
+                Draw_Text(CRam1,20,35,0x0000,1);
+                snprintf( outStringTemp, 256, "Energy %lu", energy);
+                strConstRamCpy(CRam1,outStringTemp);
+                Draw_Text(CRam1,10,25,0x0000,1);
+            } else if (source == 0x01) {
+                pc.printf("Noise Detected\n");
+                strConstRamCpy(CRam1,"Noise Det.");
+                Draw_Text(CRam1,21,10,0x0000,1);
+            } else if (source == 0x00) {
+                distance = AS3935lightningDistanceKm();
+                pc.printf("Distance updated: %d\n",distance);
+            } else if (source == 0x04) {
+                pc.printf("Disturber Detected\n");
+            }
+            AS3935.enable_irq();
+        }
+        check_sw3();
+        strConstRamCpy(CRam1,"*");
+        Draw_Text(CRam1,1,10,0x0000,1);
+        wait(0.5);
+//        intVal = AS3935.read();
+//        if (intVal == 1) {
+//            source = AS3935interruptSource(); //Likely we missed an interrupt
+//            pc.printf("Missed Interrupt\n");
+//            AS3935.enable_irq();
+//        }
+        check_sw3();
+        strConstRamCpy(CRam1," ");
+        Draw_Text(CRam1,1,10,0x0000,1);
+        wait(0.5);
+        if (display == 0){
+            OLED_C_Image(listening,0,25,19,19);
+            strConstRamCpy(CRam1,"            ");
+            Draw_Text(CRam1,20,45,0x0000,1);
+            snprintf( outStringTemp, 256, "Listening...");
+            strConstRamCpy(CRam1,outStringTemp);
+            Draw_Text(CRam1,20,35,0x0000,1);
+            snprintf( outStringTemp, 256, "             ");
+            strConstRamCpy(CRam1,outStringTemp);
+            Draw_Text(CRam1,10,25,0x0000,1);
+        } else { --display;}
+        strConstRamCpy(CRam1,"               ");
+        Draw_Text(CRam1,21,10,0x0000,1); 
+        //AS3935registerWrite(AS3935_DISP_LCO,1);       
+    }
+
+}
\ No newline at end of file