sohotcall

Arduino ST7920 LCD Graphic 12864 Serial Mode

Oct 9th, 2018
136
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. LCD 128x64 ST7920, Serial Mode
  3. Pinout:
  4. - GND = 5V
  5. - VCC = 5V
  6. - RS (CS) = D2 (or 5V)
  7. - R/W (SID) = D4
  8. - E (SCLK) = D5
  9. - PSB = GND
  10. - RST = 5V (or None)
  11. - BLA = 5V
  12. - BLK = GND
  13. */
  14.  
  15. //! Choose PIN
  16. #define CS 2
  17. #define SID 4
  18. #define SCLK 5
  19.  
  20. //! Choose DELAY
  21. //- Not use delay
  22. #define DELAY
  23. //- Use delay
  24. //#define DELAY delayMicroseconds(0)
  25.  
  26. //! Choose CS_INIT, CS_ON, and CS_OFF
  27. //- Use CS
  28. //#define CS_INIT pinMode(CS, OUTPUT); digitalWrite(CS, LOW);
  29. //#define CS_ON digitalWrite(CS, HIGH); DELAY;
  30. //#define CS_OFF digitalWrite(CS, LOW); DELAY;
  31. //- Not use CS (#define CS no problem)
  32. #define CS_INIT
  33. #define CS_ON
  34. #define CS_OFF
  35.  
  36. //! How to use:
  37. // LCD_init();
  38. // LCD_gotoYX(uint8_t y, uint8_t x); // Goto address y = 0.63, x = 0..7
  39. // LCD_word(uint16t d); // Set 16-bit pixels on address
  40.  
  41. //! Ready?
  42.  
  43. void LCD_init(){
  44.     CS_INIT;
  45.     pinMode(SID, OUTPUT);
  46.     digitalWrite(SID, LOW);
  47.     pinMode(SCLK, OUTPUT);
  48.     digitalWrite(SCLK, LOW);
  49.     delay(3);
  50.     LCD_send(0, 48);
  51.     LCD_send(0, 48);
  52.     LCD_send(0, 12);
  53.     LCD_send(0, 52);
  54.     LCD_send(0, 52);
  55.     LCD_send(0, 54);
  56. }
  57.  
  58. void LCD_sid(uint8_t sid){
  59.     digitalWrite(SID, sid ? HIGH : LOW);
  60.     DELAY;
  61.     digitalWrite(SCLK, HIGH);
  62.     DELAY;
  63.     digitalWrite(SCLK, LOW);
  64.     DELAY;
  65. }
  66.  
  67. void LCD_bits(uint8_t d){
  68.     for (uint8_t i = 0x80; i; i >>= 1)
  69.         LCD_sid(d & i);
  70. }
  71.  
  72. void LCD_send(uint8_t rs, uint8_t d){
  73.     CS_ON;
  74.     LCD_bits( 0xF8 | ( rs << 1 ) );
  75.     LCD_bits(d & 0xF0);
  76.     LCD_bits(d << 4);
  77.     CS_OFF;
  78. }
  79.  
  80. void LCD_gotoYX(uint8_t y, uint8_t x){
  81.     // y = 0.63, x = 0..7
  82.     // ubah jadi
  83.     // y = 0..31, x = 0..15
  84.     LCD_send( 0, 0x80 | ( y & 31 ) );
  85.     LCD_send( 0, 0x80 | x | (y >> 5 << 3));
  86. }
  87.  
  88. void LCD_word(uint16_t d){
  89.     LCD_send(1, d >> 8);
  90.     LCD_send(1, d);
  91. }
  92.  
  93. void LCD_byte(uint8_t d){
  94.     LCD_send(1, d);
  95. }
  96.  
  97. //! Ready!
  98.  
  99. void setup() {
  100.     LCD_init();
  101. }
  102.  
  103. void loop() {
  104.     for (uint8_t y = 0; y < 64; y++){
  105.         for (uint8_t x = 0; x < 8; x++){
  106.             LCD_gotoYX(y, x);
  107.             LCD_word(y & 1 ? 0xF0F0 : 0x0F0F);
  108.         }
  109.     }
  110.     delay(1000);
  111.     for (uint8_t y = 0; y < 64; y++){
  112.         for (uint8_t x = 0; x < 8; x++){
  113.             LCD_gotoYX(y, x);
  114.             LCD_word(y & 1 ? 0x0F0F : 0xF0F0);
  115.         }
  116.     }
  117.     delay(1000);
  118. }
RAW Paste Data