基于Nios II的LCD16027驱动实例

时间:2019-08-27
本文章向大家介绍基于Nios II的LCD16027驱动实例,主要包括基于Nios II的LCD16027驱动实例使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

实验平台:DE2-115

软件版本:Quartus II 15.1

为节约时间,Qsys中系统的搭建就不啰嗦了,直接贴图。

DE2-115中SDRAM的配置如下图所示:

Hardware code:

 1 module LCD1602(
 2 clk,
 3 rst_n,
 4 led,
 5 lcd1602_RS,  
 6 lcd1602_RW,  
 7 lcd1602_data,
 8 lcd1602_E,
 9 sdram_addr, 
10 sdram_ba,   
11 sdram_cas_n,
12 sdram_cke,  
13 sdram_cs_n, 
14 sdram_dq,   
15 sdram_dqm,  
16 sdram_ras_n,
17 sdram_we_n,
18 sdram_clk,
19 lcd1602_vl,
20 lcd1602_on 
21  );
22 
23 input           clk;
24 output        lcd1602_RS;
25 output        lcd1602_RW;
26 inout    [7:0]    lcd1602_data;
27 output        lcd1602_E;
28 output      lcd1602_vl;
29 output      lcd1602_on;
30 
31 input           rst_n;
32 output[12:0]sdram_addr;
33 output[1:0]    sdram_ba;
34 output        sdram_cas_n;
35 output        sdram_cke;
36 output        sdram_cs_n;
37 inout    [31:0]sdram_dq;
38 output[3:0]    sdram_dqm;
39 output        sdram_ras_n;
40 output        sdram_we_n;
41 output      sdram_clk;
42 
43 output[3:0] led;
44 
45 wire c0;
46 reg[3:0] vl_cnt;
47 always@(posedge clk or negedge rst_n)
48   if(!rst_n)
49     vl_cnt <= 4'd0;
50   else
51     vl_cnt <= vl_cnt + 4'd1;
52      
53 assign lcd1602_vl = (vl_cnt > 9);
54 assign lcd1602_on = 1;
55      
56   
57 
58 PLL    PLL_inst (
59     .inclk0 ( clk ),
60     .c0 ( c0 ),
61     .c1 ( sdram_clk )
62     );
63 
64 my_cpu u0 (
65     .clk_clk       (c0),            //     clk.clk
66     .lcd1602_RS    (lcd1602_RS),    // lcd1602.RS
67     .lcd1602_RW    (lcd1602_RW),    //        .RW
68     .lcd1602_data  (lcd1602_data),  //        .data
69     .lcd1602_E     (lcd1602_E),     //        .E
70 
71     .reset_reset_n (rst_n),         //   reset.reset_n
72     .sdram_addr    (sdram_addr),    //   sdram.addr
73     .sdram_ba      (sdram_ba),      //        .ba
74     .sdram_cas_n   (sdram_cas_n),   //        .cas_n
75     .sdram_cke     (sdram_cke),     //        .cke
76     .sdram_cs_n    (sdram_cs_n),    //        .cs_n
77     .sdram_dq      (sdram_dq),      //        .dq
78     .sdram_dqm     (sdram_dqm),     //        .dqm
79     .sdram_ras_n   (sdram_ras_n),   //        .ras_n
80     .sdram_we_n    (sdram_we_n),    //          .we_n
81     .pio_led_export(led)
82 );
83 
84 
85 
86 endmodule
View Code

Software code:

 1 /*
 2  * LCD1602.h
 3  *
 4  *  Created on: 2019年8月26日
 5  *      Author: Legend
 6  */
 7 
 8 #ifndef LCD1602_H_
 9 #define LCD1602_H_
10 
11 #define lcd_write_cmd(base,data)   IOWR(base,0,data)
12 #define lcd_read_cmd(base)         IORD(base,1)
13 #define lcd_write_data(base,data)  IOWR(base,2,data)
14 #define lcd_read_cmd(base)         IORD(base,3)
15 
16 void LCD_Init();
17 void LCD_Show_Text(char*Text);
18 void LCD_Line2();
19 void LCD_Text();
20 
21 
22 
23 
24 #endif /* LCD1602_H_ */
View Code
 1 /*
 2  * LCD1602.c
 3  *
 4  *  Created on: 2019年8月25日
 5  *      Author: Legend
 6  */
 7 
 8 #include <stdio.h>
 9 #include <altera_avalon_pio_regs.h>
10 #include <alt_types.h>
11 #include <unistd.h>
12 #include <system.h>
13 #include <string.h>
14 #include <io.h>
15 #include <sys/alt_irq.h>
16 #include "../inc/lcd1602.h"
17 
18 
19 void LCD_Init()
20 {
21     lcd_write_cmd(LCD1602_BASE,0x38);  //初始化LCD
22     usleep(2000);
23 
24     lcd_write_cmd(LCD1602_BASE,0x0c);  //关显示,光标闪烁方式
25     usleep(2000);
26 
27     lcd_write_cmd(LCD1602_BASE,0x01);  //清显示
28     usleep(2000);
29 
30     lcd_write_cmd(LCD1602_BASE,0x06);  //光标迁移方式,不允许整屏移动
31     usleep(2000);
32 
33     lcd_write_cmd(LCD1602_BASE,0x80);  //显示光标指示的位置
34     usleep(2000);
35     }
36 
37 void LCD_Show_Text(char*Text)
38 {
39     int i;
40     for(i=0;i<strlen(Text);i++)
41     {
42         lcd_write_data(LCD1602_BASE,Text[i]);
43         usleep(2000);
44 
45     }
46 }
47 
48 void LCD_Line2()  //换行
49 {
50     lcd_write_cmd(LCD1602_BASE,0xc0);
51     usleep(2000);
52     }
53 
54 
55 int main()
56 {
57     char Text1[16]=" Hello DE2-115 ";
58     char Text2[16]=" Legend of NCU ";
59     LCD_Init();
60     LCD_Show_Text(Text1);
61     LCD_Line2();
62     LCD_Show_Text(Text2);
63 
64     return 0;
65     }
66 
67 
68 
69 //http://blog.sina.com.cn/s/blog_457bf05a0100k8vy.html
View Code

实际效果图:

原文地址:https://www.cnblogs.com/571328401-/p/11416685.html