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%.
This commit is contained in:
finga 2021-02-18 22:00:24 +01:00
commit 0065fb630f
3 changed files with 75 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
*.o
*.elf
*.hex

41
firmware/src/Makefile Normal file
View file

@ -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) $<

31
firmware/src/main.c Normal file
View file

@ -0,0 +1,31 @@
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdbool.h>
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;
}
}