📄 flylcd.c
字号:
#include<P18f452.h>
#include"FlyLCD.h"
#include"delays.h"
#define LCD_CS PORTAbits.RA0
#define LCD_SID PORTAbits.RA1
#define LCD_SCLK PORTAbits.RA2
#define DIR_LCD_CS TRISAbits.TRISA0
#define DIR_LCD_SID TRISAbits.TRISA1
#define DIR_LCD_SCLK TRISAbits.TRISA2
#define DISP_8Bits_Basic 0x30 /* basic command */
#define DISP_4Bits_Basic 0x20
#define DISP_8Bits_Enhance 0x34 /* enhance command */
#define DISP_4Bits_Enhance 0x24
#define DISP_ON 0x0C /* Display on */
#define DISP_ON_C 0x0E /* Display on, Cursor on */
#define DISP_ON_B 0x0F /* Display on, Cursor on, Blink cursor */
#define DISP_OFF 0x08 /* Display off */
#define CLR_DISP 0x01 /* Clear the Display */
#define ENTRY_INC 0x06 /* */
#define ENTRY_INC_S 0x07 /* */
#define ENTRY_DEC 0x04 /* */
#define ENTRY_DEC_S 0x05 /* */
#define DISP_BACK 0X02
#define WAIT_ORDER 0X01
#define E_8BIT_GON 0X36 //8位数据,扩充指令,绘图显示开
#define E_8BIT_GOFF 0X34 //8位数据,扩充指令,绘图显示关
/* * ************************************************************************
* * function name : Send_byte()
* * function : 发送一字节给LCD
* * Input : Byte_Send 也就是byte to send
* * Output : None
* * 注意 :在send_byte() 没有给出CS引脚的电平,目的在于方便以后多
次调用该函数而不会产生CS片选信号一会儿高一会儿底的现象
所以使用者必须自己保证CS在写数据或读数据的过程中为高电平
* * ************************************************************************/
void Send_Byte( char Byte_Send )
{
char i;
for(i=0;i<8;i++)
{
if(Byte_Send&0x80)
LCD_SID=1;
else
LCD_SID=0; /* convert int to bit variable */
Byte_Send<<=1; /* sending the next bit */
LCD_SCLK=0;
LCD_SCLK=1; /* produce a rising edge */
}
}
/* * ************************************************************************
* * function name : Write_Cmd_LCD()
* * function : 对LCD模块写入一字节指令
* * Input : LCD_CMD
* * Output : None
* * ************************************************************************/
void WriteCmdLCD( char command_data) //LCD_CMD
{
LCD_SCLK=0;
LCD_CS=1; // 选中蕊片,直到写命令完成
Send_Byte(0xf8); //Send write command command
Send_Byte(command_data&0XF0); // send high four bits to LCD
Send_Byte((command_data&0X0F)<<4); // send Low four bits to LCD
LCD_CS=0;
}
/* * ************************************************************************
* * function name : Write_Data_LCD()
* * function : 对LCD模块写入一字节数据
* * Input : LCD_Data
* * Output : None
* * ************************************************************************/
void WriteDataLCD( char LCD_Data )
{
LCD_SCLK=0;
LCD_CS=1;
Send_Byte(0XFA); // meaning: write data to LCD ; cause:RW=0,RS=1
Send_Byte(LCD_Data&0xF0);
Send_Byte((LCD_Data&0X0F)<<4); // 注意运算符的优先级 开始的时候错在:Send_Byte(LCD_Data&0X0F<<4)没有加括号
LCD_CS=0;
}
/* * ************************************************************************
* * function name : LCDinitial
* * function : 初始化LCD模块
* * Input : None
* * Output : None
* * 初始化步骤如下:POWER ON----> wait time>40ms--->function set ---->wait time>100us--->function set (repeat)--->wait time>37us----> display on/off control--->wait time>100us--->display clear---->wait time>10ms--->entry mode set ----> Initialization end
* * 对于串行接口方式因为每一次传送都有8位,所以应该选择8位介面的初始化方式,在功能设置应该设为8位的接口
* * ************************************************************************/
void LCDinitial()
{
//ADCON1=0X07; //定义数字io口
DIR_LCD_CS=0;
DIR_LCD_SCLK=0;
DIR_LCD_SID=0;
LCD_SCLK=0;
LCD_SID=0;
LCD_CS=1;
WriteCmdLCD(DISP_8Bits_Basic); //功能设置:8位数据接口 基本功能
delays(60,2);
WriteCmdLCD(DISP_8Bits_Basic); //功能设置:8位数据接口 基本功能
delays(60,2);
WriteCmdLCD(DISP_ON); //开显示并且光标跳动
delays(60,2);
WriteCmdLCD(CLR_DISP); // 清屏
delays(40,101);
WriteCmdLCD(ENTRY_INC); // 进入模式设置
delays(60,2);
}
/***********************************************************************
** 函数名称: putcLCD()
** 功能描述: LCD写char
** 输 入 : LCD_Char
** 输 出 : 无
***********************************************************************/
void putcLCD( char LCD_Char )
{
WriteDataLCD(LCD_Char);
}
/***********************************************************************
** 函数名称: putrsLCD()
** 功能描述: LCD写入ROM字符串
** 输 入: 字符指针 Str
** 输 出: 无
** 注 意: 写之前要写入:0xfa 表明是写数据
***********************************************************************/
void putrsLCD( char const *Str)
{
char temp;
temp=*Str; // Str_Temp 所赋的值是Str所指向的字符
while(temp!=0x00) // 字符串还没有结束
{
LCD_CS=1;
/**********注意时序问题*********/
Send_Byte(0xFA); // 表明接下来的操作是写数据
Send_Byte(temp&0xf0); //发送高字节
Send_Byte((temp&0x0f)<<4); //low nibble
LCD_CS=0;
Str++;
temp=*Str;
}
return ;
}
/***********************************************************************
** 函数名称: putrsLCD()
** 功能描述: LCD写入RAM字符串
** 输 入: 字符指针 Str
** 输 出: 无
***********************************************************************/
void putsLCD(char *Str)
{
char temp;
temp=*Str; // Str_Temp 所赋的值是Str所指向的字符
while(temp!=0x00) // 字符串还没有结束
{
LCD_CS=1;
/**********注意时序问题*********/
Send_Byte(0xFA); // 表明接下来的操作是写数据
Send_Byte(temp&0xf0); //发送高字节
Send_Byte((temp&0x0f)<<4); //low nibble
LCD_CS=0;
Str++;
temp=*Str;
}
return ;
}
/***********************************************************************
** 函数名称: putbiLCD()
** 功能描述: LCD写入二进制数
** 输 入: bidata
** 输 出: 无
***********************************************************************/
void putbiLCD( char bidata )
{
char temp;
char i=0;
for(i=7;i>=0;i--)
{
temp=((bidata&(0x01<<i))>>i)+0X30;
WriteDataLCD(temp);
}
}
/***********************************************************************
** 函数名称: putrsLCD()
** 功能描述: LCD写入十六进制数
** 输 入: HEX_Val
** 输 出: 无
***********************************************************************/
void puthexLCD( char HEX_Val)
{
char temp;
WriteDataLCD('0');
WriteDataLCD('x'); // 使前两位是0x
temp= (HEX_Val >> 4) & 0x0f ;
if ( temp > 9 )temp += 0x37 ; /* 0-9 的BCD码为X+0X30 */
else temp += 0x30 ; /* A-F 的BCD码为X+0X37 */
WriteDataLCD(temp) ;
temp= HEX_Val & 0x0f ;
if ( temp > 9 )temp += 0x37 ;
else temp += 0x30 ;
WriteDataLCD(temp) ;
}
/***********************************************************************
** 函数名称: putIntLCD( )
** 功能描述: LCD写入整数
** 输 入: Int_Val
** 输 出: 无
***********************************************************************/
void putIntLCD( long Int_Val)
{
char temp[12],tempLCD[12];
char i=0,j=1,Int_length=1;
long Int_Val_Temp=Int_Val;
if(Int_Val==0)
tempLCD[1]=0+0x30;
else
{
while(Int_Val_Temp>0)
{
i++; /* 数组从temp[1]开始 */
temp[i]=Int_Val_Temp%10+0x30; /* 因为LCD只能用BCD码来显示,所以将0-9+0x30转成BCD码 */
Int_Val_Temp/=10;
}
Int_length=i;
while(i>0)
{
tempLCD[j]=temp[i];
j++;
i--;
}
}
for(i=1;i<=Int_length;i++)
{
WriteDataLCD(tempLCD[i]);
}
}
/***********************************************************************
** 函数名称: putFloatLCD( )
** 功能描述: LCD写入符点数float
** 输 入: Float_Val
** 输 出: 无
***********************************************************************/
void putFloatLCD( float Float_Val, char n )
{
char i=0,intlength;
char temp[12],temp1[12];
int intpart;
float fracpart;
intpart=(int)Float_Val;fracpart=Float_Val-intpart;
if(intpart==0)
temp[0]=0+0x30,i=1;
else
{
while(intpart>0)
{
temp[i]=intpart%10+0x30;
intpart/=10;
i++;
}
intlength=i;
}
temp[i]='.';
i++;
fracpart*=10;
for(;i<=n;i++)
{
temp[i]=(int)fracpart+48;
fracpart-=(int)fracpart;
fracpart*=10;
}
for(i=0;i<intlength;i++)
temp1[11-i]=temp[i];
for(i=0;i<intlength;i++)
temp[i]=temp1[12-intlength+i];
i=0;
while (i<n) //若到达字串尾则退出
{
WriteDataLCD(temp[i]);
i++;
}
}
/***********************************************************************
** 函数名称: setcurLCD
** 功能描述: 设置光标的位置为CurY行,CurX列
** 输 入: CurX=position(1:8) CurY=line(1:4)
** 输 出: 无
***********************************************************************/
void setcurLCD( char CurY, char CurX)
{
switch(CurY)
{
case 1: WriteCmdLCD(0X80+CurX-1);
break; // 写入第一行CurX列的地址
case 2: WriteCmdLCD(0X90+CurX-1);
break; // 写入第二行C1111urX列的地址
case 3: WriteCmdLCD(0X88+CurX-1);
break; // 写入第三行CurX列的地址
case 4: WriteCmdLCD(0X98+CurX-1);
break; // 写入第四行CurX列的地址
default: break;
}
}
/***********************************************************************
** 函数名称: clearlcd(void)
** 功能描述: 清除液晶显示
** 输 入: 无
** 输 出: 无
***********************************************************************/
void clearlcd(void)
{
WriteCmdLCD(0x01); //清屏
delays( 60,2 );
}
/***********************************************************************
** 函数名称: lcd_picture(void)
** 功能描述: 图片液晶显示
** 输 入: 无
** 输 出: 无
***********************************************************************/
void Lcd_Picture( const rom char *puts )
{
char i,j,k;
int bb=0;
// clearlcd();
// WriteCmdLCD(0x40); //Set Display Start Line = com0
// delays(,2);
WriteCmdLCD(0x36);
for(i=0;i<=0x1F;i++)
{
for(j=0;j<0x08;j++)
{
// WriteCmdLCD(0x36);
WriteCmdLCD((0x80|i)); //Set Page Address
WriteCmdLCD((0x80|j)); //Set Column Address = 0
// WriteCmdLCD(0x32);
for(k=0;k<2;k++)
{
WriteDataLCD(puts[bb]);
bb++;
}
}
}
for(i=0;i<=0x1F;i++)
{
for(j=0x08;j<0x10;j++)
{
// WriteCmdLCD(0x36);
WriteCmdLCD((0x80|i)); //Set Page Address
WriteCmdLCD((0x80|j)); //Set Column Address = 0
// WriteCmdLCD(0x32);
for(k=0;k<2;k++)
{
WriteDataLCD(puts[bb]);
bb++;
}
}
}
WriteCmdLCD(0x32);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -