/****************************************Copyright (c)**************************************************** ** ** File name: lcd.c ** Created by: XiaoYi ** Created date: 2020-10-16 ** Version: v1.0 ** Descriptions: The original ** Link address: https://blog.csdn.net/weixin_45006076 ** *********************************************************************************************************/ #include #include "main.h" #include "i2c.h" #include "oled.h" #include "codetab.h" void oled_write_cmd(unsigned char cmd)//写命令 { HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x00,I2C_MEMADD_SIZE_8BIT,&cmd,1,0x100); } void oled_write_data(unsigned char data)//写数据 { HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x40,I2C_MEMADD_SIZE_8BIT,&data,1,0x100); } void oled_set_pos(unsigned char x, unsigned char y) //设置起始点坐标 { oled_write_cmd(0xb0+y); oled_write_cmd(((x&0xf0)>>4)|0x10); oled_write_cmd((x&0x0f)|0x01); } void oled_fill(unsigned char fill_Data)//全屏填充 { unsigned char m,n; for(m=0;m<8;m++) { oled_write_cmd(0xb0+m); //page0-page1 oled_write_cmd(0x00); //low column start address oled_write_cmd(0x10); //high column start address for(n=0;n<128;n++) { oled_write_data(fill_Data); } } } void oled_clear_screen(void)//清屏 { oled_fill(0x00); } //-------------------------------------------------------------- // Prototype : void oled_wakeup(void) // Calls : // Parameters : none // Description : 将OLED从休眠中唤醒 //-------------------------------------------------------------- void oled_wakeup(void) { oled_write_cmd(0X8D); //设置电荷泵 oled_write_cmd(0X14); //开启电荷泵 oled_write_cmd(0XAF); //OLED唤醒 } //-------------------------------------------------------------- // Prototype : void oled_sleep(void) // Calls : // Parameters : none // Description : 让OLED休眠 -- 休眠模式下,OLED功耗不到10uA //-------------------------------------------------------------- void oled_sleep(void) { oled_write_cmd(0X8D); //设置电荷泵 oled_write_cmd(0X10); //关闭电荷泵 oled_write_cmd(0XAE); //OLED休眠 } //-------------------------------------------------------------- // Prototype : void OLED_ShowChar(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize) // Calls : // Parameters : x,y -- 起始点坐标(x:0~127, y:0~7); ch[] -- 要显示的字符串; TextSize -- 字符大小(1:6*8 ; 2:8*16) // Description : 显示codetab.h中的ASCII字符,有6*8和8*16可选择 //-------------------------------------------------------------- void oled_show_string(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize) { unsigned char c = 0,i = 0,j = 0; switch(TextSize) { case 1: { while(ch[j] != '\0') { c = ch[j] - 32; if(x > 126) { x = 0; y++; } oled_set_pos(x,y); for(i=0;i<6;i++) oled_write_data(F6x8[c][i]); x += 6; j++; } }break; case 2: { while(ch[j] != '\0') { c = ch[j] - 32; if(x > 120) { x = 0; y++; } oled_set_pos(x,y); for(i=0;i<8;i++) oled_write_data(F8X16[c*16+i]); oled_set_pos(x,y+1); for(i=0;i<8;i++) oled_write_data(F8X16[c*16+i+8]); x += 8; j++; } }break; } } //-------------------------------------------------------------- // Prototype : void oled_show_chinese(unsigned char x, unsigned char y, unsigned char N) // Calls : // Parameters : x,y -- 起始点坐标(x:0~127, y:0~7); N:汉字在codetab.h中的索引 // Description : 显示codetab.h中的汉字,16*16点阵 //-------------------------------------------------------------- void oled_show_chinese(unsigned char x, unsigned char y, unsigned char N) { unsigned char wm=0; unsigned int adder=32*N; oled_set_pos(x , y); for(wm = 0;wm < 16;wm++) { oled_write_data(F16x16[adder]); adder += 1; } oled_set_pos(x,y + 1); for(wm = 0;wm < 16;wm++) { oled_write_data(F16x16[adder]); adder += 1; } } //-------------------------------------------------------------- // Prototype : void oled_draw_bmp(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[]); // Calls : // Parameters : x0,y0 -- 起始点坐标(x0:0~127, y0:0~7); x1,y1 -- 起点对角线(结束点)的坐标(x1:1~128,y1:1~8) // Description : 显示BMP位图 //-------------------------------------------------------------- void oled_draw_bmp(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char *bmp) { unsigned int j=0; unsigned char x,y; if(y1%8==0) y = y1/8; else y = y1/8 + 1; for(y=y0;y