//DATA1 //Program to do simple data logging //Written for PIC12F683 to be compiled with the CC5X compiler //Clock frequency left at 4MHz default //PREPROCESSOR section ===================================================== #include "12F683.H" //PIC device file #include "delay_ms.h" #pragma config = 0b.11.1100.0100//Core device configuration //11. Brown-out Detect disabled //11 Code Protects off //0 MCLR/GP3 set to digital input //0. Powerup timer enabled //0 Watchdog timer disabled //100 Oscillator set to INTRCIO //VARIABLES section ======================================================== #define ESC GPIO.1 //GP1 (Pin7) is ESC #define LED GPIO.5 //GP5 (Pin2) is LED uns16 ADC_VALUE; //ADC value uns16 LEVEL1; //1st warning threshold char p; //p used in varous counters //FUNCTIONS section ======================================================== void write_data() //Function to write to EEPROM memory { GIE = 0; //Disable all interrupts EECON1.2 = 1; //WREN: enable writing to memory PIR1.7 = 0; //EEIF: Clear this bit which gets set after each write EECON2 = 0x55; //Required before writing to memory EECON2 = 0xAA; //Required before writing to memory EECON1.1 = 1; //WR: Writes data EEDAT to address EEADR while(EECON1.1==1); //Wait for write to complete (becomes 0 when complete) EEADR++; //Increment the address by 1 for next write (8 bit 0-255) //Overflows to 0 so data will keep over-writing when addresses are used up EECON1.2 = 0; //WREN: disable write cycles //GIE = 1; //Enable interrupts if being used } //----------------------------------------------------------------------------- void adc_value() //Function to run ADC conversion { //ADCON0 structure... //1 Right justify //0 Vdd reference //00 Not used //10 Input channel (CHS0) is AN2 (THIS PART CHANGES BELOW) //0 ADC capture (GO) not started //1 ADC enabled ADCON0 = 0b.1000.1101; //Makes AN3 channel active //Run ADC for(p=0; p<1; p++); //Delay for acquisition capacitor to charge (1=15uS, 10=90uS) ADIF = 0; //Clear the ADC Complete flag ADCON0.1 = 1; //GO: Start ADC acquisition while(ADIF==0); //Wait for conversion to complete (becomes 0 when complete) //Determine complete ADC value ADC_VALUE = ADRESH; //Copies ADRESH value from an 8bit register to 16bit variable ADC_VALUE = ADC_VALUE<<8; //Moves all bits 8 places left ADC_VALUE = ADC_VALUE|ADRESL; //| merges lower 8 bits with upper 2 //Write both ADC values to memory // EEDAT = ADRESH; //Copy high portion of ADC value to EEPROM data register // write_data(); //Write to memory EEDAT = ADRESL; //Copy high portion of ADC value to EEPROM data register write_data(); //Write to memory ESC=1; //Signal to measure time to write to EEPROM for(p=0; p<1; p++); ESC=0; } //=========================================================================== void main() //main program { //Main port settings (all ports are listed for convenience; some are commented out) // TRISIO0 = 0; //Sets GP0/AN0 (pin7) to Output TRISIO1 = 0; //Sets GP1/AN1 (pin6) to Output (ESC) // TRISIO2 = 1; //sets GP2/AN2 (pin5) to Input (PACK1) // TRISIO3 = 1; //sets GP3 (pin4) to Input TRISIO4 = 1; //sets GP4/AN3 (pin3) to Input (PACK2) TRISIO5 = 0; //Sets GP5 (pin2) to Output (LED) // ANS0 = 0; //Sets AN0 (pin7) to digital ANS1 = 0; //Sets AN1 (pin6) to digital // ANS2 = 1; //Sets AN2 (pin5) to analogue (PACK1) ANS3 = 1; //Sets AN3 (pin3) to analogue (PACK2) CMCON0 = 0b.111; //Switches all 3 comparator ports off //Static ADC settings ANSEL.4 = 1; //ADCS: Set AD Conversion Clock to Fosc/16 ANSEL.5 = 0; //part of above ANSEL.6 = 1; //part of above ADIE = 0; //ADC interrupts disabled //Static EEPROM data logging settings EECON1.3 = 0; //WRERR: clear on startup in case previously set EEADR = 0; //Set address to be used for first write //----------------------------------------------------------------------------- ESC=0; //Flash LED three times on startup for (p=0; p<5; p++) { LED=1; delay_ms(500); LED=0; delay_ms(500); } //Set warning thresholds to suit resistor divider and pack voltages LEVEL1=500; //ADC trigger level while(1) //Run the following in an infinite loop { adc_value(); //Find ADC value if (ADC_VALUE<=LEVEL1) //If battery reaches threshold { while (ADC_VALUE>1) LED=1; //Keep LED on } } //end while } //end main