1: /*ver2 2: * Uses routines from delay.c 3: * Potentiometer Readout RA0 PIN 2 4: * 5: * Frequency in RC2 PIN 13 6: * 7: */ 8: 9: 10: #include 11: #include 12: #include 13: #include 14: #include "pic1687x.h" 15: #include "delay.h" 16: #include "adc.h" 17: #include "uart.h" 18: #include "spi.h" 19: #include "string.h" 20: 21: /*************************************** 22: VARIABLES 23: ***************************************/ 24: 25: bank2 volatile bit timeout, first; 26: bank2 unsigned int copy, serieel; 27: bank3 unsigned char buf[13]; 28: 29: bank2 int time_ctr, toerental, gasstand, data, gasstand_data, toerental_data, delay, ready, eerste_puls, teller; 30: bank2 volatile long int stamp, period; 31: bank2 volatile unsigned int pulse_ctr; 32: 33: static bit CHIP_SELECT @ ((unsigned)&PORTB*8+5); // CHIP SELECT 34: static bit INJECTOR @ ((unsigned)&PORTB*8+4); // INJECTOR 35: 36: #define CHIP_STROBE ((CHIP_SELECT = 1),(CHIP_SELECT = 0)) 37: 38: 39: 40: /* Sample code to set up the A2D module */ 41: void init_a2d(void){ 42: ADCON0=0; // select Fosc/2 43: ADCON1=0x80; // select left justify result. A/D port configuration 0 44: ADON=1; // turn on the A2D conversion module 45: } 46: 47: 48: /* Return an 10 bit result */ 49: long int read_a2d(unsigned char channel){ 50: long int value_ADRESH; 51: channel&=0x07; // truncate channel to 3 bits 52: ADCON0&=0xC5; // clear current channel select 53: ADCON0|=(channel<<3); // apply the new channel select 54: ADGO=1; // initiate conversion on the selected channel 55: while(ADGO)continue; 56: value_ADRESH=((ADRESH<<8)+(ADRESL)); 57: return(value_ADRESH); // return 10 Bits of the result 58: } 59: 60: 61: 62: /*************************************** 63: INTERRUPT 64: ***************************************/ 65: void interrupt ISR(void) 66: { 67: int j; 68: 69: if(CCP1IF)// check for rising edge on input pin 70: { 71: CCP1IF = 0;// clear flag 72: if(first)// is this the first capture ?? 73: { 74: INJECTOR = 1; 75: TMR1ON = 0;// then we reset the timers and counters 76: TMR1H = 0; 77: TMR1L =21;// place an offset in timer 0 from the time the interrupt has been called 78: TMR1ON = 1; 79: first = 0; 80: stamp = 0xFFFF;// offset for timing purposes 81: timeout = 0; 82: time_ctr = 0; 83: pulse_ctr = 1; 84: eerste_puls =1; 85: } 86: else 87: {// for a good precision 88: INJECTOR = 1; 89: copy = (CCPR1H << 8) | CCPR1L; 90: ready = 1; 91: first = 1; 92: } 93: } 94: 95: if(TMR1IF) 96: { 97: time_ctr++; 98: TMR1IF = 0; 99: stamp += 0xFFFF; 100: } 101: 102: if(RCIF) 103: { 104: if(RCREG == 0x49) //De letter I die stuurt de PC software op 105: { 106: serieel =0; 107: j = 0; 108: } 109: else 110: { 111: buf[j++] = RCREG; 112: if(j>= 12) 113: { 114: serieel =1; 115: j = 0; 116: } 117: } 118: RCIF = 0; 119: } 120: 121: if(T0IF) 122: { 123: teller++; 124: T0IF = 0; 125: } 126: 127: } 128: 129: 130: void InitUART(void) 131: { 132: BRGH = 1; // High Speed Mode 133: SPBRG = 10; // 115200BPS @ 20MHz 134: // SYNC = 0; // ASYNCHRONOUS 135: SPEN = 1; // Enable Serial Port 136: CREN = 1; // ENABLE RECEPTION 137: // SREN = 0; // NO EFFECT 138: // TXIE = 0; // DISABLE TX INTERRUPTS 139: RCIE = 1; // ENABLE RX INTERRUPTS 140: // TX9 = 0; // 8 BIT TRANSMISSION 141: // RX9 = 0; // 8 BIT RECEPTION 142: TXEN = 1; // Enable Transmit 143: 144: } 145: 146: void putch(unsigned char byte) 147: { 148: while(!TRMT); // Wait for TX Buffer Empty 149: TXREG = byte; 150: } 151: 152: 153: void InitTimer0(void) 154: { 155: T0CS = 0; 156: PS2 = 1; 157: PS1 = 1; 158: PS0 = 1; 159: } 160: 161: int gasdata(int gas_data_in) 162: { 163: double gas; 164: gas_data_in = gas_data_in/15; 165: gas = ceil((gas_data_in)); 166: gas = gas * 15; 167: gas_data_in = gas; 168: return gas_data_in; 169: } 170: 171: int toerendata(double toeren_data) 172: { 173: double toeren; 174: toeren = ceil((toeren_data/4)); 175: toeren = toeren * 4; 176: toeren_data = toeren; 177: return toeren_data; 178: } 179: 180: 181: /*************************************** 182: INIT REGISTER 183: ***************************************/ 184: void init_reg(void) 185: { 186: OPTION = 0x40; 187: PEIE = 1; 188: CCP1IF = 0; 189: CCP1IE = 1; 190: TMR1IF = 0; 191: TMR1IE = 1; 192: CCP1CON = 0x05; 193: T1CON = 0x01; 194: 195: T0IF = 0; 196: T0IE = 1; 197: GIE = 1; 198: } 199: 200: 201: 202: main() 203: { 204: int adc, spi_addres; 205: unsigned char gas_buf[4], toeren_buf[4], delay_buf[4]; 206: int gas_data, toeren_data, delay_data, spi_data, gas_data_in; 207: 208: PIR1 = 0; // clear any pending interrupts 209: PEIE = 1; // enable perhipheral interrupts 210: GIE = 1; // global interrupts enabled 211: 212: 213: init_a2d(); // initialise the A2D module 214: InitUART(); // initialise the UART module 215: init_reg(); // initialise the CCPCON module 216: InitSPI(); // initialise the SPI module 217: CHIP_STROBE;// just telling the EEPROM to get alive 218: TRISA = 0xF3; // control lines are output 219: TRISB = 0x01; // Bit 0 input rest output Port B 220: TRISC = 0x90; // Bit 5 input Receive data SPI and Bit 7 input Receive data UART 221: PORTA = 0; 222: PORTB = 0; 223: PORTC = 0; 224: TMR0 = 0xff; 225: 226: for(;;) 227: { 228: adc=read_a2d(0); // sample the analog value on RA0 229: 230: if(serieel) // Er staat data klaar op de serieele poort en we gaan dus MEMORY vullen met de data 231: { 232: gas_data_in = 0; 233: toeren_data = 0; 234: delay_data = 0; 235: strcpy(gas_buf, buf); 236: strncpy(gas_buf + 4,"\0",2); 237: gas_data_in = atoi(gas_buf); 238: gas_data_in = gasdata(gas_data_in); 239: 240: strcpy(toeren_buf, buf + 4); 241: strncpy(toeren_buf + 4,"\0",2); 242: toeren_data = atoi(toeren_buf); 243: toeren_data = toerendata(toeren_data); 244: 245: strcpy(delay_buf, buf + 8); 246: strncpy(delay_buf + 4,"\0",2); 247: delay_data = atoi(delay_buf); 248: 249: gas_data_in = (gas_data_in << 8); 250: spi_addres = (gas_data_in | toeren_data); 251: 252: if(delay_data > 255) 253: { 254: delay_data = 255; 255: } 256: 257: do 258: { 259: spi_WREN(); 260: spi_write(spi_addres, delay_data); 261: DelayMs(15); 262: spi_data =spi_read(spi_addres); 263: }while(delay_data != spi_data); 264: printf("spi_data = %d",spi_data); 265: printf("delay_data = %d",delay_data); 266: 267: 268: /* 269: i = 32768; 270: do 271: { i=i+256; 272: spi_WREN(); 273: spi_write(i,1); 274: DelayMs(1); 275: }while(i<65534); 276: 277: i = 32768; 278: do 279: { i=i+256; 280: spi_data =spi_read(i); 281: printf("spi_data = %d",spi_data); 282: }while(i<65534); 283: i=0; 284: */ 285: 286: serieel = 0; 287: } 288: 289: if(eerste_puls) 290: { 291: DelayUs(spi_data); 292: INJECTOR = 0; 293: eerste_puls = 0; 294: } 295: 296: if(ready) //Er is dan een frequentie meting afgerond 297: { 298: DelayUs(spi_data); 299: INJECTOR = 0; 300: ready = 0; 301: 302: adc = (adc/4); 303: gas_data = 0; 304: toeren_data = 0; 305: ready = 0; 306: spi_data = 0; 307: period = 5000000 / copy; 308: period = (period /12); 309: toeren_data = toerendata(period); // dan hebben we hier de waarde van toerental 310: gas_data = gasdata(adc); // dan hebben we nu de waarde van de gasstand 311: gas_data = (gas_data << 8); 312: spi_addres = (gas_data | toeren_data); // en is dit het adres van de EEPROM 313: spi_data =spi_read(spi_addres); // spi_data heeft de waarde die op spi_addres staat 314: 315: 316: gas_data = gasdata(adc); 317: // sprintf(buf, "%04d%04d%04d",gas_data,period,spi_data); 318: // printf("%04d",gas_data); 319: // printf("%04d",period); 320: // printf("%04d",spi_data); 321: 322: // printf("SPI_ADDRESS_DATA= %4d\n\r", spi_address_data); 323: // TMR0 = 0xff;// TMR0 LADEN MET 0x01 IS GELIJK AAN 100uS DELAY 324: // spi_data = 25; //Komt overeen met een pulstijd van 2mS 325: 326: }; 327: 328: /* if(teller >= spi_data) 329: { 330: INJECTOR =0; 331: teller = 0; 332: }*/ 333: } 334: } 335: