From aae5b19f83144486a9042d44588738ab3dcad179 Mon Sep 17 00:00:00 2001 From: helgie22 Date: Fri, 18 Sep 2026 12:25:51 +0000 Subject: [PATCH] commiting code that didnt work properly on new branch --- include/analog_out.h | 20 ++++++++++++++++++++ include/motor_driver.h | 18 ++++++++++++++++++ include/timer_msec.h | 10 ++++++++++ src/analog_out.cpp | 16 ++++++++++++++++ src/motor_driver.cpp | 34 ++++++++++++++++++++++++++++++++++ src/timer_msec.cpp | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 131 insertions(+) create mode 100644 include/analog_out.h create mode 100644 include/motor_driver.h create mode 100644 include/timer_msec.h create mode 100644 src/analog_out.cpp create mode 100644 src/motor_driver.cpp create mode 100644 src/timer_msec.cpp diff --git a/include/analog_out.h b/include/analog_out.h new file mode 100644 index 0000000..972be62 --- /dev/null +++ b/include/analog_out.h @@ -0,0 +1,20 @@ +#ifndef ANALOG_OUT_H +#define ANALOG_OUT_H + +#include +#include + +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 \ No newline at end of file diff --git a/include/motor_driver.h b/include/motor_driver.h new file mode 100644 index 0000000..9bc1c4a --- /dev/null +++ b/include/motor_driver.h @@ -0,0 +1,18 @@ +#ifndef MOTOR_DRIVER_H +#define MOTOR_DRIVER_H + +#include + +// 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 \ No newline at end of file diff --git a/include/timer_msec.h b/include/timer_msec.h new file mode 100644 index 0000000..1ec1dad --- /dev/null +++ b/include/timer_msec.h @@ -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); +}; \ No newline at end of file diff --git a/src/analog_out.cpp b/src/analog_out.cpp new file mode 100644 index 0000000..a643be3 --- /dev/null +++ b/src/analog_out.cpp @@ -0,0 +1,16 @@ +#include + +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); +} \ No newline at end of file diff --git a/src/motor_driver.cpp b/src/motor_driver.cpp new file mode 100644 index 0000000..96859e3 --- /dev/null +++ b/src/motor_driver.cpp @@ -0,0 +1,34 @@ +#include "motor_driver.h" +#include + +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); +} \ No newline at end of file diff --git a/src/timer_msec.cpp b/src/timer_msec.cpp new file mode 100644 index 0000000..7eb7227 --- /dev/null +++ b/src/timer_msec.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +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); +} \ No newline at end of file