平台:Code Composer Studio 10.4.0
MSP432P401R SimpleLink™ 微控制器 LaunchPad™ 开发套件
(MSP-EXP432P401R)
API (机翻)
I2C API官方手册
void I2C_cancel (I2C_Handle handle)
取消所有I2C传输。
void I2C_close (I2C_Handle handle)
关闭I2C驱动程序实例
int_fast16_t I2C_control (I2C_Handle handle, uint_fast16_t cmd, void *controlArg)
在驱动程序实例上执行特定的实现特性
void I2C_init (void)
用于初始化I2C驱动程序
I2C_Handle I2C_open (uint_least8_t index, I2C_Params *params)
打开I2C驱动程序实例
void I2C_Params_init (I2C_Params *params)
将I2C_Params结构初始化为其默认值
bool I2C_transfer (I2C_Handle handle, I2C_Transaction *transaction)
对一个从机进行I2C传输
int_fast16_t I2C_transferTimeout (I2C_Handle handle, I2C_Transaction *transaction, uint32_t timeout)
对一个从机进行I2C传输(带超时)
上机实战
引脚配置
I2C引脚配置
工作指示灯LED1引脚配置
文件结构
I2C 初始化、读写函数
myI2C.c
/*
* myI2C.c
*
* Created on: 2021年8月4日
* Author: Royic
*/
#include "./inc/myI2C.h"
#define NULL 0
I2C_Handle hi2c1;
void My_I2C_Init(I2C_Handle *hi2cHandle, uint_least8_t index)
{
// One-time init of I2C driver
I2C_init();
// initialize optional I2C bus parameters
I2C_Params params;
I2C_Params_init(¶ms);
params.bitRate = I2C_400kHz;
// Open I2C bus for usage
*hi2cHandle = I2C_open(index, ¶ms);
}
bool My_I2C_Read(I2C_Handle *hi2cHandle, uint_least8_t slaveAddress, uint8_t *readBuf, size_t readCount)
{
I2C_Transaction transaction = {0};
transaction.slaveAddress = slaveAddress;
transaction.writeBuf = NULL;
transaction.writeCount = 0;
// Read from I2C slave device
transaction.readBuf = readBuf;
transaction.readCount = readCount;
return I2C_transfer(*hi2cHandle, &transaction);
}
bool My_I2C_Write(I2C_Handle *hi2cHandle, uint_least8_t slaveAddress, uint8_t *writeBuf, size_t writeCount)
{
I2C_Transaction transaction = {0};
transaction.slaveAddress = slaveAddress;
transaction.readBuf = NULL;
transaction.readCount = 0;
// Write to I2C slave device
transaction.writeBuf = writeBuf;
transaction.writeCount = writeCount;
return I2C_transfer(*hi2cHandle, &transaction);
}
myI2C.h
/*
* myI2C.h
*
* Created on: 2021年8月4日
* Author: Royic
*/
#ifndef INC_MYI2C_H_
#define INC_MYI2C_H_
#include "./inc/main.h"
// Import I2C Driver definitions
#include void My_I2C_Init(I2C_Handle *hi2cHandle, uint_least8_t index); bool My_I2C_Write(I2C_Handle *hi2cHandle, uint_least8_t slaveAddress, uint8_t *writeBuf, size_t writeCount); bool My_I2C_Read(I2C_Handle *hi2cHandle, uint_least8_t slaveAddress, uint8_t *readBuf, size_t readCount); extern I2C_Handle hi2c1; #endif /* INC_MYI2C_H_ */ OLED初始化和测试代码 main.c /* * ======== main_tirtos.c ======== */ #include "./inc/main.h" /* POSIX Header files */ #include /* RTOS header files */ #include /* Driver configuration */ #include #include #include "./inc/myTask.h" #include "./inc/myI2C.h" #include "./OLED/OLED.h" /* * ======== main ======== */ int main(void) { /* Call driver init functions */ Board_init(); GPIO_init(); My_Task_Init(mainThread, 1, 1024); BIOS_start(); return (0); } /* * ======== mainThread ======== */ void *mainThread(void *arg0) { My_Task_Init(LED_Task, 1, 1024); My_I2C_Init(&hi2c1, I2C1); OLED_Init(); OLED_Clear(); OLED_Display_On(); OLED_ShowString(0, 0, "MSP432P401R OLED", 1); OLED_ShowString(0, 2, " 2021-08-04", 1); while(1) { usleep(1000); } } main.h /* * main.h * * Created on: 2021年8月2日 * Author: Royic */ #ifndef INC_MAIN_H_ #define INC_MAIN_H_ /* For usleep() */ #include #include #include /* Driver configuration */ #include "ti_drivers_config.h" #endif /* INC_MAIN_H_ */ OLED 驱动程序 OLED.c /* * OLED.c * * Created on: Jan 17, 2021 * Author: Royic */ #include "OLED.h" #include #include "OLED_Font.h" //OLED的显存 //存放格式如下. //[0]0 1 2 3 ... 127 //[1]0 1 2 3 ... 127 //[2]0 1 2 3 ... 127 //[3]0 1 2 3 ... 127 //[4]0 1 2 3 ... 127 //[5]0 1 2 3 ... 127 //[6]0 1 2 3 ... 127 //[7]0 1 2 3 ... 127 void Write_IIC_Command(unsigned char I2C_Command)//写命令 { uint8_t Datas[2] = {0}; Datas[0] = OLED_WriteCom_Addr; Datas[1] = I2C_Command; My_I2C_Write(&Scr12864_HI2C, OLED_ADDRESS>>1, Datas, 2); } /* * 第1个参数为I2C操作句柄 第2个参数为从机设备地址 第3个参数为从机寄存器地址 第4个参数为从机寄存器地址长度 第5个参数为发送的数据 第6个参数为传输数据的大小 第7个参数为操作超时时间 */ void Write_IIC_Data(unsigned char IIC_Data)//写数据 { uint8_t Datas[2] = {0}; Datas[0] = OLED_WriteData_Addr; Datas[1] = IIC_Data; My_I2C_Write(&Scr12864_HI2C, OLED_ADDRESS>>1, Datas, 2); } void OLED_WR_Byte(unsigned dat,unsigned cmd) { if(cmd) { Write_IIC_Data(dat); } else { Write_IIC_Command(dat); } } /******************************************** // fill_Picture ********************************************/ void fill_picture(unsigned char fill_Data) { unsigned char m,n; for(m=0;m<8;m++) { OLED_WR_Byte(0xb0+m,0); //page0-page1 OLED_WR_Byte(0x00,0); //low column start address OLED_WR_Byte(0x10,0); //high column start address for(n=0;n<128;n++) { OLED_WR_Byte(fill_Data,1); } } } //坐标设置 void OLED_Set_Pos(unsigned char x, unsigned char y) { OLED_WR_Byte(0xb0+y,OLED_CMD); OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD); OLED_WR_Byte((x&0x0f),OLED_CMD); } //开启OLED显示 void OLED_Display_On(void) { OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令 OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON } //关闭OLED显示 void OLED_Display_Off(void) { OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令 OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF } //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!! void OLED_Clear(void) { uint8_t i,n; for(i=0;i<8;i++) { OLED_WR_Byte (0xb0+i,OLED_CMD); //设置页地址(0~7) OLED_WR_Byte (0x00,OLED_CMD); //设置显示位置—列低地址 OLED_WR_Byte (0x10,OLED_CMD); //设置显示位置—列高地址 for(n=0;n<128;n++) OLED_WR_Byte(0,OLED_DATA); } //更新显示 } void OLED_On(void) { uint8_t i,n; for(i=0;i<8;i++) { OLED_WR_Byte (0xb0+i,OLED_CMD); //设置页地址(0~7) OLED_WR_Byte (0x00,OLED_CMD); //设置显示位置—列低地址 OLED_WR_Byte (0x10,OLED_CMD); //设置显示位置—列高地址 for(n=0;n<128;n++) OLED_WR_Byte(1,OLED_DATA); } //更新显示 } //在指定位置显示一个字符,包括部分字符 //x:0~127 //y:0~63 //mode:0,反白显示;1,正常显示 //size:选择字体 1——8*16 0——6*8 void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size) { unsigned char c=0,i=0; c=chr-' ';//得到偏移后的值 if(x>Max_Column-1) {x=0;y=y+2;} if(Char_Size) { OLED_Set_Pos(x,y); for(i=0;i<8;i++) OLED_WR_Byte(F8X16[c*16+i],OLED_DATA); OLED_Set_Pos(x,y+1); for(i=0;i<8;i++) OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA); } else { OLED_Set_Pos(x,y); for(i=0;i<6;i++) OLED_WR_Byte(F6x8[c][i],OLED_DATA); } } //m^n函数 uint32_t oled_pow(uint8_t m,uint8_t n) { uint32_t result=1; while(n--) result*=m; return result; } //显示2个数字 //x,y :起点坐标 //len :数字的位数 //size:字体大小 //mode:模式 0,填充模式;1,叠加模式 //num:数值(0~4294967295); void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size2) { uint8_t t,temp; uint8_t enshow=0; for(t=0;t temp=(num/oled_pow(10,len-t-1))%10; if(enshow==0&&t<(len-1)) { if(temp==0) { OLED_ShowChar(x+(size2/2)*t,y,' ',size2); continue; } else enshow=1; } OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2); } } //显示一个字符号串 void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size) { unsigned char j=0; while (chr[j]!='