Interface Verilog Code
Interface Verilog Code
endcase
i=i+1;
end
endmodule
#PACE: Start of PACE I/O Pin Assignments
NET "clk" LOC = "p80" ;
NET "q<0>" LOC = "p114" ;
NET "q<1>" LOC = "p109" ;
NET "q<2>" LOC = "p108" ;
NET "q<3>" LOC = "p107" ;
NET "q<4>" LOC = "p102" ;
NET "q<5>" LOC = "p101" ;
NET "q<6>" LOC = "p100" ;
NET "s<0>" LOC = "p96" ;
NET "s<1>" LOC = "p95" ;
NET "s<2>" LOC = "p94" ;
NET "s<3>" LOC = "p90" ;
NET "s<4>" LOC = "p86" ;
NET "s<5>" LOC = "p81" ;
Apply manual clock, to change each state press manual clockk each time.
LCD
Module LCD (clk, lcd_rs, lcd_rw, lcd_e, lcd_4, lcd_5, lcd_6, lcd_7);
parameter n = 27;
parameter k = 17;
(* LOC="p80" *) input clk; // synthesis attribute PERIOD clk "100.0 MHz"
reg [n-1:0] count=0;
reg lcd_busy=1; // Lumex LCM-S01602DTR/B
reg lcd_stb;
reg [7:0] lcd_code;
reg [6:0] lcd_stuff;
(* LOC="p96" *) output reg lcd_rs;
(* LOC="p95" *) output reg lcd_rw;
(* LOC="p97" *) output reg lcd_7;
(* LOC="p100" *) output reg lcd_6;
(* LOC="p101" *) output reg lcd_5;
(* LOC="p102" *) output reg lcd_4;
(* LOC="p94" *) output reg lcd_e;
always @ (posedge clk) begin
count <= count + 1;
case (count[k+7:k+2])
0: lcd_code <= 8'b00000010; // function set
1: lcd_code <= 8'b00000010;
2: lcd_code <= 8'b00001100;
3: lcd_code <= 8'b00000000; // display on/off control
4: lcd_code <= 8'b00001100;
5: lcd_code <= 8'b00000000; // display clear
6: lcd_code <= 8'b00000001;
7: lcd_code <= 8'b00000000; // entry mode set
8: lcd_code <= 8'b00000110;
// 9: lcd_code <= 6'h24; // H
10: lcd_code <= 8'h21;
//11: lcd_code <= 6'h26; // e
12: lcd_code <= 8'h25;
// 13: lcd_code <= 6'h26; // l
14: lcd_code <= 8'h2C;
// 15: lcd_code <= 6'h26; // l
16: lcd_code <= 8'h2C;
// 17: lcd_code <= 6'h26; // o
18: lcd_code <= 8'h2F;
// 19: lcd_code <= 6'h22; //
20: lcd_code <= 8'h20;
//21: lcd_code <= 6'h25; // W
22: lcd_code <= 8'h27;
// 23: lcd_code <= 6'h26; // o
24: lcd_code <= 8'h2F;
// 25: lcd_code <= 6'h27; // r
26: lcd_code <= 8'h22;
// 27: lcd_code <= 6'h26; // l
28: lcd_code <= 8'h2C;
// 29: lcd_code <= 6'h26; // d
30: lcd_code <= 8'h24;
// 31: lcd_code <= 6'h22; // !
32: lcd_code <= 8'h21;
default: lcd_code <= 8'b00010000;
endcase
if (lcd_rw)
lcd_busy <= 0;
lcd_stb <= ^count[k+1:k+0] & ~lcd_rw & lcd_busy; // clkrate / 2^(k+2)
lcd_stuff <= {lcd_stb,lcd_code};
{lcd_e,lcd_rs,lcd_rw,lcd_7,lcd_6,lcd_5,lcd_4} <= lcd_stuff;
end
endmodule
module LCD_HEX(col,row,clk,disp_sel,ss);
output [3:0] col;
initial col=4'b0001;
input [3:0] row;
input clk;
output [5:0] disp_sel;
output [6:0] ss;
reg [3:0]col;
reg[6:0] ss;
reg[5:0] disp_sel;
reg [11:0]dclk;
always @*
begin
case (col)
4'b0001:case (row)
4'b0001:ss= 7'b0111111; //0
4'b0010:ss= 7'b0000110; //1
4'b0100:ss= 7'b1011011; //2
4'b1000:ss= 7'b1001111; //3
default:ss= 7'b0000000;
endcase
4'b0010: case (row)
4'b0001:ss = 7'b1100110; //4
4'b0010:ss = 7'b1101101; //5
4'b0100:ss = 7'b1111101; //6
4'b1000:ss = 7'b0000111; //7
default:ss = 7'b0000000;
endcase
4'b0100: case (row)
4'b0001:ss = 7'b1111111; //8
4'b0010:ss = 7'b1100111; //9
4'b0100:ss = 7'b1110111; //a
4'b1000:ss = 7'b1111100; //b
default:ss = 7'b0000000;
endcase
4'b1000: case (row)
4'b0001:ss = 7'b0111001; //c
4'b0010:ss = 7'b1011110; //d
4'b0100:ss = 7'b1111001; //e
4'b1000:ss = 7'b1110001; //f
default:ss = 7'b0000000;
endcase
default:ss=7'b0000000;
endcase
end
endmodule
UCF file
#PACE: Start of PACE I/O Pin Assignments
NET "clk" LOC = "p80" ;
NET "col<0>" LOC = "p138" ;
NET "col<1>" LOC = "p132" ;
NET "col<2>" LOC = "p131" ;
NET "col<3>" LOC = "p125" ;
NET "disp_sel<0>" LOC = "p96" ;
NET "disp_sel<1>" LOC = "p95" ;
NET "disp_sel<2>" LOC = "p94" ;
NET "disp_sel<3>" LOC = "p90" ;
NET "disp_sel<4>" LOC = "p86" ;
NET "disp_sel<5>" LOC = "p81" ;
NET "row<0>" LOC = "p124" ;
NET "row<1>" LOC = "p123" ;
NET "row<2>" LOC = "p116" ;
NET "row<3>" LOC = "p115" ;
NET "ss<0>" LOC = "p114" ;
NET "ss<1>" LOC = "p109" ;
NET "ss<2>" LOC = "p108" ;
NET "ss<3>" LOC = "p107" ;
NET "ss<4>" LOC = "p102" ;
NET "ss<5>" LOC = "p101" ;
NET "ss<6>" LOC = "p100" ;
dc motor
module motor_clock(clk,inp,output1);
input clk,inp;
reg iclk;
begin
delay=delay+1;
iclk=delay[8];
end
begin
if(cnt<=9'b110010000)
begin
output1=seq;
cnt=cnt+1;
end
else
begin
output1=4'b1111;
end
end
endmodule
Stepper motor
Write a Verilog code to control the speed and direction of the stepper motor
always@(clk_div)
clk_int=clk_div[15];
always@(posedge clk_int)
begin
if (dir == 0)
shift_reg = {shift_reg[0] , shift_reg[3:1]};
else
shift_reg = {shift_reg[2:0] , shift_reg[3]};
d_out = shift_reg;
end
endmodule
Second method
Main program
module step_motor(
input clk,
input rst,
input dir,
input en,
output [3:0] signal_out
);
endmodule
module step_driver(
input rst,
input dir,
input clk,
input en,
output reg [3:0] signal
);
// Output Logic
// Depending on the state
// output signal has a different
// value.
always @ (posedge clk)
begin
if (present_state == sig4)
signal = 4'b1000;
else if (present_state == sig3)
signal = 4'b0100;
else if (present_state == sig2)
signal = 4'b0010;
else if (present_state == sig1)
signal = 4'b0001;
else
signal = 4'b0000;
end
endmodule
module clock_div(
input clk,
input rst,
output reg new_clk
);
// The constant that defines the clock speed.
// Since the system clock is 100MHZ,
// define_speed = 100MHz/(2*desired_clock_frequency)
localparam define_speed = 26'd5000000;
a. Square wave
always@(posedge temp[3])
begin
if (reset)
counter = 8'b00000000;
else if ((counter < 255) && (enable == 0))
begin
counter = counter+1;
dac_out = 8'b00000000;
end
else if (counter == 0)
enable = 0;
else
begin
enable = 1;
counter = counter-1;
dac_out = 8'b11111111;
end
end
endmodule
b. Triangular wave
always@(posedge clk)
temp = temp + 1;
always@(posedge temp[3])
begin
if (reset)
counter = 8'b00000000;
else if ((counter < 255) && (enable == 0))
begin
counter = counter+1;
dac_out = counter;
end
else if (counter == 0)
enable = 0;
else
begin
enable = 1;
counter = counter-1;
dac_out = counter;
end
end
endmodule
c. Sawtooth wave
always@(posedge temp[3])
begin
if (reset)
counter = 8'b00000000;
else
counter = counter+1;
dac_out = counter;
end
endmodule
d. Sine wave
always@(posedge clk)
temp = temp + 1;
Elevator
always @ (o_door,o_floor,r_callfloor)
begin
if (r_finish !=1)
begin
if (o_floor == 1 && o_door == 1) //stage 0
begin
r_floor = 4'b0001;
r_door = 0;
o_elevator = 0;
r_finish = 1;
end
else if (o_floor == 1 && o_door == 0 && r_callfloor == 1) //stage 1
begin
r_floor = 4'b0001;
r_door = 1;
o_elevator = 0;
r_finish = 0;
end
else if (o_floor == 1 && o_door == 0 && r_callfloor != 1) //stage 2
begin
r_floor = 4'b0010;
r_door = 0;
o_elevator = 2'b01;
r_finish = 0;
end
else if (o_floor == 2 && o_door == 0 && r_callfloor == 1) //stage 3
begin
r_floor = 4'b0001;
r_door = 0;
o_elevator = 2'b10;
r_finish = 0;
end
else if (o_floor == 2 && o_door == 1) //stage 4
begin
r_floor = 4'b0010;
r_door = 0;
o_elevator = 0;
r_finish = 1;
end
end
end
endmodule