47 lines
No EOL
1 KiB
C
47 lines
No EOL
1 KiB
C
#include "config.h"
|
|
#include <avr/io.h>
|
|
#include <util/delay.h>
|
|
#include "LM335.h"
|
|
|
|
void ADC_Init()
|
|
{
|
|
// Set AVCC as the voltage reference with external capacitor at AREF pin
|
|
ADMUX = (1 << REFS0);
|
|
|
|
// Set ADC clock prescaler to 64 (125kHz ADC clock)
|
|
ADCSRA = (1 << ADPS2) | (1 << ADPS1);
|
|
|
|
// Enable ADC, enable interrupts
|
|
ADCSRA |= (1 << ADEN); // | (1 << ADIE);
|
|
}
|
|
|
|
uint16_t ADC_Read(uint8_t channel)
|
|
{
|
|
// Select ADC channel
|
|
ADMUX = (ADMUX & 0xF8) | (channel & 0x07);
|
|
|
|
// Start ADC conversion
|
|
ADCSRA |= (1 << ADSC);
|
|
|
|
// Wait for ADC conversion to complete
|
|
while (ADCSRA & (1 << ADSC))
|
|
;
|
|
|
|
// Read ADC result
|
|
return ADC;
|
|
}
|
|
|
|
float LM335_ReadTemperature()
|
|
{
|
|
// Read ADC value from LM335 pin (PA0)
|
|
uint16_t adcValue = ADC_Read(0x00);
|
|
|
|
float vref = 4.64;
|
|
int vfactor = 100;
|
|
float voltage = (adcValue * vref) / 1024;
|
|
|
|
// Calculate temperature in Celsius using LM335 formula
|
|
float temperature = (voltage - 2.92) * vfactor;
|
|
|
|
return temperature;
|
|
} |