单片机 STM32 LCD1602 驱动

买的3.3V的LCD1602
听说5V的LCD1602也是一样驱动,只是2号引脚接5V, 其他不变。
在这里插入图片描述
lcd1602.c


#include  "lcd1602.h"
#include "sys.h"
#include "delay.h"
#include "stdio.h"

void GPIO_Configuration( void )
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE );
	GPIO_InitStructure.GPIO_Pin	= GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
	GPIO_InitStructure.GPIO_Speed	= GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode	= GPIO_Mode_Out_PP;
	GPIO_Init( GPIOB, &GPIO_InitStructure );

	GPIO_InitStructure.GPIO_Pin	= GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode	= GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed	= GPIO_Speed_50MHz;
	GPIO_Init( GPIOA, &GPIO_InitStructure );
}


void LCD1602_Write_Cmd( u8 cmd )
{
	LCD_RS_Clr();
	LCD_RW_Clr();
	LCD_EN_Set();

	GPIO_Write( GPIOA, (GPIO_ReadOutputData( GPIOA ) & 0xff00) | cmd );

	DATAOUT( cmd );
	delay_ms( 5 );
	LCD_EN_Clr();
}


void LCD1602_Write_Dat( u8 dat )
{
	LCD_RS_Set();
	LCD_RW_Clr();
	LCD_EN_Set();


	GPIO_Write( GPIOA, (GPIO_ReadOutputData( GPIOA ) & 0xff00) | dat );

	delay_ms( 5 );
	LCD_EN_Clr();
}


void LCD1602_ClearScreen( void )
{
	LCD1602_Write_Cmd( 0x01 );
}


void LCD1602_Set_Cursor( u8 x, u8 y )
{
	u8 addr;

	if ( y == 0 )
		addr = 0x00 + x;
	else
		addr = 0x40 + x;
	LCD1602_Write_Cmd( addr | 0x80 );
}


void LCD1602_Show_Str( u8 x, u8 y, u8 *str )
{
	LCD1602_Set_Cursor( x, y );
	while ( *str != '\0' )
	{
		LCD1602_Write_Dat( *str++ );
	}
}


void LCD1602_Init( void )
{
	GPIO_Configuration();

	LCD1602_Write_Cmd( 0x38 );      /* 16*2显示,5*7点阵,8位数据口 */
	delay_ms( 5 );
	LCD1602_Write_Cmd( 0x0c );      /* 开显示,光标关闭 */
	delay_ms( 5 );
	LCD1602_Write_Cmd( 0x06 );      /* 文字不动,地址自动+1 */
	delay_ms( 5 );
	LCD1602_Write_Cmd( 0x01 );      /* 清屏 */
	delay_ms( 5 );
}



lcd1602.h



#ifndef __LCD1602_H
#define __LCD1602_H 
#include "sys.h"


#define LCD_RS_Set()	GPIO_SetBits( GPIOB, GPIO_Pin_12 )
#define LCD_RS_Clr()	GPIO_ResetBits( GPIOB, GPIO_Pin_12 )

#define LCD_RW_Set()	GPIO_SetBits( GPIOB, GPIO_Pin_13 )
#define LCD_RW_Clr()	GPIO_ResetBits( GPIOB, GPIO_Pin_13 )

#define LCD_EN_Set()	GPIO_SetBits( GPIOB, GPIO_Pin_14 )
#define LCD_EN_Clr()	GPIO_ResetBits( GPIOB, GPIO_Pin_14 )

#define DATAOUT( x ) GPIO_Write( GPIOA, x )


void GPIO_Configuration( void );


void LCD1602_Wait_Ready( void );


void LCD1602_Write_Cmd( u8 cmd );


void LCD1602_Write_Dat( u8 dat );


void LCD1602_ClearScreen( void );


void LCD1602_Set_Cursor( u8 x, u8 y );


void LCD1602_Show_Str( u8 x, u8 y, u8 *str );


void LCD1602_Init( void );


#endif

usage

1 LCD1602_Init()初始化
2 LCD1602_Show_Str(0,0,“test”);

猜你喜欢

转载自blog.csdn.net/x1131230123/article/details/106037702