1.3寸IIC OLED屏幕显示汉字,图片实验

前言:

本次实验采用IIC接口的OLED屏幕实现,显示汉字和图片的实验,采用STM32RCT6型号单片机,PC12,PC11为模拟IIC模式驱动OLED.本次实验采用PcToLcd软件获取显示的值,本人采用的是:点阵格式-->阴码  取模方式-->列行式  取模走向-->逆向  输出选项-->紧凑型  格式-->C51格式

OLED部分驱动

此模块采用iic接口,iic的代码,我就不在编写了,不过要注意的是,IIC在发送数据后,额外给一个时钟,不处理应答信号,这是OLED的数据手册的规定

void IIC_SendByte(uint8_t bitdata)
{
    for(uint8_t i=0;i<8;i++)
    {
        IIC_W_SDA(bitdata & (0x80>>i));
        IIC_W_SCL(1);
        IIC_W_SCL(0);
    }
    IIC_W_SCL(1);  //在发送完毕后直接给一个额外时钟,不处理应答
    IIC_W_SCL(0);   
}

OLED写指令

void OLED_WriteCmd(uint8_t cmd)
{
    IIC_Start();
    IIC_SendByte(0x78);
    IIC_SendByte(0x00);
    IIC_SendByte(cmd);
    IIC_End(); 
}

OLED写数据

void OLED_WriteCData(uint8_t data)
{
    IIC_Start();
    IIC_SendByte(0x78);
    IIC_SendByte(0x40);
    IIC_SendByte(data);
    IIC_End();    
}

设置横纵坐标

void OLED_SetPos(uint8_t x,uint8_t y)
{
	OLED_WriteCmd(0xB0 | y);					//设置Y位置
	OLED_WriteCmd(0x10 | ((x & 0xF0) >> 4));	//设置X位置高4位
	OLED_WriteCmd(0x00 | (x & 0x0F));
}

1.显示8X16数字

显示代码

//字库代码
static  uint8_t char8x16_1[] = {
    0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00
};


/*
*    x  --> 横坐标值
*    y  --> 纵坐标值
*    ch --> 字库数组
*/
void OLED_SetChar8x16(uint8_t x,uint8_t y,uint8_t *ch)//8*16
{
    uint16_t i;
    if(y==0)//判断Y是否为原点0
    {
        OLED_SetPos(x*8,y);  //绘制高位
        for(i=0;i<8;i++)
        {
            OLED_WriteCData(ch[i]);
        }
        OLED_SetPos(x*8,y+1); //绘制低位
        for(i=8;i<16;i++)
        {
            OLED_WriteCData(ch[i]);
        }
    }
    else  //y不为原点
    {
        OLED_SetPos(x*8,y*2);
        for(i=0;i<8;i++)
        {
            OLED_WriteCData(ch[i]);
        }
        OLED_SetPos(x*8,(y*2)+1);
        for(i=8;i<16;i++)
        {
            OLED_WriteCData(ch[i]);
        }        
    }
}

//主函数代码
int main(void)
{
	OLED_Init();
	while (1)
	{
		OLED_SetChar8x16(0,0,char8x16_1);
		OLED_SetChar8x16(1,0,char8x16_1);
		OLED_SetChar8x16(2,0,char8x16_1);
		OLED_SetChar8x16(3,0,char8x16_1);

		OLED_SetChar8x16(0,1,char8x16_1);
		OLED_SetChar8x16(1,1,char8x16_1);
		OLED_SetChar8x16(2,1,char8x16_1);
		OLED_SetChar8x16(3,1,char8x16_1);


		OLED_SetChar8x16(0,2,char8x16_1);
		OLED_SetChar8x16(1,2,char8x16_1);
		OLED_SetChar8x16(2,2,char8x16_1);
		OLED_SetChar8x16(3,2,char8x16_1);

		OLED_SetChar8x16(0,3,char8x16_1);
		OLED_SetChar8x16(1,3,char8x16_1);
		OLED_SetChar8x16(2,3,char8x16_1);
		OLED_SetChar8x16(3,3,char8x16_1);
	}
}

测试结果:

2.显示16X16汉字

显示代码:

//显示字库
static  uint8_t char16x16_he[] = {
    0x00,0x80,0x60,0xF8,0x07,0x04,0xE4,0x24,0x24,0xE4,0x04,0x04,0xFC,0x04,0x04,0x00,
    0x01,0x00,0x00,0xFF,0x00,0x00,0x0F,0x04,0x04,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00
};

static  uint8_t char16x16_qiu[] = {
    0x20,0x20,0x20,0xFF,0x20,0x20,0x00,0xFC,0x84,0x84,0x84,0x82,0x83,0x82,0x80,0x00,
    0x10,0x30,0x10,0x0F,0x08,0x48,0x40,0x7F,0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x00
};

static  uint8_t char16x16_hong[] = {
    0x12,0x64,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xFC,0x16,0x25,0x84,0xFC,0x00,0x00,
    0x04,0x7E,0x01,0x10,0x30,0x1F,0x08,0x08,0x00,0x13,0x12,0x12,0x52,0x82,0x7E,0x00
};

/*
*    x  --> 横坐标值
*    y  --> 纵坐标值
*    ch --> 字库数组
*/
void OLED_SetChar16x16(uint8_t x,uint8_t y,uint8_t *ch)//16*16
{
    uint16_t i;
    if(y==0)
    {
        OLED_SetPos(x*16,y);
        for(i=0;i<16;i++)
        {
            OLED_WriteCData(ch[i]);
        }

        OLED_SetPos(x*16,y+1);
        for(i=16;i<32;i++)
        {
            OLED_WriteCData(ch[i]);
        }
    }
    else
    {
        OLED_SetPos(x*16,y*2);
        for(i=0;i<16;i++)
        {
            OLED_WriteCData(ch[i]);
        }

        OLED_SetPos(x*16,(y*2)+1);
        for(i=16;i<32;i++)
        {
            OLED_WriteCData(ch[i]);
        }        
    }

}

//主函数
int main(void)
{
	OLED_Init();
	while (1)
	{
		OLED_SetChar16x16(0,0,char16x16_he);
		OLED_SetChar16x16(1,0,char16x16_qiu);
		OLED_SetChar16x16(2,0,char16x16_hong);

		OLED_SetChar16x16(0,1,char16x16_he);
		OLED_SetChar16x16(1,1,char16x16_qiu);
		OLED_SetChar16x16(2,1,char16x16_hong);

		OLED_SetChar16x16(0,2,char16x16_he);
		OLED_SetChar16x16(1,2,char16x16_qiu);
		OLED_SetChar16x16(2,2,char16x16_hong);

		OLED_SetChar16x16(0,3,char16x16_he);
		OLED_SetChar16x16(1,3,char16x16_qiu);
		OLED_SetChar16x16(2,3,char16x16_hong);
	}
}

测试结果:

3.显示128X64位图

显示代码:

//图片数组值
static uint8_t bmp[] = {
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xE0,0xF8,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFE,0xF8,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0xC0,0xE0,0xF8,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xF8,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
    0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
    0x80,0x80,0x80,0x80,0x80,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xE0,0xF8,
    0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xF8,0xE0,0xC0,0xC0,
    0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
    0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
    0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
    0x1F,0x1F,0x3F,0x3F,0x3F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xEF,0xEF,0xEF,
    0xCF,0xCF,0x8F,0x8F,0x8F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,
    0x0F,0x0F,0x0F,0x0F,0x0F,0x07,0x87,0xE7,0xF7,0xFF,0xFF,0xFF,0xFF,0xFF,0x1F,0x0F,
    0x07,0x03,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,
    0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x1F,0x7F,0xFF,0xFF,0xFF,
    0xFF,0xF7,0xF7,0xC7,0x87,0x07,0x07,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,
    0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x8F,0x8F,0x8F,0xCF,0xCF,0xEF,0xEF,0xEF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x7F,0x3F,0x3F,0x3F,0x1F,0x1F,0x1F,0x0F,0x0F,0x07,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0x03,0x03,0x07,
    0x07,0x0F,0x0F,0x0F,0x1F,0x1F,0x1F,0x3F,0x3E,0x7E,0x7C,0x7C,0xFC,0xF8,0xF8,0xF0,
    0xF0,0xF0,0xE0,0xF0,0xF8,0xFE,0xFF,0xFF,0xFF,0x9F,0x8F,0x07,0x03,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x07,
    0x0F,0x1F,0x7F,0xFF,0xFF,0xFF,0xFC,0xF8,0xF0,0xE0,0xF0,0xF0,0xF0,0xF8,0xF8,0xFC,
    0x7C,0x7C,0x7E,0x3E,0x3F,0x1F,0x1F,0x1F,0x0F,0x0F,0x0F,0x07,0x07,0x03,0x03,0x03,
    0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC1,0xF1,
    0xFD,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFE,0xFE,0xFC,
    0xFC,0xF8,0xF8,0xF8,0xF8,0xF0,0xF0,0xE0,0xE0,0xC0,0xC0,0x80,0x80,0x80,0x80,0x80,
    0x80,0xC0,0xC0,0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF8,0xF8,
    0xF8,0xFC,0xFC,0xFC,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF1,0xC1,0x80,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0xF8,0xFE,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x7F,0x7F,0x3F,0x3F,0x3F,0x1F,0x1F,0x3F,
    0x3F,0x3F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
    0xF8,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x80,0xC0,0xF0,0xFC,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0x7F,0x7F,0x7F,0x3F,0x3F,0x3F,0x1F,0x1F,0x0F,0x0F,0x0F,0x07,0x07,0x07,0x03,
    0x03,0x03,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0x03,0x03,0x07,0x07,0x07,
    0x0F,0x0F,0x0F,0x1F,0x1F,0x1F,0x3F,0x3F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFC,0xF0,0xE0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

/*
*    ch  --> 图片数组值
*/
void OLED_SetBmp(uint8_t *ch)
{
    u8 i,j;
    for(i=0;i<8;i++)
    {
        OLED_SetPos(0,i);
        for(j=0;j<128;j++)
        {
            OLED_WriteCData(ch[j+(i*128)]);
        }
    }
}

//主函数
int main(void)
{
	OLED_Init();
	while (1)
	{
		OLED_SetBmp(bmp);
	}
}

测试结果

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在1.3寸IIC通信OLED上绘制波形,可以按照以下步骤进行: 1. 初始化OLED屏幕,设置其分辨率和IIC通信地址。 2. 创建一个缓冲区,在其中绘制波形数据。可以选择使用C语言中的数组或指针来存储波形数据。 3. 将缓冲区中的波形数据转换为屏幕像素坐标,并在屏幕上绘制波形。可以使用C语言中的for循环来遍历波形数据,并使用OLED屏幕的API函数来绘制像素。 4. 在需要更新波形时,清空缓冲区并重新绘制波形数据。 以下是一个示例代码,演示了如何在1.3寸IIC通信OLED上绘制正弦波: ```c #include <stdio.h> #include <math.h> #include "oled.h" #define PI 3.1415926 // 初始化OLED屏幕 void oled_init() { // 设置OLED屏幕的分辨率和IIC通信地址 // ... } // 绘制正弦波 void draw_sine_wave() { int n, x, y; float angle, sin_value; int buffer[128]; // 清空缓冲区 for (n = 0; n < 128; n++) { buffer[n] = 0; } // 生成正弦波数据 for (n = 0; n < 128; n++) { angle = (float)n / 128.0 * 2 * PI; sin_value = sin(angle); buffer[n] = (int)(sin_value * 20 + 20); // 将正弦波数据转换为像素坐标 } // 绘制正弦波 for (n = 0; n < 127; n++) { x = n; y = buffer[n]; oled_draw_pixel(x, y, 1); x = n + 1; y = buffer[n + 1]; oled_draw_line(n, buffer[n], x, y, 1); } } int main() { oled_init(); while (1) { draw_sine_wave(); // 等待一段时间后清空屏幕并重新绘制波形 // ... } return 0; } ``` 需要注意的是,以上代码仅为示例,实际应用中还需要根据具体需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值