STMicroelectronics STM8 Manual page 109

Table of Contents

Advertisement

Now for the SPI configuration part. Assuming you know how to interpret timing diagrams and
understand device datasheets, SPI configuration should not be a problem. Here in the case of
MAX7219, we have configured the SPI port as to send MSB first, we have also selected a fast peripheral
clock, we have made the STM8 SPI act like a master with proper SPI mode and we have set the sort of
duplex. The last two parameters are not important as we are not using hardware slave select option
and CRC feature.
void SPI_setup(void)
{
SPI_DeInit();
SPI_Init(SPI_FIRSTBIT_MSB,
SPI_BAUDRATEPRESCALER_2,
SPI_MODE_MASTER,
SPI_CLOCKPOLARITY_HIGH,
SPI_CLOCKPHASE_1EDGE,
SPI_DATADIRECTION_1LINE_TX,
SPI_NSS_SOFT,
0x00);
SPI_Cmd(ENABLE);
}
The timing diagram of MAX7219 suggests that CS should be low in order for MAX7219 to receive data.
Then it suggests that when idle, clock must be high, data transfer is done on every rising edge of the
clock. All these are what required for setting up the SPI hardware.
void MAX72xx_write(unsigned char address, unsigned char value)
{
while(SPI_GetFlagStatus(SPI_FLAG_BSY));
GPIO_WriteLow(CS_port, CS_pin);
SPI_SendData(address);
while(!SPI_GetFlagStatus(SPI_FLAG_TXE));
SPI_SendData(value);
while(!SPI_GetFlagStatus(SPI_FLAG_TXE));
GPIO_WriteHigh(CS_port, CS_pin);
}

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the STM8 and is the answer not in the manual?

Questions and answers

Subscribe to Our Youtube Channel

Table of Contents

Save PDF