[FM33LG0 Series Development Board Review] 05.I2C, SPI
[Copy link]
1. Introduction
The FM33LG0 series MCU has at most 1 I2C interface supporting master-slave mode and 3 SPI interfaces supporting master-slave mode; I2C supports 7-bit or 10-bit slave addresses, and supports three transmission speeds: 100kbps standard mode, 400kbps fast mode and 1Mbps ultra-fast mode, and supports wake-up function. The only regret is that it does not support multi-host mode; for the SPI interface, full-duplex 4-wire serial transceiver, the data word length can be customized to 8/16/24/32 bits, which is convenient for application in different occasions; there is a half-duplex 4-wire serial synchronous transceiver mode dedicated to TFT screen drive; for the two signal pins MISO and MOSI, the pin order can be exchanged through software configuration. In case the hardware engineer accidentally connects the wrong line, there is no need to worry about re-modifying the hardware, which is very convenient.
2. Implement functions
On the development board, 1 I2C and 2 SPI interfaces have been brought out through pin headers. We use the I2C interface and SPI1 interface to drive two OLED screens, one OLED screen is an I2C communication interface, and the other OLED screen is an SPI interface. Combined with the RTC function implemented in the previous post, the clock animation and the current date and time information are displayed;
3. Main code
OLED1:I2C
/*******************************************************************************
* [url=home.php?mod=space&uid=159083]@brief[/url] * @param
* @retval
* [url=home.php?mod=space&uid=1020061]@attention[/url] *******************************************************************************/
void OLED1_InitI2C(void)
{
FL_GPIO_InitTypeDef GPIO_InitStruct;
FL_I2C_MasterMode_InitTypeDef I2C_InitStruct;
FL_GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.pin = FL_GPIO_PIN_11 | FL_GPIO_PIN_12;
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_OPENDRAIN;
GPIO_InitStruct.pull = FL_DISABLE;
FL_GPIO_Init(GPIOA, &GPIO_InitStruct);
I2C_InitStruct.clockSource = FL_CMU_I2C_CLK_SOURCE_APBCLK;
I2C_InitStruct.baudRate = 400000;
FL_I2C_MasterMode_Init(I2C, &I2C_InitStruct);
}
……
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void OLED1_DrawImage(uint8_t Width, uint8_t Height, const uint8_t *ImageData)
{
uint8_t x = 0, y = 0;
uint8_t Buffer[0x80];
uint16_t Index = 0;
for(y = 0; y < Height / 8; y++)
{
OLED1_SetPosition(32, y);
for(x = 0; x < Width; x++)
{
Buffer[x] = ImageData[Index++];
}
OLED1_WriteBuffer(Buffer, Width);
}
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void OLED1_Init(void)
{
OLED1_InitI2C();
OLED1_InitCFG();
OLED1_Clear(0x00);
TASK_Append(TASK_ID_OLED1, OLED1_Handler, 100);
}
……
OLED2:SPI
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void OLED2_InitSPI(void)
{
FL_DMA_InitTypeDef DMA_InitStruct;
FL_GPIO_InitTypeDef GPIO_InitStruct;
FL_SPI_InitTypeDef SPI_InitStruct;
FL_SPI_StructInit(&SPI_InitStruct);
SPI_InitStruct.baudRate = FL_SPI_BAUDRATE_DIV2;
SPI_InitStruct.bitOrder = FL_SPI_BIT_ORDER_MSB_FIRST;
SPI_InitStruct.clockPhase = FL_SPI_PHASE_EDGE1;
SPI_InitStruct.clockPolarity = FL_SPI_POLARITY_NORMAL;
SPI_InitStruct.dataWidth = FL_SPI_DATA_WIDTH_8B;
SPI_InitStruct.mode = FL_SPI_WORK_MODE_MASTER;
SPI_InitStruct.softControl = FL_ENABLE;
SPI_InitStruct.transferMode = FL_SPI_TRANSFER_MODE_FULL_DUPLEX;
FL_SPI_Init(SPI1, &SPI_InitStruct);
FL_DMA_StructInit(&DMA_InitStruct);
DMA_InitStruct.circMode = FL_DISABLE;
DMA_InitStruct.dataSize = FL_DMA_BANDWIDTH_8B;
DMA_InitStruct.direction = FL_DMA_DIR_PERIPHERAL_TO_RAM;
DMA_InitStruct.memoryAddressIncMode = FL_DMA_MEMORY_INC_MODE_INCREASE;
DMA_InitStruct.priority = FL_DMA_PRIORITY_HIGH ;
DMA_InitStruct.periphAddress = FL_DMA_PERIPHERAL_FUNCTION1;
FL_DMA_Init(DMA, &DMA_InitStruct, FL_DMA_CHANNEL_3);
FL_DMA_StructInit(&DMA_InitStruct);
DMA_InitStruct.circMode = FL_DISABLE;
DMA_InitStruct.dataSize = FL_DMA_BANDWIDTH_8B;
DMA_InitStruct.direction = FL_DMA_DIR_RAM_TO_PERIPHERAL;
DMA_InitStruct.memoryAddressIncMode = FL_DMA_MEMORY_INC_MODE_INCREASE;
DMA_InitStruct.priority = FL_DMA_PRIORITY_HIGH ;
DMA_InitStruct.periphAddress = FL_DMA_PERIPHERAL_FUNCTION2;
FL_DMA_Init(DMA, &DMA_InitStruct, FL_DMA_CHANNEL_4);
FL_GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.pin = FL_GPIO_PIN_2 | FL_GPIO_PIN_3 |
FL_GPIO_PIN_4 |FL_GPIO_PIN_5;
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
FL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
……
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void OLED2_Init(void)
{
OLED2_InitSPI();
OLED2_HardwareReset();
OLED2_ConfigureREG();
OLED2_Clear(0x00);
TASK_Append(TASK_ID_OLED2, OLED2_Handler, 500);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void OLED_ShowChar(uint8_t X, uint8_t Y, char ch, uint8_t Size)
{
uint8_t Data = 0;
for(uint8_t i = 0; i < Size; i++)
{
OLED2_SetPosition(X + i, Y);
if(Size == 12)
{
Data = ASCII_1206[ch - 0x20][i];
}
else
{
Data = ASCII_1608[ch - 0x20][i];
}
OLED2_WriteDAT(Data);
}
}
……
4. Operation effect
5. Engineering Source
Project_I2C_SPI.zip
(379.68 KB, downloads: 22)
|