復旦微FM33LG048軟體I2C驅動OLED的demo,幫助新手快速從STM32上手FM33

本文內容為原創

demo任務:利用復旦微FM33LG048外設I2C驅動OLED顯示
demo目的:通過demo熟悉復旦微的I2C、GPIO外設,幫助復旦微的新學習者快速了解復旦微庫函數
demo設備:MCU——FM33LG048;OLED——四針腳OLED,型號:JMD096D.
demo源碼下載鏈接:鏈接:https://pan.baidu.com/s/1PlgHTTuCnY7R7XjDrwusmQ?pwd=tlb9
提取碼:tlb9

移植教程:

#include "main.h"
#include "fm33_assert.h"

#include "OLED.h"
/*********************************************主函數*******************************************/
int main(void)
{
/* Initialize FL Driver Library */
FL_Init();

/* Configure the system clock */
MF_Clock_Init();
OLED_Init(); // 初始化OLED
OLED_ShowString(2,1,"hello"); //顯示hello字符串
}

FL_Init();在復旦微官方例程中已經給出,未修改任何代碼,故這裡不再贅述。

MF_Clock_Init();主要用於初始化各外設以及配置系統時鐘,需要注意,為了方便移植OLED,將MF_Clock_Init()中GPIO初始化相關代碼移動到OLED.c文件中。

demo所用函數如下,直觀展示了與SMT32的對比,方便新手快速從SMT32入手復旦微FM33。

/******************************************************復旦微GPIO初始化************************************************************************************/
void MF_I2C_MASTER_Init(void)void MF_I2C_MASTER_Init(void)
{
FL_GPIO_InitTypeDef GPIO_InitStruct;
FL_I2C_MasterMode_InitTypeDef I2C_InitStruct;
/* PA11 I2C_SCL */
GPIO_InitStruct.pin = FL_GPIO_PIN_11;
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.pull = FL_DISABLE;
GPIO_InitStruct.remapPin = FL_DISABLE;
GPIO_InitStruct.analogSwitch = FL_DISABLE;
FL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* PA12 I2C_SDA */
GPIO_InitStruct.pin = FL_GPIO_PIN_12;
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;

GPIO_InitStruct.pull = FL_DISABLE;
GPIO_InitStruct.remapPin = FL_DISABLE;
GPIO_InitStruct.analogSwitch = FL_DISABLE;
FL_GPIO_Init(GPIOA, &GPIO_InitStruct);
I2C_InitStruct.clockSource = FL_CMU_I2C_CLK_SOURCE_RCHF;
I2C_InitStruct.baudRate = 100000;
FL_I2C_MasterMode_Init(I2C, &I2C_InitStruct); }

以下是STM32的GPIO初始化

/*****************************************************SMT32 GPIO初始化**********************************************/

void OLED_I2C_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);

OLED_W_SCL(1);
OLED_W_SDA(1);
}
需要注意,在復旦微庫函數中,沒有STM32的GPIO_WriteBit(*GPIOx,GPIO_Pin_x,Value)對應的庫函數,需要自己封裝。
以下給出兩者代碼不同之處
/**************************************STM32向特定引腳寫入0,1數據********************************************/
#define OLED_W_SCL(x) GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))
#define OLED_W_SDA(x) GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))​
BitAction為枚舉類型,只能是0或1
/*************************************復旦微向特定引腳寫入0,1數據*********************************************/
void OLED_W_SCL(uint8_t x)
{ if(x==0)
{ FL_GPIO_ResetOutputPin(GPIOA,FL_GPIO_PIN_11); }
else
{ FL_GPIO_SetOutputPin(GPIOA,FL_GPIO_PIN_11); } }

void OLED_W_SDA(uint8_t x)
{ if(x==0)
{ FL_GPIO_ResetOutputPin(GPIOA,FL_GPIO_PIN_12); }
else
{ FL_GPIO_SetOutputPin(GPIOA,FL_GPIO_PIN_12); }}​
完整代碼在附件。
其他部分復旦微FM33與SMT32高度類似,不再展示。

技術文檔

類型標題檔案
軟件Code

★博文內容均由個人提供,與平台無關,如有違法或侵權,請與網站管理員聯繫。

★文明上網,請理性發言。內容一周內被舉報5次,發文人進小黑屋喔~

評論