新闻中心

EEPW首页>嵌入式系统>设计应用> 51单片机_1602液晶自定义心形图案

51单片机_1602液晶自定义心形图案

作者: 时间:2016-11-30 来源:网络 收藏
# include

unsigned char table1[]={0x03,0x07,0x0f,0x1f,0x1f,0x1f,0x1f,0x1f,
0x18,0x1E,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,
0x07,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,
0x10,0x18,0x1c,0x1E,0x1E,0x1E,0x1E,0x1E,
0x0f,0x07,0x03,0x01,0x00,0x00,0x00,0x00,
0x1f,0x1f,0x1f,0x1f,0x1f,0x0f,0x07,0x01,
0x1f,0x1f,0x1f,0x1f,0x1f,0x1c,0x18,0x00,
0x1c,0x18,0x10,0x00,0x00,0x00,0x00,0x00};//心图案
#define CLEARSCREEN LCD_write_command(0x01)

本文引用地址: //m.amcfsurvey.com/article/201611/323849.htm

/**************定义接口************************/

#define LCDIO P0
sbit LCD1602_RS=P2^2;
sbit LCD1602_RW=P2^1;
sbit LCD1602_EN=P2^0;

/**************定义函数************************/
void LCD_write_command(unsigned char command);//写入指令函数
void LCD_write_dat(unsigned char dat);//写入数据函数
void LCD_set_xy( unsigned char x, unsigned char y );//设置显示位置函数
void LCD_dsp_char( unsigned x,unsigned char y,unsigned char dat);//显示一个字符函数
void LCD_dsp_string(unsigned char X,unsigned char Y,unsigned char *s);//显示字符串函数
void LCD_init(void);//初始化函数
void delay_nms(unsigned int n);//延时函数
/********************************************/

/************初始化函数****************/
void LCD_init(void)
{
CLEARSCREEN;//clear screen
LCD_write_command(0x38);//set 8 bit data transmission mode
LCD_write_command(0x0c);//open display (enable lcd display)
LCD_write_command(0x80);//set lcd first display address
CLEARSCREEN;//clear screen
}
/****************************************************/

/**************写指令函数********************************/
void LCD_write_command(unsigned char command)
{
LCDIO=command;
LCD1602_RS=0;
LCD1602_RW=0;
LCD1602_EN=0;
LCD1602_EN=1;
delay_nms(10);
}
/***************************************************/
/****************写数据函数************************/
void LCD_write_dat(unsigned char dat)
{
LCDIO=dat;
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_EN=0;
delay_nms(1);
LCD1602_EN=1;
}
/****************************************************/

/***************设置显示位置**************************/
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == 1)
address = 0x80 + x;
else
address =0xc0+ x;
LCD_write_command(address);
}
/***************************************************/

/****************显示一个字符**********************/
void LCD_dsp_char( unsigned x,unsigned char y,unsigned char dat)
{
LCD_set_xy( x, y );
LCD_write_dat(dat);
}
/**********************************************/

/***************显示字符串函数***************/
void LCD_dsp_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y );
while (*s)
{
LCD_write_dat(*s);
s ++;
}
}
/***********************************************/

/********** 延时**********************/
void delay_nms(unsigned int n)
{
unsigned int i=0,j=0;
for (i=n;i>0;i--)
for (j=0;j<10;j++);
}
/**************************************/

/***********主函数**************/
void main(void)
{
unsigned char i,j,k,tmp;
LCD_init();
delay_nms(100);
tmp=0x40;//设置CGRAM地址的格式字
k=0;
for(j=0;j<8;j++)
{
for(i=0;i<8;i++)
{
LCD_write_command(tmp+i); // 设置自定义字符的 CGRAM 地址
delay_nms(2);
LCD_write_dat(table1[k]); // 向CGRAM写入自定义字符表的数据
k++;
delay_nms(2);
}
tmp=tmp+8;
}
LCD_dsp_string(1,1,"heart: ");//在第一行第一列显示“heart”
for (i=0;i<4;i++)
{
LCD_dsp_char( 10+i,1,i);//在第一行第10列位置显示心图案的上半部
delay_nms(1);
}
for (i=4;i<8;i++)
{
LCD_dsp_char( 10+i-4,2,i);//在第二行第10列位置显示心图案的下半部
delay_nms(1);
}
while (1);
}
/************************ 感谢陈青华老师的帮助********************************************/



评论


技术专区

关闭