max7219.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 GPIOB
  14. #define SPI_CS GPIO_Pin_4
  15. #define SPI_SCK GPIO_Pin_5
  16. #define SPI_MOSI GPIO_Pin_6
  17. #define SPI_PINS (SPI_SCK|SPI_MOSI)
  18. void MAX7219_Config(void) {
  19. SPI_DeInit(SPI1);
  20. /* Enable clock for SPI */
  21. CLK_PeripheralClockConfig(CLK_Peripheral_SPI1, ENABLE);
  22. /* Set the MOSI,MISO and SCK at high level */
  23. GPIO_ExternalPullUpConfig(SPI_PORT, SPI_PINS, ENABLE);
  24. /* Configure LOAD pin */
  25. GPIO_Init(SPI_PORT, SPI_CS, GPIO_Mode_Out_PP_High_Fast);
  26. GPIO_SetBits(SPI_PORT, SPI_CS);
  27. /* Init SPI */
  28. SPI_Init(SPI1, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_2, SPI_Mode_Master,
  29. SPI_CPOL_High, SPI_CPHA_2Edge, SPI_Direction_1Line_Tx, SPI_NSS_Soft, 0x00);
  30. /* SPI Enable */
  31. SPI_Cmd(SPI1, ENABLE);
  32. /* Настройка MAX71219 */
  33. MAX7219_WriteData(RegDecodeMode, 0x00); // все без BCD декодирования
  34. MAX7219_WriteData(RegScanLimit, MAX7219_DIGITS); // сколько цифр используем
  35. MAX7219_WriteData(RegIntensity, MAX7219_BRIGHT); // яркость из 16
  36. MAX7219_WriteData(RegPower, MAX7219_ON); // включили питание
  37. }
  38. void MAX7219_WriteData(max7219_reg_t reg, uint8_t data)
  39. {
  40. /*!< Wait wait until the completion of the transfer. */
  41. while (SPI_GetFlagStatus(SPI1, SPI_FLAG_BSY) == SET) {}
  42. /* Down LOAD pin */
  43. GPIO_ResetBits(SPI_PORT, SPI_CS);
  44. /*!< Wait until the transmit buffer is empty */
  45. while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET) {}
  46. /* Send the register where the data will be stored */
  47. //SPI_SendData(SPI1, reg);
  48. SPI1->DR = reg; /* Write in the DR register the data to be sent*/
  49. /*!< Wait until the transmit buffer is empty */
  50. while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET) {}
  51. /* Send the data to be stored */
  52. //SPI_SendData(SPI1, data);
  53. SPI1->DR = data; /* Write in the DR register the data to be sent*/
  54. /*!< Wait wait until the completion of the transfer. */
  55. while (SPI_GetFlagStatus(SPI1, SPI_FLAG_BSY) == SET) {}
  56. /* Up LOAD pin */
  57. GPIO_SetBits(SPI_PORT, SPI_CS);
  58. }