Skip to content

Commit

Permalink
Added math to calculate flow rate
Browse files Browse the repository at this point in the history
  • Loading branch information
k105la committed Aug 3, 2020
1 parent 3061eed commit 3c1cb26
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
31 changes: 16 additions & 15 deletions board/adc/adc.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
#include "../driver/adc.h"


void adcInit(void) {
/* Enable ADC */
ADCSRA = (1 << ADEN) | (1 << ADSC);
/* Left Adjust Results */
//ADCSRB = (1 << ADLAR);
//printf("ADC has started\n");
}

/*
void getPSI(void) {

void getPascal(void) {
while(ADCSRA & (1 << ADSC));
ADCSRA |= (1 << ADSC); // Start ADC conversion
float rawPresureData = ADCH; // Max is 8bit value
printf("PSI: %.2f", (rawPresureData / 255.0) * 14.5);
delay_ms(100);
int ten_bit_value = ADCL + (ADCH * 256);
kiloPascal = ten_bit_value / SENSITIVITY;
pascal = fabs(kiloPascal - 0.98) * 1000.00;
printf("Pa: %.2f\n", pascal);
_delay_ms(250);
}

uint8_t adcStart(void) {
void getFlowRate(void) {
while(ADCSRA & (1 << ADSC));
ADCSRA |= (1 << ADSC); // Start ADC conversion
loop_until_bit_is_clear(ADCSRA, ADSC);
uint8_t rawPresureData = ADCH; // Max is 8bit value
return rawPresureData;
int ten_bit_value = ADCL + (ADCH * 256);
kiloPascal = ten_bit_value / SENSITIVITY;
pascal = fabs(kiloPascal - 0.98) * 1000.00;
flow = sqrt(pascal / 33); // Bernoulli's equation
printf("Flow rate %.2f cm/s\n", flow);
_delay_ms(250);
}
*/
12 changes: 8 additions & 4 deletions board/driver/adc.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include <stdio.h>
#include <avr/io.h>
#include <stdint.h>
#define OFFSET 44
#include <math.h>
#include <util/delay.h>
#define SENSITIVITY 44.00

float kiloPascal, pascal;
float flow;
void adcInit(void);
//void getPSI(void);
//uint8_t adcStart(void);
void getPascal(void);
void getFlowRate(void);
11 changes: 4 additions & 7 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
static FILE mystdout = FDEV_SETUP_STREAM(print, NULL, _FDEV_SETUP_RW);

int main(void) {
adcInit();
usartInit(MYUBRR);
adcInit();
usartInit(MYUBRR);
stdout = &mystdout;
while(1) {
while(ADCSRA & (1 << ADSC));
ADCSRA |= (1 << ADSC); // Start ADC conversion
// TODO: Must subtract ADCH from offset.
printf("PSI: %.2f\n", (ADCH / 255.0) * 14.5);
}
getFlowRate();
}
}

0 comments on commit 3c1cb26

Please sign in to comment.