const float BETA = 3950; // should match the Beta Coefficient of the thermistor
void init_ADC(void);
unsigned char start_ADC(void);
uint16_t read_ADC(void);
void Temp(uint16_t value); // Function declaration
void setup() {
Serial.begin(9600);
init_ADC();
start_ADC();
uint16_t x = read_ADC();
float y;
while (1) {
Temp(x); // Pass x to Temp function
delay(1000); // Delay for 1 second
}
}
void init_ADC() {
volatile char *admux = (volatile char *)0x7C;
volatile char *adcsra = (volatile char *)0x7A;
*admux = 0x40; // Internal 1.1V voltage reference with external capacitor at AREF pin
sei(); // Enable global interrupt;
*adcsra = 0xA7; // 11010111 128 factor and adc enable adc start
}
unsigned char start_ADC() {
volatile char *adcsra = (volatile char *)0x7A;
*adcsra = *adcsra | 0x40; // Starting the ADC
}
uint16_t read_ADC() {
// volatile char *adcsra = (volatile char *)0x7A;
volatile unsigned char *L = (volatile unsigned char *)0x78;
volatile unsigned char *H = (volatile unsigned char *)0x79;
uint16_t value;
//while ((*adcsra & 0x10) == 0); // Wait for conversion to be completed
value = (*H << 8) | *L; // Combine the upper and lower bits to get 10-bit result
// *adcsra = *adcsra | 0x10; // Clear the flag
return value;
}
void Temp(uint16_t value) {
float analogValue = float(value); // Convert to float
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.println(celsius); // Print temperature
}
ISR(ADC_vect) {
// ADC conversion complete interrupt handler
ADCSRA |= (1 << 4); // Clear interrupt flag
}
void loop() {
// Your main loop code here
}