From 0065fb630fada99649bb167fc454872a699ae4e3 Mon Sep 17 00:00:00 2001 From: finga Date: Thu, 18 Feb 2021 22:00:24 +0100 Subject: [PATCH] Generate a PWM signal for the display Generate a variable duty cycle PWM signal for the dimmable display backlight at a frequency of 1.25kHz. For demo und testing purposes PD5 is currently fading between 0 and 100%. --- .gitignore | 3 +++ firmware/src/Makefile | 41 +++++++++++++++++++++++++++++++++++++++++ firmware/src/main.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 .gitignore create mode 100644 firmware/src/Makefile create mode 100644 firmware/src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a39790f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +*.elf +*.hex diff --git a/firmware/src/Makefile b/firmware/src/Makefile new file mode 100644 index 0000000..e546649 --- /dev/null +++ b/firmware/src/Makefile @@ -0,0 +1,41 @@ +.SUFFIXES: + +MCU := atmega328p +PROGRAMMER := usbtiny + +TARGET := main.hex +BIN := main.elf +OBJ := main.o + +SHELL := sh +CC := avr-gcc +OBJCOPY := avr-objcopy +SIZE := avr-size +AVRDUDE := avrdude + +CFLAGS := -mmcu=$(MCU) -Os -Wall -Werror -Wextra -Wpedantic + +all: $(TARGET) + +$(TARGET): $(BIN) + ${OBJCOPY} -O ihex -j .text -j .data $< $@ + +$(BIN): $(OBJ) + $(CC) $(CFLAGS) $< -o $@ + +%.o: %.c + $(CC) $(CFLAGS) -Os -c $< -o $@ + +.PHONY: flash clean check size + +flash: $(TARGET) size + $(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -U flash:w:$<:a + +clean: + $(RM) main.o main.elf main.hex + +check: + cppcheck main.c + +size: $(BIN) + $(SIZE) --format=avr --mcu=$(MCU) $< diff --git a/firmware/src/main.c b/firmware/src/main.c new file mode 100644 index 0000000..fc9b488 --- /dev/null +++ b/firmware/src/main.c @@ -0,0 +1,31 @@ +#define F_CPU 8000000UL + +#include +#include +#include + +int main(void) { + // FastPWM: 1.25kHz + TCCR0A = (1 << WGM01) | (1 << WGM00) | (1 << COM0B1); + TCCR0B = (1 << CS01) | (1 << WGM02); // prescaler = 8; + OCR0A = 100; + OCR0B = 10; + DDRD |= (1 << PD5); + + bool reverse = 0; + + while (1) { + _delay_ms(1); + + if (OCR0B > 100) + reverse = !reverse; + + if (reverse) + OCR0B++; + else + if (OCR0B > 0) + OCR0B--; + else + reverse = !reverse; + } +}