提供新唐M031KG6AE用IIC写的oled的驱动
时间: 2023-09-16 19:16:22 浏览: 158
oled iic 驱动代码
以下是基于新唐M031KG6AE芯片的IIC OLED驱动程序示例,供参考:
```c
#include "NuMicro.h"
#define OLED_I2C_ADDR 0x3C
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define OLED_CMD_MODE 0x00
#define OLED_DAT_MODE 0x40
#define OLED_CMD_SET_CONTRAST 0x81
#define OLED_CMD_DISPLAY_ALL_ON_RESUME 0xA4
#define OLED_CMD_DISPLAY_ALL_ON 0xA5
#define OLED_CMD_NORMAL_DISPLAY 0xA6
#define OLED_CMD_INVERT_DISPLAY 0xA7
#define OLED_CMD_DISPLAY_OFF 0xAE
#define OLED_CMD_DISPLAY_ON 0xAF
#define OLED_CMD_SET_DISPLAY_OFFSET 0xD3
#define OLED_CMD_SET_COM_PINS 0xDA
#define OLED_CMD_SET_VCOM_DETECT 0xDB
#define OLED_CMD_SET_DISPLAY_CLOCK_DIV 0xD5
#define OLED_CMD_SET_PRECHARGE 0xD9
#define OLED_CMD_SET_MULTIPLEX 0xA8
#define OLED_CMD_SET_LOW_COLUMN 0x00
#define OLED_CMD_SET_HIGH_COLUMN 0x10
#define OLED_CMD_SET_START_LINE 0x40
#define OLED_CMD_MEMORY_MODE 0x20
#define OLED_CMD_COM_SCAN_INC 0xC0
#define OLED_CMD_COM_SCAN_DEC 0xC8
#define OLED_CMD_SEG_REMAP 0xA0
#define OLED_CMD_CHARGE_PUMP 0x8D
#define OLED_CMD_SET_COL_ADDR 0x21
#define OLED_CMD_SET_PAGE_ADDR 0x22
const uint8_t u8InitData[] =
{
OLED_CMD_DISPLAY_OFF, // display off
OLED_CMD_SET_DISPLAY_CLOCK_DIV, // set clock frequency
0x80, // suggested ratio
OLED_CMD_SET_MULTIPLEX, // set multiplex ratio
OLED_HEIGHT - 1, // height - 1
OLED_CMD_SET_DISPLAY_OFFSET, // set display offset
0x00,
OLED_CMD_SET_START_LINE | 0x00, // set start line
OLED_CMD_CHARGE_PUMP, // set charge pump
0x14, // enable charge pump
OLED_CMD_MEMORY_MODE, // set memory addressing mode
0x00,
OLED_CMD_SEG_REMAP | 0x01, // set segment re-map 0 to 127
OLED_CMD_COM_SCAN_DEC, // set COM output scan direction
OLED_CMD_SET_COM_PINS, // set COM pins hardware configuration
0x12,
OLED_CMD_SET_CONTRAST, // set contrast
0xCF,
OLED_CMD_SET_PRECHARGE, // set pre-charge period
0xF1,
OLED_CMD_SET_VCOM_DETECT, // set Vcomh deselect level
0x40,
OLED_CMD_DISPLAY_ALL_ON_RESUME, // set display resume normal
OLED_CMD_NORMAL_DISPLAY, // set normal display
OLED_CMD_DISPLAY_ON, // display on
};
void OLED_WriteCmd(uint8_t u8Cmd)
{
uint8_t u8Data[2];
u8Data[0] = OLED_CMD_MODE;
u8Data[1] = u8Cmd;
I2C_WriteMultiBytes(I2C0, OLED_I2C_ADDR, u8Data, 2);
}
void OLED_WriteData(uint8_t u8Data)
{
uint8_t u8Tmp[2];
u8Tmp[0] = OLED_DAT_MODE;
u8Tmp[1] = u8Data;
I2C_WriteMultiBytes(I2C0, OLED_I2C_ADDR, u8Tmp, 2);
}
void OLED_SetPos(uint8_t u8X, uint8_t u8Y)
{
OLED_WriteCmd(OLED_CMD_SET_COL_ADDR);
OLED_WriteCmd(u8X);
OLED_WriteCmd(OLED_WIDTH - 1);
OLED_WriteCmd(OLED_CMD_SET_PAGE_ADDR);
OLED_WriteCmd(u8Y);
OLED_WriteCmd(7);
}
void OLED_Fill(uint8_t u8Data)
{
uint8_t u8Page, u8Col;
for(u8Page = 0; u8Page < 8; u8Page++)
{
OLED_SetPos(0, u8Page);
for(u8Col = 0; u8Col < OLED_WIDTH; u8Col++)
{
OLED_WriteData(u8Data);
}
}
}
void OLED_Init(void)
{
uint8_t u8Idx;
GPIO_SetMode(PB, BIT14, GPIO_MODE_OUTPUT);
GPIO_SetMode(PB, BIT15, GPIO_MODE_OUTPUT);
PB14 = 1;
PB15 = 1;
CLK_EnableModuleClock(I2C0_MODULE);
CLK_SetModuleClock(I2C0_MODULE, CLK_I2C_SRC_HXT, 0);
CLK_SetModuleClock(I2C0_MODULE, CLK_I2C_SRC_HXT, NULL);
CLK_SetModuleClock(I2C0_MODULE, CLK_I2C_SRC_HXT, NULL);
I2C_Open(I2C0, 400000);
I2C_EnableInt(I2C0);
NVIC_EnableIRQ(I2C0_IRQn);
for(u8Idx = 0; u8Idx < sizeof(u8InitData); u8Idx++)
{
OLED_WriteCmd(u8InitData[u8Idx]);
}
OLED_Fill(0x00);
}
void OLED_ShowChar(uint8_t u8X, uint8_t u8Y, uint8_t u8Char)
{
uint8_t u8Page, u8Col;
u8Char -= 32;
for(u8Page = 0; u8Page < 8; u8Page++)
{
OLED_SetPos(u8X, u8Y + u8Page);
for(u8Col = 0; u8Col < 6; u8Col++)
{
if(Font6x8[u8Char][u8Col] & (1 << u8Page))
{
OLED_WriteData(0xff);
}
else
{
OLED_WriteData(0x00);
}
}
}
}
void OLED_ShowStr(uint8_t u8X, uint8_t u8Y, const char *pStr)
{
while(*pStr)
{
OLED_ShowChar(u8X, u8Y, *pStr++);
u8X += 6;
}
}
void I2C0_IRQHandler(void)
{
uint32_t u32Status;
u32Status = I2C_GET_STATUS(I2C0);
if(I2C_GET_TIMEOUT_FLAG(I2C0))
{
I2C_ClearTimeoutFlag(I2C0);
}
else
{
switch(u32Status)
{
case 0x08: // START has been transmitted
case 0x10: // Repeat START has been transmitted
I2C_SET_DATA(I2C0, (OLED_I2C_ADDR << 1) | 0x00);
I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI);
break;
case 0x18: // SLA+W has been transmitted; ACK has been received
case 0x28: // DATA has been transmitted; ACK has been received
break;
case 0x20: // SLA+W has been transmitted; NOT ACK has been received
case 0x30: // DATA has been transmitted; NOT ACK has been received
case 0x38: // Arbitration lost in SLA+R/W or NOT ACK bit
default:
break;
}
}
I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI);
}
```
以上代码利用IIC接口驱动了一块OLED显示屏。在初始化时,设置了一系列的初始化命令,包括时钟频率、高低位列地址、显示偏移等参数,通过IIC接口逐个发送给OLED屏幕。在显示时,通过OLED_ShowChar函数显示单个字符,通过OLED_ShowStr函数显示一个字符串。
阅读全文