3.10 STEP 9: A State Machine for the Main Controller

3.10.1 Synopsis

A main control unit is needed to coordinate the actions of the different modules.

The interface of the module is depicted in the next drawing:

 

The main controller interface

 

When the trigger button is pressed the shutter shall be opened and stay opened for the selected exposure time. This is done by setting the TIMER_GO signal one period to high, so the exposure controller opens the shutter for the selected exposure time. After the exposure time has passed the film has to be transported. So the motor needs a signal to start the transport Therefore MOTOR_GO is set to high for one period.

Now two things can appear: The motor has successfully transported the film, which is signalled by the MOTOR_READY signal. In this case a new photo can be made by the camera. It is the TRIGGER signal has to be examined again.

Or an error occurred while transporting the film. In this case the MOTOR_ERROR signal is set and the ERROR output signal has to be set, so the display indicate this case. When the cause of the error is removed (e.g. a new film is inserted) this has to be signalled by the user by pressing the trigger button.

3.10.2 Implementation

The VHDL code for the camera controller

library ieee;
use ieee.std_logic_1164.all;

entity MAIN_CTRL is
  port(CLK      : in  std_ulogic;
       RESET      : in  std_ulogic;
       TRIGGER     : in  std_ulogic;
       EXPOSE      : in  std_ulogic;
       MOTOR_READY : in  std_ulogic;
       MOTOR_ERROR : in  std_ulogic;
       ERROR       : out std_ulogic;
       TIMER_GO    : out std_ulogic;
       MOTOR_GO    : out std_ulogic);
end MAIN_CTRL;
  • The new data type for the FSM states
    is not used anywhere else. Therefore
    its definition is not placed in a sepa-
    rate package but in the architecture
    where it is actually used.
    Though the declaration of a new enu-
    meration type for the possible states is
    probably the easiest way to imple-
    ment a FSM, it is not always the best
    choice. Please look up the tutorial to
    find out about alternatives.
architecture RTL of MAIN_CTRL is
  type T_STATE is (IDLE, TAKE_PIC, DELAY,
    WAIT_EXP_TIME, FILM_TRANSPORT,
    WAIT_TRANSPORT, BROKEN);
  signal STATE:      T_STATE;
  signal NEXT_STATE: T_STATE;
begin
  process (STATE, TRIGGER, EXPOSE,
           MOTOR_ERROR, MOTOR_READY)
  begin
    NEXT_STATE <= STATE;
    case STATE is
      when IDLE =>
        if TRIGGER = '1' then
          NEXT_STATE <= TAKE_PIC;
        end if;
      when TAKE_PIC =>
        NEXT_STATE <= DELAY;
      when DELAY =>
        NEXT_STATE <= WAIT_EXP_TIME;
      when WAIT_EXP_TIME =>
        if EXPOSE = '0' then
          NEXT_STATE <= FILM_TRANSPORT;
        end if;
      when FILM_TRANSPORT =>
        NEXT_STATE <= WAIT_TRANSPORT;
      when WAIT_TRANSPORT =>
        if MOTOR_ERROR = '1' then
          NEXT_STATE <= BROKEN;
        elsif MOTOR_READY = '1' then
          NEXT_STATE <= IDLE;
        end if;
      when others =>
        NULL;
    end case;
  end process;

  process (CLK, RESET)
  begin
    if (RESET = '1') then
      STATE <= IDLE;
    elsif (CLK'event and CLK = '1') then
      STATE <= NEXT_STATE;
    end if;     -- end of clocked process
  end process;
  with STATE select
    TIMER_GO <= '1' when TAKE_PIC,
                '0' when others;

  with STATE select
    MOTOR_GO <= '1' when FILM_TRANSPORT,
                '0' when others;

  with STATE select
    ERROR    <= '1' when BROKEN,
                '0' when others;
end RTL;
     
  • The calculation of the next state is
    placed in a separate combinational
    process.
  • The default action is to keep the cur-
    rent state.
  • While in IDLE state the camera waits
    for the user to press the trigger.
  • TAKE_PIC starts the exposure con-
    troller. The exposure controller needs
    1 clock cycle to start the actual expo-
    sure.
  • The end of the exposure is reached
    when the shutter is closed again.
    The next task is to move on the film.
  • While waiting for the end of the film
    transport, two events may occur:
    Either the motor supervision module
    reports an error, then something is
    broken, or the motor terminates nor-
    mally, then we are ready to take the
    next picture.
  • All other states are ignored, i.e. the
    controller will stay in the BROKEN
    state forever.
  • The registers that store the state
    information are modelled with a sec-
    ond process. The next state informa-
    tion replaces the current state data
    with every rising clock edge and thus
    triggers the next state calculation pro-
    vided that the state has changed.
  • The output signals depend upon the
    current state and are generated with
    concurrent statements. This is possi-
    ble because the FSM stays in the
    TAKE_PIC and FILM_TRANS-
    PORT states for one clock cycle, only