My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ultralcd_st7920_u8glib_rrd.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef ULCDST7920_H
  2. #define ULCDST7920_H
  3. #include "Marlin.h"
  4. #ifdef U8GLIB_ST7920
  5. //set optimization so ARDUINO optimizes this file
  6. #pragma GCC optimize (3)
  7. #define ST7920_CLK_PIN LCD_PINS_D4
  8. #define ST7920_DAT_PIN LCD_PINS_ENABLE
  9. #define ST7920_CS_PIN LCD_PINS_RS
  10. //#define PAGE_HEIGHT 8 //128 byte framebuffer
  11. //#define PAGE_HEIGHT 16 //256 byte framebuffer
  12. #define PAGE_HEIGHT 32 //512 byte framebuffer
  13. #define WIDTH 128
  14. #define HEIGHT 64
  15. #include <U8glib.h>
  16. static void ST7920_SWSPI_SND_8BIT(uint8_t val)
  17. {
  18. uint8_t i;
  19. for( i=0; i<8; i++ )
  20. {
  21. WRITE(ST7920_CLK_PIN,0);
  22. WRITE(ST7920_DAT_PIN,val&0x80);
  23. val<<=1;
  24. WRITE(ST7920_CLK_PIN,1);
  25. }
  26. }
  27. #define ST7920_CS() {WRITE(ST7920_CS_PIN,1);u8g_10MicroDelay();}
  28. #define ST7920_NCS() {WRITE(ST7920_CS_PIN,0);}
  29. #define ST7920_SET_CMD() {ST7920_SWSPI_SND_8BIT(0xf8);u8g_10MicroDelay();}
  30. #define ST7920_SET_DAT() {ST7920_SWSPI_SND_8BIT(0xfa);u8g_10MicroDelay();}
  31. #define ST7920_WRITE_BYTE(a) {ST7920_SWSPI_SND_8BIT((a)&0xf0);ST7920_SWSPI_SND_8BIT((a)<<4);u8g_10MicroDelay();}
  32. #define ST7920_WRITE_BYTES(p,l) {uint8_t i;for(i=0;i<l;i++){ST7920_SWSPI_SND_8BIT(*p&0xf0);ST7920_SWSPI_SND_8BIT(*p<<4);p++;}u8g_10MicroDelay();}
  33. uint8_t u8g_dev_rrd_st7920_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
  34. {
  35. uint8_t i,y;
  36. switch(msg)
  37. {
  38. case U8G_DEV_MSG_INIT:
  39. {
  40. OUT_WRITE(ST7920_CS_PIN,LOW);
  41. OUT_WRITE(ST7920_DAT_PIN,LOW);
  42. OUT_WRITE(ST7920_CLK_PIN,HIGH);
  43. ST7920_CS();
  44. u8g_Delay(120); //initial delay for boot up
  45. ST7920_SET_CMD();
  46. ST7920_WRITE_BYTE(0x08); //display off, cursor+blink off
  47. ST7920_WRITE_BYTE(0x01); //clear CGRAM ram
  48. u8g_Delay(15); //delay for CGRAM clear
  49. ST7920_WRITE_BYTE(0x3E); //extended mode + GDRAM active
  50. for(y=0;y<HEIGHT/2;y++) //clear GDRAM
  51. {
  52. ST7920_WRITE_BYTE(0x80|y); //set y
  53. ST7920_WRITE_BYTE(0x80); //set x = 0
  54. ST7920_SET_DAT();
  55. for(i=0;i<2*WIDTH/8;i++) //2x width clears both segments
  56. ST7920_WRITE_BYTE(0);
  57. ST7920_SET_CMD();
  58. }
  59. ST7920_WRITE_BYTE(0x0C); //display on, cursor+blink off
  60. ST7920_NCS();
  61. }
  62. break;
  63. case U8G_DEV_MSG_STOP:
  64. break;
  65. case U8G_DEV_MSG_PAGE_NEXT:
  66. {
  67. uint8_t *ptr;
  68. u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
  69. y = pb->p.page_y0;
  70. ptr = (uint8_t*)pb->buf;
  71. ST7920_CS();
  72. for( i = 0; i < PAGE_HEIGHT; i ++ )
  73. {
  74. ST7920_SET_CMD();
  75. if ( y < 32 )
  76. {
  77. ST7920_WRITE_BYTE(0x80 | y); //y
  78. ST7920_WRITE_BYTE(0x80); //x=0
  79. }
  80. else
  81. {
  82. ST7920_WRITE_BYTE(0x80 | (y-32)); //y
  83. ST7920_WRITE_BYTE(0x80 | 8); //x=64
  84. }
  85. ST7920_SET_DAT();
  86. ST7920_WRITE_BYTES(ptr,WIDTH/8); //ptr is incremented inside of macro
  87. y++;
  88. }
  89. ST7920_NCS();
  90. }
  91. break;
  92. }
  93. #if PAGE_HEIGHT == 8
  94. return u8g_dev_pb8h1_base_fn(u8g, dev, msg, arg);
  95. #elif PAGE_HEIGHT == 16
  96. return u8g_dev_pb16h1_base_fn(u8g, dev, msg, arg);
  97. #else
  98. return u8g_dev_pb32h1_base_fn(u8g, dev, msg, arg);
  99. #endif
  100. }
  101. uint8_t u8g_dev_st7920_128x64_rrd_buf[WIDTH*(PAGE_HEIGHT/8)] U8G_NOCOMMON;
  102. u8g_pb_t u8g_dev_st7920_128x64_rrd_pb = {{PAGE_HEIGHT,HEIGHT,0,0,0},WIDTH,u8g_dev_st7920_128x64_rrd_buf};
  103. u8g_dev_t u8g_dev_st7920_128x64_rrd_sw_spi = {u8g_dev_rrd_st7920_128x64_fn,&u8g_dev_st7920_128x64_rrd_pb,&u8g_com_null_fn};
  104. class U8GLIB_ST7920_128X64_RRD : public U8GLIB
  105. {
  106. public:
  107. U8GLIB_ST7920_128X64_RRD(uint8_t dummy) : U8GLIB(&u8g_dev_st7920_128x64_rrd_sw_spi) {}
  108. };
  109. #endif //U8GLIB_ST7920
  110. #endif //ULCDST7920_H