1622 views|0 replies
- Last login
- 2024-11-16
- Online Time
- 616 hours
- Prestige
- 6948 points
- Points
- 6300 points
|
LCD.c文件:
#include "LPC11XX.h"
#include "gpio.h"
#include "main.h"
#include "Delay.h"
#include "LCD.h"
/*-----------设置D0-D7为输出口-------------*/
void Set_GPIO_Output(void)
{
LPC_GPIO2->DIR |= (0x1<<0); //设置D0-D7为输出
LPC_GPIO2->DIR |= (0x1<<1);
LPC_GPIO2->DIR |= (0x1<<2);
LPC_GPIO2->DIR |= (0x1<<3);
LPC_GPIO2->DIR |= (0x1<<4);
LPC_GPIO2->DIR |= (0x1<<5);
LPC_GPIO2->DIR |= (0x1<<6);
LPC_GPIO2->DIR |= (0x1<<7);
}
/*-----------设置D0-D7为输入口-------------*/
void Set_GPIO_Input(void)
{
LPC_GPIO2->DIR &= ~(0x1<<0); //设置D0-D7为输入
LPC_GPIO2->DIR &= ~(0x1<<1);
LPC_GPIO2->DIR &= ~(0x1<<2);
LPC_GPIO2->DIR &= ~(0x1<<3);
LPC_GPIO2->DIR &= ~(0x1<<4);
LPC_GPIO2->DIR &= ~(0x1<<5);
LPC_GPIO2->DIR &= ~(0x1<<6);
LPC_GPIO2->DIR &= ~(0x1<<7);
}
/*-----------LCD端口初始化-------------*/
void LCD_GPIO_Init(void)
{
//P2口可以用这种方式设置为GPIO,这种方式较合理正式。
LPC_IOCON->PIO2_0 &= (~0x07); //设置P2.0~P2.10为GPIO
LPC_IOCON->PIO2_1 &= (~0x07);
LPC_IOCON->PIO2_2 &= (~0x07);
LPC_IOCON->PIO2_3 &= (~0x07);
LPC_IOCON->PIO2_4 &= (~0x07);
LPC_IOCON->PIO2_5 &= (~0x07);
LPC_IOCON->PIO2_6 &= (~0x07);
LPC_IOCON->PIO2_7 &= (~0x07);
LPC_IOCON->PIO2_8 &= (~0x07);
LPC_IOCON->PIO2_9 &= (~0x07);
LPC_IOCON->PIO2_10 &=(~0x07);
LPC_GPIO2->DIR |= (0x1<<0); //设置D0-D7为输出
LPC_GPIO2->DIR |= (0x1<<1);
LPC_GPIO2->DIR |= (0x1<<2);
LPC_GPIO2->DIR |= (0x1<<3);
LPC_GPIO2->DIR |= (0x1<<4);
LPC_GPIO2->DIR |= (0x1<<5);
LPC_GPIO2->DIR |= (0x1<<6);
LPC_GPIO2->DIR |= (0x1<<7);
LPC_GPIO2->DIR |= (0x1<<8); //RS 设为输出
LPC_GPIO2->DIR |= (0x1<<9); //R/W 设为输出
LPC_GPIO2->DIR |= (0x1<<10); //EN 设为输出
LPC_GPIO3->DIR |= (0x1<<3); //BackLighting 液晶屏背光设为输出
GPIOSetValue( 3, 3, 1 ); //BackLighting位写1,开启液晶屏背光,写0关闭液晶屏背光
}
/*-----------写数据到D0-D7-------------*/
void Write_Data_Port(unsigned char Data)
{
LPC_GPIO2->DATA &= ~(0xff);
LPC_GPIO2->DATA |=Data;
}
/*-----------读D0-D7的数据-------------*/
unsigned char Read_Data_Port(void)
{
//unsigned char In_Data=0;
//In_Data=LPC_GPIO2->DATA;
//return In_Data;
return(LPC_GPIO2->DATA);
}
/*******************************************************************************
* 函 数 名: uchar Chk_LCD_busy(void)
* 函数功能: 读液晶忙通道数据
* 入口参数: 无
* 返 回:无
*******************************************************************************/
unsigned char Chk_LCD_busy(void)
{
unsigned char gR_data;
unsigned char gwait_time=0xff; //设置忙超时数
Set_GPIO_Input(); //接收口设为输入口
LCD_RS_Low(); //表示状态
LCD_RW_High(); //选择读
LCD_EN_High();
Delay_Us(30);
gR_data=Read_Data_Port();
Delay_Us(10);
while(TESTBIT(gR_data,7)) //表示busy
{
--gwait_time;
if(!gwait_time)
{
LCD_EN_Low();
Set_GPIO_Output(); //恢复为输出口
return 0;
}
}
LCD_EN_Low();
Set_GPIO_Output(); //恢复为输出口
return 1;
}
/******************************************************************************
* 函 数 名: void Write_LCD_command(uchar gcmd,uchar gvalue)
* 函数功能: 写指令
* 入口参数: gcmd--指令 gvalue--是否查忙
* 返 回: 无
*******************************************************************************/
void Write_LCD_command(unsigned char gcmd,unsigned char gvalue)
{
if(gvalue) //写命令时大部分情况下是在LCD空闲模式下写
{
if(Chk_LCD_busy())
{
LCD_RS_Low(); //选择指令
LCD_RW_Low(); //选择写
LCD_EN_High(); //使能
Write_Data_Port(gcmd); //赋指令
Delay_Us(15);
LCD_EN_Low();
}
}
else
{
LCD_RS_Low(); //选择指令
LCD_RW_Low(); //选择写
LCD_EN_High(); //使能
Write_Data_Port(gcmd); //赋指令
Delay_Us(10);
LCD_EN_Low();
}
}
/******************************************************************************
* 函 数 名: void Write_LCD_data(uchar gdata)
* 函数功能: 写数据
* 入口参数: gdata--数据
* 返 回:None ***************************************************************************/ /*----------- Write data-------------*/ void Write_LCD_data(unsigned char gdata) { if(Chk_LCD_busy()) //Write data must be written in LCD idle mode { LCD_RS_High(); //Select data LCD_RW_Low(); //Select write LCD_EN_High(); //Enable Write_Data_Port(gdata); Delay_Us(15); LCD_EN_Low(); } } /****************************************************************************** * Function name: void INIT_LCD(void) * Function: Initialize 1602LCD * Entry parameter: None * Return: None ***************************************************************************/ /*-----------LCD initialization function-------------*/ void INIT_LCD(void) { LCD_GPIO_Init(); Delay_Ms(15); Write_LCD_command(0x38,0); //Set 16X2 display, 5X7 dot matrix, 8-bit data transfer, do not detect busy signal Delay_Ms(5); Write_LCD_command(0x38,0); Delay_Ms(5); Write_LCD_command(0x38,0); //Set 16X2 display, 5X7 dot matrix, 8-bit data transfer, do not detect busy signal Delay_Us(100); Write_LCD_command(0x38,1); //Set 16X2 display, 5X7 dot matrix, 8-bit data transfer, detect busy signal Write_LCD_command(0x08,1); //Close display, no cursor, detect busy signal Write_LCD_command(0x01,1); //Clear screen, cursor return, AC=0, detect busy signal Delay_Ms(2); Write_LCD_command(0x06,1); //Display cursor right position, detect busy signal Write_LCD_command(0x0C,1); //Display function on, no cursor, detect busy signal } /****************************************************************************** * Function name: void Display_LCD_string(uchar gadd_start,uchar gline,uchar glength,uchar *pdata) * Function: Display string * Input parameters: gadd_start-column number, uchar gline-row number, glength-data length, pdata-array element * Return: None *******************************************************************************/ /*-----------Display string-------------*/ // Column number, row number, data length, array element void Display_LCD_string(unsigned char gadd_start,unsigned char gline,unsigned char glength,const unsigned char *pdata) { unsigned char gaddress; //uchar *pdat; unsigned char gcount=0; if(!gline) //Line 0 { gaddress=0x80+gadd_start; } //Address correspondence else { gaddress=0xc0+gadd_start; } //Line 1 for(;gcountDATA &=~(1<<8) //给P2.8位写0
#define LCD_RS_High() LPC_GPIO2->DATA |=(1<<8) //给P2.8位写1
#define LCD_RW_Low() LPC_GPIO2->DATA &=~(1<<9) //给P2.9位写0
#define LCD_RW_High() LPC_GPIO2->DATA |=(1<<9) //给P2.9位写1
#define LCD_EN_Low() LPC_GPIO2->DATA &=~(1<<10) //给P2.10位写0
#define LCD_EN_High() LPC_GPIO2->DATA |=(1<<10) //给P2.10位写1
void Set_GPIO_Output(void);
void Set_GPIO_Input(void);
void LCD_GPIO_Init(void);
void Write_Data_Port(unsigned char Data);
unsigned char Read_Data_Port(void);
unsigned char Chk_LCD_busy(void);
void Write_LCD_command(unsigned char gcmd,unsigned char gvalue);
void Write_LCD_data(unsigned char gdata);
void INIT_LCD(void);
void Display_LCD_string(unsigned char gadd_start,unsigned char gline,unsigned char glength,const unsigned char *pdata);
void DispChar_XY_LCD(unsigned char x,unsigned char y,unsigned char gdata);
void DispNum_XY_LCD(unsigned char x,unsigned char y,unsigned char gdata);
void Clear_Display(void);
#endif
|
|