olikraus / u8g2

U8glib library for monochrome displays, version 2
Other
5.07k stars 1.05k forks source link

Issue setting up st7920 with atmega 328P #2389

Open awu30 opened 7 months ago

awu30 commented 7 months ago

Hi Oli,

Im working on a project that uses st7920 and atmega328p. Currently im trying to set up the st7920 lcd display with the example code in the u8g2 library and are trying to print something on to the screen. So far it compiles and flashes but there are no characters displayed. I not sure where the problem is, this situation is very similar to a post made about a year and half ago (https://github.com/olikraus/u8g2/issues/1931). Im also wiring my circuit on a breadboard, and i believe im experiencing a similar issue. Additionally, im using the library on a non-arduino / stand alone atmega platform. Again any help or clues is very much appreciated. Thank you!

include

include <avr/io.h>

include <util/delay.h>

include "u8x8_avr.h"

include <avr/power.h>

include "uart.h"

define CS_DDR DDRB

define CS_PORT PORTB

define CS_BIT 2

define DC_DDR DDRB

define DC_PORT PORTB

define DC_BIT 1

define RESET_DDR DDRB

define RESET_PORT PORTB

define RESET_BIT 0

u8g2_t u8g2;

uint8_t u8x8_gpio_and_delay (u8x8_t u8x8, uint8_t msg, uint8_t arg_int, void arg_ptr) { // Re-use library for delays if (u8x8_avr_delay(u8x8, msg, arg_int, arg_ptr)) return 1;

switch (msg) { // called once during init phase of u8g2/u8x8 // can be used to setup pins case U8X8_MSG_GPIO_AND_DELAY_INIT: CS_DDR |= _BV(CS_BIT); DC_DDR |= _BV(DC_BIT); RESET_DDR |= _BV(RESET_BIT); break; // CS (chip select) pin: Output level in arg_int case U8X8_MSG_GPIO_CS: if (arg_int) CS_PORT |= _BV(CS_BIT); else CS_PORT &= ~_BV(CS_BIT); break; // DC (data/cmd, A0, register select) pin: Output level in arg_int case U8X8_MSG_GPIO_DC: if (arg_int) DC_PORT |= _BV(DC_BIT); else DC_PORT &= ~_BV(DC_BIT); break; // Reset pin: Output level in arg_int case U8X8_MSG_GPIO_RESET: if (arg_int) RESET_PORT |= _BV(RESET_BIT); else RESET_PORT &= ~_BV(RESET_BIT); break; default: u8x8_SetGPIOResult(u8x8, 1); break; } return 1; }

int main (void) { init_usart();

u8g2_Setup_st7920_s_128x64_1(&u8g2, U8G2_R0, u8x8_byte_4wire_sw_spi, u8x8_gpio_and_delay);
u8g2_InitDisplay(&u8g2);
u8g2_SetPowerSave(&u8g2, 0);
processAndSendChar();
u8g2_ClearBuffer(&u8g2);
u8g2_SetFont(&u8g2, u8g2_font_ncenB14_tr);
u8g2_DrawStr(&u8g2, 1, 15, "Hello!");
u8g2_SendBuffer(&u8g2);

while (1) {

}

}

Makefile:

CC = avr-gcc PROJECT_NAME = main OBJCPY = avr-objcopy SIZE = avr-size MCU = atmega328p F_CPU = 8000000 U8G2_SRC = /Users/arthurwu/u8g2/csrc CFLAGS = \ -mmcu=$(MCU) \ -DF_CPU=$(F_CPU)UL \ -Os \ -std=gnu99 \ -Werror \ -ffunction-sections \ -fdata-sections \ -I$(U8G2_SRC)/ \ -I/Users/arthurwu/u8g2/sys/avr/avr-libc/lib \ -I/Users/arthurwu/engg2800-2024s1-g16/embedded/01_SRC \ -DAVR_USE_HW_SPI \ -DSCK_DDR=DDRB \ -DSCK_BIT=5 \ -DMOSI_DDR=DDRB \ -DMOSI_BIT=3 LDFLAGS = \ -Wl,--gc-sections \ -mmcu=$(MCU) AVRDUDE=avrdude

SRC = $(wildcard $(U8G2_SRC)/.c) $(wildcard /Users/arthurwu/u8g2/sys/avr/avr-libc/lib/.c) $(wildcard ./*.c) /Users/arthurwu/engg2800-2024s1-g16/embedded/01_SRC/uart.c

OBJ = $(SRC:.c=.o)

main.hex: main.elf $(OBJCPY) -O ihex -R .eeprom -R .fuse -R .lock -R .signature main.elf main.hex

main.elf: $(OBJ) $(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $@

size: main.elf $(SIZE) --mcu=$(MCU) --format=avr main.elf

clean: rm -f $(OBJ) main.elf main.hex

flash: main.hex $(AVRDUDE) -p $(MCU) -c avrisp2 -Uflash:w:main.hex:i

.PHONY: size

size: $(PROJECT_NAME).elf

avr-size --format=avr --mcu=$(MCU) $(PROJECT_NAME).elf

image I used this schematic

olikraus commented 6 months ago

Wiring of V0 is missing...

awu30 commented 6 months ago

Wiring of V0 is missing...

Hi Oli, Thanks for your reply. I was originally using the example code under atmega328p inside the library. But when I tested it with a logic analyser there was no spi signal so i copied the init function and macro from the as7 folder and now it works. Just want to point it out, it would be great for other beginners like me if you could update the example. Thanks!!

eagl1 commented 6 months ago

Wiring of V0 is missing...

No need to wire it. He have to adjust the small pot on the back of the LCD until the chars are displayed.