Commit b1216078 authored by Martin Vítek's avatar Martin Vítek
Browse files

Add ADC

parent 95d8a26f
#pragma once
#include <avr/io.h>
#include <stdint.h>
#include <stddef.h>
//In unsigned mode GND is at offset (VREF/2)-(VREF*0.05) = 500-50 = 450mV
//But page 236, Figure 22-11 say, that offset is 200 ADC
class ADC
{
private:
volatile uint8_t channel;
uint8_t ReadCalibrationByte( uint8_t index )
{
uint8_t result;
/* Load the NVM Command register to read the calibration row. */
NVM_CMD = NVM_CMD_READ_CALIB_ROW_gc;
result = pgm_read_byte(index);
/* Clean up NVM Command register. */
NVM_CMD = NVM_CMD_NO_OPERATION_gc;
return( result );
}
public:
enum ADC_values
{
TEMPERATURE_SENSOR,
BANDGAP_VOLTAGE,
AVCC,
CURRENT,
THERMOCOUPLE,
SUPPLY_VOLTAGE,
AMBIENT_TEMPERATURE
};
volatile uint16_t adc_results[7];
volatile uint16_t results[7];
ADC()
{
PORTA.DIRCLR = PIN1_bm | PIN3_bm | PIN4_bm | PIN5_bm;
PORTCFG.MPCMASK = PIN1_bm | PIN3_bm | PIN4_bm | PIN5_bm;
PORTA.PIN1CTRL = PORT_ISC_INPUT_DISABLE_gc;
//1V reference, enable temp sensor
ADCA.REFCTRL = ADC_REFSEL_INT1V_gc | ADC_BANDGAP_bm | ADC_TEMPREF_bm;
//125kHz
ADCA.PRESCALER = ADC_PRESCALER_DIV256_gc;
//Set callibration values from signature row - may not bee working!!!
ADCA.CALL = ReadCalibrationByte( offsetof(NVM_PROD_SIGNATURES_t, ADCACAL0) );
ADCA.CALH = ReadCalibrationByte( offsetof(NVM_PROD_SIGNATURES_t, ADCACAL1) );
//Enable IRQ
ADCA.CH0.INTCTRL = ADC_CH_INTMODE_COMPLETE_gc | ADC_CH_INTLVL_LO_gc;
//Enable ADC
ADCA.CTRLA |= ADC_ENABLE_bm;
}
void start_conversion()
{
channel = 0;
ADCA.CH0.CTRL &= ~(ADC_CH_INPUTMODE1_bm | ADC_CH_INPUTMODE0_bm);
ADCA.CH0.MUXCTRL = ADC_CH_MUXINT_TEMP_gc;
ADCA.CTRLA |= ADC_CH0START_bm;
//OR
//ADCA.CH0.CTRL = ADC_CH_START_bm;
}
void conversion_handler()
{
adc_results[channel++] = ADCA.CH0.RES;
switch (channel)
{
case 1: ADCA.CH0.MUXCTRL = ADC_CH_MUXINT_BANDGAP_gc;
ADCA.CTRLA |= ADC_CH0START_bm;
break;
case 2: ADCA.CH0.MUXCTRL = ADC_CH_MUXINT_SCALEDVCC_gc;
ADCA.CTRLA |= ADC_CH0START_bm;
break;
case 3: ADCA.CH0.CTRL |= ADC_CH_INPUTMODE_SINGLEENDED_gc;
ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN1_gc;
ADCA.CTRLA |= ADC_CH0START_bm;
break;
case 4: ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN3_gc;
ADCA.CTRLA |= ADC_CH0START_bm;
break;
case 5: ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN4_gc;
ADCA.CTRLA |= ADC_CH0START_bm;
break;
case 6: ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN5_gc;
ADCA.CTRLA |= ADC_CH0START_bm;
break;
default:
break;
}
}
};
......@@ -46,6 +46,7 @@ set(SOURCE_FILES
Uart.cpp
Uart.h
Backlight.h
ADC.h
)
add_avr_executable(safe-soldering-station ${SOURCE_FILES})
......@@ -64,6 +64,7 @@ void SafeSolderingStation::init()
uart.init();
lcd.init();
irq_init();
backlight.set_intensity(100);
}
void SafeSolderingStation::wellcome()
......@@ -118,3 +119,15 @@ void SafeSolderingStation::test_backlight()
lcd.write_num(backlight.get_intensity());
}
}
void SafeSolderingStation::test_adc()
{
adc.start_conversion();
_delay_ms(20);
lcd.gotoxy_new(0,5);
lcd.write_num(adc.adc_results[adc.AVCC]);
lcd.gotoxy_new(1,5);
lcd.write_num(adc.adc_results[adc.SUPPLY_VOLTAGE]);
}
......@@ -7,6 +7,7 @@
#include "encoder.h"
#include "Uart.h"
#include "Backlight.h"
#include "ADC.h"
class SafeSolderingStation
{
......@@ -26,6 +27,7 @@ class SafeSolderingStation
HD44780 lcd;
Uart uart;
Backlight backlight;
ADC adc;
//Methods
SafeSolderingStation();
......@@ -35,6 +37,7 @@ class SafeSolderingStation
void test_encoder();
void test_backlight();
void test_adc();
};
//Global instance
......
......@@ -22,6 +22,11 @@ ISR(PORTB_INT1_vect)
sss.enc.B_handler();
}
//ADC
ISR(ADCA_CH0_vect)
{
sss.adc.conversion_handler();
}
ISR(BADISR_vect)
{
......
......@@ -7,4 +7,7 @@ ISR(PORTA_INT0_vect);
ISR(PORTB_INT0_vect);
ISR(PORTB_INT1_vect);
//ADC
ISR(ADCA_CH0_vect);
ISR(BADISR);
......@@ -12,12 +12,16 @@ int main()
sss.wellcome();
sss.lcd.gotoxy_new(0,0);
sss.lcd.write_text("Duty: %");
sss.lcd.write_text("3V4: ");
sss.lcd.gotoxy_new(1,0);
sss.lcd.write_text("24V: ");
for(;;)
{
//sss.test_encoder();
sss.test_backlight();
//sss.test_backlight();
sss.test_adc();
_delay_ms(50);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment