ssd1306_stc8h3k.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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: SSD1306/SSD1315 12864 OLED
  16. * Board: STC8H3K32
  17. *
  18. * P32 -> SCL
  19. * P33 -> SDA
  20. * GND -> GND
  21. * 3.3V -> VCC
  22. */
  23. #include "fw_hal.h"
  24. #include "ssd1306.h"
  25. void I2C_Init(void)
  26. {
  27. // Master mode
  28. I2C_SetWorkMode(I2C_WorkMode_Master);
  29. /**
  30. * I2C clock = FOSC / 2 / (__prescaler__ * 2 + 4)
  31. * SSD1306 works with i2c clock up to 1.3 MHz, beyond this value, display may fail.
  32. */
  33. I2C_SetClockPrescaler(0x10);
  34. // Switch alternative port
  35. I2C_SetPort(I2C_AlterPort_P32_P33);
  36. // Start I2C
  37. I2C_SetEnabled(HAL_State_ON);
  38. }
  39. void GPIO_Init(void)
  40. {
  41. // SDA
  42. GPIO_P3_SetMode(GPIO_Pin_3, GPIO_Mode_InOut_QBD);
  43. // SCL
  44. GPIO_P3_SetMode(GPIO_Pin_2, GPIO_Mode_Output_PP);
  45. }
  46. int main(void)
  47. {
  48. int y1, y2;
  49. uint8_t d1, d2;
  50. SYS_SetClock();
  51. GPIO_Init();
  52. I2C_Init();
  53. SSD1306_Init();
  54. while(1)
  55. {
  56. SSD1306_DrawLine(0, 0, 127, 0, 1);
  57. SSD1306_DrawLine(0, 0, 0, 63, 1);
  58. SSD1306_DrawLine(127, 0, 127, 63, 1);
  59. SSD1306_DrawLine(0, 63, 127, 63, 1);
  60. SSD1306_UpdateScreen(); // display
  61. SYS_Delay(1000);
  62. SSD1306_Fill(0);
  63. SSD1306_ToggleInvert(); // Invert display
  64. SSD1306_UpdateScreen();
  65. SYS_Delay(1000);
  66. SSD1306_ToggleInvert(); // Invert display
  67. SSD1306_UpdateScreen();
  68. SYS_Delay(1000);
  69. y1 = 64, y2 = 0;
  70. while (y1 > 0)
  71. {
  72. SSD1306_DrawLine(0, y1, 127, y2, 1);
  73. SSD1306_UpdateScreen();
  74. y1 -= 2;
  75. y2 += 2;
  76. }
  77. SYS_Delay(1000);
  78. SSD1306_Fill(0);
  79. y1 = 127, y2 = 0;
  80. while (y1 > 0)
  81. {
  82. SSD1306_DrawLine(y1, 0, y2, 63, 1);
  83. SSD1306_UpdateScreen();
  84. y1 -= 2;
  85. y2 += 2;
  86. }
  87. SYS_Delay(1000);
  88. }
  89. }