From d1b6c828cbf6c482cb3dc440289e63121b47b9c7 Mon Sep 17 00:00:00 2001 From: jakeg00dwin Date: Sun, 28 Jul 2024 17:29:37 -0700 Subject: [PATCH] Patched from High Repo --- src/ADC/ADC.c | 70 ++++++++++++++++++++++------------------- src/ADC/ADC.h | 21 +++++++++++-- src/ADC/CMakeLists.txt | 19 +++++------ src/load/CMakeLists.txt | 24 ++++++-------- src/load/load.c | 62 ++++++++++++++++++++++-------------- 5 files changed, 113 insertions(+), 83 deletions(-) diff --git a/src/ADC/ADC.c b/src/ADC/ADC.c index 44e41e4..be0ff82 100644 --- a/src/ADC/ADC.c +++ b/src/ADC/ADC.c @@ -13,57 +13,62 @@ #include "RegEdit.h" #include "avr/io.h" -static uint8_t porta_out; -static uint8_t porta_dir; +#define MAX_PIN_NUM 7 static bool IsInvalidPin(uint8_t pin_num){ - if(pin_num > 7){ + if(pin_num > MAX_PIN_NUM){ return true; } return false; } +void ADC_Setup(void) +{ + //Clears control register A for ADC0 + RegEdit_SetNum((void *) &ADC0.CTRLA, 0x00); + + //Sets The sample accumulation number to 32. + RegEdit_SetNum((void *) &ADC0.CTRLB, 0x5); + + //Sets the voltage reference to VDD or VCC. + RegEdit_SetBit((void *) &ADC0.CTRLC, 4); + + //Sets the pre-scalar for the adc sample rate. + RegEdit_SetBit((void *) &ADC0.CTRLC, 2); + + //Setup an Initalization delay. + RegEdit_OR_Num((void *) &ADC0.CTRLD, (2<<5)); + + //Set the bit for ADC variation during readings. + RegEdit_SetBit((void *) &ADC0.CTRLD, 4); +} + void ADC_Init(uint8_t pin_num) { if(IsInvalidPin(pin_num)){return;} - //Save the porta status. - porta_out = RegEdit_ReadReg((void *) &PORTA.OUT); - porta_dir = RegEdit_ReadReg((void *) &PORTA.DIR); //set the direction to input - RegEdit_SetBit((void *) &PORTA.DIRCLR, pin_num); + RegEdit_ClearBit((void *) &PORTA.DIR, pin_num); //Disable the pull-up resistor - RegEdit_SetBit((void *) &PORTA.OUTCLR, pin_num); + RegEdit_ClearBit((void *) &PORTA.OUT, pin_num); //Disable input buffer //We do some kinda nasty address addition but it saves //memory and means we don't need a switch statment. - //PORT_ISC_INPUT_DISABLE_gc RegEdit_SetBit( (void *) (&PORTA.PIN0CTRL)+pin_num, PORT_ISC_INPUT_DISABLE_gc ); - //Ensure we use full 10bit resolution - //RegEdit_ClearBit((void *) &ADC0.CTRLA, 2); - - //Use VDD as our voltage refernce. - //RegEdit_SetBit((void *) &ADC0.CTRLC, 4); - } -void ADC_Enable(uint8_t pin_num) +void ADC_Enable(void) { - if(IsInvalidPin(pin_num)){return;} - - //Use muxpos to select pins for ADC to connect. - RegEdit_OR_Num((void *) &ADC0.MUXPOS, pin_num); - //Set the enable bit in the CTRLA register RegEdit_SetBit((void *) &ADC0.CTRLA, 0); } @@ -71,15 +76,16 @@ void ADC_Enable(uint8_t pin_num) void ADC_Disable() { - //Set for no ADC connections. - RegEdit_ClearRegister((void *) &ADC0.MUXPOS); - //Clear the enable ADC flag RegEdit_ClearBit((void *) &ADC0.CTRLA, 0); - - //Restore the port registers - RegEdit_SetNum((void *) &PORTA.OUT, porta_out); - RegEdit_SetNum((void *) &PORTA.DIR, porta_dir); +} + + +void ADC_SetPin(uint8_t pin_num) +{ + if(IsInvalidPin(pin_num)){return;} + RegEdit_ClearRegister((void *) &ADC0.MUXPOS); + RegEdit_SetNum((void *) &ADC0.MUXPOS, pin_num); } @@ -88,8 +94,6 @@ uint16_t ADC_ReadValue_Impl(uint8_t pin_num) RegEdit_SetNum((void *) &ADC0.COMMAND, ADC_STCONV_bm); /* Wait until ADC conversion done */ - //while ( !(ADC0.INTFLAGS & ADC_RESRDY_bm) ) - //while(!(RegEdit_IsBitSet((void *) &ADC0.INTFLAGS, ADC_RESSEL_bm))) while ( !(ADC0.INTFLAGS & ADC_RESRDY_bm) ) { ; @@ -97,8 +101,10 @@ uint16_t ADC_ReadValue_Impl(uint8_t pin_num) /* Clear the interrupt flag by writing 1: */ ADC0.INTFLAGS = ADC_RESRDY_bm; - - return (uint16_t) ADC0.RES; + + uint16_t adc_val = (uint16_t) ADC0.RES; + adc_val = adc_val >> 5; + return adc_val; } diff --git a/src/ADC/ADC.h b/src/ADC/ADC.h index 9de277c..3792eab 100644 --- a/src/ADC/ADC.h +++ b/src/ADC/ADC.h @@ -32,9 +32,8 @@ void ADC_Init(uint8_t pin_num); /** * @brief Enables the ADC - * @param pin_num The number of the pin 0-7 you are enabling */ -void ADC_Enable(uint8_t pin_num); +void ADC_Enable(void); /** @@ -53,5 +52,23 @@ void ADC_Disable(); */ extern uint16_t (*ADC_ReadValue)(uint8_t pin_num); +/** + * @brief Sets up the ADC + * + * This function sets up the ADC to take and accumulate 32 samples. It also + * sets the inital delay to 32 ADC clock cycles, and sets the VREF to VDD or + * VCC. + * + * This function should only need to be called once. + */ +void ADC_Setup(void); + + +/** + * @brief Sets the pin used in the MUX for ADC0. + * + * @param pin_num The number of the pin in Port A. + */ +void ADC_SetPin(uint8_t pin_num); #endif //ADC_H diff --git a/src/ADC/CMakeLists.txt b/src/ADC/CMakeLists.txt index b159630..3b39d8c 100644 --- a/src/ADC/CMakeLists.txt +++ b/src/ADC/CMakeLists.txt @@ -1,21 +1,16 @@ +add_library(ADC STATIC + ADC.c +) +target_include_directories(ADC PUBLIC + ${CMAKE_CURRENT_LIST_DIR} +) + if(UNIT_TESTING) - add_library(ADC STATIC - ADC.c - ) - target_include_directories(ADC PUBLIC - ${CMAKE_CURRENT_LIST_DIR} - ) target_link_libraries(ADC MockRegEdit ) else() - add_library(ADC STATIC - ADC.c - ) - target_include_directories(ADC PUBLIC - ${CMAKE_CURRENT_LIST_DIR} - ) target_link_libraries(ADC RegEdit ) diff --git a/src/load/CMakeLists.txt b/src/load/CMakeLists.txt index 5493a0b..a61e227 100644 --- a/src/load/CMakeLists.txt +++ b/src/load/CMakeLists.txt @@ -1,22 +1,18 @@ +add_library(load STATIC + load.c +) +target_include_directories(load PUBLIC + ${CMAKE_CURRENT_LIST_DIR} +) + if(UNIT_TESTING) - add_library(load STATIC - load.c - ) - target_include_directories(load PUBLIC - ${CMAKE_CURRENT_LIST_DIR} - ) target_link_libraries(load MockRegEdit + MockADC ) - else() - add_library(load STATIC - load.c - ) - target_include_directories(load PUBLIC - ${CMAKE_CURRENT_LIST_DIR} - ) target_link_libraries(load - RegEdit + RegEdit + ADC ) endif() diff --git a/src/load/load.c b/src/load/load.c index 2872347..6343875 100644 --- a/src/load/load.c +++ b/src/load/load.c @@ -1,43 +1,59 @@ +/* + * Author: Jake G + * Date: 2024 + * filename: load.c + * description: module_purpose + */ + +#ifndef __AVR_ATtiny404__ +#define __AVR_ATtiny404__ +#endif + + #include #include "load.h" -#include "ADC.h" -#include "RegEdit.h" -#define G1 1 -#define G2 3 -#define G3 2 +#ifndef UNIT_TESTING + #include "ADC.h" + #include "RegEdit.h" +#else + #include "MockADC/MockADC.h" + #include "MockRegEdit.h" +#endif + -#define SAMPLE_QTY 32 void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin) { ADC_Init(adc_pin); - ADC_Enable(adc_pin); - uint16_t val = 0; - for(int i = 0; i < 32; i++){ - val += ADC_ReadValue(adc_pin); - } - val /= 32; + ADC_Enable(); + ADC_SetPin(adc_pin); + uint16_t val = ADC_ReadValue(adc_pin); + ADC_Disable(); - if(val < 527 || val > 1000){ + if(val > 527 && val < 1000){ + RegEdit_SetBit((void *) &PORTA.DIR, out_pin); + RegEdit_SetBit((void *) &PORTA.OUT, out_pin); + } + else{ RegEdit_ClearBit((void *) &PORTA.OUT, out_pin); - RegEdit_ClearBit((void *) &PORTA.DIR, out_pin); } } void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin) { ADC_Init(adc_pin); - ADC_Enable(adc_pin); - uint16_t val = 0; - for(int i = 0; i < 32; i++){ - val = ADC_ReadValue(adc_pin); - } + ADC_Enable(); + ADC_SetPin(adc_pin); + uint16_t val = ADC_ReadValue(adc_pin); ADC_Disable(); - if(val < 527 || val > 1000){ - RegEdit_ClearBit((void *) &PORTB.OUT, out_pin); - RegEdit_ClearBit((void *) &PORTB.DIR, out_pin); + if(val > 527 && val < 1000){ + RegEdit_SetBit((void *) &PORTB.DIR, out_pin); + RegEdit_SetBit((void *) &PORTB.OUT, out_pin); } -} \ No newline at end of file + else{ + RegEdit_ClearBit((void *) &PORTB.OUT, out_pin); + } +}