Compare commits
5 commits
a88fae412e
...
b96945a2a5
Author | SHA1 | Date | |
---|---|---|---|
|
b96945a2a5 | ||
|
2d65048f8e | ||
|
bf39006dea | ||
|
71ff471fde | ||
|
724237147f |
4 changed files with 44 additions and 10 deletions
|
@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.20)
|
|||
|
||||
# Use the fancy version substitution
|
||||
project(main
|
||||
VERSION 0.1.2
|
||||
VERSION 0.2.0
|
||||
DESCRIPTION "template for cmake + cpputest"
|
||||
LANGUAGES C CXX
|
||||
)
|
||||
|
|
|
@ -32,6 +32,10 @@ The position will now be saved into the EEPROM memory.
|
|||
To automatically move the fader into the saved location preform a quick
|
||||
press of the button.
|
||||
|
||||
The speed can be adjusted by changing the input value of the speed pot. The
|
||||
speed adjustment affects the on/off ratio of the motor and has a programmed
|
||||
minimum speed value in the function to prevent endless looping.
|
||||
|
||||
|
||||
## Building Project(Windows)
|
||||
|
||||
|
|
11
inc/main.h
11
inc/main.h
|
@ -42,6 +42,7 @@
|
|||
#define PWM_PIN1 PB0 // Pin 5 - Motor PWM 1
|
||||
#define PWM_PIN2 PB1 // Pin 6 - Motor PWM 2
|
||||
#define ADC_PIN PB3 // Pin 2 - Fader position reading
|
||||
#define SPEED_PIN PB2 // Pin 7/ADC1
|
||||
#define BUTTON_PIN PB4 // Pin 3 - Button input
|
||||
|
||||
|
||||
|
@ -97,9 +98,15 @@ void InitProg(void);
|
|||
|
||||
|
||||
/**
|
||||
* @brief Reads the ADC pin
|
||||
* @brief Reads the ADC pin from the fader.
|
||||
*/
|
||||
uint16_t ReadADC(void);
|
||||
uint16_t ReadFader(void);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Reads the ADC value from the Speed pot.
|
||||
*/
|
||||
uint8_t ReadSpeed(void);
|
||||
|
||||
|
||||
/**
|
||||
|
|
37
src/main.c
37
src/main.c
|
@ -83,7 +83,28 @@ void InitProg(void) {
|
|||
InitTimer0();
|
||||
}
|
||||
|
||||
uint16_t ReadADC(void) {
|
||||
uint8_t ReadSpeed(void) {
|
||||
// Initialize ADC
|
||||
ADMUX = (0 << MUX1) | (1 << MUX0); // Select ADC1 (PB2)
|
||||
ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0); // Enable ADC, prescaler 8
|
||||
|
||||
ADCSRA |= (1 << ADSC); // Start conversion
|
||||
while (ADCSRA & (1 << ADSC)) {
|
||||
} // Wait for conversion to finish
|
||||
|
||||
uint8_t val = (uint8_t)(ADC >> 2);
|
||||
|
||||
//We want to set a minimum acceptable speed.
|
||||
if(val < 32){
|
||||
val = 32;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
//change to ReadFader(void)
|
||||
uint16_t ReadFader(void) {
|
||||
// Initialize ADC
|
||||
ADMUX = (1 << MUX1) | (1 << MUX0); // Select ADC3 (PB3)
|
||||
ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0); // Enable ADC, prescaler 8
|
||||
|
@ -108,7 +129,7 @@ static inline uint8_t diff(uint8_t a, uint8_t b) {
|
|||
}
|
||||
|
||||
void MotorSetSavePos() {
|
||||
uint8_t pos = (uint8_t)(ReadADC() >> 2);
|
||||
uint8_t pos = (uint8_t)(ReadFader() >> 2);
|
||||
eeprom_write_byte((uint8_t *)ROM_SS1_ADR, pos);
|
||||
return;
|
||||
}
|
||||
|
@ -119,10 +140,12 @@ uint8_t MotorGetSavedPos(void) {
|
|||
|
||||
void MotorMoveTo(uint8_t target) {
|
||||
|
||||
uint8_t pos = (uint8_t)(ReadADC() >> 2);
|
||||
uint8_t on_delay = ReadFader();
|
||||
uint8_t off_delay = 255 - on_delay;
|
||||
uint8_t pos = (uint8_t)(ReadFader() >> 2);
|
||||
|
||||
while (diff(target, pos) > 8) {
|
||||
pos = (uint8_t)(ReadADC() >> 2);
|
||||
pos = (uint8_t)(ReadFader() >> 2);
|
||||
if (target > pos) {
|
||||
MotorMove(1);
|
||||
} else {
|
||||
|
@ -130,9 +153,9 @@ void MotorMoveTo(uint8_t target) {
|
|||
}
|
||||
|
||||
// The delay ratio controlls the PWM waveforms.
|
||||
//_delay_ms(5);
|
||||
// MotorCoast();
|
||||
// _delay_ms(5);
|
||||
_delay_ms((double) on_delay);
|
||||
MotorCoast();
|
||||
_delay_ms((double) off_delay);
|
||||
}
|
||||
|
||||
return;
|
||||
|
|
Loading…
Add table
Reference in a new issue