用STM32F103C8T6驱动0.96寸OLED显示屏显示折线图,可以用于显示温读,湿度等数据,让数据的变化更直观,界面更炫酷
制作出来的实物图如下:
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "usart.h"
- #include "usmart.h"
- #include "OLED_I2C.h"
- extern unsigned char BMP1[];
- unsigned char BMP2[8192];
- //将128*8的数组转为128*64
- void Change_to_12864(unsigned char BMP[])
- {
- unsigned int j=0;
- unsigned char x,y,n,BMP3[1024];
- for(y=0;y<64;y=y+8)
- {
- for(x=0;x<128;x++)
- {
- BMP3[j]=BMP[j];
- for(n=0;n<8;n++)
- {
- if(BMP3[j]&0x01)
- BMP2[x+(y+n)*128]=1;
- BMP3[j]=BMP3[j]>>1;
- }
- j++;
- }
- }
- }
- //将128*64的数组转为128*8
- void Change_to_1288(unsigned char BMP4[])
- {
- unsigned int j=0;
- unsigned char x,y,n,temp;
-
- for(y=0;y<64;y=y+8)
- {
- for(x=0;x<128;x++)
- {
- for(n=0;n<8;n++)
- {
- temp|=BMP4[x+(y+n)*128]<<7;
- if(n<7)
- temp=temp>>1;
- }
- BMP1[j]=temp;
- temp=0;
- j++;
- }
- }
- }
- //画线
- //x1,y1:起点坐标
- //x2,y2:终点坐标
- void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2)
- {
- u16 t;
- int xerr=0,yerr=0,delta_x,delta_y,distance;
- int incx,incy,uRow,uCol;
- delta_x=x2-x1; //计算坐标增量
- delta_y=y2-y1;
- uRow=x1;
- uCol=y1;
- if(delta_x>0)incx=1; //设置单步方向
- else if(delta_x==0)incx=0;//垂直线
- else {incx=-1;delta_x=-delta_x;}
- if(delta_y>0)incy=1;
- else if(delta_y==0)incy=0;//水平线
- else{incy=-1;delta_y=-delta_y;}
- if( delta_x>delta_y)distance=delta_x; //选取基本增量坐标轴
- else distance=delta_y;
- for(t=0;t<=distance+1;t++ )//画线输出
- {
- BMP2[uRow+uCol*128]=1;//画点
- xerr+=delta_x ;
- yerr+=delta_y ;
- if(xerr>distance)
- {
- xerr-=distance;
- uRow+=incx;
- }
- if(yerr>distance)
- {
- yerr-=distance;
- uCol+=incy;
- }
- }
- }
- //显示全屏
- void OLED_Fast(unsigned char BMP[])
- {
- unsigned int j=0;
- unsigned char x,y;
- for(y=0;y<8;y++)
- {
- OLED_SetPos(0,y);
- for(x=0;x<128;x++)
- {
- WriteDat(BMP[j]);
- j++;
- }
- }
- }
- //显示折线图
- void ZXT(unsigned char num)
- {
- static unsigned char num2=0;
- unsigned char x,y;
- LCD_DrawLine(122,63-num2,126,63-num);//画线
- num2=num;
- Change_to_1288((unsigned char *)BMP2);
- for(x=0;x<124;x++)//画横轴和横轴刻度
- {
- if(x%8==0&&x!=0)
- BMP1[x+7*128]|=0xC0;
- else
- BMP1[x+7*128]|=0x80;
- }
- for(y=0;y<8;y++)//画纵轴
- {
- BMP1[y*128]|=0xFF;
- }
- for(y=0;y<8;y++)//画纵轴刻度
- {
- BMP1[1+y*128]|=0x80;
- }
- for(y=0;y<64;y++)//图像左移4个像素
- {
- for(x=0;x<124;x++)
- {
- BMP2[x+y*128]=BMP2[x+y*128+4];
- }
- }
- for(y=0;y<64;y++)//清除最右边的一条画线
- {
- for(x=124;x<128;x++)
- {
- BMP2[x+y*128]=0;
- }
- }
- }
- //LED状态设置函数
- void led_set(u8 sta)
- {
- LED1=sta;
- }
- //函数参数调用测试函数
- void test_fun()
- {
- OLED_DrawBMP(0,0,128,8,(unsigned char *)BMP1);
- }
- int main(void)
- {
- u8 y;
- u8 s[17]={44,48,55,50,53,31,40,49,48,42,38,38,25,29,46,39,47};
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化为115200
- LED_Init(); //LED端口初始化
- I2C_Configuration();
- OLED_Init();
- usmart_dev.init(SystemCoreClock/1000000); //初始化USMART
- OLED_CLS();
- while(1)
- {
- ZXT(s[y]);//输入0到63的数
- OLED_Fast((unsigned char *)BMP1);
- y++;
- if(y==17)y=0;
- delay_ms(100);
- }
- }
复制代码
所有资料51hei提供下载:
OLED-显示折线图.7z
(209.24 KB, 下载次数: 119)
|