Xilinx FPGA中使用LCD1602的方法

一、xps中的设置
1.在xps添加一个xps general purpose io,并将其命名为“char_lcd”。
注:这里的命名有一定的规则,将其命名为“char_lcd”,是因为在后来的c程序中调用lcd基地址的时候使用的名字为“xpar_char_lcd_baseaddr”,如果将这个命名为其他的(比如xx),那么在下面的lcd.h中也要将
#define lcd_baseaddr xpar_char_lcd_baseaddr
这一句中的char_lcd改成对应的名称。
2.配置char_lcd中channel 1的gpio data channel width为11并在ports中设置为connected to external ports。
3.在system.ucf加入相应的管教约束,这里提供genesys digilent开发板中对应的管脚约束,管脚的名称自然而言和上面使用的名称对应。
#char_lcd constraints
#char_lcd_gpio_io_pin corresponds to lcd_e
#char_lcd_gpio_io_pin corresponds to lcd_rs
#char_lcd_gpio_io_pin corresponds to lcd_rw
#char_lcd_gpio_io_pin corresponds to lcd_d7, to lcd_d6... to lcd_d0
net char_lcd_gpio_io_pin loc = aa5 | iostandard=lvcmos33 | tig | pulldown;
net char_lcd_gpio_io_pin loc = v7 | iostandard=lvcmos33 | tig | pulldown;
net char_lcd_gpio_io_pin loc = w6 | iostandard=lvcmos33 | tig | pulldown;
net char_lcd_gpio_io_pin loc = ad7 | iostandard=lvcmos33 |tig | pulldown;
net char_lcd_gpio_io_pin loc = ac7 | iostandard=lvcmos33 | tig | pulldown;
net char_lcd_gpio_io_pin loc = ac5 | iostandard=lvcmos33 | tig | pulldown;
net char_lcd_gpio_io_pin loc = ab6 | iostandard=lvcmos33 | tig | pulldown;
net char_lcd_gpio_io_pin loc = ac4 | iostandard=lvcmos33 | tig | pulldown;
net char_lcd_gpio_io_pin loc = ab5 | iostandard=lvcmos33 | tig | pulldown;
net char_lcd_gpio_io_pin loc = ab7 | iostandard=lvcmos33 | tig | pulldown;
net char_lcd_gpio_io_pin loc = y8 | iostandard=lvcmos33 | tig | pulldown;
二、sdk中的代码模板
1.将lcd.h、lcd.c、sleep.h和sleep.c添加到工程的src文件夹中,并在需要调用lcd的代码中添加
#include lcd.h
2.在使用lcd之前需要进行初始化,添加下面代码:
lcdoff();
lcdclear();
lcdon();
lcdinit();
3.需要在lcd中显示时,调用下面函数
lcdprintstring(first_line, second_line; //first_line和second_line都是char*类型
附件:博文中提到文件的源代码:
1.lcd.h
/*******************************************************************************
*
* xilinx is providing this design, code, or information as is
* solely for use in developing programs and solutions for
* xilinx devices. by providing this design, code, or information
* as one possible implementation of this feature, application
* or standard, xilinx is making no representation that this
* implementation is free from any claims of infringement,
* and you are responsible for obtaining any rights you may require
* for your implementation. xilinx expressly disclaims any
* warranty whatsoever with respect to the adequacy of the
* implementation, including but not limited to any warranties or
* representations that this implementation is free from claims of
* infringement, implied warranties of merchantability and fitness
* for a particular purpose.
*
* (c) copyright 2007 xilinx, inc.
* all rights reserved.
*
******************************************************************************/
#ifndef lcd_h
#define lcd_h
//=====================
// test functions
//=====================
void lcdtest();
void lcdtestmenu();
void lcdtestinput(char ch);
//=====================
// external functions
//=====================
#define lcd_baseaddr xpar_char_lcd_baseaddr //根据需要修改这里
void lcdon();
void lcdoff();
void lcdclear();
void lcdinit();
void lcdenabledisplayshift();
void lcdenablecursorblink();
void lcddisabledisplayshift();
void lcddisablecursorblink();
void movecursorhome();
void movecursorright();
void movecursorleft();
void lcdsetline(int line);
void lcdprintchar(char c);
void lcdprintstring(char * line1, char * line2);
//=====================
// internal functions
//=====================
void initinst(void);
void writeinst(unsigned long inst1, unsigned long inst2);
void writedata(unsigned long data1, unsigned long data2);
void writeinst8(unsigned long inst);
void writedata8(unsigned long data);
#endif
2.lcd.c
/******************************************************************************
*
* xilinx is providing this design, code, or information as is
* as a courtesy to you, solely for use in developing programs and
* solutions for xilinx devices. by providing this design, code,
* or information as one possible implementation of this feature,
* application or standard, xilinx is making no representation
* that this implementation is free from any claims of infringement,
* and you are responsible for obtaining any rights you may require
* for your implementation. xilinx expressly disclaims any
* warranty whatsoever with respect to the adequacy of the
* implementation, including but not limited to any warranties or
* representations that this implementation is free from claims of
* infringement, implied warranties of merchantability and fitness
* for a particular purpose.
*
* (c) copyright 2007 xilinx inc.
* all rights reserved.
*
******************************************************************************/
#include
#include sleep.h
#include xgpio_l.h
#include xparameters.h
#include lcd.h
// usec delay timer during initialization, important to change if
// clock speed changes
#define init_delay 10000
#define inst_delay 1000 //usec delay timer between instructions
#define data_delay 1000 //usec delay timer between data
/*
#------------------------------------------------------------------------------
# io pad location constraints / properties for character lcd gpio
#------------------------------------------------------------------------------
net char_lcd_gpio_io loc = aa5 | iostandard=lvcmos33 | tig | pulldown; #lcd_e
net char_lcd_gpio_io loc = v7 | iostandard=lvcmos33 | tig | pulldown; #lcd_rs
net char_lcd_gpio_io loc = w6 | iostandard=lvcmos33 | tig | pulldown; #lcd_rw
net char_lcd_gpio_io loc = ad7 | iostandard=lvcmos33 |tig | pulldown; #lcd_d7
net char_lcd_gpio_io loc = ac7 | iostandard=lvcmos33 | tig | pulldown; #lcd_d6
net char_lcd_gpio_io loc = ac5 | iostandard=lvcmos33 | tig | pulldown; #lcd_d5
net char_lcd_gpio_io loc = ab6 | iostandard=lvcmos33 | tig | pulldown; #lcd_d4
net char_lcd_gpio_io loc = ac4 | iostandard=lvcmos33 | tig | pulldown; #lcd_d3
net char_lcd_gpio_io loc = ab5 | iostandard=lvcmos33 | tig | pulldown; #lcd_d2
net char_lcd_gpio_io loc = ab7 | iostandard=lvcmos33 | tig | pulldown; #lcd_d1
net char_lcd_gpio_io loc = y8 | iostandard=lvcmos33 | tig | pulldown; #lcd_d0
*/
//==============================================================================
//
// internal functions
//
//==============================================================================
void waitforbusyflag(void)
{
xuint32 lcd_status;
xgpio_msetdatadirection(lcd_baseaddr, 1, 0xfffff8ff);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000100);
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000500);
usleep(10);
lcd_status = xgpio_mreadreg(lcd_baseaddr, 1);
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000100);
usleep(10);
//xil_printf(\r\nlcd status = 0x%x, lcd_status);
while (lcd_status & 0x00000080){
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000100);
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000500);
usleep(10);
lcd_status = xgpio_mreadreg(lcd_baseaddr, 1);
//xil_printf(\r\nlcd status = 0x%x, lcd_status);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000100);
usleep(10);
}
//set lcd data to output again
xgpio_msetdatadirection(lcd_baseaddr, 1, 0xfffff800);
}
void initinst(void)
{
xuint32 lcd_status;
/* 4-bit mode
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000003); //function set 4-bit mode,
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000043); //set enable and data
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000003);
usleep(init_delay);
*/
xgpio_msetdatadirection(lcd_baseaddr, 1, 0xfffff800);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x0000003c);//function set, 8-bit mode, 2 lines, 8 x 11 dots
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x0000043c); //set enable and data
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x0000003c);
//read the busy flag
xgpio_msetdatadirection(lcd_baseaddr, 1, 0xfffff8ff);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000100);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000500);
lcd_status = xgpio_mreadreg(lcd_baseaddr, 1);
//xil_printf(\r\nlcd status = 0x%x, lcd_status);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000100);
while (lcd_status & 0x00000080){
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000100);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000500);
lcd_status = xgpio_mreadreg(lcd_baseaddr, 1);
//xil_printf(\r\nlcd status = 0x%x, lcd_status);
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000100);
}
//set lcd data to output again
xgpio_msetdatadirection(lcd_baseaddr, 1, 0xfffff800);
}
//write instruction on 8 bits
void writeinst8(unsigned long inst)
{
unsigned long printinst;
xgpio_msetdatadirection(lcd_baseaddr, 1, 0xfffff800);
printinst = 0x00000400 | inst;
xgpio_msetdatareg(lcd_baseaddr, 1, inst); //write data
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, printinst); //set enable
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, inst); //turn off enable
usleep(10);
waitforbusyflag();
// usleep(inst_delay);
}
void writeinst(unsigned long inst1, unsigned long inst2)
{
unsigned long printinst;
printinst = 0x00000040 | inst1;
xgpio_msetdatareg(lcd_baseaddr, 1, inst1); //write data
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, printinst); //set enable
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, inst1); //turn off enable
usleep(10);
printinst = 0x00000040 | inst2;
xgpio_msetdatareg(lcd_baseaddr, 1, printinst); //set enable and data
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, inst2); //turn off enable
usleep(inst_delay);
}
void writedata8(unsigned long data)
{
unsigned long rs_data, enable_rs_data;
// int busy=true;
rs_data = 0x00000200 | data; //sets rs, data1
enable_rs_data = 0x00000600 | data;
xgpio_msetdatareg(lcd_baseaddr, 1, rs_data); //write data, rs
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, enable_rs_data); //set enable, keep data, rs
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, rs_data); //turn off enable
usleep(10);
waitforbusyflag();
// usleep(data_delay);
}
void writedata(unsigned long data1, unsigned long data2)
{
unsigned long rs_data, enable_rs_data;
// int busy=true;
rs_data = 0x00000020 | data1; //sets rs, data1
enable_rs_data = 0x00000060 | data1;
xgpio_msetdatareg(lcd_baseaddr, 1, rs_data); //write data, rs
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, enable_rs_data); //set enable, keep data, rs
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, rs_data); //turn off enable
usleep(10);
rs_data = 0x00000020 | data2; //sets rs, data2
enable_rs_data = 0x00000060 | data2; //sets rs, data2
xgpio_msetdatareg(lcd_baseaddr, 1, enable_rs_data); //set enable, rs, data
usleep(10);
xgpio_msetdatareg(lcd_baseaddr, 1, rs_data); //turn off enable
usleep(data_delay);
}
//==================================================================================
//
// external functions
//
//==================================================================================
void lcdon()
{
//printf(display on\r\n);
// writeinst(0x00000000, 0x0000000e);
writeinst8(0x0000000e); //display on, cursor on, cursor blink off
}
void lcdoff()
{
// printf(display off\r\n);
// writeinst(0x00000000, 0x00000008);
writeinst8(0x00000008); //display off, cursor off
}
void lcdclear()
{
// printf(display clear\r\n);
// writeinst(0x00000000, 0x00000001);
// writeinst(0x00000000, 0x00000010);
writeinst8(0x00000001); //clear display
writeinst8(0x00000002); //return home
}
void lcdinit()
{
// sets char lcd reg to write mode
xgpio_msetdatadirection(lcd_baseaddr, 1, 0x00000000);
// zeroes char lcd reg
xgpio_msetdatareg(lcd_baseaddr, 1, 0x00000000);
// lcd init
usleep(15000); //after vcc>4.5v wait 15ms to init char lcd
initinst();
usleep(4100); //wait 4.1ms
initinst();
usleep(100); //wait 100us
initinst();
initinst();
// function set
// writeinst(0x00000002, 0x00000002);
writeinst8(0x00000038);
// display off
// writeinst(0x00000000, 0x00000008);
writeinst8(0x00000008);
// display clear
// writeinst(0x00000000, 0x00000001);
writeinst8(0x00000001);
// entry mode set
// writeinst(0x00000000, 0x00000006);
writeinst8(0x00000006); //cursor moves right, no shift
// display on
// writeinst(0x00000000, 0x0000000e);
writeinst8(0x0000000e);//display on, cursor on, cursor blink off
}
void lcdenabledisplayshift()
{
writeinst8(0x00000007); //cursor moves right, shift enabled
}
void lcdenablecursorblink()
{
writeinst8(0x0000000f);//display on, cursor on, cursor blink on
}
void lcddisabledisplayshift()
{
writeinst8(0x00000006); //cursor moves right, no shift
}
void lcddisablecursorblink()
{
writeinst8(0x0000000e);//display on, cursor on, cursor blink off
}
void movecursorhome()
{
// writeinst(0x00000000, 0x00000002);
writeinst8(0x00000002);
}
void movecursorleft()
{
// writeinst(0x00000001, 0x00000000);
writeinst8(0x00000010);
}
void movecursorright()
{
// writeinst(0x00000001, 0x00000004);
writeinst8(0x00000014);
}
void lcdsetline(int line)
{
//line1 = 1, line2 = 2
int i;
if((line - 1))
{
movecursorhome();
for(i=0; i> 4) & 0x0000000f), (c & 0x0000000f));
writedata8(c);
}
void lcdprintstring(char * line1, char * line2)
{
int i=0;
lcdsetline(1);
for(i=0; i<16; i++)
{
if(line1[i])
lcdprintchar(line1[i]);
else
break;
}
i=0;
//xil_printf (\r\nfirst line printed);
lcdsetline(2);
// writeinst8(0x000000c0);
for(i=0; i<16; i++)
{
if(line2[i])
lcdprintchar(line2[i]);
else
break;
}
//xil_printf (\r\nsecond line printed);
return;
}
3.sleep.h
/*******************************************************************************
*
* xilinx is providing this design, code, or information as is
* solely for use in developing programs and solutions for
* xilinx devices. by providing this design, code, or information
* as one possible implementation of this feature, application
* or standard, xilinx is making no representation that this
* implementation is free from any claims of infringement,
* and you are responsible for obtaining any rights you may require
* for your implementation. xilinx expressly disclaims any
* warranty whatsoever with respect to the adequacy of the
* implementation, including but not limited to any warranties or
* representations that this implementation is free from claims of
* infringement, implied warranties of merchantability and fitness
* for a particular purpose.
*
* (c) copyright 2007 xilinx, inc.
* all rights reserved.
*
******************************************************************************/
#ifndef sleep_h
#define sleep_h
void nanosleep(unsigned int nanoseconds);
void usleep(unsigned int useconds);
void sleep(unsigned int seconds);
#endif
4.sleep.c
/******************************************************************************
*
* xilinx is providing this design, code, or information as is
* as a courtesy to you, solely for use in developing programs and
* solutions for xilinx devices. by providing this design, code,
* or information as one possible implementation of this feature,
* application or standard, xilinx is making no representation
* that this implementation is free from any claims of infringement,
* and you are responsible for obtaining any rights you may require
* for your implementation. xilinx expressly disclaims any
* warranty whatsoever with respect to the adequacy of the
* implementation, including but not limited to any warranties or
* representations that this implementation is free from claims of
* infringement, implied warranties of merchantability and fitness
* for a particular purpose.
*
* (c) copyright 2007 xilinx inc.
* all rights reserved.
*
******************************************************************************/
#include sleep.h
//#include xtime_l.h
#include xparameters.h
void nanosleep(unsigned int nanoseconds)
{
/* not implemented */
}
void usleep(unsigned int useconds)
{
int i,j;
for (j=0;j
探寻手机里那些不为人知的闪光功能 京东金机奖评选温情启动
应急照明控制器如何进行智能控制和远程管理
2020年的工业机器人行业或将进入调整期
IPv6发展现状也许并不如我们意
MPLS广域网VPN技术解析
Xilinx FPGA中使用LCD1602的方法
如何将楼梯走廊内一个普通的双控开关用plc来进行控制?
从系统下手 本本WIN XP环境下的优化策略
TI毫米波雷达传感器在自动泊车系统的应用优势
京瓷将投资625亿日元扩建日本半导体工厂
高功率LED的封装基板有哪些种类?
因代码不规范,程序员枪击4名同事,一个用生命维护代码的程序员
数据的护城河,湖泊和海洋,去中心化数据交换可以有哪些帮助?
100W+100W功率放大器电路原理图
爆美国至少两千家机构可破解苹果iPhone的加密内容
浅谈骁龙662与骁龙888的性能差异
喜讯!热烈庆祝昊芯上海办公室正式成立!
如何计算逆变器容量
AES HWIP技术规格
西门子S7-200 SMART系列PLC与S7-1200实现S7通信