m05e02 ok, calibration random
This commit is contained in:
12
.vscode/settings.json
vendored
12
.vscode/settings.json
vendored
@@ -2,16 +2,6 @@
|
|||||||
"editor.insertSpaces": false,
|
"editor.insertSpaces": false,
|
||||||
"editor.detectIndentation": false,
|
"editor.detectIndentation": false,
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"io.h": "c",
|
"header.h": "c"
|
||||||
"delay.h": "c",
|
|
||||||
"interrupt.h": "c",
|
|
||||||
"timer.h": "c",
|
|
||||||
"usart.h": "c",
|
|
||||||
"utils.h": "c",
|
|
||||||
"bitmanip.h": "c",
|
|
||||||
"rush_header.h": "c",
|
|
||||||
"twi.h": "c",
|
|
||||||
"header.h": "c",
|
|
||||||
"adc.h": "c"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,30 +3,40 @@
|
|||||||
// 24.2 : The ADC generates a 10-bit result which is presented in the ADC Data Registers, ADCH and ADCL
|
// 24.2 : The ADC generates a 10-bit result which is presented in the ADC Data Registers, ADCH and ADCL
|
||||||
|
|
||||||
void adc_init(uint8_t prescaler_value) {
|
void adc_init(uint8_t prescaler_value) {
|
||||||
ADMUX = (1 << REFS0); // Table 24-3 : set voltage reference, AVCC with external capacitor at AREF pin
|
ADMUX = (1 << REFS0); // Table 24-3 : set voltage reference, AVCC with external capacitor at AREF pin
|
||||||
ADCSRA = (1 << ADEN); // 24.9.2 : enable ADC
|
ADCSRA = (1 << ADEN); // 24.9.2 : enable ADC
|
||||||
ADCSRA |= (1 << ADATE); // 24.9.2 : enable Auto Trigger -> it will start a conversion on the selected channel in ADMUX when the selected source (in ADCSRB) is triggered
|
ADCSRA |= (1 << ADATE); // 24.9.2 : enable Auto Trigger -> it will start a conversion on the selected channel in ADMUX when the selected source (in ADCSRB) is triggered
|
||||||
ADCSRA |= (1 << ADIE); // 24.9.2 : enable ADC Interrupt
|
ADCSRA |= (1 << ADIE); // 24.9.2 : enable ADC Interrupt
|
||||||
ADCSRA |= ADC_PRESCALE_SET(prescaler_value); // Table 24-5 : prescaler ADC
|
ADCSRA |= ADC_PRESCALE_SET(prescaler_value); // Table 24-5 : prescaler ADC
|
||||||
|
|
||||||
ADCSRB = ADC_TRIGGER_TIMER_1_COMPARE_B; // Table 24-6 : ADC Auto Trigger Source
|
ADCSRB = ADC_TRIGGER_TIMER_1_COMPARE_B; // Table 24-6 : ADC Auto Trigger Source
|
||||||
ADMUX = (ADMUX & 0b11110000) | (adc_channel & 0b00001111); // Table 24-4 : Select ADC channel 0
|
ADMUX = (ADMUX & 0b11110000) | (adc_channel & 0b00001111); // Table 24-4 : Select ADC channel 0
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t calibrate_temperature(uint16_t value, uint8_t speed) { // 24.8 : Temperature Measurement
|
||||||
|
// calibration random
|
||||||
|
uint16_t offset = (540 - (25*speed));
|
||||||
|
value = (value - offset) / speed;
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void adc_print_dec(uint16_t value) {
|
void adc_print_dec(uint16_t value) {
|
||||||
|
if (adc_channel == ADC_THERMISTOR) {
|
||||||
|
value = calibrate_temperature(value, 5);
|
||||||
|
}
|
||||||
char buffer[17] = {0};
|
char buffer[17] = {0};
|
||||||
uint16_to_string(value, buffer);
|
uint16_to_string(value, buffer);
|
||||||
uart_printstr(buffer);
|
uart_printstr(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(ADC_vect) { // Table 12-6 : interrupt vector for ADC Conversion Complete
|
ISR(ADC_vect) { // Table 12-6 : interrupt vector for ADC Conversion Complete
|
||||||
uint16_t value = ADC; // 24.9.3.2 : read ADC 16 bits precision
|
uint16_t value = ADC; // 24.9.3.2 : read ADC 16 bits precision
|
||||||
adc_print_dec(value);
|
adc_print_dec(value);
|
||||||
adc_channel = (adc_channel + 1) % 3; // loop through channels
|
adc_channel = (adc_channel + 1) % 3; // loop through channels
|
||||||
ADMUX = (ADMUX & 0b11110000) | (adc_channel & 0b1111); // Table 24-4 : Select ADC channel
|
ADMUX = (ADMUX & 0b11110000) | (adc_channel & 0b1111); // Table 24-4 : Select ADC channel
|
||||||
if (adc_channel != 0) {
|
if (adc_channel != 0) {
|
||||||
uart_printstr(", ");
|
uart_printstr(", ");
|
||||||
ADCSRA |= (1 << ADSC); // 24.9.2 : start next conversion
|
ADCSRA |= (1 << ADSC); // 24.9.2 : start next conversion
|
||||||
} else {
|
} else {
|
||||||
uart_printstr("\r\n");
|
uart_printstr("\r\n");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,8 @@ void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits);
|
|||||||
void uint16_to_string(uint16_t value, char *out);
|
void uint16_to_string(uint16_t value, char *out);
|
||||||
// adc.c
|
// adc.c
|
||||||
void adc_init(uint8_t prescaler_value);
|
void adc_init(uint8_t prescaler_value);
|
||||||
uint16_t adc_read(uint8_t channel);
|
uint16_t calibrate_temperature(uint16_t value, uint8_t speed);
|
||||||
|
void adc_print_dec(uint16_t value);
|
||||||
// uart.c
|
// uart.c
|
||||||
void uart_init();
|
void uart_init();
|
||||||
void uart_tx(char c);
|
void uart_tx(char c);
|
||||||
@@ -38,5 +39,8 @@ void uart_printstr_endl(const char* str);
|
|||||||
//
|
//
|
||||||
// MACROS
|
// MACROS
|
||||||
//
|
//
|
||||||
|
#define ADC_POTENTIOMETER 0
|
||||||
|
#define ADC_PHOTORESISTOR 1
|
||||||
|
#define ADC_THERMISTOR 2
|
||||||
|
|
||||||
#endif // HEADER_H
|
#endif // HEADER_H
|
||||||
@@ -12,14 +12,14 @@
|
|||||||
// - P = 64 -> Fadc = 16,000,000 / 64 = 250,000 = 250KHz -> OK
|
// - P = 64 -> Fadc = 16,000,000 / 64 = 250,000 = 250KHz -> OK
|
||||||
// - P = 128 -> Fadc = 16,000,000 / 128 = 125,000 = 125KHz -> OK
|
// - P = 128 -> Fadc = 16,000,000 / 128 = 125,000 = 125KHz -> OK
|
||||||
|
|
||||||
#define ADC_PRESCALER 128 // Table 24-5 : can only be 2, 4, 8, 16, 32, 64, or 128
|
#define ADC_PRESCALER 128 // Table 24-5 : can only be 2, 4, 8, 16, 32, 64, or 128
|
||||||
|
|
||||||
volatile uint8_t adc_channel = 0; // Table 14-6 : alternate function for RV1 on PC0 -> ADC0, LDR on PC1 -> ADC1, NTC on PC2 -> ADC2 ...
|
volatile uint8_t adc_channel = ADC_POTENTIOMETER; // Table 14-6 : alternate function for RV1 on PC0 -> ADC0, LDR on PC1 -> ADC1, NTC on PC2 -> ADC2 ...
|
||||||
|
|
||||||
// read 3 analog values at 10 bits and print them in uart as decimal : potentiometer RV1 (Variable Resistor), photoresistor LDR (Light Dependent Resistor), thermistor NTC (Negative Temperature Coefficient)
|
// read 3 analog values at 10 bits and print them in uart as decimal : potentiometer RV1 (Variable Resistor), photoresistor LDR (Light Dependent Resistor), thermistor NTC (Negative Temperature Coefficient)
|
||||||
int main() {
|
int main() {
|
||||||
char buffer[4];
|
char buffer[4];
|
||||||
SREG |= ENABLE_GLOBAL_INTERRUPT; // 7.3.1 : Status Register, bit 7 : I – Global Interrupt Enable
|
SREG |= ENABLE_GLOBAL_INTERRUPT; // 7.3.1 : Status Register, bit 7 : I – Global Interrupt Enable
|
||||||
uart_init();
|
uart_init();
|
||||||
adc_init(ADC_PRESCALER);
|
adc_init(ADC_PRESCALER);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user