Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

state machine

Status
Not open for further replies.

brian02

Programmer
Nov 18, 2007
5
NL
Hi guys,

I was wondering if someone could help me with a simple state machine i'm trying to get to work.

I'll try to explain what it needs to do and then my "source code" so far.

Suppose you step out of the car and the lights are still on and door is open, then a beeper should alert you about it.

The first 5 seconds the beeper should beep with a pause of 1 second between the beep sounds. After that with a pause of 500 milliseconds. And then with no pause untill door is closed.

This is my code:



library IEEE;
use IEEE.std_logic_1164.all;


entity carbeeper is
port ( clk : in STD_LOGIC;
reset : in STD_LOGIC;
door : in STD_LOGIC;
light : in STD_LOGIC;
beeper : out STD_LOGIC );
end carbeeper;



-- SYMBOLIC ENCODED state machine: Sreg0
type Sreg0_type is (S1, S2, S3, S4, S5, S6);
signal Sreg0: Sreg0_type;

Signal state: bit_vector( 2 downto 0 );


begin


Sreg0_machine:
process (clk,reset)
begin

if (reset = '1') then state <= "000";
elsif (clk ='1' and clk'event) then
case state is


when "000"=> --idle


if (door = '1' & lamp = '1') then beeper <= '1';
state <= "001";

else beeper <= '0';
state <="000"


when "001" -- door open?


when S3 =>
if yes then
Sreg0 <= S4;
Reset low;
elsif no then
Sreg0 <= S1;
Reset low;
end if;
when S4 =>
if yes then
Sreg0 <= S5;
beeper on;
clk high;
elsif no then
Sreg0 <= S1;
clk high;
end if;
when S5 =>
if yes then
Sreg0 <= S6;
switch off;
end if;
when S6 =>
if no then
Sreg0 <= S5;
beeper on;
elsif yes then
Sreg0 <= S1;
end if;
when others =>
null;
end case;
end if;
end if;
end process;

end carbeeper;
 
Hi Brian,

You need to close the first case statement and open the second one:

Code:
  ...

    when "001"        -- door open?
 
  end case;

  -- This is a different type!!!
  case Sreg0 is          
            
    when S3 =>
         
  ...
Bye...
 
Hi,

Did ya mean like this:

when "001" -- door open?
end case;

Sreg0 is
when S3 =>
if yes then
Sreg0 <= S4;
Reset low;
elsif no then
Sreg0 <= S1;
Reset low;
end if;

How can i implement a method to do the beep sound with different speeds?

Thanks in advance!
 
Hi Brian,
To beep sound with different speeds, you can define new state. I suppose "010". The idea is as follows :



............
variable count : integer range 0 to MAXTIME; --you can define MAXTIME as you like
signal SPEED : integer range 0 to MAXTIME;
--SPEED is the time you want to change in your design

when "010 =>
if count<SPEED then
state <= "010";
count:=count+1;
beep<='1';
else
state <= "---" -- Go to the new state
beep<='0';
end if;
....

I advise you should design your state machine with 2 process. (one process combinatory and one process sequential). It is clear and it make your design better when working.
 
Hi bonami172,

Thanks for the sample code.

I think you kind of misunderstood me.

I'll try to explain.

Suppose the door is open and the lights are still on, the car will beep to notify you about it.

The sound of the beep will be short at first, like this:

Beep > 1000ms delay > beep > 1000ms delay > beep > 1000ms delay.

And if the lights are still on and the door too. It will beep faster. The delay will be shorter. This time with 500 ms.

And if the lights are still on and the door still open. Then at this point the car should beep with no delay.

I dont understand how to do this with the count and speed variable.

Can you explain that for me please?

Thanks in advance!
 
Hi,
I try to help you with my source code. I don't know how many beep 500ms you want ? But i think you can modify the source ode as you like.
I suppose the clock frequency is 1MHz.
Here is the state machine by using one process. I like to design this with 2 process but i think you are familar with one process. So, il will try this way.

type state_type is (S0,S1, S2, S3, S4, S5);
signal state: state_type;


begin


process (clk,reset)
variable count : integer range 0 to 1023;
variable speed : integer range 0 to 1023;
begin

if (reset = '1') then
state <= S0;
count: =0;
speed : =1000;

elsif (clk ='1' and clk'event) then
case state is
when S0 => idle
if (door = '1' and lamp = '1') then
beeper <= '1';
state <= S1;
else
beeper <= '0';
state <= S0"
when S1 => ---1000 ms
state <= S1;
beeper <='0';
if (count=speed) then
beeper <='1';
count:=0;
if (door = '1' and lamp = '1') then
state<=S2;
else
state<=S0;
end if;
else
count:=count+1;
end if;
when S2 => 1000 ms
state <= S2;
beeper <='0';
if (count=speed) then
beeper <='1';
count:=0;
if (door = '1' and lamp = '1') then
state<=S3;
else
state<=S0;
end if;
else
count:=count+1;
end if;
when S3 => ---- 1000ms
state <= S3;
beeper <='0';
if (count=speed) then
beeper <='1';
count:=0;
if (door = '1' and lamp = '1') then
state<=S4;
speed:=500 ms ; --change speed
else
state<=S0;
end if;
else
count:=count+1;
end if;
when S4 => ---- 500ms
state <= S4;
beeper <='0';
if (count=speed) then
beeper <='1';
count:=0;
if (door = '1' and lamp = '1') then
state<=S5;
else
state<=S0;
end if;
else
count:=count+1;
end if;
when S5 => -------beep no delay
if (door = '1' and lamp = '1') then
state <=S5;
beeper <='1';
else
beeper <='0';
state <=S0;
end if;
end case;
end process;
 
Hi bonami172,

Thanks for your reply.

I am better at programming in c/c++, java, C# and php.

These languages kind of look a like.

The syntax of vhdl is totally different.

I'll study this and see how it works.

If i don't get it, i know where to find ya :p

Thank you very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top