max7219_8x8led_stc8g1k08.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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: MAX7219 8x8 LED Matrix
  16. * Board: STC8G1K08, STC8G1K08A
  17. *
  18. * P5.4 -> DIN
  19. * P3.2 -> SCLK
  20. * P5.5 -> CS
  21. */
  22. /**
  23. *
  24. * PlatformIO configuration example
  25. [env:stc8g1k08]
  26. platform = intel_mcs51
  27. board = stc8g1k08
  28. build_flags =
  29. -D__CONF_FOSC=36864000UL
  30. -D__CONF_MCU_MODEL=MCU_MODEL_STC8G1K08
  31. -D__CONF_CLKDIV=0x00
  32. -D__CONF_IRCBAND=0x01
  33. -D__CONF_VRTRIM=0x20
  34. -D__CONF_IRTRIM=0xA3
  35. -D__CONF_LIRTRIM=0x03
  36. upload_protocol = custom
  37. upload_port = /dev/ttyUSB0
  38. upload_speed = 115200
  39. upload_flags =
  40. -p$UPLOAD_PORT
  41. -s$UPLOAD_SPEED
  42. -e
  43. upload_command = ${platformio.packages_dir}/tool-stc8prog/stc8prog $UPLOAD_FLAGS -f $SOURCE
  44. */
  45. #include "fw_hal.h"
  46. #define MAX7219_CS P55
  47. #define DECODE_MODE 0x09
  48. #define INTENSITY 0x0A
  49. #define SCAN_LIMIT 0x0B
  50. #define SHUT_DOWN 0x0C
  51. #define DISPLAY_TEST 0x0F
  52. const uint8_t numbers[]={
  53. 0x00,0x00,0x7C,0xC6,0xC6,0xCE,0xD6,0xD6, // -0-.
  54. 0xE6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,
  55. 0x00,0x00,0x18,0x38,0x78,0x18,0x18,0x18, // -1-
  56. 0x18,0x18,0x18,0x7E,0x00,0x00,0x00,0x00,
  57. 0x00,0x00,0x7C,0xC6,0x06,0x0C,0x18,0x30, // -2-
  58. 0x60,0xC0,0xC6,0xFE,0x00,0x00,0x00,0x00,
  59. 0x00,0x00,0x7C,0xC6,0x06,0x06,0x3C,0x06, // -3-
  60. 0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00,
  61. 0x00,0x00,0x0C,0x1C,0x3C,0x6C,0xCC,0xFE, // -4-
  62. 0x0C,0x0C,0x0C,0x1E,0x00,0x00,0x00,0x00,
  63. 0x00,0x00,0xFE,0xC0,0xC0,0xC0,0xFC,0x0E, // -5-
  64. 0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00,
  65. 0x00,0x00,0x38,0x60,0xC0,0xC0,0xFC,0xC6, // -6-
  66. 0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,
  67. 0x00,0x00,0xFE,0xC6,0x06,0x06,0x0C,0x18, // -7-
  68. 0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00,
  69. 0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7C,0xC6, // -8-
  70. 0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,
  71. 0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7E,0x06, // -9-
  72. 0x06,0x06,0x0C,0x78,0x00,0x00,0x00,0x00};
  73. void MAX7219_write(uint8_t addr, uint8_t dat)
  74. {
  75. MAX7219_CS = 0;
  76. SPI_TxRx(addr);
  77. SPI_TxRx(dat);
  78. MAX7219_CS = 1;
  79. }
  80. void MAX7219_init(void)
  81. {
  82. MAX7219_write(SHUT_DOWN, 0x01); // 0x00:shutdown, 0x01:normal
  83. MAX7219_write(DECODE_MODE, 0x00); // Bypass code B decoder, no-decode operation
  84. MAX7219_write(SCAN_LIMIT, 0x07); // Scan-limit, 0:1-digit, 1:2-digits, ... 7:8-digits
  85. MAX7219_write(INTENSITY, 0x01); // 0x00:min, 0xFF:max
  86. MAX7219_write(DISPLAY_TEST, 0x00); // 0x00:normal, 0x01:test mode
  87. }
  88. void SPI_init(void)
  89. {
  90. // MAX7219 can work with frequency up to 20MHz
  91. SPI_SetClockPrescaler(SPI_ClockPreScaler_4);
  92. // Clock idles low
  93. SPI_SetClockPolarity(HAL_State_OFF);
  94. // Data transfer is driven by lower SS pin
  95. SPI_SetClockPhase(SPI_ClockPhase_LeadingEdge);
  96. // MSB first
  97. SPI_SetDataOrder(SPI_DataOrder_MSB);
  98. // Define the output pins
  99. SPI_SetPort(SPI_AlterPort_P12P54_P13_P14_P15);
  100. // Ignore SS pin, use MSTR to swith between master/slave mode
  101. SPI_IgnoreSlaveSelect(HAL_State_ON);
  102. // Master mode
  103. SPI_SetMasterMode(HAL_State_ON);
  104. // Start SPI
  105. SPI_SetEnabled(HAL_State_ON);
  106. }
  107. void main()
  108. {
  109. uint8_t pos = 0, size = sizeof(numbers), i, j;
  110. SYS_SetClock();
  111. // Configure GPIO pins before SPI and device
  112. // SCLK(P32), CS(P55)
  113. GPIO_P3_SetMode(GPIO_Pin_2, GPIO_Mode_Output_PP);
  114. GPIO_P5_SetMode(GPIO_Pin_5, GPIO_Mode_Output_PP);
  115. // MOSI(P54)
  116. GPIO_P5_SetMode(GPIO_Pin_4, GPIO_Mode_InOut_QBD);
  117. // Configure SPI and device
  118. SPI_init();
  119. MAX7219_init();
  120. while(1)
  121. {
  122. for (i = 0; i < 8; i++)
  123. {
  124. j = (pos + i) % size;
  125. MAX7219_write(i + 1, numbers[j]);
  126. }
  127. pos = (pos + 1) % size;
  128. SYS_Delay(100);
  129. }
  130. }