Advertisement
mitbal

LCD Driver

May 30th, 2011
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 4.42 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3.  
  4. entity LCD_Driver is
  5.     Port ( Clock : in  STD_LOGIC;
  6.            New_Line : in  STD_LOGIC;
  7.            Reset : in  STD_LOGIC;
  8.            Home_Line : in  STD_LOGIC;
  9.            Write_Now : in  STD_LOGIC;
  10.            Data : in  STD_LOGIC_VECTOR (7 downto 0);
  11.               Busy : out std_logic;
  12.            LCD_DB : out  STD_LOGIC_VECTOR (7 downto 0);
  13.            LCD_E : out  STD_LOGIC;
  14.            LCD_RS : out  STD_LOGIC;
  15.            LCD_RW : out  STD_LOGIC);
  16. end LCD_Driver;
  17.  
  18. architecture Behavioral of LCD_Driver is
  19.  
  20.     constant FunctionSet : std_logic_vector(7 downto 0) := "00111100";
  21.  
  22.     constant EntryModeSet : std_logic_vector(7 downto 0) := x"06";
  23.  
  24.     constant DisplayOnOff : std_logic_vector(7 downto 0) := x"0C";
  25.  
  26.     constant ClearDisplay : std_logic_vector(7 downto 0) := x"01";
  27.  
  28.     constant DDRAMAddress : std_logic_vector(7 downto 0) := x"80";
  29.  
  30.     type State_type is (Boot, SetFunction, SetFunctionb, SetEntryMode, SetEntryModeb, SetDisplay, SetDisplayb, Clear, Clearb, SetAddress, SetAddressb, Idle, WriteLCD, WriteLCDb);
  31.     signal State : State_type := Boot;
  32.  
  33.     constant BootDelay : integer := 1500000;
  34.     constant Delay : integer := 2000;
  35.     constant ClearDelay : integer := 100000;
  36.     constant E_Delay : integer := 12;
  37.    
  38.     signal e_count : integer := 0;
  39.     signal counter : integer := 0;
  40.  
  41.     signal DataIn : std_logic_vector(7 downto 0);
  42. begin
  43.  
  44.     LCD_RW <= '0'; -- Always Write, No Read
  45.  
  46.     DataIn <= Data;
  47.  
  48.     process(Clock, Reset) begin
  49.         if Reset = '1' then
  50.            
  51.         elsif rising_edge(Clock) then
  52.             case State is
  53.                 when Boot =>
  54.                     Busy <= '1';
  55.                     if counter = BootDelay then
  56.                         State <= SetFunction;
  57.                         counter <= 0;
  58.                         --Busy <= '1';
  59.                     else
  60.                         counter <= counter + 1;
  61.                     end if;
  62.                 when SetFunction =>
  63.                     LCD_DB <= FunctionSet;
  64.                     LCD_RS <= '0';
  65.                     LCD_E <= '1';
  66.                     State <= SetFunctionb;
  67.                 when SetFunctionb =>
  68.                     if e_count = E_Delay then
  69.                         LCD_E <= '0';
  70.                     else
  71.                         e_count <= e_count + 1;
  72.                     end if;
  73.                    
  74.                     if counter = Delay then
  75.                         State <= SetEntryMode;
  76.                         counter <= 0;
  77.                         e_count <= 0;
  78.                     else
  79.                         counter <= counter + 1;
  80.                     end if;
  81.                 when SetEntryMode =>
  82.                     LCD_DB <= EntryModeSet;
  83.                     LCD_RS <= '0';
  84.                     LCD_E <= '1';
  85.                     State <= SetEntryModeb;
  86.                 when SetEntryModeb =>
  87.                     if e_count = E_Delay then
  88.                         LCD_E <= '0';
  89.                     else
  90.                         e_count <= e_count + 1;
  91.                     end if;
  92.                     --LCD_E <= '0';
  93.                     if counter = Delay then
  94.                         State <= SetDisplay;
  95.                         counter <= 0;
  96.                         e_count <= 0;
  97.                     else
  98.                         counter <= counter + 1;
  99.                     end if;
  100.                 when SetDisplay =>
  101.                     LCD_DB <= DisplayOnOff;
  102.                     LCD_RS <= '0';
  103.                     LCD_E <= '1';
  104.                     State <= SetDisplayb;
  105.                 when SetDisplayb =>
  106.                     if e_count = E_Delay then
  107.                         LCD_E <= '0';
  108.                     else
  109.                         e_count <= e_count + 1;
  110.                     end if;
  111.                     --LCD_E <= '0';
  112.                     if counter = Delay then
  113.                         State <= Clear;
  114.                         counter <= 0;
  115.                         e_count <= 0;
  116.                     else
  117.                         counter <= counter + 1;
  118.                     end if;
  119.                 when Clear =>
  120.                     LCD_DB <= ClearDisplay;
  121.                     LCD_RS <= '0';
  122.                     LCD_E <= '1';
  123.                     State <= Clearb;
  124.                 when Clearb =>
  125.                     if e_count = E_Delay then
  126.                         LCD_E <= '0';
  127.                     else
  128.                         e_count <= e_count + 1;
  129.                     end if;
  130.                     --LCD_E <= '0';
  131.                     if counter = ClearDelay then
  132.                         State <= SetAddress;
  133.                         counter <= 0;
  134.                         e_count <= 0;
  135.                     else
  136.                         counter <= counter + 1;
  137.                     end if;
  138.                 when SetAddress =>
  139.                     LCD_DB <= DDRAMAddress;
  140.                     LCD_RS <= '0';
  141.                     LCD_E <= '1';
  142.                     State <= SetAddressb;
  143.                 when SetAddressb =>
  144.                     if e_count = E_Delay then
  145.                         LCD_E <= '0';
  146.                     else
  147.                         e_count <= e_count + 1;
  148.                     end if;
  149.                     --LCD_E <= '0';
  150.                     if counter = Delay then
  151.                         State <= Idle;
  152.                         counter <= 0;
  153.                         e_count <= 0;
  154.                     else
  155.                         counter <= counter + 1;
  156.                     end if;
  157.                 when Idle =>
  158.                     Busy <= '0';
  159.                     if Write_Now = '1' then
  160.                         State <= WriteLCD;
  161.                     end if;
  162.                 when WriteLCD =>
  163.                     Busy <= '1';
  164.                     LCD_DB <= DataIn;
  165.                     LCD_RS <= '1';
  166.                     LCD_E <= '1';
  167.                     State <= WriteLCDb;
  168.                 when WriteLCDb =>
  169.                     if e_count = E_Delay then
  170.                         LCD_E <= '0';
  171.                     else
  172.                         e_count <= e_count + 1;
  173.                     end if;
  174.                     LCD_E <= '0';
  175.                     if counter = Delay then
  176.                         State <= Idle;
  177.                         counter <= 0;
  178.                         e_count <= 0;
  179.                     else
  180.                         counter <= counter + 1;
  181.                     end if;
  182.             end case;
  183.         end if;
  184.     end process;
  185.  
  186. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement