pcd8544_stc8h3k.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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: PCD8544 - Nokia5110 84x48 LCD
  16. * Board: STC8H3K32
  17. *
  18. * P37 -> RES, RESET
  19. * P36 -> DC, A0
  20. * P35 -> CE, Chip Select
  21. * P32 -> SCK, SCL, CLK, Clock
  22. * P12 -> BL, Backlight
  23. * P34 -> MOSI, Din
  24. * GND -> GND
  25. * 3.3V -> VCC
  26. */
  27. #include "fw_hal.h"
  28. #include "pcd8544.h"
  29. void SPI_Init(void)
  30. {
  31. SPI_SetClockPrescaler(SPI_ClockPreScaler_16);
  32. // Clock is low when idle
  33. SPI_SetClockPolarity(HAL_State_OFF);
  34. // Data transfer is driven by lower SS pin
  35. SPI_SetClockPhase(SPI_ClockPhase_LeadingEdge);
  36. // MSB first
  37. SPI_SetDataOrder(SPI_DataOrder_MSB);
  38. // Define the output pins
  39. SPI_SetPort(SPI_AlterPort_P35_P34_P33_P32);
  40. // Ignore SS pin, use MSTR to swith between master/slave mode
  41. SPI_IgnoreSlaveSelect(HAL_State_ON);
  42. // Master mode
  43. SPI_SetMasterMode(HAL_State_ON);
  44. // Start SPI
  45. SPI_SetEnabled(HAL_State_ON);
  46. }
  47. void GPIO_Init(void)
  48. {
  49. // Configure GPIO pins before SPI and device
  50. // DIN(P34)
  51. GPIO_P3_SetMode(GPIO_Pin_4, GPIO_Mode_InOut_QBD);
  52. // SCLK(P32)
  53. GPIO_P3_SetMode(GPIO_Pin_2|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7, GPIO_Mode_Output_PP);
  54. GPIO_P1_SetMode(GPIO_Pin_2, GPIO_Mode_Output_PP);
  55. }
  56. int main(void)
  57. {
  58. uint8_t y1, y2;
  59. SYS_SetClock();
  60. GPIO_Init();
  61. SPI_Init();
  62. PCD8544_Init();
  63. while(1)
  64. {
  65. PCD8544_SetBackLightState(HAL_State_ON);
  66. SYS_Delay(500);
  67. PCD8544_SetDisplayAllOn();
  68. SYS_Delay(200);
  69. PCD8544_SetDisplayInverted();
  70. SYS_Delay(200);
  71. PCD8544_SetDisplayBlank();
  72. SYS_Delay(200);
  73. PCD8544_SetDisplayNormal();
  74. SYS_Delay(500);
  75. PCD8544_Fill(0);
  76. PCD8544_DrawLine(0, 0, 83, 0, 1);
  77. PCD8544_DrawLine(0, 0, 0, 47, 1);
  78. PCD8544_DrawLine(83, 0, 83, 47, 1);
  79. PCD8544_DrawLine(0, 47, 83, 47, 1);
  80. PCD8544_UpdateScreen();
  81. SYS_Delay(500);
  82. PCD8544_GotoXY(3, 3);
  83. PCD8544_Puts("LCD:PCD8544", &Font_5x7, 1);
  84. PCD8544_GotoXY(3, 11);
  85. PCD8544_Puts("STC8 FwLib", &Font_5x7, 1);
  86. PCD8544_GotoXY(3, 19);
  87. PCD8544_Puts("A demo of PCD8544", &Font_3x5, 1);
  88. PCD8544_GotoXY(3, 25);
  89. PCD8544_Puts("Font 3x5: 01234567890", &Font_3x5, 1);
  90. PCD8544_UpdateScreen();
  91. SYS_Delay(500);
  92. PCD8544_SetPowerDownMode(HAL_State_ON);
  93. SYS_Delay(1000);
  94. PCD8544_SetPowerDownMode(HAL_State_OFF);
  95. SYS_Delay(1000);
  96. PCD8544_SetDisplayInverted();
  97. SYS_Delay(1000);
  98. PCD8544_SetDisplayNormal();
  99. SYS_Delay(1000);
  100. for (y1 = 0x05; y1 < 0x08; y1++)
  101. {
  102. for (y2 = 0x00; y2 < 0x60; y2+=2)
  103. {
  104. PCD8544_GotoXY(3, 32);
  105. PCD8544_Putc(HEX_TABLE[y1 >> 4], &Font_5x7, 1);
  106. PCD8544_Putc(HEX_TABLE[y1 & 0x0F], &Font_5x7, 1);
  107. PCD8544_Putc(' ', &Font_5x7, 1);
  108. PCD8544_Putc(HEX_TABLE[y2 >> 4], &Font_5x7, 1);
  109. PCD8544_Putc(HEX_TABLE[y2 & 0x0F], &Font_5x7, 1);
  110. PCD8544_UpdateScreen();
  111. PCD8544_SetContrast(y1, y2);
  112. SYS_Delay(100);
  113. }
  114. SYS_Delay(1000);
  115. }
  116. PCD8544_SetContrast(0x06, 0x20);
  117. PCD8544_Fill(0);
  118. y1 = 47, y2 = 0;
  119. while (y1 < 0xF0)
  120. {
  121. PCD8544_DrawLine(0, y1, 83, y2, 1);
  122. PCD8544_UpdateScreen();
  123. y1 -= 2;
  124. y2 += 2;
  125. SYS_Delay(100);
  126. }
  127. SYS_Delay(1000);
  128. PCD8544_Fill(0);
  129. y1 = 83, y2 = 0;
  130. while (y1 < 0xF0)
  131. {
  132. PCD8544_DrawLine(y1, 0, y2, 47, 1);
  133. PCD8544_UpdateScreen();
  134. y1 -= 2;
  135. y2 += 2;
  136. SYS_Delay(100);
  137. }
  138. SYS_Delay(1000);
  139. PCD8544_Fill(0);
  140. PCD8544_UpdateScreen();
  141. SYS_Delay(1000);
  142. PCD8544_SetBackLightState(HAL_State_OFF);
  143. SYS_Delay(1000);
  144. }
  145. }