微處理器 - STCmicro STC15W4K56S4 - C/C++ - 1.54" ePaper 152x152 Black-Yellow







main.c

#include "stc15w4k56s4.h"

#define SDA   P20
#define SCK   P21
#define CS    P22
#define DC    P23
#define RST   P24
#define BUSY  P25

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

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

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

    P0 = 0xff;
    P1 = 0xff;
    P2 = 0xff;
    P3 = 0xff;
    P4 = 0xff;
}

void spi_cmd(unsigned char cmd)
{
    unsigned char bit = 0;

    CS = 0;
    DC = 0;
    for (bit = 0; bit < 8; bit++) {
        SCK = 0;
        if ((cmd & 0x80) == 0x80) {
            SDA = 1;
        }
        else {
            SDA = 0;
        }
        SCK = 1;
        cmd <<= 1;
    }
    CS = 1;
}

void spi_dat(unsigned char dat)
{
    unsigned char bit = 0;

    CS = 0;
    DC = 1;
    for (bit = 0; bit < 8; bit++) {
        SCK = 0;
        if ((dat & 0x80) == 0x80) {
            SDA = 1;
        }
        else {
            SDA = 0;
        }
        SCK = 1;
        dat <<= 1;
    }
    CS = 1;
}

void wait_busy(void)
{
    do {
        spi_cmd(0x71);
        delayms(1);
    } while (BUSY == 0);
}

void lcd_init(void)
{
    spi_cmd(0x06);
    spi_dat(0x17);
    spi_dat(0x17);
    spi_dat(0x17);
    spi_cmd(0x04);

    wait_busy();

    spi_cmd(0x00);
    spi_dat(0x0f);
    spi_dat(0x0d);

    spi_cmd(0x61);
    spi_dat(0x98);
    spi_dat(0x00);
    spi_dat(0x98);

    spi_cmd(0X50);
    spi_dat(0x77);
}

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

    spi_cmd(0x10);
    for (y = 0; y < 152; y++) {
        for (x = 0; x < (152 / 8); x++) {
            spi_dat(0xff);
        }
    }
    spi_cmd(0x13);
    for (y = 0; y < 152; y++) {
        for (x = 0; x < (152 / 8); x++) {
            spi_dat(0xff);
        }
    }
    spi_cmd(0x12);
    wait_busy();
}

void main(void)
{
    unsigned int x = 0;
    unsigned int y = 0;
  
    gpio_init();
    AUXR |= 0x80;

    RST = 0;
    delayms(300);
    RST = 1;
    delayms(300);

    lcd_init();
    lcd_clear();

    spi_cmd(0x10);
    for (y = 0; y < 152; y++) {
        for (x = 0; x < (152 / 8); x++) {
            if (y < 50) {
                spi_dat(0x00);
            }
            else {
                spi_dat(0xff);
            }
        }
    }

    spi_cmd(0x13);
    for (y = 0; y < 152; y++) {
        for (x = 0; x < (152 / 8); x++) {
            if ((y > 50) && (y < 100)) {
                spi_dat(0x00);
            }
            else{
                spi_dat(0xff);
            }
        }
    }
    spi_cmd(0x12);
    wait_busy();

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

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

完成