Patched from High Repo

This commit is contained in:
jakeg00dwin 2024-07-28 17:29:37 -07:00
parent f259aed4e3
commit d1b6c828cb
5 changed files with 113 additions and 83 deletions

View File

@ -13,57 +13,62 @@
#include "RegEdit.h" #include "RegEdit.h"
#include "avr/io.h" #include "avr/io.h"
static uint8_t porta_out; #define MAX_PIN_NUM 7
static uint8_t porta_dir;
static bool IsInvalidPin(uint8_t pin_num){ static bool IsInvalidPin(uint8_t pin_num){
if(pin_num > 7){ if(pin_num > MAX_PIN_NUM){
return true; return true;
} }
return false; 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) void ADC_Init(uint8_t pin_num)
{ {
if(IsInvalidPin(pin_num)){return;} 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 //set the direction to input
RegEdit_SetBit((void *) &PORTA.DIRCLR, pin_num); RegEdit_ClearBit((void *) &PORTA.DIR, pin_num);
//Disable the pull-up resistor //Disable the pull-up resistor
RegEdit_SetBit((void *) &PORTA.OUTCLR, pin_num); RegEdit_ClearBit((void *) &PORTA.OUT, pin_num);
//Disable input buffer //Disable input buffer
//We do some kinda nasty address addition but it saves //We do some kinda nasty address addition but it saves
//memory and means we don't need a switch statment. //memory and means we don't need a switch statment.
//PORT_ISC_INPUT_DISABLE_gc
RegEdit_SetBit( RegEdit_SetBit(
(void *) (&PORTA.PIN0CTRL)+pin_num, (void *) (&PORTA.PIN0CTRL)+pin_num,
PORT_ISC_INPUT_DISABLE_gc 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 //Set the enable bit in the CTRLA register
RegEdit_SetBit((void *) &ADC0.CTRLA, 0); RegEdit_SetBit((void *) &ADC0.CTRLA, 0);
} }
@ -71,15 +76,16 @@ void ADC_Enable(uint8_t pin_num)
void ADC_Disable() void ADC_Disable()
{ {
//Set for no ADC connections.
RegEdit_ClearRegister((void *) &ADC0.MUXPOS);
//Clear the enable ADC flag //Clear the enable ADC flag
RegEdit_ClearBit((void *) &ADC0.CTRLA, 0); RegEdit_ClearBit((void *) &ADC0.CTRLA, 0);
}
//Restore the port registers
RegEdit_SetNum((void *) &PORTA.OUT, porta_out); void ADC_SetPin(uint8_t pin_num)
RegEdit_SetNum((void *) &PORTA.DIR, porta_dir); {
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); RegEdit_SetNum((void *) &ADC0.COMMAND, ADC_STCONV_bm);
/* Wait until ADC conversion done */ /* 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) ) while ( !(ADC0.INTFLAGS & ADC_RESRDY_bm) )
{ {
; ;
@ -98,7 +102,9 @@ uint16_t ADC_ReadValue_Impl(uint8_t pin_num)
/* Clear the interrupt flag by writing 1: */ /* Clear the interrupt flag by writing 1: */
ADC0.INTFLAGS = ADC_RESRDY_bm; 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;
} }

View File

@ -32,9 +32,8 @@ void ADC_Init(uint8_t pin_num);
/** /**
* @brief Enables the ADC * @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); 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 #endif //ADC_H

View File

@ -1,21 +1,16 @@
add_library(ADC STATIC
ADC.c
)
target_include_directories(ADC PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
if(UNIT_TESTING) if(UNIT_TESTING)
add_library(ADC STATIC
ADC.c
)
target_include_directories(ADC PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(ADC target_link_libraries(ADC
MockRegEdit MockRegEdit
) )
else() else()
add_library(ADC STATIC
ADC.c
)
target_include_directories(ADC PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(ADC target_link_libraries(ADC
RegEdit RegEdit
) )

View File

@ -1,22 +1,18 @@
add_library(load STATIC
load.c
)
target_include_directories(load PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
if(UNIT_TESTING) if(UNIT_TESTING)
add_library(load STATIC
load.c
)
target_include_directories(load PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(load target_link_libraries(load
MockRegEdit MockRegEdit
MockADC
) )
else() else()
add_library(load STATIC
load.c
)
target_include_directories(load PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(load target_link_libraries(load
RegEdit RegEdit
ADC
) )
endif() endif()

View File

@ -1,43 +1,59 @@
/*
* Author: Jake G
* Date: 2024
* filename: load.c
* description: module_purpose
*/
#ifndef __AVR_ATtiny404__
#define __AVR_ATtiny404__
#endif
#include <avr/io.h> #include <avr/io.h>
#include "load.h" #include "load.h"
#include "ADC.h"
#include "RegEdit.h"
#define G1 1 #ifndef UNIT_TESTING
#define G2 3 #include "ADC.h"
#define G3 2 #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) void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin)
{ {
ADC_Init(adc_pin); ADC_Init(adc_pin);
ADC_Enable(adc_pin); ADC_Enable();
uint16_t val = 0; ADC_SetPin(adc_pin);
for(int i = 0; i < 32; i++){ uint16_t val = ADC_ReadValue(adc_pin);
val += ADC_ReadValue(adc_pin);
}
val /= 32;
ADC_Disable(); 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.OUT, out_pin);
RegEdit_ClearBit((void *) &PORTA.DIR, out_pin);
} }
} }
void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin) void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin)
{ {
ADC_Init(adc_pin); ADC_Init(adc_pin);
ADC_Enable(adc_pin); ADC_Enable();
uint16_t val = 0; ADC_SetPin(adc_pin);
for(int i = 0; i < 32; i++){ uint16_t val = ADC_ReadValue(adc_pin);
val = ADC_ReadValue(adc_pin);
}
ADC_Disable(); ADC_Disable();
if(val < 527 || val > 1000){ if(val > 527 && val < 1000){
RegEdit_SetBit((void *) &PORTB.DIR, out_pin);
RegEdit_SetBit((void *) &PORTB.OUT, out_pin);
}
else{
RegEdit_ClearBit((void *) &PORTB.OUT, out_pin); RegEdit_ClearBit((void *) &PORTB.OUT, out_pin);
RegEdit_ClearBit((void *) &PORTB.DIR, out_pin);
} }
} }