用STM32F103C8T6控制0.96寸OLED显示屏显示矩形,直线和圆,动态生成,是移植的正点原子TFT显示屏的程序
制作出来的实物图如下:
单片机源程序如下:
- #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;
- }
- }
- }
- //画矩形
- //(x1,y1),(x2,y2):矩形的对角坐标
- void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2)
- {
- LCD_DrawLine(x1,y1,x2,y1);
- LCD_DrawLine(x1,y1,x1,y2);
- LCD_DrawLine(x1,y2,x2,y2);
- LCD_DrawLine(x2,y1,x2,y2);
- }
- //在指定位置画一个指定大小的圆
- //(x,y):中心点
- //r :半径
- void LCD_Draw_Circle(u16 x0,u16 y0,u8 r)
- {
- int a,b,x,y;
- int di;
- a=0;b=r;
- di=3-(r<<1); //判断下个点位置的标志
- while(a<=b)
- {
- BMP2[x0+a+(y0-b)*128]=1; //5
- BMP2[x0+b+(y0-a)*128]=1; //0
- BMP2[x0+b+(y0+a)*128]=1; //4
- BMP2[x0+a+(y0+b)*128]=1; //6
- BMP2[x0-a+(y0+b)*128]=1; //1
- BMP2[x0-b+(y0+a)*128]=1;
- BMP2[x0-a+(y0-b)*128]=1; //2
- BMP2[x0-b+(y0-a)*128]=1; //7
- a++;
- //使用Bresenham算法画圆
- if(di<0)di +=4*a+6;
- else
- {
- di+=10+4*(a-b);
- b--;
- }
- }
- }
- //显示全屏
- 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++;
- }
- }
- }
- //LED状态设置函数
- void led_set(u8 sta)
- {
- LED1=sta;
- }
- //函数参数调用测试函数
- void test_fun()
- {
- OLED_DrawBMP(0,0,128,8,(unsigned char *)BMP1);
- }
- int main(void)
- {
- u8 h,y,x,z;
- 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();
- h=12;
- while(1)
- {
- for(y=0;y<64;y++)
- {
- for(x=0;x<128;x++)
- {
- BMP2[x+y*128]=0;
- }
- }
- LCD_Draw_Circle(32+z,32,12);
- LCD_Draw_Circle(96-z,32,h);
- LCD_DrawRectangle(56,48,72,56);
- Change_to_1288((unsigned char *)BMP2);
- OLED_Fast((unsigned char *)BMP1);
- if(z<8)
- z=z+1;
- else
- z=z-1;
- if(z==8)z=16;
- if(z==10)z=0;
- }
- }
复制代码
所有资料51hei提供下载:
OLED-画圆矩形直线.7z
(209.67 KB, 下载次数: 204)
|