微處理器 - STCmicro STC15W4K56S4 - C/C++ - 1.45" TFT 240x240 ST7789V






腳位說明
1LED K
2LED K
3GND
4GND
5FMARK
6SDA
7WR
8RS(SCL)
9CS
10RESET
11IOVCC
12IOVCC
13VCC
14VC
15GND
16GND
17NC
18NC
19NC
20NC
21NC
22NC
23LED A
24LED A

跳線

腳位說明
1IOVCC
2RESET
3CS
4RS(SCL)
5WR
6SDA
7FMARK
8GND
9LED K
10VCC
11GND
12LED A

main.c

#include "stc15w4k56s4.h"

#define RST   P54
#define CS    P43
#define SCL   P42
#define WR    P41
#define SDA   P40

#define Command   0
#define Parameter 1

void delayms(unsigned int ms)
{
    unsigned int cnt = 0;

    while (ms--) {
        for (cnt = 0; cnt < 1000; cnt++) {
        }
    }
}

void Write(unsigned char flag, unsigned char dat)
{
    int i = 0;

    CS = 0;
    SCL = 0;
    SDA = flag;
    SCL = 1;

    for (i = 0; i < 8; i++) {
        SCL = 0;
        if ((dat & 0x80) == 0x80) {
            SDA = 1;
        }
        else {
            SDA = 0;
        }
        dat <<= 1;
        SCL = 1;
    }
    CS = 1;
}

void reset(void)
{
  RST = 0;
  delayms(100);
  RST = 1;
  delayms(100);
}

void init(void)
{
    Write(Command, 0x11);
    delayms(120);   
      
    Write(Command, 0x36);
    Write(Parameter, 0x00); 

    Write(Command, 0x3a);
    Write(Parameter, 0x05);

    Write(Command, 0x2a); 
    Write(Parameter, 0x00); 
    Write(Parameter, 0x00); 
    Write(Parameter, 0x00); 
    Write(Parameter, 0xef); 

    Write(Command, 0x2b); 
    Write(Parameter, 0x00); 
    Write(Parameter, 0x00); 
    Write(Parameter, 0x00); 
    Write(Parameter, 0xef);

    Write(Command, 0xb2);
    Write(Parameter, 0x0c);
    Write(Parameter, 0x0c);
    Write(Parameter, 0x00);
    Write(Parameter, 0x33);
    Write(Parameter, 0x33);

    Write(Command, 0xb7);
    Write(Parameter, 0x56);

    Write(Command, 0xbb);
    Write(Parameter, 0x1e); 

    Write(Command ,0xc0); 
    Write(Parameter, 0x2c); 

    Write(Command, 0xc2); 
    Write(Parameter, 0x01);

    Write(Command, 0xc3);
    Write(Parameter, 0x13);

    Write(Command, 0xc4); 
    Write(Parameter, 0x20);

    Write(Command, 0xc6);
    Write(Parameter, 0x0f);

    Write(Command, 0xd0);
    Write(Parameter, 0xa4);
    Write(Parameter, 0xa1);

    Write(Command, 0xe0);
    Write(Parameter, 0xd0);
    Write(Parameter, 0x03);
    Write(Parameter, 0x08);
    Write(Parameter, 0x0e);
    Write(Parameter, 0x11);
    Write(Parameter, 0x2b);
    Write(Parameter, 0x3b);
    Write(Parameter, 0x44);
    Write(Parameter, 0x4c);
    Write(Parameter, 0x2b);
    Write(Parameter, 0x16);
    Write(Parameter, 0x15);
    Write(Parameter, 0x1e);
    Write(Parameter, 0x21);

    Write(Command, 0xe1);
    Write(Parameter, 0xd0);
    Write(Parameter, 0x03);
    Write(Parameter, 0x08);
    Write(Parameter, 0x0e);
    Write(Parameter, 0x11);
    Write(Parameter, 0x2b);
    Write(Parameter, 0x3b);
    Write(Parameter, 0x54);
    Write(Parameter, 0x4c);
    Write(Parameter, 0x2b);
    Write(Parameter, 0x16);
    Write(Parameter, 0x15);
    Write(Parameter, 0x1e);
    Write(Parameter, 0x21);

    Write(Command, 0x51);
    Write(Parameter, 0x20);

    Write(Command, 0xe7);
    Write(Parameter, 0x00);

    Write(Command, 0x29);
}

void color(void)
{
    unsigned int x = 0;
    unsigned int y = 0;

    Write(Command, 0x2c); 
    for (y = 0; y < 240; y++) {
        for (x = 0; x < 240; x++) {
            Write(Parameter, 0xf1);
            Write(Parameter, 0x00);
        }
    }
}

void gpio_init(void)
{
    P0M0 = 0x00;
    P0M1 = 0x00;
    P1M0 = 0x00;
    P1M1 = 0x00;
    P2M0 = 0x00;
    P2M1 = 0x00;
    P3M0 = 0x00;
    P3M1 = 0x00;
    P4M0 = 0x00;
    P4M1 = 0x00;
    P5M0 = 0x00;
    P5M1 = 0x00;
}

void main(void)
{
    gpio_init();
    AUXR |= 0x80;

    reset();
    init();
    color();

    while (1) {
        P55 = 0;
        delayms(1000);
        P55 = 1;
        delayms(1000);
    }
}

Makefile

all:
	sdcc main.c
	packihx main.ihx > main.hex

flash:
	sudo stcgal -p /dev/ttyO2 -P stc15 -o clock_source=external main.hex

clean:
	rm -rf main.ihx main.lst main.mem main.rst main.lk main.map main.rel main.sym main.hex

完成