nicolaasb
nicolaasb

Reputation: 37

How do I combine incoming data and character in VHDL?

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.

Upvotes: 2

Views: 2870

Answers (1)

Paebbels
Paebbels

Reputation: 16221

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.

Upvotes: 2

Related Questions