what is wrong with my code?

OP #2110153
Rate this post
• ▲ useful
▼ not useful
I AM A NEWBIE IN VERILOG AND JUST DONT KNOW WHAT IS WRONG WITH MY CODE.
THIS IS THE CRAZY SIMPLE QUESTION :

Develop a Verilog model for a thermostat that has two 8-bit unsigned 
binary inputs representing the target temperature and the actual 
temperature in degrees Fahrenheit (˚F). Assume that both temperatures 
are above freezing (32˚F). The detector has two outputs: one to turn a 
heater on when the actual temperature is more than 5˚F below target, and 
one to turn a cooler onwhen the actual temperature is more than 5˚F 
above target.

AND THIS IS MY CODE:

module*C2*(switch,clk,heater_on,cooler_on,enable_a ctual,enable_target);


input clk;
input enable_actual,enable_target;
input*[7:0]*switch*;
reg [7:0]*actual,target;
output* heater_on,*cooler_on;

always @(posedge clk)
begin
if (enable_actual) actual <= switch;

else if (enable_target) target <= switch;

end

assign*heater_on*=*actual*<*target*-*5;
assign*cooler_on*=*actual*>*target*+*5;

endmodule

IT DIDN'T SHOW ANY ERROR DURING COMPILATION.THE PROBLEM IS THAT THE 
OUTPUT WHICH HEATER_ON AND COOLER_ON DIDN'T ON CORRECTLY.WHAT IS WRONG 
WITH MY CODE??CAN SOMEBODY PLS HELP ME.

PICTURE2 IS THE SIMULATION WHEN I WANT THE HEATER_ON ONLY TO BE ON
PICTURE4 IS THE SIMULATION WHEN I WANT THE COOLER_ON ONLY TO BE ON

BUT THE SIMULATION WAS NOT WHAT I EXPECTED FOR:HELP PLS
Attached files:
Guest #2147975
Rate this post
• ▲ useful
▼ not useful
I took the liberty to format your code a little:
1
module C2 (switch, clk, heater_on, cooler_on, enable_actual, enable_target);
2
    input  clk;
3
    input  enable_actual,
4
    input  enable_target;
5
    input  [7:0] switch;
6
    reg    [7:0] actual;
7
    reg    [7:0] target;
8
    output heater_on;
9
    output cooler_on;
10
    
11
    always @(posedge clk)
12
    begin
13
        if (enable_actual)
14
            actual <= switch;
15
        else
16
            if (enable_target)
17
                target <= switch;
18
    end
19
    
20
    assign heater_on = actual < (target-5);
21
    assign cooler_on = actual > (target+5);
22
endmodule

please add the signals "actual" and "target" to your simulation.

it also would be nice to see the 8 bit registers as decimals instead of 
characters.

as long as "actual" and "target" are not set to something else than "0", 
don't wonder "heater_on = actual < (target-5);" is true, as (0-5) = 
(256-5) = 251

so I would set target first.

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [vhdl]VHDL-Code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.