PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 12, 2022

[FIXED] How do I combine incoming data and character in VHDL?

 July 12, 2022     arrays, lcd, message, vhdl     No comments   

Issue

I want do drive a 4x16 LCD display using VHDL. The first line should say "FREQ: 000 RPM" where the zeroes represent incoming 8-bit frequency data. Same for the next lines with different data, also 8-bit. My VHDL code is as follows:

-- 16 Characters
subtype string16_type is string(1 to 16);
-- 4 string of 16 characters
type message4x16_type is array(1 to 4) of string16_type;
-- Define messages displayed
constant message_home:  message4x16_type := (    --1234567890123456
                                             1 => "FREE MODE       ",
                                             2 => "PARCOURS        ",
                                             3 => "- - - - - - - - ",
                                             4 => " - - - - - - - -");
constant message_info:  message4x16_type := (    --1234567890123456
                                             1 => "FREQ:  000 RPM  ",
                                             2 => "SPEED: 000 KM/H ",
                                             3 => "DIST:  000 KM   ",
                                             4 => "MORE INFO       ");
-- Character amount in  line
signal character_counter: integer range 1 to 16;
-- Line amount on LCD
signal line_counter     : integer range 1 to 4;

Then follows a state machine, with state write_char looking partly like this:

if msg_select = '0' then
    aline := message_home(line_counter);
elsif msg_select = '1' then
    aline := message_info(line_counter);
end if;
data <= std_logic_vector(to_unsigned(character'pos(aline(character_counter)),8));

Everything works smoothily this way but I can't think of a way to implement the frequency data into the message, like using %i in C. I am aware of the 'record' statement but not sure how to use it in this situation. Any other ways to implement the data are very welcome.

Thanks on forehand.


Solution

Declaring types, constants and signals as before:

-- 16 characters
type lcd_line_type is array(0 to 15) of character;
-- 4 lines of 16 characters
type message4x16_type is array(0 to 3) of lcd_line_type;
-- Define messages displayed
constant message_home : message4x16_type := (
--1234567890123456
 "FREE MODE       ",
 "PARCOURS        ",
 "- - - - - - - - ",
 " - - - - - - - -"
);
constant message_info :  message4x16_type := (
--1234567890123456
 "FREQ:  000 RPM  ",
 "SPEED: 000 KM/H ",
 "DIST:  000 KM   ",
 "MORE INFO       "
);
-- Character amount in line
signal character_counter : integer range 0 to 15;
-- Line amount on LCD
signal line_counter : integer range 0 to 3;

subtype rawchar is std_logic_vector(7 downto 0);
type rawstring is array(natural range <>) of rawchar;
signal rpm : rawstring(2 downto 0);
signal kmh : rawstring(2 downto 0);

function to_rawchar(char : character) return rawchar is
begin
  return std_logic_vector(to_unsigned(character'pos(char), 8));
end function;

Usage example:

if msg_select = '0' then
  data <= to_rawchar(message_home(line_counter)(character_counter));
elsif msg_select = '1' then
  case line_counter is
    when 0 =>
      -- replace 000 by rpm(2:0)
      case character_counter is
        when 6 => data <= rpm(2);
        when 7 => data <= rpm(1);
        when 8 => data <= rpm(0);
        when others => data <= to_rawchar(message_info(0)(character_counter));
      end case;
    when 1 =>
      -- replace 000 by kmh(2:0)
      case character_counter is
        when 7 => data <= kmh(2);
        when 8 => data <= kmh(1);
        when 9 => data <= kmh(0);
        when others => data <= to_rawchar(message_info(1)(character_counter));
      end case;
    -- ...
  end case;
end if;

The first case tests for the requested line. The second case overrides the constant values at particular positions.

I additionally extracted the char to slv conversion into a function.



Answered By - Paebbels
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing