📄 lcd.c
字号:
#include "LCD.H"
#include <hw_types.h>
#include <hw_memmap.h>
#include <hw_sysctl.h>
#include <hw_gpio.h>
#include <sysctl.h>
#include <gpio.h>
#define uint unsigned int
#define uchar unsigned char
#define uint8 unsigned char
#define X1 0x80
#define X2 0x88
#define Y 0x80
#define comm 0
#define dat 1
// 将较长的标识符定义成较短的形式
#define SysCtlPeriEnable SysCtlPeripheralEnable
#define SysCtlPeriDisable SysCtlPeripheralDisable
#define GPIOPinTypeIn GPIOPinTypeGPIOInput
#define GPIOPinTypeOut GPIOPinTypeGPIOOutput
// 定义KEY
#define KEY_PERIPH SYSCTL_PERIPH_GPIOG
#define KEY_PORT GPIO_PORTG_BASE
#define KEY_PIN GPIO_PIN_5
void lcd_wr (uchar dat_comm,uchar content);
void delay (uint us);
void Set_0(unsigned char ucLED);
/***************************************************************************************************
功能:初始化控制LCD的IO
参数:ucLED是LED名称,取值下列值之一或者它们之间的“或运算”组合形式
RS
sclk
std
返回:无
***************************************************************************************************/
void LCDIO_Init(unsigned char ucLED)
{
if ( ucLED & RS )
{
SysCtlPeriEnable(SYSCTL_PERIPH_GPIOA); // 使能GPIOD端口
GPIOPinTypeOut(GPIO_PORTA_BASE , GPIO_PIN_0); // 设置PD0为输入类型
Set_0(RS); //
}
if ( ucLED & sclk )
{
SysCtlPeriEnable(SYSCTL_PERIPH_GPIOA); // 使能GPIOD端口
GPIOPinTypeOut(GPIO_PORTA_BASE , GPIO_PIN_4); // 设置PD2输入类型
Set_0(sclk); //
}
if ( ucLED & std )
{
SysCtlPeriEnable(SYSCTL_PERIPH_GPIOA); // 使能GPIOG端口
GPIOPinTypeOut(GPIO_PORTA_BASE , GPIO_PIN_2); // 设置PG3输入类型
Set_0(std); //
}
}
/***************************************************************************************************
功能:初始化LCD
参数:无
返回:无
***************************************************************************************************/
void init_lcd (void)
{ uchar i,j;
/***清cgram*****/
for(i=0;i<8;i++)
{ for(j=0;j<32;j++)
{ lcd_wr (comm,0x34);/***关显示***/
lcd_wr (comm,0x80+j);
lcd_wr (comm,0x80+i);
lcd_wr (comm,0x30);
lcd_wr (dat,0);
lcd_wr (dat,0);
}
}
lcd_wr (comm,0x36);
lcd_wr(comm,0x30); /*30---基本指令动作*/
delay (100);
lcd_wr(comm,0x01); /* 清屏,地址指针指向00H*/
delay (100);
lcd_wr(comm,0x06); /*光标的移动方向*/
delay (100);
lcd_wr(comm,0x0c); /*开显示,关游标*/
}
/***************************************************************************************************
功能:给LCD控制线的某个线置0
参数:ucLED是控制线名称,取值下列值之一或者它们之间的“或运算”组合形式
RS
sclk
std
返回:无
***************************************************************************************************/
void Set_0(unsigned char ucLED)
{
if ( ucLED & RS )
{
GPIOPinWrite(GPIO_PORTA_BASE , GPIO_PIN_0 , 0x00); // PD0输出低电平
}
if ( ucLED & sclk )
{
GPIOPinWrite(GPIO_PORTA_BASE , GPIO_PIN_4 , 0x00); // PG2输出低电平
}
if ( ucLED & std )
{
GPIOPinWrite(GPIO_PORTA_BASE , GPIO_PIN_2 , 0x00); // PG3输出低电平
}
}
/***************************************************************************************************
功能:给LCD控制线的某个线置1
参数:ucLED是控制线名称,取值下列值之一或者它们之间的“或运算”组合形式
RS Set_1(RS);
sclk Set_1(sclk);
std Set_1(std);
返回:无
***************************************************************************************************/
void Set_1(unsigned char ucLED)
{
if ( ucLED & RS )
{
GPIOPinWrite(GPIO_PORTA_BASE , GPIO_PIN_0 , 0x01); // PD0输出高电平
}
if ( ucLED & sclk )
{
GPIOPinWrite(GPIO_PORTA_BASE , GPIO_PIN_4 , 0x01); // PG2输出高电平
}
if ( ucLED & std )
{
GPIOPinWrite(GPIO_PORTA_BASE , GPIO_PIN_2 , 0x01); // PA2输出高电平
}
}
/*-----------
name: lcd_wr()
function: write data or code to lcd
description: dat_comm=1 write data; dat_comm=0 write code;
device: arm1138
--------------*/
void lcd_wr (unsigned char dat_comm,unsigned char content)
{
unsigned char a,i,j;
Set_1(RS);
a=content;
Set_0(sclk);
Set_1(std);
for(i=0;i<5;i++)
{
Set_1(sclk);
Set_0(sclk);
}
Set_0(std);
Set_1(sclk);
Set_0(sclk);
if(dat_comm)
Set_1(std); //data
else
Set_0(std); //command
Set_1(sclk);
Set_0(sclk);
Set_0(std);
Set_1(sclk);
Set_0(sclk);
for(j=0;j<2;j++)
{
for(i=0;i<4;i++)
{
if(a & 0x80)
Set_1(std);
else
Set_0(std);
a=a<<1;
Set_1(sclk);
Set_0(sclk);
}
Set_0(std);
for(i=0;i<4;i++)
{
Set_1(sclk);
Set_0(sclk);
}
}
Set_0(RS);
}
/***************************************************************************************************
功能:在指定位置显示汉字串或字符串
参数:x是行坐标,y是列坐标,*pData是要写入的汉字或字符
返回:无
***************************************************************************************************/
void LcdDispStr(uchar x,uchar y,uchar *pData)
{ uchar i;
lcd_wr(comm,0x30); /*30---基本指令动作*/
if(x>7) x=7;
if(y>1) y=1;
if(y==0){ lcd_wr(comm,0x80+x);}
else {lcd_wr(comm,0x90+x);}
for(i=0;pData[i]!='\0';i++)
{
lcd_wr(dat,pData[i]);
}
}
/***************************************************************************************************
功能:写单个字符
参数:asc是要写入的字符
返回:无
***************************************************************************************************/
void LcdLoadChar(uchar asc)
{
lcd_wr (dat,asc);/***写完一个地址自动加一,连续的写就行 *****/
}
//delay time
void delay (uint us)
{
while(us--);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -