max7219.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_SS)
  16. #define MAX7219_LOAD0 PinRes(PIN_SS)
  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. const uint8_t digitsInUse = 8;
  25. // ïðîãðàììíûé SPI. Âûâîäèò áàéò íà÷èíàÿ ñî ñòàðøåãî áèòà
  26. // âûõîäíûå ïèíû -- SCK, MOSI
  27. static void spi_SendByte (uint8_t DataByte) {
  28. uint8_t i; // ñ÷åò÷èê áèò
  29. for (i=8; i!=0; i--) {
  30. PinRes(PIN_SCK); // âûäàëè ñòðîá
  31. if (bit_is_set(DataByte,7)) { // åñëè áèò 7 == 1
  32. PinSet(PIN_MOSI); // MOSI = 1
  33. } else { // åñëè áèò 7 == 0
  34. PinRes(PIN_MOSI); // MOSI = 0
  35. }
  36. PinSet(PIN_SCK); // çàù¸ëêíóëè ñòðîá
  37. DataByte <<= 1; // ñäâèã âëåâî íà 1 áèò
  38. }
  39. }
  40. void MAX7219_writeData(uint8_t data_register, uint8_t data)
  41. {
  42. MAX7219_LOAD0;
  43. // Send the register where the data will be stored
  44. spi_SendByte(data_register);
  45. // Send the data to be stored
  46. spi_SendByte(data);
  47. MAX7219_LOAD1;
  48. }
  49. void MAX7219_Init(void) {
  50. // íàñòðîéêà ïèíîâ SPI
  51. pinMode(PIN_SS, OUTPUT);
  52. pinMode(PIN_MOSI, OUTPUT);
  53. pinMode(PIN_SCK, OUTPUT);
  54. PinSet(PIN_SS);
  55. PinSet(PIN_MOSI);
  56. PinSet(PIN_SCK);
  57. // Íàñòðîéêà MAX71219
  58. MAX7219_writeData(MAX7219_MODE_DECODE, 0x00); // âñå áåç BCD äåêîäèðîâàíèÿ
  59. MAX7219_writeData(MAX7219_MODE_SCAN_LIMIT, digitsInUse - 1); // Scan limit runs from 0.
  60. MAX7219_writeData(MAX7219_MODE_INTENSITY, MAX7219_BRIGHT); // ÿðêîñòü èç 16
  61. MAX7219_writeData(MAX7219_MODE_POWER,MAX7219_ON); // âêëþ÷èëè ïèòàíèå
  62. }