Fixed set_addr_win

Fixed set_orientation
Fixed minor bugs and typos
This commit is contained in:
Sebastian 2016-12-06 19:51:07 +01:00
parent d8180ba10a
commit 0d81f98ab9
7 changed files with 115 additions and 24 deletions

60
Makefile Normal file
View File

@ -0,0 +1,60 @@
AVRMCU ?= atmega8
F_CPU ?= 16000000
ISPPORT ?= /dev/kaboard
VERSION = 0.1
HEADERS = spi.h st7735.h st7735initcmds.h
SRC = main.c spi.c st7735.c
TARGET = st7735_test
OBJDIR = bin
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
SRC_TMP = $(subst ../,,$(SRC))
OBJ = $(SRC_TMP:%.c=$(OBJDIR)/$(AVRMCU)/%.o)
CFLAGS = -I ../sss7core/ -Os -Wall -Wstrict-prototypes
CFLAGS += -ffunction-sections -fdata-sections
CFLAGS += -fshort-enums -fpack-struct -funsigned-char -funsigned-bitfields
CFLAGS += -mmcu=$(AVRMCU) -DF_CPU=$(F_CPU)UL -DVERSION=$(VERSION)
LDFLAGS = -mmcu=$(AVRMCU) -Wl,--gc-sections
all: start $(OBJDIR)/$(AVRMCU)/$(TARGET).hex size
@echo ":: Done !"
start:
@echo "ST7735 for AVR $(VERSION)"
@echo "=========================="
@echo ":: Building for $(AVRMCU)"
@echo ":: MCU operating frequency is $(F_CPU)Hz"
$(OBJDIR)/$(AVRMCU)/%.o : %.c $(HEADERS) Makefile
@mkdir -p $$(dirname $@)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/$(AVRMCU)/$(TARGET).elf : $(OBJ)
$(CC) $(LDFLAGS) $+ -o $@
$(OBJDIR)/$(AVRMCU)/$(TARGET).hex : $(OBJDIR)/$(AVRMCU)/$(TARGET).elf
$(OBJCOPY) -O ihex $< $@
size : $(OBJDIR)/$(AVRMCU)/$(TARGET).elf
@echo
@$(SIZE) --mcu=$(AVRMCU) -C $(OBJDIR)/$(AVRMCU)/$(TARGET).elf
@echo
clean :
@rm -rf $(OBJDIR)
flash : all
avrdude -c arduino \
-p $(AVRMCU) -P $(ISPPORT) \
-U flash:w:$(OBJDIR)/$(AVRMCU)/$(TARGET).hex
test : flash
screen $(ISPPORT) 38400

21
main.c Normal file
View File

@ -0,0 +1,21 @@
#include<avr/io.h>
#include<util/delay.h>
#include "spi.h"
#include "st7735.h"
void main() {
spi_init();
st7735_init();
st7735_set_orientation(ST7735_LANDSCAPE);
st7735_fill_rect(0, 0, 160, 128, ST7735_COLOR_GREEN);
st7735_fill_rect(0, 0, 80, 127, ST7735_COLOR_RED);
st7735_fill_rect(0, 0, 80, 64, ST7735_COLOR_CYAN);
st7735_draw_fast_hline(80, 64, 40, ST7735_COLOR_BLUE);
st7735_draw_fast_vline(120, 64, 32, ST7735_COLOR_BLUE);
}

4
spi.c
View File

@ -1,9 +1,9 @@
#include "spi.h" #include "spi.h"
void SPI_init(void) { void spi_init(void) {
// Set MOSI and SCK, SS/CS output, all others input // Set MOSI and SCK, SS/CS output, all others input
DDRB = (1<<PB3) | (1<<PB5) | (1<<PB2); DDRB = (1<<PB3) | (1<<PB5) | (1<<PB2);
// Enable SPI, Master, set clock rate fck/4, mode 0, // Enable SPI, Master, set clock rate fck/4, mode 0
SPCR = (1<<SPE) | (1<<MSTR); SPCR = (1<<SPE) | (1<<MSTR);
// Set SS/CS // Set SS/CS

2
spi.h
View File

@ -3,7 +3,7 @@
#include<avr/io.h> #include<avr/io.h>
void SPI_init(void); void spi_init(void);
static inline void spi_write(uint8_t byte) { static inline void spi_write(uint8_t byte) {
SPDR = byte; SPDR = byte;

View File

@ -6,26 +6,26 @@
#include "spi.h" #include "spi.h"
#include "st7735initcmds.h" #include "st7735initcmds.h"
static uint8_t st7735_row_start = 0; uint8_t st7735_row_start = 0;
static uint8_t st7735_column_start = 0; uint8_t st7735_column_start = 0;
static uint8_t st7735_width = st7735_default_width; uint8_t st7735_width = 0;
static uint8_t st7735_height = st7735_default_height_18; uint8_t st7735_height = 0;
static enum ST7735_ORIENTATION st7735_orientation = ST7735_LANDSCAPE; enum ST7735_ORIENTATION st7735_orientation = ST7735_LANDSCAPE;
static inline void st7735_set_rs() { static inline void st7735_set_rs() {
PORTB |= (1 << PB1); PORTD |= (1 << PD6);
} }
static inline void st7735_unset_rs() { static inline void st7735_unset_rs() {
PORTB &= ~(1 << PB1); PORTD &= ~(1 << PD6);
} }
static inline void st7735_set_rst() { static inline void st7735_set_rst() {
PORTB |= (1 << PB0); PORTD |= (1 << PD7);
} }
static inline void st7735_unset_rst() { static inline void st7735_unset_rst() {
PORTB &= ~(1 << PB0); PORTD &= ~(1 << PD7);
} }
static inline void st7735_write_cmd(enum ST7735_COMMANDS cmd) { static inline void st7735_write_cmd(enum ST7735_COMMANDS cmd) {
@ -81,7 +81,9 @@ void st7735_run_command_list(const uint8_t *addr) {
_delay_ms(500); _delay_ms(500);
} }
else { else {
_delay_ms(ms); for(int i = 0; i < ms; i += 5) {
_delay_ms(5);
}
} }
} }
@ -91,7 +93,7 @@ void st7735_run_command_list(const uint8_t *addr) {
void st7735_init() { void st7735_init() {
// Set rs and rst output // Set rs and rst output
DDRB |= (1 << PB1) | (1 << PB0); DDRD |= (1 << PD6) | (1 << PD7);
st7735_reset(); st7735_reset();
@ -106,12 +108,16 @@ void st7735_init() {
st7735_run_command_list(st7735_red_init3); st7735_run_command_list(st7735_red_init3);
st7735_column_start = 2; st7735_column_start = 2;
st7735_row_start = 1; st7735_row_start = 1;
st7735_width = st7735_default_width;
st7735_height = st7735_default_height_18;
break; break;
case ST7735_RED_18_REDTAB: case ST7735_RED_18_REDTAB:
st7735_run_command_list(st7735_red_init1); st7735_run_command_list(st7735_red_init1);
st7735_run_command_list(st7735_red_init_red2); st7735_run_command_list(st7735_red_init_red2);
st7735_run_command_list(st7735_red_init3); st7735_run_command_list(st7735_red_init3);
st7735_width = st7735_default_width;
st7735_height = st7735_default_height_18;
break; break;
case ST7735_RED_18_BLACKTAB: case ST7735_RED_18_BLACKTAB:
@ -121,6 +127,8 @@ void st7735_init() {
// Change MADCTL color filter for black // Change MADCTL color filter for black
st7735_write_cmd(ST7735_MADCTL); st7735_write_cmd(ST7735_MADCTL);
st7735_write_data(0xC0); st7735_write_data(0xC0);
st7735_width = st7735_default_width;
st7735_height = st7735_default_height_18;
break; break;
case ST7735_RED144_GREENTAB: case ST7735_RED144_GREENTAB:
@ -130,6 +138,8 @@ void st7735_init() {
st7735_height = st7735_default_height_144; st7735_height = st7735_default_height_144;
st7735_column_start = 2; st7735_column_start = 2;
st7735_row_start = 3; st7735_row_start = 3;
st7735_width = st7735_default_width;
st7735_height = st7735_default_height_144;
break; break;
} }
} }
@ -149,7 +159,7 @@ void st7735_set_orientation(enum ST7735_ORIENTATION orientation) {
st7735_write_cmd(ST7735_MADCTL); st7735_write_cmd(ST7735_MADCTL);
switch (orientation) { switch (orientation) {
case ST7735_LANDSCAPE: case ST7735_PORTRAIT:
if(st7735_type == ST7735_RED_18_BLACKTAB) { if(st7735_type == ST7735_RED_18_BLACKTAB) {
st7735_write_data(MADCTL_MX | MADCTL_MY | MADCTL_RGB); st7735_write_data(MADCTL_MX | MADCTL_MY | MADCTL_RGB);
} else { } else {
@ -166,7 +176,7 @@ void st7735_set_orientation(enum ST7735_ORIENTATION orientation) {
break; break;
case ST7735_PORTRAIT: case ST7735_LANDSCAPE:
if(st7735_type == ST7735_RED_18_BLACKTAB) { if(st7735_type == ST7735_RED_18_BLACKTAB) {
st7735_write_data(MADCTL_MY | MADCTL_MV | MADCTL_RGB); st7735_write_data(MADCTL_MY | MADCTL_MV | MADCTL_RGB);
} else { } else {
@ -183,7 +193,7 @@ void st7735_set_orientation(enum ST7735_ORIENTATION orientation) {
st7735_height = st7735_default_width; st7735_height = st7735_default_width;
break; break;
case ST7735_LANDSCAPE_INV: case ST7735_PORTRAIT_INV:
if (st7735_type == ST7735_RED_18_BLACKTAB) { if (st7735_type == ST7735_RED_18_BLACKTAB) {
st7735_write_data(MADCTL_RGB); st7735_write_data(MADCTL_RGB);
} else { } else {
@ -199,7 +209,7 @@ void st7735_set_orientation(enum ST7735_ORIENTATION orientation) {
} }
break; break;
case ST7735_PORTRAIT_INV: case ST7735_LANDSCAPE_INV:
if (st7735_type == ST7735_RED_18_BLACKTAB) { if (st7735_type == ST7735_RED_18_BLACKTAB) {
st7735_write_data(MADCTL_MX | MADCTL_MV | MADCTL_RGB); st7735_write_data(MADCTL_MX | MADCTL_MV | MADCTL_RGB);
} else { } else {
@ -220,7 +230,7 @@ void st7735_set_orientation(enum ST7735_ORIENTATION orientation) {
void st7735_set_addr_win(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) { void st7735_set_addr_win(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) {
st7735_write_cmd(ST7735_CASET); // Column addr set st7735_write_cmd(ST7735_CASET); // Column addr set
st7735_write_data(0x00); st7735_write_data(0x00);
st7735_write_cmd(x0 + st7735_column_start); // XSTART st7735_write_data(x0 + st7735_column_start); // XSTART
st7735_write_data(0x00); st7735_write_data(0x00);
st7735_write_data(x1 + st7735_column_start); // XEND st7735_write_data(x1 + st7735_column_start); // XEND

View File

@ -3,11 +3,11 @@
#include<stdint.h> #include<stdint.h>
const uint8_t st7735_default_width = 128; static const uint8_t st7735_default_width = 128;
// for 1.44" display // for 1.44" display
const uint8_t st7735_default_height_144 = 128; static const uint8_t st7735_default_height_144 = 128;
// for 1.8" display // for 1.8" display
const uint8_t st7735_default_height_18 = 160; static const uint8_t st7735_default_height_18 = 160;
enum ST7735_DISPLAY_TYPE { enum ST7735_DISPLAY_TYPE {
ST7735_BLUE, ST7735_BLUE,
@ -17,7 +17,7 @@ enum ST7735_DISPLAY_TYPE {
ST7735_RED144_GREENTAB ST7735_RED144_GREENTAB
}; };
const enum ST7735_DISPLAY_TYPE st7735_type = ST7735_RED_18_REDTAB; static const enum ST7735_DISPLAY_TYPE st7735_type = ST7735_RED_18_BLACKTAB;
// ST7735 commands // ST7735 commands

View File

@ -5,7 +5,7 @@
#include "st7735.h" #include "st7735.h"
const uint8_t DELAY_FLAG = (1 << 7); #define DELAY_FLAG 0x80
static const uint8_t PROGMEM st7735_blue_init[] = { // Initialization commands for 7735B screens static const uint8_t PROGMEM st7735_blue_init[] = { // Initialization commands for 7735B screens
18, // 18 commands in list: 18, // 18 commands in list: