• 问题描述:

由于LED和LCD有共用引脚、导致调用LCD显示时会修改LED所用gpio的引脚值,导致LED混乱。

  • 解决方法:
static int x=0x00;
static  unsigned char LED5=1;
x^=LED5;
GPIOC->ODR=(x<<13)&0xFF00;
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);

ODR寄存器控制所有LED的亮灭、LCD显示会改变ODR寄存器,因此对LED控制时,需要重新赋值,上面程序实现的是基本的LED闪烁,由于需要位不断取反,所以可以利用异或运算:^,可以实现不断翻转,当然也可以直接if else实现:

static int i=0;
if(i==0)
{
GPIOC->ODR=(1<<13)&0xFF00;
i=1;
}
else
{
GPIOC->ODR=(0<<13)&0xFF00;
i=0;
}
  •  LCD驱动的改进:

原来驱动:

/*******************************************************************************
* Function Name  : LCD_DisplayStringLine
* Description    : Displays a maximum of 20 char on the LCD.
* Input          : - Line: the Line where to display the character shape .
*                    This parameter can be one of the following values:
*                       - Linex: where x can be 0..9
*                  - *ptr: pointer to string to display on LCD.
* Output         : None
* Return         : None
*******************************************************************************/
void LCD_DisplayStringLine(u8 Line, u8 *ptr)
{
	u32 i = 0;
	u16 refcolumn = 319;//319;

	while ((*ptr != 0) && (i < 20))	 //	20
	{
		LCD_DisplayChar(Line, refcolumn, *ptr);
		refcolumn -= 16;
		ptr++;
		i++;
	}
}


/*******************************************************************************
* Function Name  : LCD_DisplayChar
* Description    : Displays one character (16dots width, 24dots height).
* Input          : - Line: the Line where to display the character shape .
*                    This parameter can be one of the following values:
*                       - Linex: where x can be 0..9
*                  - Column: start column address.
*                  - Ascii: character ascii code, must be between 0x20 and 0x7E.
* Output         : None
* Return         : None
*******************************************************************************/
void LCD_DisplayChar(u8 Line, u16 Column, u8 Ascii)
{
	Ascii -= 32;
	LCD_DrawChar(Line, Column, &ASCII_Table[Ascii * 24]);
}

可以看到 LCD_DisplayStringLine()函数实际上会打印一段char数组,当这个数组全部打印完或者超了20 个(一行最多20个)时,就会停下,但是有一个问题,比如我这一次打印了15个字,下次打印了1个字,在同一行,那么第一次打印的后3个字就不会被刷新掉,所以一般选择加空格来盖掉上一次打印的东西,例如:

sprintf(buf_show,"      PA7:%d%%      ",PWM7_hand*100/5000);
LCD_DisplayStringLine(Line5,(uint8_t *)buf_show);

在打印东西的后边加空格,这样很麻烦,如果使用%d自己缩进(%03d固定格式,前面补0,例如:001,010,100这样打印),我们就不知道后边有多少个空,所以最好自己补齐20个

改进代码:

void LCD_DisplayStringLine(u8 Line, u8 *ptr)
{
	u32 i = 0;
	u16 refcolumn = 319;//319;

	char a=' ';
	while (i < 20)	 //	20
	{
		if(*ptr==0)
		{
	  	LCD_DisplayChar(Line, refcolumn, ' ');
		}
		else
		{
			LCD_DisplayChar(Line, refcolumn, *ptr);
			
			ptr++;
		}	
		refcolumn -= 16;
		i++;
	}
}

如果这个数组打印完成,但是i没有到20,那么就在它的后面继续打印空格,知道满了一行20个。

Line是第几行,refcolumn是总共多少列,每个字符占16个位置,*ptr是数组的内容,也就是要打印的字。ptr是数组内容的地址 ,&ptr是指针的地址。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐