move adc to seperate file

rewrite_for_hd774
Eggert Jung 5 years ago
parent 92f31a1849
commit 911967c9cc

65
adc.c

@ -0,0 +1,65 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include "adc.h"
#include "buffer.h"
volatile buffer_t adc_buf[4];
volatile float adc_avrg[4];
void initADC()
{
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;
//TODO write non broken adc code
//Reading 10bit conversion result
uint16_t ADC_reading = ADCL; //copy the first LSB bits
ADC_reading |= ADCH << 8; //copy remaing byte
ADC_reading *= 0.33;
insert_to_buffer(ADC_reading, &adc_buf[current_channel]);
//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;
// //}
//}
current_channel++;
if(current_channel == 4)
current_channel = 0;
set_ADC_Channel(current_channel);
ADCSRA |= (1 << ADSC); //Start next conversion
}

12
adc.h

@ -0,0 +1,12 @@
#ifndef _ADC_H_
#define _ADC_H_
#include "buffer.h"
extern volatile buffer_t adc_buf[4];
extern volatile float adc_avrg[4];
void initADC(void);
#endif//_ADC_H_

@ -5,11 +5,10 @@
#include "buffer.h"
#include "pid.h"
#include "adc.h"
void modbusGet(void);
volatile buffer_t adc_buf[4];
volatile float adc_avrg[4];
volatile float output;
volatile struct pid controller;
@ -18,20 +17,6 @@ volatile float setpoint_1 = 130;
volatile float setpoint_2 = 150;
volatile float setpoint_3 = 150;
void initADC(void)
{
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"
}
int main(){
DDRD |= (1 << 4); // LED
DDRD |= (1 << 3); // FU PWM
@ -139,33 +124,3 @@ ISR(TIMER1_COMPA_vect) {
PORTB &= ~(0x0E);
}
ISR(ADC_vect)
{
static uint8_t init[4] = {0,0,0,0};
static uint8_t current_channel = 0;
//Reading 10bit conversion result
uint16_t ADC_reading = ADCL; //copy the first LSB bits
ADC_reading |= ADCH << 8; //copy remaing byte
ADC_reading *= 0.33;
insert_to_buffer(ADC_reading, &adc_buf[current_channel]);
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] = tmp;
}
else{
adc_avrg[current_channel] = get_buffer_mean(&adc_buf[current_channel]);
init[current_channel]=0;
}
}
current_channel++;
if(current_channel == 4)
current_channel = 0;
set_ADC_Channel(current_channel);
ADCSRA |= (1 << ADSC); //Start next conversion
}

Loading…
Cancel
Save