Adjusted file to use F_CPU after DIV8 and allow speed control during movement.
This commit is contained in:
		
							parent
							
								
									c2ed2cead0
								
							
						
					
					
						commit
						49d8ca037b
					
				
					 1 changed files with 15 additions and 11 deletions
				
			
		
							
								
								
									
										22
									
								
								src/main.c
									
										
									
									
									
								
							
							
						
						
									
										22
									
								
								src/main.c
									
										
									
									
									
								
							| 
						 | 
					@ -1,8 +1,10 @@
 | 
				
			||||||
#ifndef F_CPU
 | 
					#ifndef F_CPU
 | 
				
			||||||
#define F_CPU 4800000UL
 | 
					#error "F_CPU not defined: defaulting to 9.6MHz"
 | 
				
			||||||
 | 
					#define F_CPU 1200000UL
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "main.h"
 | 
					#include "main.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>
 | 
				
			||||||
| 
						 | 
					@ -49,6 +51,7 @@ int main() {
 | 
				
			||||||
  while (1) {
 | 
					  while (1) {
 | 
				
			||||||
    UpdateButtonOutput(&btn1);
 | 
					    UpdateButtonOutput(&btn1);
 | 
				
			||||||
    UpdateButtonInput(&btn1);
 | 
					    UpdateButtonInput(&btn1);
 | 
				
			||||||
 | 
					    //MotorMoveTo(0);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return 0;
 | 
					  return 0;
 | 
				
			||||||
| 
						 | 
					@ -85,6 +88,7 @@ void InitProg(void) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint8_t ReadSpeed(void) {
 | 
					uint8_t ReadSpeed(void) {
 | 
				
			||||||
  // Initialize ADC
 | 
					  // Initialize ADC
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ADMUX = (0 << MUX1) | (1 << MUX0);                  // Select ADC1 (PB2)
 | 
					  ADMUX = (0 << MUX1) | (1 << MUX0);                  // Select ADC1 (PB2)
 | 
				
			||||||
  ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0); // Enable ADC, prescaler 8
 | 
					  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);
 | 
					  uint8_t val = (uint8_t)(ADC >> 2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // We want to set a minimum acceptable speed.
 | 
					    //Map the speed value to the 100%-50% duty cycle range.
 | 
				
			||||||
  if (val < 32) {
 | 
					    //val = (uint8_t)( (float)val / 255.0 ) * 100;
 | 
				
			||||||
    val = 32;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return val;
 | 
					  return val;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -139,11 +141,12 @@ uint8_t MotorGetSavedPos(void) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void MotorMoveTo(uint8_t target) {
 | 
					void MotorMoveTo(uint8_t target) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  uint8_t on_delay = ReadFader();
 | 
					  uint8_t on_delay = ReadSpeed();
 | 
				
			||||||
  uint8_t off_delay = 255 - on_delay;
 | 
					 | 
				
			||||||
  uint8_t pos = (uint8_t)(ReadFader() >> 2);
 | 
					  uint8_t pos = (uint8_t)(ReadFader() >> 2);
 | 
				
			||||||
 | 
					  uint8_t idx =0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  while (diff(target, pos) > 8) {
 | 
					  while (diff(target, pos) > 8) {
 | 
				
			||||||
 | 
					    on_delay = ReadSpeed();
 | 
				
			||||||
    pos = (uint8_t)(ReadFader() >> 2);
 | 
					    pos = (uint8_t)(ReadFader() >> 2);
 | 
				
			||||||
    if (target > pos) {
 | 
					    if (target > pos) {
 | 
				
			||||||
      MotorMove(1);
 | 
					      MotorMove(1);
 | 
				
			||||||
| 
						 | 
					@ -152,11 +155,11 @@ void MotorMoveTo(uint8_t target) {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // The delay ratio controlls the PWM waveforms.
 | 
					    // The delay ratio controlls the PWM waveforms.
 | 
				
			||||||
    for(uint8_t i = 0; i < on_delay; i++){
 | 
					    for (idx = 0; idx < on_delay; idx++) {
 | 
				
			||||||
      _delay_us(MOTOR_PULSE);
 | 
					      _delay_us(MOTOR_PULSE);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    MotorCoast();
 | 
					    MotorCoast();
 | 
				
			||||||
    for(uint8_t i = 0; i < off_delay; i++){
 | 
					    for (;idx < 255; idx++) {
 | 
				
			||||||
      _delay_us(MOTOR_PULSE);
 | 
					      _delay_us(MOTOR_PULSE);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -165,6 +168,7 @@ void MotorMoveTo(uint8_t target) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Using the compatable bool type.
 | 
					// Using the compatable bool type.
 | 
				
			||||||
 | 
					// The motor being used seems to stop working below a 50% duty cycle.
 | 
				
			||||||
void MotorMove(uint8_t fwd) {
 | 
					void MotorMove(uint8_t fwd) {
 | 
				
			||||||
  if (fwd) {
 | 
					  if (fwd) {
 | 
				
			||||||
    PORTB |= (1 << PWM_PIN1);
 | 
					    PORTB |= (1 << PWM_PIN1);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue