max7219.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* MAX7219 Interaction Code
  2. * ---------------------------
  3. * For more information see
  4. * http://www.adnbr.co.uk/articles/max7219-and-7-segment-displays
  5. * ----------------------------------------------------------------------------
  6. * "THE BEER-WARE LICENSE" (Revision 42):
  7. * <shilow@ukr.net> wrote this file. As long as you retain this notice you
  8. * can do whatever you want with this stuff. If we meet some day, and you think
  9. * this stuff is worth it, you can buy me a beer in return. Shilov V.N.
  10. * ----------------------------------------------------------------------------
  11. */
  12. #include "max7219.h"
  13. #define SPI_PORT GPIOC
  14. #define SPI_SCK GPIO_PIN_5
  15. #define SPI_MOSI GPIO_PIN_6
  16. #define SPI_PINS (SPI_SCK|SPI_MOSI)
  17. #define SPI_LOAD_PORT GPIOD
  18. #define SPI_LOAD GPIO_PIN_3
  19. #define GPIO_SetBits(port, pin) port->ODR |= (uint8_t)pin
  20. #define GPIO_ResetBits(port, pin) GPIOx->ODR &= (uint8_t)(~PortPins)
  21. void MAX7219_Config(void) {
  22. /* Set the MOSI,MISO and SCK at high level */
  23. SPI_PORT->CR1 |= (uint8_t)SPI_PINS;
  24. /* Configure LOAD pin to Push-Pull, High, Fast*/
  25. SPI_LOAD_PORT->ODR |= SPI_LOAD;
  26. SPI_LOAD_PORT->DDR |= SPI_LOAD;
  27. SPI_LOAD_PORT->CR1 |= SPI_LOAD;
  28. SPI_LOAD_PORT->CR2 &= (uint8_t)(~(SPI_LOAD));
  29. /* Enable clock for SPI */
  30. CLK->PCKENR1 |= 1 << 0x01;
  31. /* SPI_MODE_MASTER, SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_2, SPI_CLOCKPOLARITY_HIGH, SPI_CLOCKPHASE_2EDGE */
  32. SPI->CR1 = 0x04 | 0x00 | 0x00 | 0x02 | 0x01;
  33. /* SPI_DATADIRECTION_1LINE_TX, SPI_NSS_SOFT */
  34. SPI->CR2 = 0xC0 | 0x02 | SPI_CR2_SSI;
  35. /* SPI Enable */
  36. SPI->CR1 |= SPI_CR1_SPE;
  37. /* Настройка MAX71219 */
  38. MAX7219_WriteData(RegDecodeMode, 0x00); // все без BCD декодирования
  39. MAX7219_WriteData(RegScanLimit, MAX7219_DIGITS); // сколько цифр используем
  40. MAX7219_WriteData(RegIntensity, MAX7219_BRIGHT); // яркость из 16
  41. MAX7219_WriteData(RegPower, MAX7219_ON); // включили питание
  42. }
  43. void MAX7219_WriteData(max7219_reg_t reg, uint8_t data)
  44. {
  45. /*!< Wait wait until the completion of the transfer. */
  46. while ((SPI->SR & 0x80) != RESET);
  47. /* Down LOAD pin */
  48. SPI_LOAD_PORT->ODR &= (uint8_t)(~SPI_LOAD);
  49. /*!< Wait until the transmit buffer is empty */
  50. while ((SPI->SR & 0x02) == RESET);
  51. /* Send the register where the data will be stored */
  52. SPI->DR = reg; /* Write in the DR register the data to be sent*/
  53. /*!< Wait until the transmit buffer is empty */
  54. while ((SPI->SR & 0x02) != RESET);
  55. /* Send the data to be stored */
  56. SPI->DR = data; /* Write in the DR register the data to be sent*/
  57. /*!< Wait wait until the completion of the transfer. */
  58. while ((SPI->SR & 0x80) != RESET);
  59. /* Up LOAD pin */
  60. SPI_LOAD_PORT->ODR |= SPI_LOAD;
  61. }