【STM32】 LCD画坐标系和图象

2019-07-20 03:55发布

鼓捣了一晚上搞出一个基于原子哥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; //&cedil;ü&ETH;&Acirc;&acute;&yacute;&Iuml;&Ocirc;&Ecirc;&frac34;&micro;&Auml;&Ecirc;&yacute;&frac34;&Yacute;
  
  POINT_COLOR=WHITE;//&iquest;&ordf;&Ecirc;&frac14;&raquo;&aelig;&Ouml;&AElig;
  for(i=width;i>0;i--)DP(xOrigin+i,yOrigin-*(PLOT_NUM+i),WHITE);//&raquo;&aelig;&Ouml;&AElig;&Iacute;&frac14;&Iuml;ó&micro;&atilde;
  LCD_ShowString(xOrigin+width,yOrigin-NewValue-16,120,12,12,NewValueString);//&raquo;&aelig;&Ouml;&AElig;&Ecirc;&yacute;&Ouml;&micro;
  POINT_COLOR=RED;LCD_Draw_Circle(xOrigin+width,yOrigin-NewValue,1);//&raquo;&aelig;&Ouml;&AElig;&sup1;&acirc;±ê
  POINT_COLOR=RED;LCD_Draw_Circle(xOrigin+width,yOrigin-NewValue,2);//&raquo;&aelig;&Ouml;&AElig;&sup1;&acirc;±ê
}//&cedil;ü&ETH;&Acirc;&Euml;ù&Iuml;&Ocirc;&Ecirc;&frac34;&micro;&Auml;&Ecirc;&yacute;&frac34;&Yacute;&sup2;&cent;&raquo;&shy;&ETH;&Acirc;&micro;&Auml;&Ecirc;&yacute;&frac34;&Yacute;&micro;&atilde;

return;
}

哇我的缩进和注释全没了
见附件。




LCD.zip (28.46 KB, 下载次数: 25) 2019-1-30 18:54 上传 点击文件名下载附件



友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
妙行
1楼-- · 2019-07-20 05:54
例程如下
基于原子的【探索者A盘 实验9 PWM输出实验】的源码
硬件探索者开发板和一块2.4寸LCD

//V0.1 LZC 2019.01.29
int main(void)
{
     u16 led0pwmval=0;     //&Otilde;&frac14;&iquest;&Otilde;±&Egrave;=&cedil;&Atilde;&Ecirc;&yacute;&Ouml;&micro;/300x100%
     u8 dir=1;       //&sup2;ú&Eacute;ú&Egrave;&yacute;&frac12;&Ccedil;&sup2;¨
     u8 pwmset[12];    //&acute;&aelig;&acute;&cent;pwm&Otilde;&frac14;&iquest;&Otilde;±&Egrave;&Eacute;è&para;¨
     u8 adcx;
     u8 adcxset[12];    //&acute;&aelig;&acute;&cent;&sup1;&acirc;&acute;&laquo;&cedil;&ETH;&AElig;÷&Eacute;è&para;¨

     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//&Eacute;è&Ouml;&Atilde;&Iuml;&micro;&Iacute;&sup3;&Ouml;&ETH;&para;&Iuml;&Oacute;&Aring;&Iuml;&Egrave;&frac14;&para;·&Ouml;×é2
     delay_init(168);  //&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;&Ntilde;&Oacute;&Ecirc;±&ordm;&macr;&Ecirc;&yacute;
     uart_init(115200);//&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;&acute;&reg;&iquest;&Uacute;&sup2;¨&Igrave;&Oslash;&Acirc;&Ecirc;&Icirc;&ordf;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&micro;&Auml;&frac14;&AElig;&Ecirc;&yacute;&AElig;&micro;&Acirc;&Ecirc;,&Ouml;&Oslash;×°&Ocirc;&Oslash;&Ouml;&micro;500&pound;&not;&Euml;ù&Ograve;&OcircWM&AElig;&micro;&Acirc;&Ecirc;&Icirc;&ordf; 1M/500=2Khz.
     LCD_Plot_Init(200,240,10,10,20,290,1);
     while(1) //&Ecirc;&micro;&Iuml;&Ouml;±&Egrave;&frac12;&Iuml;&Ouml;&micro;&acute;&Oacute;0-300&micro;&Yacute;&Ocirc;&ouml;&pound;&not;&micro;&frac12;300&ordm;ó&acute;&Oacute;300-0&micro;&Yacute;&frac14;&otilde;&pound;&not;&Ntilde;&shy;&raquo;·
     {
          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&micro;&Yacute;&Ocirc;&ouml;
          else led0pwmval--; //dir==0 led0pwmval&micro;&Yacute;&frac14;&otilde;
          if(led0pwmval>300)dir=0;//led0pwmval&micro;&frac12;&acute;&iuml;300&ordm;ó&pound;&not;·&frac12;&Iuml;ò&Icirc;&ordf;&micro;&Yacute;&frac14;&otilde;
          if(led0pwmval==0)dir=1; //led0pwmval&micro;&Yacute;&frac14;&otilde;&micro;&frac12;0&ordm;ó&pound;&not;·&frac12;&Iuml;ò&cedil;&Auml;&Icirc;&ordf;&micro;&Yacute;&Ocirc;&ouml;
  
          RefreshPlot(200-led0pwmval/2);
          TIM_SetCompare1(TIM14,led0pwmval); //&ETH;&THORN;&cedil;&Auml;±&Egrave;&frac12;&Iuml;&Ouml;&micro;&pound;&not;&ETH;&THORN;&cedil;&Auml;&Otilde;&frac14;&iquest;&Otilde;±&Egrave;
     }
}

视频移步
https://www.bilibili.com/video/av42160472/


二九结狐六体
2楼-- · 2019-07-20 09:39
 精彩回答 2  元偷偷看……

一周热门 更多>