微處理器 - STCmicro STC15W4K56S4 - C/C++ - 2.80" IPS 320x240 S6D04M0X21







16Bit


main.c

#include "stc15w4k56s4.h"

#define RS   P54
#define CS   P43
#define RD   P42
#define WR   P41
#define RST  P40

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

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

void lcd_write(unsigned char rs, unsigned int val)
{
    CS = 0;
    if (rs) {
        RS = 1;
    }
    else {
        RS = 0;
    }
    RD = 1;
    WR = 1;

    P0 = val;
    P2 = (val >> 8);
    WR = 0;
    WR = 1;
    CS = 1;
}

void lcd_write_cmd(unsigned int val)
{
    lcd_write(0, val);
}

void lcd_write_data(unsigned int val)
{
    lcd_write(1, val);
}

void lcd_set_color(void)
{
    unsigned int i = 0;
    unsigned int j = 0;
    unsigned int color[] = { 0xf800, 0x7e0, 0x1f, 0 };

    lcd_write_cmd(0x002a);
    lcd_write_data(0x0000);
    lcd_write_data(0x0000);
    lcd_write_data(0x0000);
    lcd_write_data(0x00ef);  

    lcd_write_cmd(0x002b);    
    lcd_write_data(0x0000);
    lcd_write_data(0x0000);
    lcd_write_data(0x0001);
    lcd_write_data(0x003f);
    lcd_write_cmd(0x002c);

    for (i = 0; i < 240; i++) {
        for (j = 0; j < 320; j++) {
            lcd_write_data(color[i / 80]);
        }
    }
}

void lcd_init(void)
{
    delayms(120);
    lcd_write_cmd(0x0001);
    delayms(120);

    lcd_write_cmd(0x00f0);    
    lcd_write_data(0x005a); 
    lcd_write_data(0x005a);

    lcd_write_cmd(0x00f3);    
    lcd_write_data(0x0000); 

    lcd_write_cmd(0x00ff);    
    lcd_write_data(0x0030); 
    lcd_write_data(0x0000);
    lcd_write_data(0x0000); 
    lcd_write_data(0x0040); 

    lcd_write_cmd(0x0011);
    delayms(120);

    lcd_write_cmd(0x00f3);
    lcd_write_data(0x0001);    
    lcd_write_data(0x0026);
    lcd_write_data(0x0026);
    lcd_write_data(0x0007); 
    lcd_write_data(0x0011);
    lcd_write_data(0x0057);
    lcd_write_data(0x0057); 
    lcd_write_data(0x002c); 

    lcd_write_cmd(0x00f4);    
    lcd_write_data(0x0060);
    lcd_write_data(0x0060);
    lcd_write_data(0x006f);
    lcd_write_data(0x006f);
    lcd_write_data(0x0044);

    lcd_write_cmd(0x00f5);    
    lcd_write_data(0x0012);
    lcd_write_data(0x0011);
    lcd_write_data(0x0003);
    lcd_write_data(0x00f0); 
    lcd_write_data(0x0030);
    lcd_write_data(0x001f);

    delayms(10);
    lcd_write_cmd(0x00f3);    
    lcd_write_data(0x0003);
    delayms(20);

    lcd_write_cmd(0x00ff);    
    lcd_write_data(0x0030); 
    lcd_write_data(0x0000);
    lcd_write_data(0x0000); 
    lcd_write_data(0x0070);  
    delayms(20);

    lcd_write_cmd(0x00f3);    
    lcd_write_data(0x000f);

    lcd_write_cmd(0x00ff);    
    lcd_write_data(0x0030); 
    lcd_write_data(0x0000);
    lcd_write_data(0x0000); 
    lcd_write_data(0x0078);    
    delayms(20);

    lcd_write_cmd(0x00f3);    
    lcd_write_data(0x001f);

    lcd_write_cmd(0x00ff);    
    lcd_write_data(0x0000); 
    lcd_write_data(0x0000);
    lcd_write_data(0x0000); 
    lcd_write_data(0x0078);  

    delayms(20);
    lcd_write_cmd(0x00f3);    
    lcd_write_data(0x001f); 
    delayms(40); 

    lcd_write_cmd(0x00f3);
    lcd_write_data(0x00ff);
    lcd_write_data(0x0026);
    lcd_write_data(0x0026);
    lcd_write_data(0x0007);
    lcd_write_data(0x0055);
    lcd_write_data(0x0057);
    lcd_write_data(0x0057); 
    lcd_write_data(0x002c); 

    delayms(50);  
    lcd_write_cmd(0x0011);
    delayms(100); 

    lcd_write_cmd(0x00f2);
    lcd_write_data(0x0013);
    lcd_write_data(0x0013);
    lcd_write_data(0x0001);
    lcd_write_data(0x0008); 
    lcd_write_data(0x0008);
    lcd_write_data(0x0008); 
    lcd_write_data(0x0008); 
    lcd_write_data(0x0010); 
    lcd_write_data(0x0000);
    lcd_write_data(0x0010);
    lcd_write_data(0x0010);

    lcd_write_cmd(0x00fd);    
    lcd_write_data(0x0055); 

    lcd_write_cmd(0x0035); 
    lcd_write_data(0x0000);    

    lcd_write_cmd(0x0036);    
    lcd_write_data(0x0048); 

    lcd_write_cmd(0x003a);    
    lcd_write_data(0x0055); 

    lcd_write_cmd(0x00fe);    
    lcd_write_data(0x0016);
    lcd_write_data(0x0016);
    lcd_write_data(0x0028);
    lcd_write_data(0x0000); 
    lcd_write_data(0x0000);
    lcd_write_data(0x0006); 
         
    lcd_write_cmd(0x00f6);    
    lcd_write_data(0x0010);
    lcd_write_data(0x0080);
    lcd_write_data(0x0000);
    lcd_write_data(0x0010);   
         
    lcd_write_cmd(0x0051);    
    lcd_write_data(0x00ff);   
         
    lcd_write_cmd(0x0053);    
    lcd_write_data(0x0000);   
         
    lcd_write_cmd(0x0055);    
    lcd_write_data(0x0000);   
         
    lcd_write_cmd(0x005e);    
    lcd_write_data(0x0000);   
         
    lcd_write_cmd(0x00ca);    
    lcd_write_data(0x0080);
    lcd_write_data(0x0080);
    lcd_write_data(0x0020);  

    lcd_write_cmd(0x00cb);    
    lcd_write_data(0x0001); 

    lcd_write_cmd(0x00f7);    
    lcd_write_data(0x0092);
    lcd_write_data(0x001a);
    lcd_write_data(0x001e);
    lcd_write_data(0x001a); 
    lcd_write_data(0x001d);
    lcd_write_data(0x001d); 
    lcd_write_data(0x001e); 
    lcd_write_data(0x0026); 
    lcd_write_data(0x001c);
    lcd_write_data(0x001c);
    lcd_write_data(0x0024);
    lcd_write_data(0x001b); 
    lcd_write_data(0x0009);
    lcd_write_data(0x0011);
    lcd_write_data(0x0018);

    lcd_write_cmd(0x00f8);    
    lcd_write_data(0x001a);
    lcd_write_data(0x0000);
    lcd_write_data(0x001e);
    lcd_write_data(0x001a); 
    lcd_write_data(0x001d);
    lcd_write_data(0x001a); 
    lcd_write_data(0x001d); 
    lcd_write_data(0x0024); 
    lcd_write_data(0x001e);
    lcd_write_data(0x001f);
    lcd_write_data(0x0027);
    lcd_write_data(0x001e); 
    lcd_write_data(0x000a);
    lcd_write_data(0x0011);
    lcd_write_data(0x0011);

    lcd_write_cmd(0x00f9);    
    lcd_write_data(0x0086);
    lcd_write_data(0x001a);
    lcd_write_data(0x0010);
    lcd_write_data(0x0010); 
    lcd_write_data(0x001d);
    lcd_write_data(0x001b); 
    lcd_write_data(0x001f); 
    lcd_write_data(0x0027); 
    lcd_write_data(0x001b);
    lcd_write_data(0x001f);
    lcd_write_data(0x0027);
    lcd_write_data(0x001e); 
    lcd_write_data(0x000a);
    lcd_write_data(0x0011);
    lcd_write_data(0x0018);

    lcd_write_cmd(0x00fa);    
    lcd_write_data(0x001a);
    lcd_write_data(0x0000);
    lcd_write_data(0x001e);
    lcd_write_data(0x001a); 
    lcd_write_data(0x001d);
    lcd_write_data(0x001a); 
    lcd_write_data(0x001d); 
    lcd_write_data(0x0024); 
    lcd_write_data(0x001e);
    lcd_write_data(0x001f);
    lcd_write_data(0x0027);
    lcd_write_data(0x001e); 
    lcd_write_data(0x000a);
    lcd_write_data(0x0011);
    lcd_write_data(0x0011);

    lcd_write_cmd(0x00fb);    
    lcd_write_data(0x0080);
    lcd_write_data(0x001a);
    lcd_write_data(0x001e);
    lcd_write_data(0x001a); 
    lcd_write_data(0x001d);
    lcd_write_data(0x001b); 
    lcd_write_data(0x001e); 
    lcd_write_data(0x0026); 
    lcd_write_data(0x0017);
    lcd_write_data(0x001a);
    lcd_write_data(0x001d);
    lcd_write_data(0x0014); 
    lcd_write_data(0x0000);
    lcd_write_data(0x0011);
    lcd_write_data(0x0018);

    lcd_write_cmd(0x00fc);    
    lcd_write_data(0x001a);
    lcd_write_data(0x0000);
    lcd_write_data(0x001e);
    lcd_write_data(0x001a); 
    lcd_write_data(0x001d);
    lcd_write_data(0x001a); 
    lcd_write_data(0x001d); 
    lcd_write_data(0x0024); 
    lcd_write_data(0x001e);
    lcd_write_data(0x001f);
    lcd_write_data(0x0027);
    lcd_write_data(0x001e); 
    lcd_write_data(0x000a);
    lcd_write_data(0x0011);
    lcd_write_data(0x0011);

    lcd_write_cmd(0x002a);    
    lcd_write_data(0x0000);
    lcd_write_data(0x0000);
    lcd_write_data(0x0000);
    lcd_write_data(0x00ef);  

    lcd_write_cmd(0x002b);    
    lcd_write_data(0x0000);
    lcd_write_data(0x0000);
    lcd_write_data(0x0001);
    lcd_write_data(0x003f);

    lcd_write_cmd(0x0029); 
    lcd_write_cmd(0x002c); 
}

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;

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

    lcd_init();
    lcd_set_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

完成