From 3c1cb265fd18047294b6aaad4dcba38b0dbe7437 Mon Sep 17 00:00:00 2001 From: akilhylton Date: Mon, 3 Aug 2020 13:50:42 -0400 Subject: [PATCH] Added math to calculate flow rate --- board/adc/adc.c | 31 ++++++++++++++++--------------- board/driver/adc.h | 12 ++++++++---- board/main.c | 11 ++++------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/board/adc/adc.c b/board/adc/adc.c index 64397b4..bbd55d0 100644 --- a/board/adc/adc.c +++ b/board/adc/adc.c @@ -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); } -*/ diff --git a/board/driver/adc.h b/board/driver/adc.h index 06147ca..aa47989 100644 --- a/board/driver/adc.h +++ b/board/driver/adc.h @@ -1,7 +1,11 @@ +#include #include -#include -#define OFFSET 44 +#include +#include +#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); diff --git a/board/main.c b/board/main.c index 8eefcc2..e2e72c0 100644 --- a/board/main.c +++ b/board/main.c @@ -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(); + } }