STM32G030F6P6 SSD1306 OLED 画点和绘制图片代码

可以实现游戏动画的画点和绘制图片代码

#ifndef __OLED_H
#define __OLED_H 

#include "i2c.h"


//-----------------²âÊÔLED¶Ë¿Ú¶¨Òå---------------- 




#define OLED_CMD  0	//дÃüÁî
#define OLED_DATA 1	//дÊý¾Ý
#define u8 unsigned char
#define u32 unsigned int

void OLED_ClearPoint(u8 x,u8 y);
void OLED_ColorTurn(u8 i);
void OLED_DisplayTurn(u8 i);

void OLED_WR_Byte(u8 dat,u8 mode);
void OLED_DisPlay_On(void);
void OLED_DisPlay_Off(void);
void OLED_Refresh(void);
void OLED_Clear(void);
void OLED_DrawPoint(u8 x,u8 y);
void OLED_DrawLine(u8 x1,u8 y1,u8 x2,u8 y2);
void OLED_DrawCircle(u8 x,u8 y,u8 r);
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size1);
void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 size1);
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size1);
void OLED_ShowChinese(u8 x,u8 y,u8 num,u8 size1);
void OLED_ScrollDisplay(u8 num,u8 space);
void OLED_WR_BP(u8 x,u8 y);
void OLED_ShowPicture(u8 x0,u8 y0,u8 x1,u8 y1,u8 BMP[]);
void OLED_Init(void);
void OLED_DrawRect(u8 x,u8 y,u8 w,u8 h);
void OLED_DrawBitmap(int x,int y,u8 *bitmap,u8 w,u8 h);
#endif
#include "oled.h"



u8 OLED_GRAM[8][128]={0};
u8 INIT_COMMAND[]={
	0xAE,//--turn off oled panel
	0x00,//---set low column address
	0x10,//---set high column address
	0x40,//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
	0x81,//--set contrast control register
	0xCF,// Set SEG Output Current Brightness
	0xA1,//--Set SEG/Column Mapping     0xa0×óÓÒ·´ÖÃ 0xa1Õý³£
	0xC8,//Set COM/Row Scan Direction   0xc0ÉÏÏ·´Öà 0xc8Õý³£
	0xA6,//--set normal display
	0xA8,//--set multiplex ratio(1 to 64)
	0x3f,//--1/64 duty
	0xD3,//-set display offset	Shift Mapping RAM Counter (0x00~0x3F)
	0x00,//-not offset
	0xd5,//--set display clock divide ratio/oscillator frequency
	0x80,//--set divide ratio, Set Clock as 100 Frames/Sec
	0xD9,//--set pre-charge period
	0xF1,//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
	0xDA,//--set com pins hardware configuration
	0x12,
	0xDB,//--set vcomh
	0x40,//Set VCOM Deselect Level
	0x20,//-Set Page Addressing Mode (0x00/0x01/0x02)
	0x02,//
	0x8D,//--set Charge Pump enable/disable
	0x14,//--set(0x10) disable
	0xA4,// Disable Entire Display On (0xa4/0xa5)
	0xA6,// Disable Inverse Display On (0xa6/a7) 
	0xAF,
};

void OLED_Refresh(void)
{
	u8 i;
	
	for(i=0;i<8;i++)
	{
	
		u8 command[3]={0xb0+i,0x00,0x10};
	 
		HAL_I2C_Mem_Write(&hi2c1,0x78,0x00,I2C_MEMADD_SIZE_8BIT,command,sizeof(command),1000);
		HAL_I2C_Mem_Write(&hi2c1,0x78,0x40,I2C_MEMADD_SIZE_8BIT,OLED_GRAM[i],sizeof(OLED_GRAM[i]),1000);
  }
}

void OLED_Clear(void)
{
	u8 i,n;
	for(i=0;i<8;i++){
		for(n=0;n<128;n++){
			OLED_GRAM[i][n]=0x00;
		}
	}
	OLED_Refresh();//¸üÐÂÏÔʾ
}


void OLED_DrawPoint(u8 x,u8 y)
{
	u8 i,m,n;
	i=y/8;
	m=y%8;
	n=1<<m;
	OLED_GRAM[i][x]|=n;
}


void OLED_DrawRect(u8 x,u8 y,u8 w,u8 h){
	int i,n;
	for(i=y;i<y+h;i++){
		for(n=x;n<x+w;n++){
			OLED_DrawPoint(n,i);
		}
	}
}

void OLED_DrawBitmap_v2(int x,int y,unsigned char *bitmap,unsigned char w,unsigned char h){
	u8 i,n,m,temp;
	
	for(i=0;i<h/8;i++){
		for(n=0;n<w;n++){
			if(x+n>0 && x+n<128){
			temp=bitmap[w*i+n];
			for(m=0;m<8;m++){
				if((temp>>m) & 0x01) OLED_DrawPoint(x+n,y+m+i*8);
			}
		}
		}
	}
		
}


void OLED_Init(void)
{
	            
HAL_I2C_Mem_Write(&hi2c1,0x78,0X00,I2C_MEMADD_SIZE_8BIT,INIT_COMMAND,sizeof(INIT_COMMAND),1000);
	

	OLED_Clear();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值