sohotcall

STM32duino S6D1121 LCD TFT Display

Oct 23rd, 2018
194
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define PINS_D0 PA0
  2. #define PINS_D1 PA1
  3. #define PINS_D2 PA2
  4. #define PINS_D3 PA3
  5. #define PINS_D4 PA4
  6. #define PINS_D5 PA5
  7. #define PINS_D6 PA6
  8. #define PINS_D7 PA7
  9. #define PINS_RST PB11
  10. #define PINS_CS PB10
  11. #define PINS_WR PB1
  12. #define PINS_RS PB0
  13. #define WIDTH 240
  14. #define HEIGHT 320
  15.  
  16. void lcdWriteDataWord(uint16_t data) {
  17.     GPIOB->regs->BRR = 0x0400; // CS LOW
  18.   GPIOB->regs->BSRR = 0x0003; // WR RS HIGH  
  19.   GPIOA->regs->ODR = (GPIOA->regs->ODR & 0xFF00) | (data >> 8);
  20.   GPIOB->regs->BRR = 0x0002; // WR LOW
  21.   GPIOB->regs->BSRR = 0x0002; // WR HIGH
  22.   GPIOA->regs->ODR = (GPIOA->regs->ODR & 0xFF00) | (data & 0xFF);
  23.   GPIOB->regs->BRR = 0x0002; // WR LOW
  24.   GPIOB->regs->BSRR = 0x003; // WR RS HIGH
  25.   GPIOB->regs->BSRR = 0x0400; // CS HIGH
  26. }
  27.  
  28. void lcdWriteCommandWord(uint16_t data) {
  29.     GPIOB->regs->BRR = 0x0400; // CS LOW
  30.   GPIOB->regs->BSRR = 0x0002; // WR HIGH  
  31.   GPIOB->regs->BRR = 0x0001; // RS LOW  
  32.   GPIOA->regs->ODR = (GPIOA->regs->ODR & 0xFF00) | (data >> 8);
  33.   GPIOB->regs->BRR = 0x0002; // WR LOW
  34.   GPIOB->regs->BSRR = 0x0002; // WR HIGH
  35.   GPIOA->regs->ODR = (GPIOA->regs->ODR & 0xFF00) | (data & 0xFF);
  36.   GPIOB->regs->BRR = 0x0002; // WR LOW
  37.   GPIOB->regs->BSRR = 0x003; // WR RS HIGH
  38.   GPIOB->regs->BSRR = 0x0400; // CS HIGH
  39. }
  40.  
  41. void lcdWriteRegisterWord(uint16_t addr, uint16_t data) {
  42.   lcdWriteCommandWord(addr);
  43.   lcdWriteDataWord(data);
  44. }
  45.  
  46. void lcdInit() {
  47.   pinMode(PINS_RS, OUTPUT);
  48.   digitalWrite(PINS_RS, HIGH);
  49.   pinMode(PINS_WR, OUTPUT);
  50.   digitalWrite(PINS_WR, HIGH);
  51.   pinMode(PINS_RST, OUTPUT);
  52.   digitalWrite(PINS_RST, HIGH);
  53.   pinMode(PINS_CS, OUTPUT);
  54.   digitalWrite(PINS_CS, HIGH);  
  55.  
  56.   pinMode(PINS_D0, OUTPUT);
  57.   pinMode(PINS_D1, OUTPUT);
  58.   pinMode(PINS_D2, OUTPUT);
  59.   pinMode(PINS_D3, OUTPUT);  
  60.   pinMode(PINS_D4, OUTPUT);  
  61.   pinMode(PINS_D5, OUTPUT);
  62.   pinMode(PINS_D6, OUTPUT);
  63.   pinMode(PINS_D7, OUTPUT);  
  64. }
  65.  
  66. void lcdReset(void) {
  67.   digitalWrite(PINS_RST, LOW);
  68.   delay(100);
  69.   digitalWrite(PINS_RST, HIGH);
  70.   delay(100);
  71. }
  72.  
  73. void lcdSetup(void) {
  74.    lcdWriteRegisterWord(0x0011,0x2004);
  75.    lcdWriteRegisterWord(0x0013,0xCC00);
  76.    lcdWriteRegisterWord(0x0015,0x2600);
  77.    lcdWriteRegisterWord(0x0014,0x252A);
  78.    lcdWriteRegisterWord(0x0012,0x0033);
  79.    lcdWriteRegisterWord(0x0013,0xCC04);
  80.    lcdWriteRegisterWord(0x0013,0xCC06);
  81.    lcdWriteRegisterWord(0x0013,0xCC4F);
  82.    lcdWriteRegisterWord(0x0013,0x674F);
  83.    lcdWriteRegisterWord(0x0011,0x2003);
  84.    lcdWriteRegisterWord(0x0030,0x2609);
  85.    lcdWriteRegisterWord(0x0031,0x242C);
  86.    lcdWriteRegisterWord(0x0032,0x1F23);
  87.    lcdWriteRegisterWord(0x0033,0x2425);
  88.    lcdWriteRegisterWord(0x0034,0x2226);
  89.    lcdWriteRegisterWord(0x0035,0x2523);
  90.    lcdWriteRegisterWord(0x0036,0x1C1A);
  91.    lcdWriteRegisterWord(0x0037,0x131D);
  92.    lcdWriteRegisterWord(0x0038,0x0B11);
  93.    lcdWriteRegisterWord(0x0039,0x1210);
  94.    lcdWriteRegisterWord(0x003A,0x1315);
  95.    lcdWriteRegisterWord(0x003B,0x3619);
  96.    lcdWriteRegisterWord(0x003C,0x0D00);
  97.    lcdWriteRegisterWord(0x003D,0x000D);
  98.    lcdWriteRegisterWord(0x0016,0x0007);
  99.    lcdWriteRegisterWord(0x0001,0x0027);
  100.    lcdWriteRegisterWord(0x0002,0x0013);
  101.    lcdWriteRegisterWord(0x0003,0x0003);
  102.    lcdWriteRegisterWord(0x0008,0x0303);
  103.    lcdWriteRegisterWord(0x000A,0x000B);
  104.    lcdWriteRegisterWord(0x000B,0x0003);
  105.    lcdWriteRegisterWord(0x000C,0x0000);
  106.    lcdWriteRegisterWord(0x0041,0x0000);
  107.    lcdWriteRegisterWord(0x0050,0x0000);
  108.    lcdWriteRegisterWord(0x0060,0x0005);
  109.    lcdWriteRegisterWord(0x0070,0x000B);
  110.    lcdWriteRegisterWord(0x0071,0x0000);
  111.    lcdWriteRegisterWord(0x0078,0x0000);
  112.    lcdWriteRegisterWord(0x007A,0x0000);
  113.    lcdWriteRegisterWord(0x0079,0x0007);
  114.    lcdWriteRegisterWord(0x0007,0x0053);
  115.    lcdWriteRegisterWord(0x0079,0x0000);
  116. }
  117.  
  118. void lcdDrawPixel(uint16_t x, uint16_t y, uint16_t color) {
  119.   if (x < 0 || x >= WIDTH) return;
  120.   if (y < 0 || y >= HEIGHT) return;
  121.    lcdWriteRegisterWord(0x0020,x); // RAM Address Set 1
  122.    lcdWriteRegisterWord(0x0021,y); // RAM Address Set 2
  123.    lcdWriteRegisterWord(0x0022,color); // Write Data to GRAM
  124. }
  125.  
  126. void lcdDrawFillRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color){
  127.   int i,j;
  128.   if (x1 >= WIDTH) return;
  129.   if (x1 < 0) x1=0;
  130.   if (x2 < 0) return;
  131.   if (x2 >= WIDTH) x2=WIDTH-1;
  132.   if (y1 >= HEIGHT) return;
  133.   if (y1 < 0) y1=0;
  134.   if (y2 < 0) return;
  135.   if (y2 >= HEIGHT) y2=HEIGHT-1;
  136.   for(j=y1;j<=y2;j++){
  137.      lcdWriteRegisterWord(0x020,x1);
  138.      lcdWriteRegisterWord(0x021,j);
  139.      lcdWriteRegisterWord(0x022,color);
  140.      for(i=x1+1;i<=x2;i++){
  141.        lcdWriteDataWord(color);
  142.      }
  143.   }
  144. }
  145.  
  146. void lcdDisplayOff(void) {
  147.    lcdWriteRegisterWord(0x0007, 0x0010); // Set GON=0 D1=0, D0=0
  148. }
  149.  
  150. void lcdDisplayOn(void) {
  151.    lcdWriteRegisterWord(0x0007, 0x0053); // Set GON=1 D1=1, D0=1
  152. }
  153.  
  154. void lcdFillScreen(uint16_t color) {
  155.   lcdDrawFillRect(0, 0, WIDTH-1, HEIGHT-1, color);
  156. }
  157.  
  158. uint16_t rgb565_conv(uint16_t r,uint16_t g,uint16_t b){
  159.    unsigned int RR,GG,BB;
  160.    RR = (r * 31 / 255) << 11;
  161.    GG = (g * 63 / 255) << 5;
  162.    BB = (b * 31 / 255);
  163.    return(RR | GG | BB);
  164. }
  165.  
  166. void setup(){
  167.     lcdInit();
  168.     lcdReset();
  169.     lcdSetup();
  170. }
  171. void loop(){
  172.     lcdFillScreen(rgb565_conv(255,0,0));
  173.     delay(1000);
  174.     lcdFillScreen(rgb565_conv(0,255,0));
  175.     delay(1000);
  176.     lcdFillScreen(rgb565_conv(0,0,255));
  177.     delay(1000);
  178.     for (uint8_t i=0; i<100; i++){
  179.         lcdFillScreen(rgb565_conv(255,0,0));
  180.         lcdFillScreen(rgb565_conv(0,255,0));
  181.         lcdFillScreen(rgb565_conv(0,0,255));
  182.     }
  183. }
RAW Paste Data