max7219.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* max7219.cpp
  2. * MAX7219 Interaction Code
  3. * ---------------------------
  4. * For more information see
  5. * http://www.adnbr.co.uk/articles/max7219-and-7-segment-displays
  6. *
  7. * Created on: 29 ñ³÷. 2016
  8. * Author: shilov
  9. */
  10. #include <user_config.h>
  11. #include <SmingCore/SmingCore.h>
  12. #include "configuration.h"
  13. #include "max7219.h"
  14. // çàù¸ëêà
  15. #define MAX7219_LOAD1 PinSet(PIN_LOAD)
  16. #define MAX7219_LOAD0 PinRes(PIN_LOAD)
  17. #define MAX7219_ON 0x01
  18. #define MAX7219_OFF 0x00
  19. #define MAX7219_BRIGHT 0x08
  20. // ïåðåâîä ÷èñëà 0-7 â íîìåð èíäèêàòîðà
  21. const uint8_t max7219_dig[8] = {
  22. 0x05,0x01,0x07,0x03,0x04,0x08,0x06,0x02
  23. };
  24. // ïðîãðàììíûé SPI. Âûâîäèò áàéò íà÷èíàÿ ñî ñòàðøåãî áèòà
  25. // âûõîäíûå ïèíû -- SCK, MOSI
  26. static void spi_SendByte (uint8_t DataByte) {
  27. uint8_t i; // ñ÷åò÷èê áèò
  28. for (i=8; i!=0; i--) {
  29. PinRes(PIN_CLK); // âûäàëè ñòðîá
  30. if (bit_is_set(DataByte,7)) { // åñëè áèò 7 == 1
  31. PinSet(PIN_DIN); // MOSI = 1
  32. } else { // åñëè áèò 7 == 0
  33. PinRes(PIN_DIN); // MOSI = 0
  34. }
  35. PinSet(PIN_CLK); // çàù¸ëêíóëè ñòðîá
  36. DataByte <<= 1; // ñäâèã âëåâî íà 1 áèò
  37. }
  38. }
  39. void MAX7219_writeData(uint8_t reg, uint8_t data)
  40. {
  41. MAX7219_LOAD0; // îïóñòèëè çàù¸ëêó
  42. // spi_SendByte(reg);
  43. // spi_SendByte(data);
  44. // software spi
  45. uint8_t i; // ñ÷åò÷èê áèò
  46. uint16_t sdata = (reg<<8) || data;
  47. for (i=16; i!=0; i--) {
  48. PinRes(PIN_CLK); // âûäàëè ñòðîá
  49. if (bit_is_set(sdata,15)) { // åñëè ñòàðøèé áèò == 1
  50. PinSet(PIN_DIN); // MOSI = 1
  51. } else { // åñëè ñòàðøèé áèò == 0
  52. PinRes(PIN_DIN); // MOSI = 0
  53. }
  54. PinSet(PIN_CLK); // çàù¸ëêíóëè ñòðîá
  55. sdata <<= 1; // ñäâèã âëåâî íà 1 áèò
  56. }
  57. MAX7219_LOAD1; // ïîäíÿëè çàù¸ëêó
  58. }
  59. void MAX7219_Init(void) {
  60. // íàñòðîéêà ïèíîâ SPI
  61. pinMode(PIN_LOAD, OUTPUT);
  62. pinMode(PIN_DIN, OUTPUT);
  63. pinMode(PIN_CLK, OUTPUT);
  64. PinSet(PIN_LOAD);
  65. PinSet(PIN_DIN);
  66. PinSet(PIN_CLK);
  67. // Íàñòðîéêà MAX71219
  68. MAX7219_writeData(MAX7219_MODE_DECODE, 0x00); // âñå áåç BCD äåêîäèðîâàíèÿ
  69. MAX7219_writeData(MAX7219_MODE_SCAN_LIMIT, MAX7219_DIGITS - 1); // Scan limit runs from 0.
  70. MAX7219_writeData(MAX7219_MODE_INTENSITY, MAX7219_BRIGHT); // ÿðêîñòü èç 16
  71. MAX7219_writeData(MAX7219_MODE_POWER,MAX7219_ON); // âêëþ÷èëè ïèòàíèå
  72. }