sohotcall

STM32duino R61509V LCD TFT Display

Oct 27th, 2018
385
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // GPIOA: X X X X . X X X X . DB7 DB6 DB5 DB4 . DB3 DB2 DB1 DB0
  2. // GPIOB: X X X X . RST CS X X . X X X X . X X WR RS
  3. #define WIDTH 240
  4. #define HEIGHT 400
  5.  
  6. void lcdWriteDataWord(uint16_t data) {
  7.     GPIOB->regs->BSRR = 0x04000000; // CS LOW,
  8.     GPIOB->regs->BSRR = 0x00000003; // , WR RS HIGH  
  9.     GPIOA->regs->ODR = (GPIOA->regs->ODR & 0xFF00) | (data >> 8);
  10.     GPIOB->regs->BSRR = 0x00020000; // WR LOW,
  11.     GPIOB->regs->BSRR = 0x00000002; // , WR HIGH
  12.     GPIOA->regs->ODR = (GPIOA->regs->ODR & 0xFF00) | (data & 0xFF);
  13.     GPIOB->regs->BSRR = 0x00020000; // WR LOW,
  14.     GPIOB->regs->BSRR = 0x00000003; // , WR RS HIGH
  15.     GPIOB->regs->BSRR = 0x00000400; // , CS HIGH
  16. }
  17.  
  18. void lcdWriteCommandWord(uint16_t data) {
  19.     GPIOB->regs->BSRR = 0x04000000; // CS LOW,
  20.     GPIOB->regs->BSRR = 0x00010002; // RS LOW, WR HIGH  
  21.     GPIOA->regs->ODR = (GPIOA->regs->ODR & 0xFF00) | (data >> 8);
  22.     GPIOB->regs->BSRR = 0x00020000; // WR LOW,
  23.     GPIOB->regs->BSRR = 0x00000002; // , WR HIGH
  24.     GPIOA->regs->ODR = (GPIOA->regs->ODR & 0xFF00) | (data & 0xFF);
  25.     GPIOB->regs->BSRR = 0x00020000; // WR LOW,
  26.     GPIOB->regs->BSRR = 0x00000003; // ,WR RS HIGH
  27.     GPIOB->regs->BSRR = 0x00000400; // , CS HIGH
  28. }
  29. void lcdWriteRegisterWord(uint16_t addr, uint16_t data) {
  30.     lcdWriteCommandWord(addr);
  31.     lcdWriteDataWord(data);
  32. }
  33.  
  34. void lcdInit() {
  35.     GPIOB->regs->CRH = (GPIOB->regs->CRH & 0xFFFF00FF) | 0x00003300; // RST CS OUT
  36.     GPIOB->regs->CRL = (GPIOB->regs->CRL & 0xFFFFFF00) | 0x00000033; // WR RS OUT
  37.     GPIOB->regs->BSRR = 0x00000C03; // , RST CS WR RS HIGH
  38.     GPIOA->regs->CRL = 0x33333333;  // DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 OUT
  39. }
  40.  
  41. void lcdReset(void) {
  42.     GPIOB->regs->BSRR = 0x08000000; // RST LOW,
  43.     delay(100);
  44.     GPIOB->regs->BSRR = 0x00000800; // , RST HIGH
  45.     delay(100);
  46. }
  47.  
  48. void lcdSetup(void) {
  49.     lcdWriteRegisterWord(0x0000, 0x0000);
  50.     lcdWriteRegisterWord(0x0000, 0x0000);
  51.     lcdWriteRegisterWord(0x0000, 0x0000);
  52.     lcdWriteRegisterWord(0x0000, 0x0000);
  53.     delay(15);
  54.     lcdWriteRegisterWord(0x0400, 0x6200);     //NL=0x31 (49) i.e. 400 rows
  55.     lcdWriteRegisterWord(0x0008, 0x0808);
  56.     lcdWriteRegisterWord(0x0300, 0x0C00);
  57.     lcdWriteRegisterWord(0x0301, 0x5A0B);
  58.     lcdWriteRegisterWord(0x0302, 0x0906);
  59.     lcdWriteRegisterWord(0x0303, 0x1017);
  60.     lcdWriteRegisterWord(0x0304, 0x2300);
  61.     lcdWriteRegisterWord(0x0305, 0x1700);
  62.     lcdWriteRegisterWord(0x0306, 0x6309);
  63.     lcdWriteRegisterWord(0x0307, 0x0C09);
  64.     lcdWriteRegisterWord(0x0308, 0x100C);
  65.     lcdWriteRegisterWord(0x0309, 0x2232);
  66.     lcdWriteRegisterWord(0x0010, 0x0016);     //69.5Hz         0016
  67.     lcdWriteRegisterWord(0x0011, 0x0101);
  68.     lcdWriteRegisterWord(0x0012, 0x0000);
  69.     lcdWriteRegisterWord(0x0013, 0x0001);
  70.     lcdWriteRegisterWord(0x0100, 0x0330);     //BT,AP
  71.     lcdWriteRegisterWord(0x0101, 0x0237);     //DC0,DC1,VC
  72.     lcdWriteRegisterWord(0x0103, 0x0D00);     //VDV
  73.     lcdWriteRegisterWord(0x0280, 0x6100);     //VCM
  74.     lcdWriteRegisterWord(0x0102, 0xC1B0);     //VRH,VCMR,PSON,PON
  75.     delay(50);
  76.     lcdWriteRegisterWord(0x0001, 0x0100);
  77.     lcdWriteRegisterWord(0x0002, 0x0100);
  78.     lcdWriteRegisterWord(0x0003, 0x1030);     //1030
  79.     lcdWriteRegisterWord(0x0009, 0x0001);
  80.     lcdWriteRegisterWord(0x000C, 0x0000);
  81.     lcdWriteRegisterWord(0x0090, 0x8000);
  82.     lcdWriteRegisterWord(0x000F, 0x0000);
  83.     lcdWriteRegisterWord(0x0210, 0x0000);
  84.     lcdWriteRegisterWord(0x0211, 0x00EF);
  85.     lcdWriteRegisterWord(0x0212, 0x0000);
  86.     lcdWriteRegisterWord(0x0213, 0x018F);     //432=01AF,400=018F
  87.     lcdWriteRegisterWord(0x0500, 0x0000);
  88.     lcdWriteRegisterWord(0x0501, 0x0000);
  89.     lcdWriteRegisterWord(0x0502, 0x005F);     //???
  90.     lcdWriteRegisterWord(0x0401, 0x0001);     //REV=1
  91.     lcdWriteRegisterWord(0x0404, 0x0000);
  92.     delay(50);
  93.     lcdWriteRegisterWord(0x0007, 0x0100);     //BASEE
  94.     delay(50);
  95.     lcdWriteRegisterWord(0x0200, 0x0000);
  96.     lcdWriteRegisterWord(0x0201, 0x0000);
  97. }
  98.  
  99. void lcdDrawPixel(uint16_t x, uint16_t y, uint16_t color) {
  100.     if (x < 0 || x >= WIDTH) return;
  101.     if (y < 0 || y >= HEIGHT) return;
  102.   lcdWriteRegisterWord(0x200, x); // MC
  103.   lcdWriteRegisterWord(0x201, y); // MP
  104.   lcdWriteRegisterWord(0x202, color); // MW
  105. }
  106.  
  107. void lcdDrawFillRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color){
  108.   int i,j;
  109.   if (x1 >= WIDTH) return;
  110.   if (x1 < 0) x1=0;
  111.   if (x2 < 0) return;
  112.   if (x2 >= WIDTH) x2=WIDTH-1;
  113.   if (y1 >= HEIGHT) return;
  114.   if (y1 < 0) y1=0;
  115.   if (y2 < 0) return;
  116.   if (y2 >= HEIGHT) y2=HEIGHT-1;
  117.  
  118.   lcdWriteRegisterWord(0x200, x1); // MC
  119.   lcdWriteRegisterWord(0x201, y1); // MP
  120.   lcdWriteRegisterWord(0x210, x1); // SC
  121.   lcdWriteRegisterWord(0x212, y1); // SP
  122.   lcdWriteRegisterWord(0x211, x2); // EC
  123.   lcdWriteRegisterWord(0x213, y2); // EP
  124.   lcdWriteCommandWord(0x202); // MW
  125.   for(j=y1; j<=y2; j++){
  126.      for(i=x1; i<=x2; i++){
  127.        lcdWriteDataWord(color);
  128.      }
  129.   }
  130. }
  131.  
  132. void lcdDisplayOff(void) {
  133.    lcdWriteRegisterWord(0x0007, 0x0010); // Set GON=0 D1=0, D0=0
  134. }
  135.  
  136. void lcdDisplayOn(void) {
  137.    lcdWriteRegisterWord(0x0007, 0x0053); // Set GON=1 D1=1, D0=1
  138. }
  139.  
  140. void lcdFillScreen(uint16_t color) {
  141.   lcdDrawFillRect(0, 0, WIDTH-1, HEIGHT-1, color);
  142. }
  143.  
  144. uint16_t rgb565_conv(uint8_t r, uint8_t g, uint8_t b) {
  145.   return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
  146. }
  147.  
  148. void setup(){
  149.     lcdInit();
  150.     lcdReset();
  151.     lcdSetup();
  152. }
  153. void loop(){
  154.     lcdFillScreen(rgb565_conv(255,0,0));
  155.     delay(1000);
  156.     lcdFillScreen(rgb565_conv(0,255,0));
  157.     delay(1000);
  158.     lcdFillScreen(rgb565_conv(0,0,255));
  159.     delay(1000);
  160.     for (uint8_t i=0; i<100; i++){
  161.         lcdFillScreen(rgb565_conv(255,0,0));
  162.         lcdFillScreen(rgb565_conv(0,255,0));
  163.         lcdFillScreen(rgb565_conv(0,0,255));
  164.     }
  165.     lcdWriteRegisterWord(0x200, 0); //MC
  166.   lcdWriteRegisterWord(0x201, 0); // MP
  167.   lcdWriteRegisterWord(0x210, 0); // SC
  168.   lcdWriteRegisterWord(0x212, 0); // SP
  169.   lcdWriteRegisterWord(0x211, WIDTH-1); //EC
  170.   lcdWriteRegisterWord(0x213, HEIGHT-1); // EP
  171.   lcdWriteCommandWord(0x202); // MW
  172.   uint16_t color = 0;
  173.   int i,j;
  174.   for(j=0; j<HEIGHT; j++){
  175.      for(i=0; i<WIDTH; i++){
  176.          color = rgb565_conv((i&4)^(j&32)?255:0, (i&8)^(j&32)?255:0, (i&16)^(j&32)?255:0);
  177.        lcdWriteDataWord(color);
  178.      }
  179.   }
  180.   delay(10000);
  181. }
RAW Paste Data