0.96寸OLED STM32F103 I2C程序
时间: 2023-06-20 22:10:04 浏览: 139
以下是使用STM32F103单片机和0.96寸OLED屏幕的I2C程序。
首先,需要确保你已经连接好了OLED屏幕和STM32F103单片机,然后将以下代码复制到你的STM32F103开发板的IAR或Keil等开发环境中进行编译:
```c
#include "stm32f10x.h"
#include "stm32f10x_i2c.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#define OLED_I2C_ADDR 0x78
void OLED_I2C_Init(void);
void OLED_I2C_WriteCmd(uint8_t I2C_Command);
void OLED_I2C_WriteData(uint8_t I2C_Data);
void OLED_Init(void);
void OLED_Clear(void);
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size);
void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no);
void OLED_Set_Pos(uint8_t x, uint8_t y);
void Delay(__IO uint32_t nCount);
int main(void)
{
OLED_I2C_Init();
OLED_Init();
OLED_Clear();
OLED_ShowString(0,2,"Hello,World!",16);
OLED_ShowString(0,4,"这是一行中文测试",16);
while(1);
}
void OLED_I2C_Init(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB,&GPIO_InitStructure);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 400000;
I2C_Init(I2C1,&I2C_InitStructure);
I2C_Cmd(I2C1,ENABLE);
}
void OLED_I2C_WriteCmd(uint8_t I2C_Command)
{
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,OLED_I2C_ADDR,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,0x00);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1,I2C_Command);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1,ENABLE);
Delay(200);
}
void OLED_I2C_WriteData(uint8_t I2C_Data)
{
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,OLED_I2C_ADDR,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,0x40);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1,I2C_Data);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1,ENABLE);
Delay(200);
}
void OLED_Init(void)
{
Delay(200);
OLED_I2C_WriteCmd(0xAE);
OLED_I2C_WriteCmd(0x00);
OLED_I2C_WriteCmd(0x10);
OLED_I2C_WriteCmd(0x40);
OLED_I2C_WriteCmd(0x81);
OLED_I2C_WriteCmd(0xCF);
OLED_I2C_WriteCmd(0xA1);
OLED_I2C_WriteCmd(0xC8);
OLED_I2C_WriteCmd(0xA6);
OLED_I2C_WriteCmd(0xA8);
OLED_I2C_WriteCmd(0x3F);
OLED_I2C_WriteCmd(0xD3);
OLED_I2C_WriteCmd(0x00);
OLED_I2C_WriteCmd(0xD5);
OLED_I2C_WriteCmd(0x80);
OLED_I2C_WriteCmd(0xD9);
OLED_I2C_WriteCmd(0xF1);
OLED_I2C_WriteCmd(0xDA);
OLED_I2C_WriteCmd(0x12);
OLED_I2C_WriteCmd(0xDB);
OLED_I2C_WriteCmd(0x40);
OLED_I2C_WriteCmd(0x20);
OLED_I2C_WriteCmd(0x02);
OLED_I2C_WriteCmd(0x8D);
OLED_I2C_WriteCmd(0x14);
OLED_I2C_WriteCmd(0xA4);
OLED_I2C_WriteCmd(0xA6);
OLED_I2C_WriteCmd(0xAF);
OLED_Clear();
}
void OLED_Clear(void)
{
uint8_t i,j;
for(i=0;i<8;i++)
{
OLED_I2C_WriteCmd(0xb0+i);
OLED_I2C_WriteCmd(0x00);
OLED_I2C_WriteCmd(0x10);
for(j=0;j<128;j++)
{
OLED_I2C_WriteData(0);
}
}
}
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
{
uint8_t c = 0,i = 0,j = 0;
while (chr[j]!='\0')
{
c = chr[j] - 32;
if(x > 120)
{
x = 0;
y++;
}
OLED_Set_Pos(x,y);
for(i=0;i<Char_Size;i++)
{
OLED_I2C_WriteData(F8X16[c*16+i]);
}
OLED_Set_Pos(x,y+1);
for(i=0;i<Char_Size;i++)
{
OLED_I2C_WriteData(F8X16[c*16+i+8]);
}
x += Char_Size;
j++;
}
}
void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no)
{
uint8_t t,adder = 0;
OLED_Set_Pos(x , y);
for(t=0;t<16;t++)
{
OLED_I2C_WriteData(CHS[2*no][t]);
adder += 1;
}
OLED_Set_Pos(x,y+1);
for(t=0;t<16;t++)
{
OLED_I2C_WriteData(CHS[2*no+1][t]);
adder += 1;
}
}
void OLED_Set_Pos(uint8_t x, uint8_t y)
{
OLED_I2C_WriteCmd(0xb0+y);
OLED_I2C_WriteCmd(((x&0xf0)>>4)|0x10);
OLED_I2C_WriteCmd((x&0x0f)|0x01);
}
void Delay(__IO uint32_t nCount)
{
for(;nCount!=0;nCount--);
}
```
在上面的代码中,我们首先定义了OLED屏幕的I2C地址,然后实现了一些函数来初始化OLED屏幕、清屏、显示字符串、显示汉字等。在主函数中,我们初始化OLED屏幕并显示一些测试信息。
请注意,这只是一个示例程序,你需要根据你的具体需求进行修改。
阅读全文