max7219.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 data_register, uint8_t data)
  40. {
  41. MAX7219_LOAD0;
  42. // Send the register where the data will be stored
  43. spi_SendByte(data_register);
  44. // Send the data to be stored
  45. spi_SendByte(data);
  46. MAX7219_LOAD1;
  47. }
  48. void MAX7219_Init(void) {
  49. // íàñòðîéêà ïèíîâ SPI
  50. pinMode(PIN_LOAD, OUTPUT);
  51. pinMode(PIN_DIN, OUTPUT);
  52. pinMode(PIN_CLK, OUTPUT);
  53. PinSet(PIN_LOAD);
  54. PinSet(PIN_DIN);
  55. PinSet(PIN_CLK);
  56. // Íàñòðîéêà MAX71219
  57. MAX7219_writeData(MAX7219_MODE_DECODE, 0x00); // âñå áåç BCD äåêîäèðîâàíèÿ
  58. MAX7219_writeData(MAX7219_MODE_SCAN_LIMIT, MAX7219_DIGITS - 1); // Scan limit runs from 0.
  59. MAX7219_writeData(MAX7219_MODE_INTENSITY, MAX7219_BRIGHT); // ÿðêîñòü èç 16
  60. MAX7219_writeData(MAX7219_MODE_POWER,MAX7219_ON); // âêëþ÷èëè ïèòàíèå
  61. }