Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aae5b19f83 |
@@ -0,0 +1,20 @@
|
||||
#ifndef ANALOG_OUT_H
|
||||
#define ANALOG_OUT_H
|
||||
|
||||
#include <timer_msec.h>
|
||||
#include <digital_out.h>
|
||||
|
||||
class Analog_out
|
||||
{
|
||||
public:
|
||||
Analog_out(int pin);
|
||||
void init(int period_ms = 1000);
|
||||
void set(int duty_cycle);
|
||||
|
||||
private:
|
||||
Timer_msec timer;
|
||||
public:
|
||||
Digital_out pin;
|
||||
};
|
||||
|
||||
#endif // ANALOG_OUT_H
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef MOTOR_DRIVER_H
|
||||
#define MOTOR_DRIVER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// Vélbúnaðar-PWM á Timer1: AIN1 = OC1A = PB1 (D9), AIN2 = OC1B = PB2 (D10)
|
||||
class Motor_driver {
|
||||
public:
|
||||
Motor_driver();
|
||||
void init(uint16_t top = 799); //
|
||||
void set_duty_cycleA(int percent); // 0-100%, AIN1
|
||||
void set_duty_cycleB(int percent); // 0-100%, AIN2
|
||||
|
||||
private:
|
||||
uint16_t top_value;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
class Timer_msec
|
||||
{
|
||||
public:
|
||||
Timer_msec();
|
||||
void init(int period_ms);
|
||||
void init(int period_ms, int duty_cycle);
|
||||
void set_duty_cycle(int duty_cycle);
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <analog_out.h>
|
||||
|
||||
Analog_out::Analog_out(int pin_number) : pin(pin_number)
|
||||
{
|
||||
}
|
||||
|
||||
void Analog_out::init(int period_ms)
|
||||
{
|
||||
pin.init();
|
||||
timer.init(period_ms, 0); // Here we can choose the default duty cycle
|
||||
}
|
||||
|
||||
void Analog_out::set(int duty_cycle)
|
||||
{
|
||||
timer.set_duty_cycle(duty_cycle);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "motor_driver.h"
|
||||
#include <avr/io.h>
|
||||
|
||||
Motor_driver::Motor_driver() : top_value(0) {}
|
||||
|
||||
void Motor_driver::init(uint16_t top) {
|
||||
top_value = top;
|
||||
|
||||
DDRB |= (1 << PB1) | (1 << PB2); // AIN1, AIN2
|
||||
|
||||
TCCR1A = 0;
|
||||
TCCR1B = 0;
|
||||
|
||||
// Fast PWM, TOP = ICR1 (WGM13:10 = 1110)
|
||||
TCCR1A |= (1 << WGM11);
|
||||
TCCR1B |= (1 << WGM13) | (1 << WGM12);
|
||||
|
||||
// Non-inverting PWM (OC1A og OC1B)
|
||||
TCCR1A |= (1 << COM1A1) | (1 << COM1B1);
|
||||
|
||||
ICR1 = top_value;
|
||||
OCR1A = 0;
|
||||
OCR1B = 0;
|
||||
|
||||
TCCR1B |= (1 << CS10); // prescaler 1
|
||||
}
|
||||
|
||||
void Motor_driver::set_duty_cycleA(int percent) {
|
||||
OCR1A = (uint16_t)((uint32_t)top_value * percent / 100);
|
||||
}
|
||||
|
||||
void Motor_driver::set_duty_cycleB(int percent) {
|
||||
OCR1B = (uint16_t)((uint32_t)top_value * percent / 100);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <timer_msec.h>
|
||||
#include <avr/io.h>
|
||||
#include <stdint.h>
|
||||
|
||||
Timer_msec::Timer_msec()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Timer_msec::init(int period_ms)
|
||||
{
|
||||
init(period_ms, 50); // calls the next function with 50% duty cycle
|
||||
}
|
||||
|
||||
void Timer_msec::init(int period_ms, int duty_cycle)
|
||||
{
|
||||
// this code sets up timer1 for a 1s @ 16Mhz Clock (mode 4)
|
||||
// counting 16000000/1024 cycles of a clock prescaled by 1024
|
||||
TCCR1A = 0; // set timer1 to normal operation (all bits in control registers A and B set to zero)
|
||||
TCCR1B = 0; //
|
||||
TCNT1 = 0; // initialize counter value to 0
|
||||
OCR1A = (uint32_t {16000} * period_ms / 1024) - 1; // assign target count to compare register A (must be less than 65536)
|
||||
OCR1B = OCR1A * (duty_cycle / 100.0); // assign target count to compare register B (should not exceed value of OCR1A)
|
||||
TCCR1B |= (1 << WGM12); // clear the timer on compare match A
|
||||
TIMSK1 |= (1 << OCIE1A); // set interrupt on compare match A
|
||||
TIMSK1 |= (1 << OCIE1B); // set interrupt on compare match B
|
||||
TCCR1B |= (1 << CS12) | (1 << CS10); // set prescaler to 1024 and start the timer
|
||||
}
|
||||
|
||||
void Timer_msec::set_duty_cycle(int duty_cycle)
|
||||
{
|
||||
OCR1B = OCR1A * (duty_cycle / 100.0);
|
||||
}
|
||||
Reference in New Issue
Block a user