encoder, adc, averaging
parent
b87e2b846d
commit
0b2e532675
@ -0,0 +1,76 @@
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#include "adc.h"
|
||||
|
||||
void adc_init()
|
||||
{
|
||||
ADMUX = 1 << REFS0 | 0 << REFS1; //Select external Vref
|
||||
|
||||
//ADC Status Register A
|
||||
ADCSRA = 1 << ADEN //Enable ADC
|
||||
| 1 << ADIE //Enable ISR after conversion complete
|
||||
//| 1<<ADATE //Freerunning-Mode
|
||||
//| 1<<ADLAR //2 results bits are left aligned
|
||||
| 1 << ADPS2 //Set clock-prescaler to 128
|
||||
| 1 << ADPS1 | 1 << ADPS0;
|
||||
ADCSRA |= 1 << ADSC; //Start first Conversion for "warmup"
|
||||
}
|
||||
|
||||
void set_ADC_Channel(uint8_t adr)
|
||||
{
|
||||
if (adr < 11)
|
||||
{
|
||||
ADMUX &= (0b11110000); //Clear MUX-Address
|
||||
ADMUX |= adr; //Set new MUX-Address
|
||||
}
|
||||
}
|
||||
|
||||
ISR(ADC_vect)
|
||||
{
|
||||
//static uint8_t init[4] = {0,0,0,0};
|
||||
static uint8_t current_channel = 0;
|
||||
|
||||
static uint16_t read_buffer[128];
|
||||
static uint8_t buffer_pos = 0;
|
||||
|
||||
|
||||
//Reading 10bit conversion result
|
||||
uint16_t ADC_reading = ADCL; //copy the first LSB bits
|
||||
ADC_reading |= ADCH << 8; //copy remaing byte
|
||||
|
||||
read_buffer[buffer_pos] = ADC_reading;
|
||||
buffer_pos++;
|
||||
|
||||
if(buffer_pos == (sizeof(read_buffer) / sizeof(read_buffer[0]))){
|
||||
uint32_t sum = 0;
|
||||
for(uint8_t i = 0; i < buffer_pos; i++){
|
||||
sum += read_buffer[i];
|
||||
}
|
||||
temp_values[current_channel] = sum/buffer_pos;
|
||||
|
||||
buffer_pos = 0;
|
||||
|
||||
current_channel++;
|
||||
if(current_channel == 4)
|
||||
current_channel = 0;
|
||||
set_ADC_Channel(current_channel);
|
||||
}
|
||||
|
||||
ADCSRA |= (1 << ADSC); //Start next conversion
|
||||
|
||||
|
||||
//TODO write non broken adc code
|
||||
//if(adc_buf[current_channel].position == BUFFER_SIZE-1){
|
||||
// //if(init[current_channel]){
|
||||
// //float tmp = (99*adc_avrg[current_channel]) + get_buffer_mean(&adc_buf[current_channel]);
|
||||
// //tmp /= 100;
|
||||
// adc_avrg[current_channel] = get_buffer_mean(&adc_buf[current_channel]);
|
||||
// //}
|
||||
// //else{
|
||||
// // adc_avrg[current_channel] = get_buffer_mean(&adc_buf[current_channel]);
|
||||
// // init[current_channel]=0;
|
||||
// //}
|
||||
//}
|
||||
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
#ifndef _ADC_H_
|
||||
#define _ADC_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
extern volatile uint16_t temp_values[4];
|
||||
|
||||
void adc_init(void);
|
||||
|
||||
#endif//_ADC_H_
|
||||
|
||||
@ -1,7 +1,15 @@
|
||||
#ifndef _MENU_H_
|
||||
#include <stdint.h>
|
||||
|
||||
void set_page(uint8_t page_num);
|
||||
extern volatile uint8_t menu_state;
|
||||
|
||||
extern volatile uint16_t temp_values[4];
|
||||
extern volatile uint16_t temp_setpoints[4];
|
||||
|
||||
void write_temps(void);
|
||||
void write_setpoints(void);
|
||||
void update_cursor(void);
|
||||
void set_item(uint8_t page_num);
|
||||
void draw_menu(void);
|
||||
|
||||
#endif
|
||||
|
||||
Loading…
Reference in New Issue