#include #include #include #include #include #include "analog.h" #define EE_Ref 1 // 4395600L; unsigned long refval =0;// = 4395600L; void initADC(){ bitClear(PRR,PRADC); // clearing the ADC Power Reduction bit // bitSet(ADCSRA,ADEN); ADCSRA = _BV(ADEN)|_BV(ADPS2)|_BV(ADPS1)|_BV(ADPS0); } void stopADC(){ bitClear(ADCSRA,ADEN); bitSet(PRR,PRADC); } static volatile byte ADC_complete; ISR(ADC_vect) { ADC_complete = true; } long readADC(){ set_sleep_mode(SLEEP_MODE_ADC); ADCSRA |= _BV(ADIE); ADC_complete=false; sleep_enable(); while(!ADC_complete){sei(); sleep_cpu(); } sleep_disable(); return ADC; } void readref(){ refval = eeprom_read_dword((const uint32_t *)EE_Ref); } long readVcc() { if(refval==0) readref(); long result = 0; // Read 1.1V reference against AVcc ADMUX = _BV(MUX5) | _BV(MUX0); _delay_ms(1); // Wait for Vref to settle uint8_t i=16 ; while(i--) result += readADC(); result >>= 2; //result = 4418850L / result; // Back-calculate AVcc in mV result = refval / result; return result; } long readtemp(){ // Temp at ADC2, TempVcc at PA3 // switch PA3 on bitSet(TEMP_SENSOR_VCC_DDR,TEMP_SENSOR_VCC_BIT); bitSet(TEMP_SENSOR_VCC_PORT,TEMP_SENSOR_VCC_BIT); long vref=readVcc(); ADMUX = TEMP_SENSOR_ADMUX; //_delay_ms(1); // Wait for LTM86 to settle readADC(); long res =0; uint8_t cpt =16; while (cpt--) res+= readADC(); res >>= 2; long temp = vref * res / 4096 ; // temp = (sqrt(118.548544+0.01388*(1777.3-temp))-10.888)*144.09221902 + 30; // Serial.print("TEMP ");Serial.println(temp); // digitalWrite(tempvcc,HIGH); // Stop Sensor bitClear(TEMP_SENSOR_VCC_PORT,TEMP_SENSOR_VCC_BIT); // bitClear(TEMP_SENSOR_VCC_DDR,TEMP_SENSOR_VCC_BIT); return temp; } float calctemp(long t){ return (sqrt(118.548544+0.01388*(1777.3-t))-10.888)*144.09221902 + 30; }