generated from TDD-Templates/cmake_cpputest_template_avr
Adjusted file to use F_CPU after DIV8 and allow speed control during movement.
This commit is contained in:
parent
c2ed2cead0
commit
49d8ca037b
26
src/main.c
26
src/main.c
|
@ -1,8 +1,10 @@
|
|||
#ifndef F_CPU
|
||||
#define F_CPU 4800000UL
|
||||
#error "F_CPU not defined: defaulting to 9.6MHz"
|
||||
#define F_CPU 1200000UL
|
||||
#endif
|
||||
|
||||
#include "main.h"
|
||||
//#include "ADC.h"
|
||||
#include <avr/eeprom.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <util/delay.h>
|
||||
|
@ -49,6 +51,7 @@ int main() {
|
|||
while (1) {
|
||||
UpdateButtonOutput(&btn1);
|
||||
UpdateButtonInput(&btn1);
|
||||
//MotorMoveTo(0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -85,6 +88,7 @@ void InitProg(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
|
||||
|
||||
|
@ -94,10 +98,8 @@ uint8_t ReadSpeed(void) {
|
|||
|
||||
uint8_t val = (uint8_t)(ADC >> 2);
|
||||
|
||||
// We want to set a minimum acceptable speed.
|
||||
if (val < 32) {
|
||||
val = 32;
|
||||
}
|
||||
//Map the speed value to the 100%-50% duty cycle range.
|
||||
//val = (uint8_t)( (float)val / 255.0 ) * 100;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
@ -139,11 +141,12 @@ uint8_t MotorGetSavedPos(void) {
|
|||
|
||||
void MotorMoveTo(uint8_t target) {
|
||||
|
||||
uint8_t on_delay = ReadFader();
|
||||
uint8_t off_delay = 255 - on_delay;
|
||||
uint8_t on_delay = ReadSpeed();
|
||||
uint8_t pos = (uint8_t)(ReadFader() >> 2);
|
||||
uint8_t idx =0;
|
||||
|
||||
while (diff(target, pos) > 8) {
|
||||
on_delay = ReadSpeed();
|
||||
pos = (uint8_t)(ReadFader() >> 2);
|
||||
if (target > pos) {
|
||||
MotorMove(1);
|
||||
|
@ -152,12 +155,12 @@ void MotorMoveTo(uint8_t target) {
|
|||
}
|
||||
|
||||
// The delay ratio controlls the PWM waveforms.
|
||||
for(uint8_t i = 0; i < on_delay; i++){
|
||||
_delay_us(MOTOR_PULSE);
|
||||
for (idx = 0; idx < on_delay; idx++) {
|
||||
_delay_us(MOTOR_PULSE);
|
||||
}
|
||||
MotorCoast();
|
||||
for(uint8_t i = 0; i < off_delay; i++){
|
||||
_delay_us(MOTOR_PULSE);
|
||||
for (;idx < 255; idx++) {
|
||||
_delay_us(MOTOR_PULSE);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -165,6 +168,7 @@ void MotorMoveTo(uint8_t target) {
|
|||
}
|
||||
|
||||
// Using the compatable bool type.
|
||||
// The motor being used seems to stop working below a 50% duty cycle.
|
||||
void MotorMove(uint8_t fwd) {
|
||||
if (fwd) {
|
||||
PORTB |= (1 << PWM_PIN1);
|
||||
|
|
Loading…
Reference in New Issue