标题:
51单片机液晶模块代码(12864 1602 5110)
[打印本页]
作者:
pdh
时间:
2016-1-11 11:28
标题:
51单片机液晶模块代码(12864 1602 5110)
包含12864 1602 5110
00000.png
(25.61 KB, 下载次数: 118)
下载附件
2016-1-11 18:58 上传
完整代码(包含头文件)下载:
液晶.rar
(4.73 KB, 下载次数: 42)
2016-1-11 11:28 上传
点击文件名下载附件
下载积分: 黑币 -5
以下是文件内容预览:
12864:
#include"yejing.h"
#define uint unsigned int
#define uchar unsigned char
//液晶
sbit CS=P3^4;
sbit SID=P3^5;
sbit SCLK=P3^6;
uchar yi[]={"欢迎使用"};
uchar er[]={"红外报警系统"};
uchar san[]={"发现物体闯入"};
uchar si[]={"已处于警戒中"};
void delay_1ms(uint x)
{
uint i,j;
for(j=0;j<x;j++)
for(i=0;i<100;i++);
}
/*************************/
/******************写指令*****************/
void send_command(uchar command_data)
{
uchar i;
uchar i_data;
i_data=0xf8;//第一个字节确定写指令
CS=1;
SCLK=0;
/*第一个字节设定为写指令*/
for(i=0;i<8;i++)
{
SID=(bit)(i_data&0x80);
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
/*写第二个字节和第三个字节确定指令功能*/
/******* 写第二个字节********/
i_data=command_data;
i_data&=0xf0; //高4位保留,低四位清零
for(i=0;i<8;i++)
{
SID=(bit)(i_data&0x80);
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
/********写第三个字节********/
i_data=command_data;
i_data<<=4; //把低四位取代高四位
for(i=0;i<8;i++)
{
SID=(bit)(i_data&0x80);
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
CS=0;
delay_1ms(10);
}
/*************************/
/******************写数据*****************/
void send_data(uchar command_data)
{
uchar i;
uchar i_data;
i_data=0xfa;//第一个字节确定写数据
CS=1;
/*第一个字节设定为写数据*/
for(i=0;i<8;i++)
{
SID=(bit)(i_data&0x80);
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
/******* 写第二个字节********/
i_data=command_data;
i_data&=0xf0;
for(i=0;i<8;i++)
{
SID=(bit)(i_data&0x80);
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
/******* 写第三个字节********/
i_data=command_data;
i_data<<=4;
for(i=0;i<8;i++)
{
SID=(bit)(i_data&0x80);
SCLK=0;
SCLK=1;
i_data=i_data<<1;
}
CS=0;
delay_1ms(10);
}
/********初始化液晶************/
void lcd_init()
{
delay_1ms(100);
send_command(0x30);
send_command(0x02);
send_command(0x06);
send_command(0x0c);
send_command(0x01);
send_command(0x80);
}
/*******显示第一行*******/
void display_1()
{
uchar i;
send_command(0x82);//发送指令显示第一行
for(i=0;i<8;i++)
{
send_data(yi[i]);
}
}
/*******显示测第二行*******/
void display_2()
{
uchar i;
send_command(0x91);//发送指令显示第二行
for(i=0;i<12;i++)
{
send_data(er[i]);
}
}
/*******显示第三行*******/
void display_3()
{
uchar i;
send_command(0x89);//发送指令显示第三行
for(i=0;i<12;i++)
{
send_data(san[i]);
}
}
/*******显示第三行*******/
void display_4()
{
uchar i;
send_command(0x89);//发送指令显示第三行
for(i=0;i<12;i++)
{
send_data(si[i]);
}
}
复制代码
5110:
/******************************
文档说明:NOKIA5110显示模块驱动
修改日期:9月8日
版本: v1
********************************/
#include "LCD5110.h"
#include "suzu.h"
/************************************************
函数名: void LCD_init(void)
功能: NKIA5110初始化配置
输入参数: 无
输出参数: 无
修改日期: 9月8日
特殊说明: 不同模块所需调的是28行代码,大概范围150-255
************************************************/
void LCD_init(void)
{
unsigned char i;
SRST=0;
for(i=0;i<150;i++);
SRST=1;
LCD_write_byte(0x21,0);
LCD_write_byte(195,0); //偏置电压设置
LCD_write_byte(0x20,0);
LCD_clear();
LCD_write_byte(0x0c,0);
SCE=0;
}
/************************************************
函数名: void LCD_write_byte(unsigned char dat,unsigned char command)
功能: 向NKIA5110写字节数据
输入参数: unsigned char dat,unsigned char command
输出参数: 无
修改日期: 9月8日
************************************************/
void LCD_write_byte(unsigned char dat,unsigned char command)
{
unsigned char i;
SCE=0;
if(command==0)
SDC=0;
else
SDC=1;
for(i=0;i<8;i++)
{
if(dat&0x80)
SDIN=1;
else
SDIN=0;
SCLK=0;
dat=dat<<1;
SCLK=1;
}
SCE=1;
}
/************************************************
函数名: void LCD_set_XY(unsigned char X,unsigned char Y)
功能: 设置NKIA5110显示XY坐标位置
输入参数: unsigned char X,unsigned char Y
输出参数: 无
修改日期: 9月8日
************************************************/
void LCD_set_XY(unsigned char X,unsigned char Y)
{
LCD_write_byte(0x40|Y,0);//列
LCD_write_byte(0x80|X,0);//行
}
/************************************************
函数名: void LCD_clear(void)
功能: NKIA5110 清屏
输入参数: 无
输出参数: 无
修改日期: 9月8日
************************************************/
void LCD_clear(void)
{
unsigned char t;
unsigned char k;
LCD_set_XY(0,0);
for(t=0;t<6;t++)
{
for(k=0;k<84;k++)
{
LCD_write_byte(0x00,1);
}
}
}
/************************************************
函数名: void LCD_write_char(unsigned char x,unsigned y,unsigned char c)
功能: NKIA5110 显示字符
输入参数: unsigned char x,unsigned y,unsigned char c
输出参数: 无
修改日期: 9月8日
************************************************/
void LCD_write_char(unsigned char x,unsigned y,unsigned char c)
{
unsigned char i;
c-=32;
LCD_set_XY(x,y);
for(i=0;i<6;i++)
{
LCD_write_byte(font6x8[c][i],1);
if(i==5)
{
y++;
LCD_set_XY(x,y);
}
}
}
/************************************************
函数名: void LCD_write_Num(unsigned char X,unsigned char Y,unsigned int num)
功能: NKIA5110 显示数字
输入参数: X为一个位置为6,Y为行取值为0~5,num为要显示的数字
输出参数: 无
修改日期: 9月8日
************************************************/
void LCD_write_Num(unsigned char X,unsigned char Y,unsigned int num)
{
unsigned char str[8],i=0,len=0;
unsigned int temp;
temp = num;
while(temp)
{
temp /=10;
len++;
}
if(!num)
{
len++;
str[0]=0x30;
}
str[len] = 0;
while(num)
{
str[len-i-1] = num%10 + 0x30;
num /=10;
i++;
}
LCD_write_string(X,Y,(char *)str);
}
/************************************************
函数名: void LCD_write_Num(unsigned char X,unsigned char Y,unsigned int num)
功能: NKIA5110 显示字符串
输入参数: X为一个位置为6,Y为行取值为0~5,*s为要显示的字符串
输出参数: 无
修改日期: 9月8日
************************************************/
void LCD_write_string(unsigned char X,unsigned char Y,char *s)
{
while(*s)
{
LCD_write_char(X,Y,*s);
s++;
X+=6;
}
}
///************************************************
// 函数名: void disp16_16(unsigned char x,unsigned char y,unsigned char address)
// 功能: NKIA5110 显示16*16的字符
// 输入参数: unsigned char x,unsigned y,unsigned char c
// 输出参数: 无
// 修改日期: 9月8日
// ************************************************/
//void disp16_16(unsigned char x,unsigned char y,unsigned char address)
//{
// unsigned char c;
// LCD_set_XY(x*8, y);
// for(c=0;c<16;c++)
// {
// LCD_write_byte(han[address*32+c],1);
// }
// LCD_set_XY(x*8, y+1);
// for(c=16;c<32;c++)
// {
// LCD_write_byte(han[address*32+c],1);
// }
//}
// /************************************************
// 函数名: void LCD_write_chinese_string(unsigned char X, unsigned char Y,
// unsigned char ch_with,unsigned char num,
// unsigned char line,unsigned char row)
// 功能: NKIA5110 显示中文字符
// 输入参数: unsigned char X, unsigned char Y,
// unsigned char ch_with,unsigned char num,
// unsigned char line,unsigned char row
// 输出参数: 无
// 修改日期: 9月8日
// ************************************************/
//void LCD_write_chinese_string(unsigned char X, unsigned char Y, unsigned char ch_with,unsigned char num,unsigned char line,unsigned char row) //x,y为坐标,ch_with为汉字宽度,num为数组中第几个汉字,line为行,row为列
//{
// unsigned char i,n;
// LCD_set_XY(X,Y); //
// for (i=0;i<num;)
// {
// for (n=0; n<ch_with*2; n++) //
// {
// if (n==ch_with) //
// {
// if (i==0) LCD_set_XY(X,Y+1);
// else
// LCD_set_XY((X+(ch_with+row)*i),Y+1);
// }
// LCD_write_byte(write_chinese[line+i][n],1);
// }
// i++;
// LCD_set_XY((X+(ch_with+row)*i),Y);
// }
//}
复制代码
1602:
#include"yejing.h"
/*液晶*/
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
#define DataPort P0
sbit RS = P2^4; //定义端口
sbit RW = P2^5;
sbit EN = P2^6;
/*------------------------------------------------
uS延时函数,含有输入参数 unsigned char t,无返回值
unsigned char 是定义无符号字符变量,其值的范围是
0~255 这里使用晶振12M,精确延时请使用汇编,大致延时
长度如下 T=tx2+5 uS
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS延时函数,含有输入参数 unsigned char t,无返回值
unsigned char 是定义无符号字符变量,其值的范围是
0~255 这里使用晶振12M,精确延时请使用汇编
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
/*------------------------------------------------
判忙函数
------------------------------------------------*/
bit LCD_Check_Busy(void)
{
DataPort= 0xFF;
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return (bit)(DataPort & 0x80);
}
/*------------------------------------------------
写入命令函数
------------------------------------------------*/
void LCD_Write_Com(unsigned char com)
{
while(LCD_Check_Busy());//忙则等待
DelayMs(5);
RS_CLR;
RW_CLR;
EN_SET;
DataPort=com;
_nop_();
EN_CLR;
}
/*------------------------------------------------
写入数据函数
------------------------------------------------*/
void LCD_Write_Data(unsigned char Data)
{
DelayMs(5);
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
}
/*------------------------------------------------
清屏函数
------------------------------------------------*/
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
DelayMs(5);
}
/*------------------------------------------------
写入字符串函数
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x); //表示第一行
}
else
{
LCD_Write_Com(0xC0 + x); //表示第二行
}
while (*s)
{
LCD_Write_Data( *s);
s ++;
}
}
/*------------------------------------------------
写入字符函数
------------------------------------------------*/
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data( Data);
}
/*------------------------------------------------
初始化函数
------------------------------------------------*/
void LCD_Init(void)
{
LCD_Write_Com(0x38); /*显示模式设置*/
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08); /*显示关闭*/
LCD_Write_Com(0x01); /*显示清屏*/
LCD_Write_Com(0x06); /*显示光标移动设置*/
DelayMs(5);
LCD_Write_Com(0x0C); /*显示开及光标设置*/
}
复制代码
作者:
whengfu
时间:
2016-1-11 11:39
好东西,菜鸟的福音,收藏了
作者:
爱的步伐XY
时间:
2016-1-11 15:23
不错,下载看看
作者:
xql1997
时间:
2016-7-19 22:10
很好很好 感谢楼主
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1