#include "STC8XXXX.H" #include "oled.h" #include "oledfont.h" #include #include #include unsigned char _buf[128*8]=0; unsigned char code height=8; unsigned char code width=128; bit _OLED_Reverse = 0; bit _OLED_Overlap = 1; static char __x, __y; void delay_ms(unsigned int ms) { unsigned int a; while(ms) { a=1800; while(a--); ms--; } return; } //反显函数 void OLED_ColorTurn(u8 i) { if(i==0) { OLED_WR_Byte(0xA6,OLED_CMD);//正常显示 } if(i==1) { OLED_WR_Byte(0xA7,OLED_CMD);//反色显示 } } //屏幕旋转180度 void OLED_DisplayTurn(u8 i) { if(i==0) { OLED_WR_Byte(0xC8,OLED_CMD);//正常显示 OLED_WR_Byte(0xA1,OLED_CMD); } if(i==1) { OLED_WR_Byte(0xC0,OLED_CMD);//反转显示 OLED_WR_Byte(0xA0,OLED_CMD); } } //延时 void IIC_delay(void) { _nop_(); } //起始信号 void I2C_Start(void) { OLED_SDA_Set(); OLED_SCL_Set(); IIC_delay(); OLED_SDA_Clr(); IIC_delay(); OLED_SCL_Clr(); } //结束信号 void I2C_Stop(void) { OLED_SDA_Clr(); OLED_SCL_Set(); IIC_delay(); OLED_SDA_Set(); } //等待信号响应 void I2C_WaitAck(void) //测数据信号的电平 { OLED_SDA_Set(); IIC_delay(); OLED_SCL_Set(); IIC_delay(); OLED_SCL_Clr(); IIC_delay(); } //写入一个字节 void Send_Byte(u8 dat) { u8 i; for(i=0;i<8;i++) { OLED_SCL_Clr();//将时钟信号设置为低电平 if(dat&0x80)//将dat的8位从最高位依次写入 { OLED_SDA_Set(); } else { OLED_SDA_Clr(); } IIC_delay(); OLED_SCL_Set(); IIC_delay(); OLED_SCL_Clr(); dat<<=1; } } //发送一个字节 //向SSD1306写入一个字节。 //mode:数据/命令标志 0,表示命令;1,表示数据; void OLED_WR_Byte(u8 dat,u8 mode) { I2C_Start(); Send_Byte(0x78); I2C_WaitAck(); if(mode){Send_Byte(0x40);} else{Send_Byte(0x00);} I2C_WaitAck(); Send_Byte(dat); I2C_WaitAck(); I2C_Stop(); } //坐标设置 void OLED_Set_Pos(u8 x, u8 y) { OLED_WR_Byte(0xb0+y,OLED_CMD); OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD); OLED_WR_Byte((x&0x0f),OLED_CMD); } //开启OLED显示 void OLED_Display_On(void) { OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令 OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON } //关闭OLED显示 void OLED_Display_Off(void) { OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令 OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF } //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!! void OLED_Clear(void) { u8 i,n; for(i=0;i<8;i++) { OLED_WR_Byte (0xb0+i,OLED_CMD); //设置页地址(0~7) OLED_WR_Byte (0x00,OLED_CMD); //设置显示位置—列低地址 OLED_WR_Byte (0x10,OLED_CMD); //设置显示位置—列高地址 for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA); } //更新显示 } //在指定位置显示一个字符,包括部分字符 //x:0~127 //y:0~63 //sizey:选择字体 6x8 8x16 void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 sizey) { u8 c=0,sizex=sizey/2; u16 i=0,size1; if(sizey==8)size1=6; else size1=(sizey/8+((sizey%8)?1:0))*(sizey/2); c=chr-' ';//得到偏移后的值 OLED_Set_Pos(x,y); for(i=0;i width) { __x = 0; __y += 1; } if (__y > height * 8) { __y = 0; } pBuf = &_buf[(y >> 3) * width + x]; mask = 1 << (y & 7); if (!color) { *pBuf++ &= ~mask; } else { *pBuf++ |= mask; } } void _swap_char(unsigned char* a,unsigned char* b) { unsigned char tmp = *a; *a = *b; *b = tmp; } /*======================================================== *????: ?0.96Oled??? *????: x1 -> ??x1?? y1 -> ??y1?? * x2 -> ??x1?? y2 -> ??y1?? *????: ? *========================================================*/ void OLED_DrawLine(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2, unsigned char color) { unsigned char i = 0; char DeltaY = 0,DeltaX = 0; float k = 0,b = 0; if(x1>x2) { i = x2;x2 = x1;x1 = i; i = y2;y2 = y1;y1 = i; i = 0; } DeltaY = y2 - y1; DeltaX = x2 - x1; if(DeltaX == 0) { if(y1<=y2) { for(y1;y1<=y2;y1++) { OLED_DrawPixel(x1,y1,color); } }else if(y1>y2) { for(y2;y2<=y1;y2++) { OLED_DrawPixel(x1,y2,color); } } } else if(DeltaY == 0) { for(x1;x1<=x2;x1++) { OLED_DrawPixel(x1,y1,color); } } else { k = ((float)DeltaY)/((float)DeltaX); b = y2 - k * x2; if((k>-1&k<1)) { for(x1;x1<=x2;x1++) { OLED_DrawPixel(x1,(int)(k * x1 + b),color); } }else if((k>=1)|(k<=-1)) { if(y1<=y2) { for(y1;y1<=y2;y1++) { OLED_DrawPixel((int)((y1 - b) / k),y1,color); } }else if(y1>y2) { for(y2;y2<=y1;y2++) { OLED_DrawPixel((int)((y2 - b) / k),y2,color); } } } } } void OLED_display(void) { OLED_DrawBMP(0,0,128,64,_buf); //OLED_DrawBMP(0,6,width[1],heigth[1]*8,_buf1); } void OLED_display_clear() { memset(_buf, 0x00, width * height); } void OLED_Draw_Byte(unsigned char *pBuf, unsigned char mask, unsigned char offset, bit reserve_hl) { if (_OLED_Overlap) { if (_OLED_Reverse) *pBuf |= ~mask; else *pBuf |= mask; } else { if (_OLED_Reverse) { /* 保留高位 Reserve upper */ if (reserve_hl) { *pBuf &= (~mask) | (0xFF << (8 - offset)); *pBuf |= (~mask) & (0xFF >> offset); } /* 保留低位 Reserve lower */ else { *pBuf &= (~mask) | (0xFF >> (8 - offset)); *pBuf |= (~mask) & (0xFF << offset); } } else { /* 保留高位 Reserve upper */ if (reserve_hl) { *pBuf &= mask | (0xFF << (8 - offset)); *pBuf |= mask & (0xFF >> offset); } /* 保留低位 Reserve lower */ else { *pBuf &= mask | (0xFF >> (8 - offset)); *pBuf |= mask & (0xFF << offset); } } } } void OLED_DrawChar(unsigned char x, unsigned char y, unsigned char chr) { unsigned char c; unsigned char i; unsigned char mask; unsigned char *pBuf; unsigned char offset; offset = y & 7; c = chr - ' '; pBuf = &_buf[(y >> 3) * width + x]; for (i = 0; i < 8; i++) { mask = asc2_1608[c][i] << offset; OLED_Draw_Byte(pBuf++, mask, offset, 0); } if (offset && y < 56 - 8) { pBuf = &_buf[((y >> 3) + 1) * 100 + x]; for (i = 0; i < 6; i++) { mask = asc2_0806[c][i] >> (8 - offset); OLED_Draw_Byte(pBuf++, mask, 8 - offset, 1); } } } void OLED_Set_Posi(unsigned char x, unsigned char y) { __x = x; __y = y; } void OLED_DrawNum(unsigned char digit, unsigned char len) { unsigned char t, i, temp; unsigned char enshow = 0; i = 0; for (t = 0; t < len; t++) { temp = (digit / oled_pow(10, len - t - 1)) % 10; if (enshow == 0 && t < (len - 1)) { if (temp == 0) { i++; continue; } else enshow = 1; } if (__x > 100 - 6) { __x = 0; __y += 8; } if (__y > 56 - 8) { __y = 0; } OLED_DrawChar(__x + (6) * (t - i), __y, temp + '0'); } __x += len; } void OLED_DrawBMP_2(u8 x0, u8 page0, u8 xsize, u8 ysize, u8 *BMP) { u16 i, j, p; for(j = page0; j < page0+ysize/8; j++) { for(i = x0; i < x0+xsize; i++) { _buf[i+(j)*width]=BMP[(i-x0)+(j-page0)*xsize]; } } }