channel =5; #INT_TIMER2 timer_routine(void) { channel++; if (channel == 7) { channel = 5; } //++++++++++++++Grüne Kontrollled bei bertieb+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ counter++; if (counter==1980) { output_high(LED_GREEN); } if (counter==2000) { output_low(LED_GREEN); counter=0; } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ msec++; //Zählen der Zykluszahl, entspticht nur dann ms Zahl wenn timer auf 1ms eingestellt ist if (msec ==60001) { msec = 1; //zeitstempel wird nach einer Minute wieder auf eins gesetzt min++; } DATA1 = read_adc_value(channel); //Auslesen eine Werts DATA2 = convert_to_volts(DATA1); //umrechnen in Volt Achtung! sehr Zeitintensiv durch Division printf("%D_%03.2f_%lu\n\r",channel,DATA2,msec); DATA1=0; DATA2=0; if(input(ON_OFF)) //checking power Button { output_low(POWER); //Power off delay_ms(1000); } } void main() { int8 errorvar; output_high(POWER); delay_ms(100); setup_spi(spi_master |spi_h_to_l |spi_clk_div_16); //setup for SPI-communication SSPSTAT&=0XBF; //löscht den 6.Bit output_high(LED_RED); errorvar = connect_with_search(); // setup connection to master error (errorvar); // assign error to error function if necessary puts("\n\rBT connected"); // bt-connection test-string //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /* //initialising the 4 ADC //Writing to the clockregister and to the setupregister of each ADC //for the possible options see at the file defines-end.h: //Operation modes, Gain settings, Polar operations, Update rates //second write-operation to set ADC into NORMAL mode //ATTENTION: Gain settings, Polar operations, Update rates have to be the same as first */ output_low(CS3); setup_adc_device(ADC_SELF,ADC_GAIN_1,ADC_UNIPOLAR,ADC_500); printf("ADC3 is calibrating\n\r"); delay_ms(3000); setup_adc_device(ADC_NORMAL,ADC_GAIN_1,ADC_UNIPOLAR,ADC_500); output_high(CS3); output_low(CS4); setup_adc_device(ADC_SELF,ADC_GAIN_1,ADC_UNIPOLAR,ADC_500); printf("ADC4 is calibrating\n\r"); delay_ms(3000); setup_adc_device(ADC_NORMAL,ADC_GAIN_1,ADC_UNIPOLAR,ADC_500); output_high(CS4); setup_timer_2(T2_DIV_BY_16, 173, 2); //Timer2 set to 2ms enable_interrupts(INT_TIMER2); enable_interrupts(GLOBAL); do {} while(1); } Hier noch die Funktionen: //+++++++Read of a 16 bit data+++++++++++++++++++++++++ long int read_adc_word() { long idata_h, idata_l; idata_h = spi_read(0); idata_l = spi_read(0); return ((idata_h << 8) | idata_l); } //setup the device paramaters(mode, gainsetting, polar operation and output rate) void setup_adc_device(int calmode, int gainsetting, int operation, int rate) { spi_write( 0x20 );//Communications Register set to write of clock register spi_write( rate );//Clock Register info here spi_write( 0x10 );//Communications Register set to write of setup register spi_write( calmode|gainsetting|operation);//Setup Register info here /* //Versuch eine zweite Calibrierung mit zweitem Kanal daher 0x11 ins com reg delay_ms(3000); spi_write( 0x11 );//Communications Register set to write of setup register spi_write( (calmode|gainsetting|operation)&0xfe);//Setup Register info here //Versuch letztes bit zu löschen siehe datenblatt (wurde auch für ersten Kanal versucht) */ } //read an adc value from the specified channel long int read_adc_value(int ch) { long int value; if (ch==1||ch==2) { output_low(CS1); if(ch%2) { spi_write(0x38);//communications register set to read of data register of channel 1 } else { spi_write(0x39);//communications register set to read of data register of channel 0 } while ( input(DRDY1) ); value=read_adc_word(); output_high(CS1); return value; } if (ch==3||ch==4) { output_low(CS2); if(ch%2) { spi_write(0x38);//communications register set to read of data register of channel 1 } else { spi_write(0x39);//communications register set to read of data register of channel 0 } while ( input(DRDY2) ); value=read_adc_word(); output_high(CS2); return value; } if (ch==5||ch==6) { output_low(CS3); // while ( !input(DRDY3) ); //VErsuch wie im Orig Driver Programm bleibt hier Hängen => DRDY wird nicht hight oder zu kurz high!! if(ch%2) { spi_write(0x38);//communications register set to read of data register of channel 1 } else { spi_write(0x39);//communications register set to read of data register of channel 0 } while (input(DRDY3) ); value=read_adc_word(); // printf("DRDY3 high"); output_high(CS3); return value; } if (ch==7||ch==8) { output_low(CS4); if(ch%2) { spi_write(0x38);//communications register set to read of data register of channel 1 } else { spi_write(0x39);//communications register set to read of data register of channel 0 } while ( input(DRDY4) ); value=read_adc_word(); output_high(CS4); return value; } } //disable the a/d conversion void adc_disable() { spi_write( 0x20 );//Communications Register set to write of clock register spi_write( 0x10 );//Clock Register info here } //+++++++++++++Convert the value read to volts+++++++++++++++++++++++++++++++++++++++++++++++++ //Caution! caused by the division this funktion takes a lot of time float convert_to_volts(long data) { return ((float)data*1.25/65535); //1.25 is Vref }