鼓捣了一晚上搞出一个基于原子哥LCD库V3.0的画坐标系图象的几个函数。
使用方法如下:
- void LCD_Plot_Init(u16 width, u16 height, u16 xUnit, u16 yUnit, u16 xOrigin, u16 yOrigin, u8 GridSwitch); //最后一个参数GridSwitch用来设置是否画栅格
- void RefreshPlot(u16 NewValue); //直接输入一个数就可以画图了
支持函数有
- typedef struct tagPlot_Init
{
u16 xOrigin;
u16 yOrigin;
u16 xUnit;
u16 yUnit;
u16 height;
u16 width;
u8 GridSwitch;
} Plot_Init;
void DrawFrame(Plot_Init PLOT_INIT); //用来绘制坐标系的外框
void DP(u16 x,u16 y,u16 color); //快速画点 其实就是给LCD_Fast_DrawPoint()套了个壳
以上部分定义在lcd.h中 在lcd.c中定义了两个全局变量,数组PLOT_NUM[]用来存放显示数据和坐标系参数结构体PLOT_INIT
具体实现代码如下
void DP(u16 x,u16 y,u16 color)
{
LCD_Fast_DrawPoint(x,y,color);
}
void LCD_Plot_Init(u16 width, u16 height, u16 xUnit, u16 yUnit, u16 xOrigin, u16 yOrigin, u8 GridSwitch){//×ø±êϵ³õʼ»¯
u16 i;
PLOT_INIT.GridSwitch=GridSwitch;
PLOT_INIT.height=height;
PLOT_INIT.width=width;
PLOT_INIT.xOrigin=xOrigin;
PLOT_INIT.yOrigin=yOrigin;
PLOT_INIT.xUnit=xUnit;
PLOT_INIT.yUnit=yUnit;
return;
}
void DrawFrame(Plot_Init PLOT_INIT){
u16 i;
//PLOT_INIT.width=width;
if(PLOT_INIT.GridSwitch){
for(i=PLOT_INIT.width/PLOT_INIT.xUnit;i>0;i--){
POINT_COLOR=LIGHTBLUE;
LCD_DrawLine(PLOT_INIT.xOrigin+i*PLOT_INIT.xUnit,PLOT_INIT.yOrigin,PLOT_INIT.xOrigin+i*PLOT_INIT.xUnit,PLOT_INIT.yOrigin-PLOT_INIT.height);
POINT_COLOR=WHITE;
LCD_Draw_Circle(PLOT_INIT.xOrigin+i*PLOT_INIT.xUnit,PLOT_INIT.yOrigin,2);
}
for(i=PLOT_INIT.height/PLOT_INIT.yUnit;i>0;i--){
POINT_COLOR=LIGHTBLUE;
LCD_DrawLine(PLOT_INIT.xOrigin,PLOT_INIT.yOrigin-i*PLOT_INIT.yUnit,PLOT_INIT.xOrigin+PLOT_INIT.width,PLOT_INIT.yOrigin-i*PLOT_INIT.yUnit);
POINT_COLOR=WHITE;
LCD_Draw_Circle(PLOT_INIT.xOrigin,PLOT_INIT.yOrigin-i*PLOT_INIT.yUnit,2);
}
}
//Draw Frame Ready
LCD_DrawLine(PLOT_INIT.xOrigin,PLOT_INIT.yOrigin,PLOT_INIT.xOrigin,PLOT_INIT.yOrigin-PLOT_INIT.height);
LCD_DrawLine(PLOT_INIT.xOrigin,PLOT_INIT.yOrigin,PLOT_INIT.xOrigin+PLOT_INIT.width,PLOT_INIT.yOrigin);
LCD_DrawLine(PLOT_INIT.xOrigin+PLOT_INIT.width,PLOT_INIT.yOrigin,PLOT_INIT.xOrigin+PLOT_INIT.width,PLOT_INIT.yOrigin-PLOT_INIT.height);
LCD_DrawLine(PLOT_INIT.xOrigin,PLOT_INIT.yOrigin-PLOT_INIT.height,PLOT_INIT.xOrigin+PLOT_INIT.width,PLOT_INIT.yOrigin-PLOT_INIT.height);
//Draw frame ready
POINT_COLOR=RED;
LCD_Draw_Circle(PLOT_INIT.xOrigin,PLOT_INIT.yOrigin,4);
POINT_COLOR=WHITE;
return;
}
void RefreshPlot(u16 NewValue){
u16 width=PLOT_INIT.width;
u16 height=PLOT_INIT.height;
u16 xOrigin=PLOT_INIT.xOrigin;
u16 yOrigin=PLOT_INIT.yOrigin;
u8 NewValueString[10];
u8 LastValueString[10];
u16 LastValue=*(PLOT_NUM+*PLOT_NUM);
u16 i;
sprintf((char*)NewValueString,"%d",NewValue);
sprintf((char*)LastValueString,"%d",*(PLOT_NUM+*PLOT_NUM));
DrawFrame(PLOT_INIT);
//ͼÏó²»ÂúÒ»ÆÁĻʱ
if(*PLOT_NUM!=width){
POINT_COLOR=BLACK;//¿ªÊ¼²Á³ý
for(i=*PLOT_NUM;i>0;i--)DP(xOrigin+i,yOrigin-*(PLOT_NUM+i),BLACK);//²Á³ýÇúÏß
LCD_ShowString(xOrigin+*PLOT_NUM+1,yOrigin-LastValue-16,120,12,12,LastValueString);//²Á³ýÊýÖµ
LCD_Draw_Circle(xOrigin+*PLOT_NUM+1,yOrigin-LastValue,1);//²Á³ý¹â±ê
LCD_Draw_Circle(xOrigin+*PLOT_NUM+1,yOrigin-LastValue,2);//²Á³ý¹â±ê
*(PLOT_NUM+*PLOT_NUM+1)=NewValue;//¸üдýÏÔʾµÄÊý¾Ý
(*PLOT_NUM)++;//¸üÐÂÊý¾ÝÊýÄ¿
POINT_COLOR=WHITE;//¿ªÊ¼»æÖÆ
for(i=*PLOT_NUM;i>0;i--){DP(xOrigin+i,yOrigin-*(PLOT_NUM+i),WHITE);}//»æÖÆÇúÏß
LCD_ShowString(xOrigin+*PLOT_NUM+1,yOrigin-NewValue-16,120,12,12,NewValueString);//»æÖÆÊýÖµ
POINT_COLOR=RED;LCD_Draw_Circle(xOrigin+*PLOT_NUM+1,yOrigin-NewValue,1);//»æÖƹâ±ê
POINT_COLOR=RED;LCD_Draw_Circle(xOrigin+*PLOT_NUM+1,yOrigin-NewValue,2);//»æÖƹâ±ê
}else{
//ͼÏóÒÑÂúÒ»ÆÁĻʱ£¬¿ªÊ¼Õû¶ÎÒƶ¯ÇúÏß
POINT_COLOR=BLACK;//¿ªÊ¼²Á³ý
for(i=width;i>0;i--)DP(xOrigin+i,yOrigin-*(PLOT_NUM+i),BLACK);//²Á³ýͼÏóµã
LCD_ShowString(xOrigin+width,yOrigin-LastValue-16,120,12,12,LastValueString);//»æÖÆÊýÖµ
LCD_Draw_Circle(xOrigin+width,yOrigin-LastValue,1);//²Á³ý¹â±ê
LCD_Draw_Circle(xOrigin+width,yOrigin-LastValue,2);//²Á³ý¹â±ê
for(i=0;i<width;i++)*(PLOT_NUM+i+1)=*(PLOT_NUM+i+2);*(PLOT_NUM+width)=NewValue; //¸üдýÏÔʾµÄÊý¾Ý
POINT_COLOR=WHITE;//¿ªÊ¼»æÖÆ
for(i=width;i>0;i--)DP(xOrigin+i,yOrigin-*(PLOT_NUM+i),WHITE);//»æÖÆͼÏóµã
LCD_ShowString(xOrigin+width,yOrigin-NewValue-16,120,12,12,NewValueString);//»æÖÆÊýÖµ
POINT_COLOR=RED;LCD_Draw_Circle(xOrigin+width,yOrigin-NewValue,1);//»æÖƹâ±ê
POINT_COLOR=RED;LCD_Draw_Circle(xOrigin+width,yOrigin-NewValue,2);//»æÖƹâ±ê
}//¸üÐÂËùÏÔʾµÄÊý¾Ý²¢»­ÐµÄÊý¾Ýµã
return;
}
哇我的缩进和注释全没了
见附件。
LCD.zip
(28.46 KB, 下载次数: 25)
2019-1-30 18:54 上传
点击文件名下载附件
基于原子的【探索者A盘 实验9 PWM输出实验】的源码
硬件探索者开发板和一块2.4寸LCD
//V0.1 LZC 2019.01.29
int main(void)
{
u16 led0pwmval=0; //Õ¼¿Õ±È=¸ÃÊýÖµ/300x100%
u8 dir=1; //²úÉúÈý½Ç²¨
u8 pwmset[12]; //´æ´¢pwmÕ¼¿Õ±ÈÉ趨
u8 adcx;
u8 adcxset[12]; //´æ´¢¹â´«¸ÐÆ÷É趨
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//ÉèÖÃϵͳÖжÏÓÅÏȼ¶·Ö×é2
delay_init(168); //³õʼ»¯ÑÓʱº¯Êý
uart_init(115200);//³õʼ»¯´®¿Ú²¨ÌØÂÊΪ115200
LCD_Init(); LCD_Clear(0x0000);
LED_Init(); //LCD_ShowString(30,60,200,16,12,"LED init successed.");
TIM14_PWM_Init(500-1,84-1); //84M/84=1MhzµÄ¼ÆÊýƵÂÊ,ÖØ×°ÔØÖµ500£¬ËùÒÔWMƵÂÊΪ 1M/500=2Khz.
LCD_Plot_Init(200,240,10,10,20,290,1);
while(1) //ʵÏֱȽÏÖµ´Ó0-300µÝÔö£¬µ½300ºó´Ó300-0µÝ¼õ£¬Ñ­»·
{
delay_ms(5);
POINT_COLOR=0xffff;
sprintf((char*)pwmset,"WM:%3d%c",100-led0pwmval/3,'%');
LCD_ShowString(30,30,200,16,12,pwmset);
if(dir)led0pwmval++;//dir==1 led0pwmvalµÝÔö
else led0pwmval--; //dir==0 led0pwmvalµÝ¼õ
if(led0pwmval>300)dir=0;//led0pwmvalµ½´ï300ºó£¬·½ÏòΪµÝ¼õ
if(led0pwmval==0)dir=1; //led0pwmvalµÝ¼õµ½0ºó£¬·½Ïò¸ÄΪµÝÔö
RefreshPlot(200-led0pwmval/2);
TIM_SetCompare1(TIM14,led0pwmval); //Ð޸ıȽÏÖµ£¬ÐÞ¸ÄÕ¼¿Õ±È
}
}
视频移步
https://www.bilibili.com/video/av42160472/
一周热门 更多>