st7567_stc8h3k.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /***
  15. * Demo: ST7567 12864 LCD
  16. * Board: STC8H3K32
  17. *
  18. * P37 -> RES, RESET 7 - 18
  19. * P36 -> DC, A0 8 - 17
  20. * P35 -> CSB, Chip Select 6 - 16
  21. * P32 -> SCK, SCL, CLK, Clock 9 - 13
  22. * P12 -> LED-A, Backlight 3 - 19
  23. * P34 -> MOSI, SDA 10 - 15
  24. * GND -> GND
  25. * 3.3V -> VCC
  26. */
  27. #include "fw_hal.h"
  28. #include "st7567.h"
  29. void SPI_Init(void)
  30. {
  31. // ST7567 doesn't work if SPI frequency is too high
  32. SPI_SetClockPrescaler(SPI_ClockPreScaler_16);
  33. // Clock is low when idle
  34. SPI_SetClockPolarity(HAL_State_OFF);
  35. // Data transfer is driven by lower SS pin
  36. SPI_SetClockPhase(SPI_ClockPhase_LeadingEdge);
  37. // MSB first
  38. SPI_SetDataOrder(SPI_DataOrder_MSB);
  39. // Define the output pins
  40. SPI_SetPort(SPI_AlterPort_P35_P34_P33_P32);
  41. // Ignore SS pin, use MSTR to swith between master/slave mode
  42. SPI_IgnoreSlaveSelect(HAL_State_ON);
  43. // Master mode
  44. SPI_SetMasterMode(HAL_State_ON);
  45. // Start SPI
  46. SPI_SetEnabled(HAL_State_ON);
  47. }
  48. void GPIO_Init(void)
  49. {
  50. // Configure GPIO pins before SPI and device
  51. // DIN(P34)
  52. GPIO_P3_SetMode(GPIO_Pin_4, GPIO_Mode_InOut_QBD);
  53. // SCLK(P32)
  54. GPIO_P3_SetMode(GPIO_Pin_2|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7, GPIO_Mode_Output_PP);
  55. GPIO_P1_SetMode(GPIO_Pin_2, GPIO_Mode_Output_PP);
  56. }
  57. int main(void)
  58. {
  59. int y1, y2;
  60. uint8_t d1, d2;
  61. SYS_SetClock();
  62. GPIO_Init();
  63. SPI_Init();
  64. ST7567_Init();
  65. while(1)
  66. {
  67. ST7567_DrawLine(0, 0, 127, 0, 1);
  68. ST7567_DrawLine(0, 0, 0, 63, 1);
  69. ST7567_DrawLine(127, 0, 127, 63, 1);
  70. ST7567_DrawLine(0, 63, 127, 63, 1);
  71. ST7567_GotoXY(3, 5);
  72. ST7567_Puts("LCD:ST7567", &Font_5x7, 1);
  73. ST7567_GotoXY(3, 13);
  74. ST7567_Puts("STC8 FwLib Demo", &Font_5x7, 1);
  75. ST7567_GotoXY(3, 21);
  76. ST7567_Puts("It's a demo of ST7567 12864 LCD", &Font_3x5, 1);
  77. ST7567_GotoXY(3, 27);
  78. ST7567_Puts("Font size 3x5, nums:01234567890", &Font_3x5, 1);
  79. ST7567_GotoXY(5, 52);
  80. ST7567_Puts("Font size: 5x7", &Font_5x7, 1);
  81. ST7567_UpdateScreen();
  82. SYS_Delay(2000);
  83. y1 = 10;
  84. while (y1 <= 0x30)
  85. {
  86. ST7567_SetContrast(y1++);
  87. SYS_Delay(100);
  88. }
  89. while (y1 >= 10)
  90. {
  91. ST7567_SetContrast(y1--);
  92. SYS_Delay(100);
  93. }
  94. while (y1 <= 0x20)
  95. {
  96. ST7567_SetContrast(y1++);
  97. SYS_Delay(100);
  98. }
  99. SYS_Delay(2000);
  100. ST7567_ToggleInvert();
  101. ST7567_UpdateScreen();
  102. SYS_Delay(2000);
  103. ST7567_ToggleInvert();
  104. ST7567_UpdateScreen();
  105. SYS_Delay(2000);
  106. ST7567_Fill(0);
  107. y1 = 64, y2 = 0;
  108. while (y1 > 0)
  109. {
  110. ST7567_DrawLine(0, y1, 127, y2, 1);
  111. ST7567_UpdateScreen();
  112. y1 -= 2;
  113. y2 += 2;
  114. SYS_Delay(100);
  115. }
  116. SYS_Delay(1000);
  117. ST7567_Fill(0);
  118. y1 = 127, y2 = 0;
  119. while (y1 > 0)
  120. {
  121. ST7567_DrawLine(y1, 0, y2, 63, 1);
  122. ST7567_UpdateScreen();
  123. y1 -= 2;
  124. y2 += 2;
  125. SYS_Delay(100);
  126. }
  127. SYS_Delay(1000);
  128. ST7567_Fill(0);
  129. ST7567_UpdateScreen();
  130. SYS_Delay(2000);
  131. }
  132. }