/* * File: WaterBrains.c * Author: dna2 * * Created on July 14, 2013, 9:48 AM */ #include #include #include #define FCY 3000000ULL #include #include "solenoidValve.h" #include "readPot.h" /* * */ /***************/ /*general setup*/ /***************/ //Turn off code protection and write protection. #pragma config GCP=OFF, GWRP=OFF //Turn off secondary debug mode #pragma config JTAGEN=OFF //Turn off fast and deep sleep watche dog timer and set clock speed. //FNOSC=FRCPLL sets the clock speed to 32MHz using the internal clock //rate=8MHz from RC circuitx4 due to internal option setting. #pragma config FWDTEN=OFF, DSWDTEN=OFF, FNOSC=FRCPLL //Make pin 7 RA3 or AN5 instead of the external oscillator (OSCIOFNC=ON) //and disable the primary oscillator. #pragma config OSCIOFNC=ON, POSCMOD=NONE //configure which pins to use for inter-integrated circuit communication. //(SCL1/SDA1) #pragma config I2C1SEL=PRI #pragma code int main(int argc, char** argv){ //Speeds up the clock to 16Mega Instructions/s CLKDIVbits.RCDIV0=0; //clock divider to 0. //Switch all pins to digital (analog pins use less power and is default) AD1PCFG=0xFFFF; //Make pins react in sync with clock at real-time rate for IO. OSCCONbits.SOSCEN=0; //Disable the secondary oscillator. //Here is where things actually start not being boiler plate: unsigned t=20; unsigned waitTime=50; int waterLevelValue=0, valveOpen=0, rawLevel=0; LEDInit(); solenoidValveInit(); AD1PCFGL=0xFDFF; //set port 9 analog, all others digital. ADCInit(); while(1){ rawLevel=readPot(); /**********************/ //This is 2/20/2014 test for calibration waterLevelValue=(rawLevel)/5.12; // __delay_ms(waitTime); writeTwoDigits(waterLevelValue); if(waterLevelValue>30){ toggleSolenoidValve(0); valveOpen=0; } //__delay_ms(waitTime); if(waterLevelValue<25){ toggleSolenoidValve(1); __delay_ms(waitTime); writeTwoDigits(waterLevelValue); valveOpen=1; } //__delay_ms(waitTime); } printf("hello World\n"); return (EXIT_SUCCESS); } void twoDigitInit(){ //setup B10 as over full indicator TRISBbits.TRISB10=0; return; } void writeTwoDigits(int value){ //Show overfull if(value>99) LATBbits.LATB10=1; else LATBbits.LATB10=0; //All done. return; } void toggleSolenoidValve(int setState){ if(setState==0){ //shutoff solenoid LATBbits.LATB8=0; //shutoff indicator LATBbits.LATB9=0; } else if(setState==1){ //solenoid LATBbits.LATB8=1; //indicator LATBbits.LATB9=1; } __delay_ms(10); return; } void solenoidValveInit(){ //set up RB8 as digital TRISBbits.TRISB8=0; //set up RB9 as digital TRISBbits.TRISB9=0; LATBbits.LATB8=0; LATBbits.LATB9=0; return; } #define ADC_VOLTAGE 9 //select input //AD1PCFGL=0xFDFF; //set all ports to digital but 9 which is analog. void ADCInit(){ //Turn on, auto sample start, auto-convert AD1CON1=0x80E4; //Vref+, Vref-, int every conversion, MUXA only AD1CON2=0x6000; //31 Tad auto-sample, Tad = 5*Tcy AD1CON3 = 0x1F05; AD1CHS = ADC_VOLTAGE; AD1PCFGbits.PCFG9 = 0; //Disable digital input on AN9 AD1CSSL = 0; //No scanned inputs return; } int readPot(){ while (!AD1CON1bits.DONE); return ADC1BUF0; }