|
@@ -12,28 +12,39 @@
|
|
|
|
|
|
#include "max7219.h"
|
|
|
|
|
|
-#define SPI_PORT GPIOB
|
|
|
-#define SPI_CS GPIO_Pin_4
|
|
|
-#define SPI_SCK GPIO_Pin_5
|
|
|
-#define SPI_MOSI GPIO_Pin_6
|
|
|
+#define SPI_PORT GPIOC
|
|
|
+#define SPI_SCK GPIO_PIN_5
|
|
|
+#define SPI_MOSI GPIO_PIN_6
|
|
|
#define SPI_PINS (SPI_SCK|SPI_MOSI)
|
|
|
+#define SPI_LOAD_PORT GPIOD
|
|
|
+#define SPI_LOAD GPIO_PIN_3
|
|
|
+
|
|
|
+#define GPIO_SetBits(port, pin) port->ODR |= (uint8_t)pin
|
|
|
+#define GPIO_ResetBits(port, pin) GPIOx->ODR &= (uint8_t)(~PortPins)
|
|
|
|
|
|
void MAX7219_Config(void) {
|
|
|
- SPI_DeInit(SPI1);
|
|
|
- /* Enable clock for SPI */
|
|
|
- CLK_PeripheralClockConfig(CLK_Peripheral_SPI1, ENABLE);
|
|
|
- /* Set the MOSI,MISO and SCK at high level */
|
|
|
- GPIO_ExternalPullUpConfig(SPI_PORT, SPI_PINS, ENABLE);
|
|
|
- /* Configure LOAD pin */
|
|
|
- GPIO_Init(SPI_PORT, SPI_CS, GPIO_Mode_Out_PP_High_Fast);
|
|
|
- GPIO_SetBits(SPI_PORT, SPI_CS);
|
|
|
- /* Init SPI */
|
|
|
- SPI_Init(SPI1, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_2, SPI_Mode_Master,
|
|
|
- SPI_CPOL_High, SPI_CPHA_2Edge, SPI_Direction_1Line_Tx, SPI_NSS_Soft, 0x00);
|
|
|
- /* SPI Enable */
|
|
|
- SPI_Cmd(SPI1, ENABLE);
|
|
|
+ /* Set the MOSI,MISO and SCK at high level */
|
|
|
+ SPI_PORT->CR1 |= (uint8_t)SPI_PINS;
|
|
|
+
|
|
|
+ /* Configure LOAD pin to Push-Pull, High, Fast*/
|
|
|
+ SPI_LOAD_PORT->ODR |= SPI_LOAD;
|
|
|
+ SPI_LOAD_PORT->DDR |= SPI_LOAD;
|
|
|
+ SPI_LOAD_PORT->CR1 |= SPI_LOAD;
|
|
|
+ SPI_LOAD_PORT->CR2 &= (uint8_t)(~(SPI_LOAD));
|
|
|
+
|
|
|
+ /* Enable clock for SPI */
|
|
|
+ CLK->PCKENR1 |= 1 << 0x01;
|
|
|
+
|
|
|
+ /* SPI_MODE_MASTER, SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_2, SPI_CLOCKPOLARITY_HIGH, SPI_CLOCKPHASE_2EDGE */
|
|
|
+ SPI->CR1 = 0x04 | 0x00 | 0x00 | 0x02 | 0x01;
|
|
|
+
|
|
|
+ /* SPI_DATADIRECTION_1LINE_TX, SPI_NSS_SOFT */
|
|
|
+ SPI->CR2 = 0xC0 | 0x02 | SPI_CR2_SSI;
|
|
|
+
|
|
|
+ /* SPI Enable */
|
|
|
+ SPI->CR1 |= SPI_CR1_SPE;
|
|
|
|
|
|
- /* Настройка MAX71219 */
|
|
|
+ /* Настройка MAX71219 */
|
|
|
MAX7219_WriteData(RegDecodeMode, 0x00); // все без BCD декодирования
|
|
|
MAX7219_WriteData(RegScanLimit, MAX7219_DIGITS); // сколько цифр используем
|
|
|
MAX7219_WriteData(RegIntensity, MAX7219_BRIGHT); // яркость из 16
|
|
@@ -44,24 +55,22 @@ void MAX7219_Config(void) {
|
|
|
void MAX7219_WriteData(max7219_reg_t reg, uint8_t data)
|
|
|
{
|
|
|
/*!< Wait wait until the completion of the transfer. */
|
|
|
- while (SPI_GetFlagStatus(SPI1, SPI_FLAG_BSY) == SET) {}
|
|
|
+ while (SPI_GetFlagStatus(SPI_FLAG_BSY) == SET) {}
|
|
|
/* Down LOAD pin */
|
|
|
- GPIO_ResetBits(SPI_PORT, SPI_CS);
|
|
|
+ SPI_LOAD_PORT->ODR &= (uint8_t)(~SPI_LOAD);
|
|
|
|
|
|
/*!< Wait until the transmit buffer is empty */
|
|
|
- while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET) {}
|
|
|
+ while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET) {}
|
|
|
/* Send the register where the data will be stored */
|
|
|
- //SPI_SendData(SPI1, reg);
|
|
|
- SPI1->DR = reg; /* Write in the DR register the data to be sent*/
|
|
|
+ SPI->DR = reg; /* Write in the DR register the data to be sent*/
|
|
|
|
|
|
/*!< Wait until the transmit buffer is empty */
|
|
|
- while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET) {}
|
|
|
+ while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET) {}
|
|
|
/* Send the data to be stored */
|
|
|
- //SPI_SendData(SPI1, data);
|
|
|
- SPI1->DR = data; /* Write in the DR register the data to be sent*/
|
|
|
+ SPI->DR = data; /* Write in the DR register the data to be sent*/
|
|
|
|
|
|
/*!< Wait wait until the completion of the transfer. */
|
|
|
- while (SPI_GetFlagStatus(SPI1, SPI_FLAG_BSY) == SET) {}
|
|
|
+ while (SPI_GetFlagStatus(SPI_FLAG_BSY) == SET) {}
|
|
|
/* Up LOAD pin */
|
|
|
- GPIO_SetBits(SPI_PORT, SPI_CS);
|
|
|
+ SPI_LOAD_PORT->ODR |= SPI_LOAD;
|
|
|
}
|