Removed the use of the tested ADC interface

This commit is contained in:
Jake Goodwin 2025-02-23 11:53:14 -08:00
parent 78fdf54421
commit 91ee864d23
2 changed files with 31 additions and 24 deletions

View File

@ -3,8 +3,8 @@ add_executable(${PROJECT_NAME}
) )
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
RegEdit #RegEdit
ADC #ADC
#timer #timer
) )

View File

@ -3,7 +3,7 @@
#endif #endif
#include "main.h" #include "main.h"
// #include "ADC.h" //#include "ADC.h"
#include <avr/eeprom.h> #include <avr/eeprom.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <util/delay.h> #include <util/delay.h>
@ -33,7 +33,7 @@ volatile uint16_t tick_count;
// ############################# // #############################
static inline void InitTimer0(void); static inline void InitTimer0(void);
static void InitBtn(btn_state *b, uint8_t input_pin, uint8_t output_pin); static void InitBtn(btn_state *b, uint8_t input_pin);
static void ClearButtonTimer(btn_state *b); static void ClearButtonTimer(btn_state *b);
static void StartButtonTimer(btn_state *b); static void StartButtonTimer(btn_state *b);
static void CheckButtonLongpress(btn_state *b); static void CheckButtonLongpress(btn_state *b);
@ -50,7 +50,6 @@ int main() {
while (1) { while (1) {
UpdateButtonOutput(&btn1); UpdateButtonOutput(&btn1);
UpdateButtonInput(&btn1); UpdateButtonInput(&btn1);
// MotorMoveTo(0);
} }
return 0; return 0;
@ -64,7 +63,7 @@ void InitProg(void) {
/*Set the debounced state to all high*/ /*Set the debounced state to all high*/
debounced_state = 0xFF; debounced_state = 0xFF;
InitBtn(&btn1, BUTTON_PIN, PIN_ACTIVE1); InitBtn(&btn1, BUTTON_PIN);
/*Wait 5ms for pull-up resistors voltage to become stable.*/ /*Wait 5ms for pull-up resistors voltage to become stable.*/
_delay_ms(5); _delay_ms(5);
@ -75,11 +74,11 @@ void InitProg(void) {
// Checks against a bit pattern we defined to represent the start of data. // Checks against a bit pattern we defined to represent the start of data.
if (eeprom_read_byte((uint8_t *)ROM_SP_ADR) == START_PATTERN) { if (eeprom_read_byte((uint8_t *)ROM_SP_ADR) == START_PATTERN) {
MotorMoveTo(eeprom_read_byte((uint8_t *)ROM_SS1_ADR)); MotorMoveTo(eeprom_read_byte((uint8_t *)POSITION1_ADR));
} else { } else {
// otherwise we write the init values for the start pattern and states. // otherwise we write the init values for the start pattern and states.
eeprom_write_byte((uint8_t *)ROM_SP_ADR, START_PATTERN); eeprom_write_byte((uint8_t *)ROM_SP_ADR, START_PATTERN);
eeprom_write_byte((uint8_t *)ROM_SS1_ADR, 0x7F); eeprom_write_byte((uint8_t *)POSITION1_ADR, 0x7F);
} }
InitTimer0(); InitTimer0();
@ -95,10 +94,11 @@ uint8_t ReadSpeed(void) {
while (ADCSRA & (1 << ADSC)) { while (ADCSRA & (1 << ADSC)) {
} // Wait for conversion to finish } // Wait for conversion to finish
uint8_t val = (uint8_t)(ADC >> 2); //Normally a bitshift of 2 is done for 8bit ADC values,
//however we want to divide by two after this as well so we
// Map the speed value to the 100%-50% duty cycle range. //do a third shift followed by an addition of 127 to yield a mapping of
// val = (uint8_t)( (float)val / 255.0 ) * 100; //approximatly 50%-96.6% duty cycle range.
uint8_t val = (uint8_t)(ADC >> 3) + 127;
return val; return val;
} }
@ -128,14 +128,15 @@ static inline uint8_t diff(uint8_t a, uint8_t b) {
return b - a; return b - a;
} }
void MotorSetSavePos() { void MotorSetSavePos(uint8_t *ADR) {
uint8_t pos = (uint8_t)(ReadFader() >> 2); uint8_t pos = (uint8_t)(ReadFader() >> 2);
eeprom_write_byte((uint8_t *)ROM_SS1_ADR, pos); //eeprom_write_byte((uint8_t *)ADR, pos);
eeprom_update_byte((uint8_t *)ADR, pos);
return; return;
} }
uint8_t MotorGetSavedPos(void) { uint8_t MotorGetSavedPos(uint8_t *ADR) {
return (uint8_t)eeprom_read_byte((uint8_t *)ROM_SS1_ADR); return (uint8_t)eeprom_read_byte((uint8_t *)POSITION1_ADR);
} }
void MotorMoveTo(uint8_t target) { void MotorMoveTo(uint8_t target) {
@ -190,21 +191,17 @@ void MotorCoast(void) {
*/ */
/*This is kinda like our button constructor*/ /*This is kinda like our button constructor*/
static void InitBtn(btn_state *b, uint8_t input_pin, uint8_t output_pin) { static void InitBtn(btn_state *b, uint8_t input_pin) {
b->is_long_pressed = 0; b->is_long_pressed = 0;
b->is_pressed = 0; b->is_pressed = 0;
b->is_active = 0; b->is_active = 0;
b->pressed_ticks = 0; b->pressed_ticks = 0;
b->timer_enabled = 0; b->timer_enabled = 0;
b->input_pin = input_pin; b->input_pin = input_pin;
b->output_pin = output_pin;
/*Configure the buttons inputs and outputs*/ /*Configure the buttons inputs and outputs*/
DDRB &= ~(1 << b->input_pin); DDRB &= ~(1 << b->input_pin);
PORTB |= (1 << b->input_pin); PORTB |= (1 << b->input_pin);
DDRB |= (1 << b->output_pin);
PORTB &= ~(1 << b->output_pin);
} }
static void ClearButtonTimer(btn_state *b) { static void ClearButtonTimer(btn_state *b) {
@ -248,6 +245,7 @@ static void UpdateButtonOutput(btn_state *b) {
/*Then start the timer and update the output*/ /*Then start the timer and update the output*/
// ToggleOutput(b); // ToggleOutput(b);
StartButtonTimer(b); StartButtonTimer(b);
return; return;
} }
@ -267,12 +265,21 @@ static void UpdateButtonOutput(btn_state *b) {
else if (!b->is_pressed) { else if (!b->is_pressed) {
/*If the button was released on a long press.*/ /*If the button was released on a long press.*/
if (b->is_long_pressed) { if (b->is_long_pressed) {
MotorSetSavePos(); MotorSetSavePos((uint8_t *)POSITION1_ADR);
} }
/*If the button pres was a short one.*/ /*If the button pres was a short one.*/
else if (!b->is_long_pressed) { else if (!b->is_long_pressed) {
if (b->is_active) { if (b->is_active) {
MotorMoveTo(MotorGetSavedPos()); /*Save the current position into position 2 EEPROM*/
MotorSetSavePos((uint8_t *)POSITION2_ADR);
/*If already in saved position then go back to original pos.*/
if(diff(MotorGetSavedPos((uint8_t *)POSITION1_ADR), ReadFader()) > 8){
MotorMoveTo(MotorGetSavedPos((uint8_t *)POSITION1_ADR));
}
else{
MotorMoveTo(MotorGetSavedPos((uint8_t *)POSITION1_ADR));
}
} else { } else {
MotorCoast(); MotorCoast();
} }