Commit 95d8a26f authored by Martin Vítek's avatar Martin Vítek
Browse files

Add backlight control over PWM

parent d8c164d6
#pragma once
#include <avr/io.h>
#include <stdint.h>
// (0 až 100) ^2 * 6.5535
const uint16_t log_scale[] = {
0, 7, 26, 59, 105, 164, 236, 321, 419, 531, 655, 793, 944,
1108, 1284, 1475, 1678, 1894, 2123, 2366, 2621, 2890, 3172, 3467, 3775, 4096, 4430,
4778, 5138, 5511, 5898, 6298, 6711, 7137, 7576, 8028, 8493, 8972, 9463, 9968, 10486,
11016, 11560, 12117, 12688, 13271, 13867, 14477, 15099, 15735, 16384, 17046, 17721,
18409, 19110, 19824, 20552, 21292, 22046, 22813, 23593, 24386, 25192, 26011, 26843,
27689, 28547, 29419, 30303, 31201, 32112, 33036, 33973, 34924, 35887, 36863, 37853,
38856, 39871, 40900, 41942, 42998, 44066, 45147, 46241, 47349, 48470, 49603, 50750,
51910, 53083, 54270, 55469, 56681, 57907, 59145, 60397, 61662, 62940, 64231, 65535
};
class Backlight
{
private:
uint8_t intensity;
public:
Backlight()
{
PORTD.DIRSET = PIN1_bm;
//Prescaller 2
TCD0.CTRLA = TC_CLKSEL_DIV2_gc;
//PWM on PIN1 and Sigle Slope PWM
TCD0.CTRLB = TC0_CCBEN_bm | TC_WGMODE_SS_gc;
}
void set_duty(uint16_t duty) { TCD0.CCB = duty; }
uint16_t get_duty() { return TCD0.CCB; }
void set_intensity(uint8_t intensity)
{
if (intensity > 100) intensity = 100;
TCD0.CCB = log_scale[intensity];
this->intensity = intensity;
}
uint8_t get_intensity() { return intensity; }
};
......@@ -45,6 +45,7 @@ set(SOURCE_FILES
SafeSolderingStation.h
Uart.cpp
Uart.h
Backlight.h
)
add_avr_executable(safe-soldering-station ${SOURCE_FILES})
......@@ -12,11 +12,11 @@ const HD44780_config lcd_config = {PORTD, PIN2_bm, PIN3_bm, PIN4_bm, PIN5_bm, PI
SafeSolderingStation sss;
SafeSolderingStation::SafeSolderingStation() : led(PORTD, PIN0_bm),
backlight(PORTD, PIN1_bm),
heating(PORTC, PIN0_bm),
enc(),
lcd(lcd_config),
uart()
uart(),
backlight()
{
}
......@@ -64,7 +64,6 @@ void SafeSolderingStation::init()
uart.init();
lcd.init();
irq_init();
backlight.on();
}
void SafeSolderingStation::wellcome()
......@@ -104,3 +103,18 @@ void SafeSolderingStation::test_encoder()
enc.event = false;
}
}
void SafeSolderingStation::test_backlight()
{
if (enc.event)
{
if (enc.count >= 3) intensity++;
if (enc.count <= -3) intensity--;
enc.count = 0;
backlight.set_intensity(intensity);
lcd.gotoxy_new(0,6);
lcd.write_num(backlight.get_intensity());
}
}
......@@ -6,6 +6,7 @@
#include "HD44780.h"
#include "encoder.h"
#include "Uart.h"
#include "Backlight.h"
class SafeSolderingStation
{
......@@ -15,14 +16,16 @@ class SafeSolderingStation
void misc_pin_init();
void irq_init();
uint8_t intensity;
public:
//Objects
output led;
output backlight;
output heating;
encoder enc;
HD44780 lcd;
Uart uart;
Backlight backlight;
//Methods
SafeSolderingStation();
......@@ -31,6 +34,7 @@ class SafeSolderingStation
void wellcome();
void test_encoder();
void test_backlight();
};
//Global instance
......
......@@ -11,9 +11,13 @@ int main()
sss.init();
sss.wellcome();
sss.lcd.gotoxy_new(0,0);
sss.lcd.write_text("Duty: %");
for(;;)
{
sss.test_encoder();
//sss.test_encoder();
sss.test_backlight();
_delay_ms(50);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment