|
【小华HC32F448测评】关于HC32f448的I2C学习及硬件I2C驱动OLED
[复制链接]
本帖最后由 学学学学学学学 于 2023-8-25 17:24 编辑
- 全局宏定义:
-
- #define I2C_SDA_PORT (GPIO_PORT_D)//类似HAL库的配置
- #define I2C_SDA_PIN (GPIO_PIN_00)
- #define I2C_SCL_PORT (GPIO_PORT_D)
- #define I2C_SCL_PIN (GPIO_PIN_01)
-
- #define I2C_SDA_FUNC (GPIO_FUNC_50) //pin脚功能配置号
- #define I2C_SCL_FUNC (GPIO_FUNC_51) //详情见数据手册引脚配置页
-
-
- #define I2C_UNIT (CM_I2C2) //硬件I2C地址
- #define I2C_FCG (FCG1_PERIPH_I2C2) //开启时钟
-
-
- #define I2C_BAUDRATE (400000UL) //I2C波特率配置
- #define I2C_TIMEOUT (0x64U) //
数据手册中说明了 i2c 2 的配置功能号是SDA为50,SCL为51,如果是I2C1 就是48 和49,对应时钟是FG1
- 硬件 I2C的GOIO口配置函数:主要需要看数据手册中关于引脚配置页的引脚功能定义
-
- /**
- * @brief I2C_Init0 配置gpio口功能,主要是功能号I2C_SCL_FUNC和SDA_FUNC,要按照数据手册写
- * @param
- * @retval LL_OK
- */
- static void I2C_GPIO_Init(void)
- {
- stc_gpio_init_t stcGpioInit;
-
- (void)GPIO_StructInit(&stcGpioInit);
-
- (void)GPIO_Init(I2C_SCL_PORT, I2C_SCL_PIN, &stcGpioInit);
- (void)GPIO_Init(I2C_SDA_PORT, I2C_SDA_PIN, &stcGpioInit);
- GPIO_SetFunc(I2C_SCL_PORT, I2C_SCL_PIN, I2C_SCL_FUNC);
- GPIO_SetFunc(I2C_SDA_PORT, I2C_SDA_PIN, I2C_SDA_FUNC);
-
- FCG_Fcg1PeriphClockCmd(I2C_FCG, ENABLE);
-
- (void)I2C_Init(I2C_UNIT);
-
- }
- I2C 2初始化函数
-
- int32_t I2C_2_Init(CM_I2C_TypeDef *I2Cx)
- {
- int32_t i32Ret;
- float32_t fErr;
- stc_i2c_init_t stcI2cInit;
-
- I2C_DeInit(I2Cx);
- (void)I2C_StructInit(&stcI2cInit);
- stcI2cInit.u32Baudrate = I2C_BAUDRATE;
- stcI2cInit.u32SclTime = 0U;
- stcI2cInit.u32ClockDiv = I2C_CLK_DIV2;
- i32Ret = I2C_Init(I2Cx, &stcI2cInit, &fErr);
-
- if (LL_OK == i32Ret) {
- I2C_BusWaitCmd(I2Cx, ENABLE);
- }
-
- I2C_Cmd(I2Cx, ENABLE);
- return i32Ret;
- }
-
- main函数
- /**
- * @brief Main function
- * @param None
- */
- int32_t main(void)
- {
-
-
- LL_PERIPH_WE(LL_PERIPH_ALL);
-
-
-
-
-
- BSP_IO_Init();
-
-
- BSP_LED_Init();
-
-
- DDL_PrintfInit(BSP_PRINTF_DEVICE, BSP_PRINTF_BAUDRATE, BSP_PRINTF_Preinit);
-
-
- I2C_GPIO_Init();
- LED_Init();
- oled_init();
-
-
- LL_PERIPH_WP(LL_PERIPH_ALL);
-
- oled_cls();
- DDL_DelayMS(200);
-
- for (;;) {
-
-
-
-
- show_title();
-
-
-
-
- }
- }
- oled.c和oled.h文件中I2C的驱动程序函数
- #include "oledfont.h"
- #include "oled.h"
- #include "hc32_ll_i2c.h"
- /**
- * @defgroup I2C_Configuration
- * @{
- */
- #define I2C_BAUDRATE (400000UL)
- #define I2C_TIMEOUT (0x64U)
- #define OLED_ADD (0x3c)
- #define DATA_Len (0x02)
- #define I2C_DIR_TX2 (0x00)
-
- /**
- * @brief OLED_WR_Byte 写入一字节/两字节
- * @param *dat
- * @retval 返回 LL_OK
- *用法举例:OLED_WR_Byte(buf),buf定义:可定义为数组,如 int8_t buf[2];
- */
- int32_t OLED_WR_Byte(uint8_t *dat)
- {
- int32_t i32Ret;
-
- I2C_SWResetCmd(I2C_UNIT,ENABLE);
- I2C_SWResetCmd(I2C_UNIT,DISABLE);
-
- i32Ret = I2C_Start(I2C_UNIT, I2C_TIMEOUT);
-
- if(LL_OK==i32Ret)
- {
-
- i32Ret=I2C_TransAddr(I2C_UNIT,(uint8_t)OLED_ADD,I2C_DIR_TX2,I2C_TIMEOUT );
-
- if(LL_OK==i32Ret)
- {
-
-
- i32Ret=I2C_TransData(I2C_UNIT,dat,DATA_Len ,I2C_TIMEOUT );
-
-
-
-
-
-
-
- }
-
-
-
-
-
-
-
- }
- (void)I2C_Stop(I2C_UNIT,I2C_TIMEOUT );
- return i32Ret;
-
- }
- /**
- * @brief oled_cmd 写入oled指令
- * @param i2c_cmd,一个8位无符号整数
- * @retval void
- */
- static void oled_cmd(uint8_t i2c_cmd)
- {
-
- uint8_t u8TXBuf[2];
- u8TXBuf[0]=0x00;
- u8TXBuf[1]=i2c_cmd;
- OLED_WR_Byte(u8TXBuf);
-
-
-
-
-
-
-
-
- }
-
- /**
- * @brief oled_data�
- * @param i2c_data 传数据
- * @retval
- */
- static void oled_data(uint8_t i2c_data)
- {
- uint8_t u8TXBuf[2];
- u8TXBuf[0]=0x40;
- u8TXBuf[1]=i2c_data;
- OLED_WR_Byte(u8TXBuf);
-
-
-
-
-
-
-
-
- }
-
- oled初始化
-
- /**
- * @brief oled_init 初始化oled
- * @param
- * @retval
- */
- void oled_init(void)
- {
-
- DDL_DelayMS(800);
-
- oled_cmd(0xAE);
-
- oled_cmd(0x20);
- oled_cmd(0x10);
- oled_cmd(0xb0);
-
- oled_cmd(0xc8);
- oled_cmd(0x00);
- oled_cmd(0x10);
-
- oled_cmd(0x40);
- oled_cmd(0x81);
- oled_cmd(0xff);
- oled_cmd(0xa1);
-
- oled_cmd(0xa6);
- oled_cmd(0xa8);
- oled_cmd(0x3F);
- oled_cmd(0xa4);
- oled_cmd(0xd3);
- oled_cmd(0x00);
-
- oled_cmd(0xd5);
- oled_cmd(0xf0);
- oled_cmd(0xd9);
- oled_cmd(0x22);
-
- oled_cmd(0xda);
- oled_cmd(0x12);
- oled_cmd(0xdb);
- oled_cmd(0x20);
- oled_cmd(0x8d);
-
- oled_cmd(0x14);
- oled_cmd(0xaf);
- }
效果:
其他主要的文件和函数放在附件里了,有需要可看
|
|