/*ver2 * Uses routines from delay.c * Potentiometer Readout RA0 PIN 2 * * Frequency in RC2 PIN 13 * Injector uit - RC4 */ #include #include #include #include #include "pic1687x.h" #include "delay.h" #include "adc.h" #include "uart.h" #include "spi.h" #include "string.h" /*************************************** VARIABLES ***************************************/ bank2 volatile bit timeout, first; bank2 unsigned int copy, serieel; bank3 unsigned char buf[13]; bank2 int time_ctr, toerental, gasstand, data, gasstand_data, toerental_data, delay, ready, eerste_puls, teller; bank2 volatile long int stamp, period; bank2 volatile unsigned int pulse_ctr; static bit CHIP_SELECT @ ((unsigned)&PORTB*8+5); // CHIP SELECT static bit INJECTOR @ ((unsigned)&PORTB*8+4); // INJECTOR static bit SELECT_SERIAL @ ((unsigned)&PORTB*8+3); // SELECT SERIAL #define CHIP_STROBE ((CHIP_SELECT = 1),(CHIP_SELECT = 0)) /* Sample code to set up the A2D module */ void init_a2d(void){ ADCON0=0; // select Fosc/2 ADCON1=0x80; // select left justify result. A/D port configuration 0 ADON=1; // turn on the A2D conversion module } /* Return an 10 bit result */ long int read_a2d(unsigned char channel){ long int value_ADRESH; channel&=0x07; // truncate channel to 3 bits ADCON0&=0xC5; // clear current channel select ADCON0|=(channel<<3); // apply the new channel select ADGO=1; // initiate conversion on the selected channel while(ADGO)continue; value_ADRESH=((ADRESH<<8)+(ADRESL)); return(value_ADRESH); // return 10 Bits of the result } /*************************************** INTERRUPT ***************************************/ void interrupt ISR(void) { int j; if(CCP1IF)// check for rising edge on input pin { CCP1IF = 0;// clear flag if(first)// is this the first capture ?? { INJECTOR = 1; TMR1ON = 0;// then we reset the timers and counters TMR1H = 0; TMR1L =21;// place an offset in timer 0 from the time the interrupt has been called TMR1ON = 1; first = 0; stamp = 0xFFFF;// offset for timing purposes timeout = 0; time_ctr = 0; pulse_ctr = 1; eerste_puls =1; } else {// for a good precision INJECTOR = 1; copy = (CCPR1H << 8) | CCPR1L; ready = 1; first = 1; } } if(TMR1IF) { time_ctr++; TMR1IF = 0; stamp += 0xFFFF; } if(RCIF) { if(RCREG == 0x49) //De letter I die stuurt de PC software op { serieel =0; j = 0; } else { buf[j++] = RCREG; if(j>= 12) { serieel =1; j = 0; } } RCIF = 0; } if(T0IF) { teller++; T0IF = 0; } } void InitUART(void) { BRGH = 1; // High Speed Mode SPBRG = 10; // 115200BPS @ 20MHz // SYNC = 0; // ASYNCHRONOUS SPEN = 1; // Enable Serial Port CREN = 1; // ENABLE RECEPTION // SREN = 0; // NO EFFECT // TXIE = 0; // DISABLE TX INTERRUPTS RCIE = 1; // ENABLE RX INTERRUPTS // TX9 = 0; // 8 BIT TRANSMISSION // RX9 = 0; // 8 BIT RECEPTION TXEN = 1; // Enable Transmit } void putch(unsigned char byte) { while(!TRMT); // Wait for TX Buffer Empty TXREG = byte; } void InitTimer0(void) { T0CS = 0; PS2 = 1; PS1 = 1; PS0 = 1; } int gasdata(int gas_data_in) { double gas; gas_data_in = gas_data_in/15; gas = ceil((gas_data_in)); gas = gas * 15; gas_data_in = gas; return gas_data_in; } int toerendata(double toeren_data) { double toeren; toeren = ceil((toeren_data/4)); toeren = toeren * 4; toeren_data = toeren; return toeren_data; } /*************************************** INIT REGISTER ***************************************/ void init_reg(void) { OPTION = 0x40; PEIE = 1; CCP1IF = 0; CCP1IE = 1; TMR1IF = 0; TMR1IE = 1; CCP1CON = 0x05; T1CON = 0x01; T0IF = 0; T0IE = 1; GIE = 1; } main() { int adc, spi_addres; unsigned char gas_buf[4], toeren_buf[4], delay_buf[4]; int gas_data, toeren_data, delay_data, spi_data, gas_data_in; PIR1 = 0; // clear any pending interrupts PEIE = 1; // enable perhipheral interrupts GIE = 1; // global interrupts enabled init_a2d(); // initialise the A2D module InitUART(); // initialise the UART module init_reg(); // initialise the CCPCON module InitSPI(); // initialise the SPI module CHIP_STROBE;// just telling the EEPROM to get alive TRISA = 0xF3; // control lines are output TRISB = 0x01; // Bit 0 input rest output Port B TRISC = 0x90; // Bit 5 input Receive data SPI and Bit 7 input Receive data UART PORTA = 0; PORTB = 0; PORTC = 0; TMR0 = 0xff; for(;;) { gas_data = 0; toeren_data = 0; ready = 0; spi_data = 0; /********************************************************************************/ /* Hier wordt iedere keer een waarde van de puls tijd uit de EEPROM gehaadld!!! */ /********************************************************************************/ adc=read_a2d(0); // sample the analog value on RA0 adc = (adc/4); // ADC waarde delen door 4 om de 10 bits waarde te kunnen opslaan op een 8 bits period = 5000000 / copy; period = (period /12); toeren_data = toerendata(period); // dan hebben we hier de waarde van toerental gas_data = gasdata(adc); // dan hebben we nu de waarde van de gasstand gas_data = (gas_data << 8); spi_addres = (gas_data | toeren_data); // en is dit het adres van de EEPROM spi_data =spi_read(spi_addres); // spi_data heeft de waarde die op spi_addres staat /*************************************************************************************/ /* Er staat data klaar op de serieele poort en we gaan dus MEMORY vullen met de data */ /*************************************************************************************/ if(serieel) { gas_data_in = 0; toeren_data = 0; delay_data = 0; strcpy(gas_buf, buf); strncpy(gas_buf + 4,"\0",2); gas_data_in = atoi(gas_buf); gas_data_in = gasdata(gas_data_in); strcpy(toeren_buf, buf + 4); strncpy(toeren_buf + 4,"\0",2); toeren_data = atoi(toeren_buf); toeren_data = toerendata(toeren_data); strcpy(delay_buf, buf + 8); strncpy(delay_buf + 4,"\0",2); delay_data = atoi(delay_buf); gas_data_in = (gas_data_in << 8); spi_addres = (gas_data_in | toeren_data); if(delay_data > 255) { delay_data = 255; } do { spi_WREN(); // Data wordt hier naar de EEPROM geschreven spi_write(spi_addres, delay_data); DelayMs(15); spi_data =spi_read(spi_addres); }while(delay_data != spi_data); // Dit blijven we net zolang doen totdat het er echt staat!! serieel = 0; } /***************************************************************************************************************/ /* Hier aangekomen dan is er een toerental meting gedaan en gaan we nu de Injector uitgang een tijdje hoog maken */ /***************************************************************************************************************/ if(eerste_puls) { //DelayUs(spi_data); if(teller >= spi_data) { INJECTOR =0; teller = 0; eerste_puls = 0; } } if(ready) //Er is dan een frequentie meting afgerond { if(teller >= spi_data) { INJECTOR =0; teller = 0; ready = 0; } // gas_data = gasdata(adc); // printf("T%04d",period); // printf("G%04d",gas_data); // printf("D%04d",spi_data); // printf("SPI_ADDRESS_DATA= %4d\n\r", spi_address_data); // TMR0 = 0xff;// TMR0 LADEN MET 0x01 IS GELIJK AAN 100uS DELAY // spi_data = 25; //Komt overeen met een pulstijd van 2mS }; } }