0% found this document useful (0 votes)
193 views

Interface Verilog Code

The document contains Verilog code for interfacing different modules like a 7-segment display, LCD display, hex keypad, DC motor, and stepper motor. The 7-segment display module uses a case statement to set the output pins for each digit from 0-9 and some additional codes. The LCD module initializes and controls the LCD pins. The hex keypad module reads the row and column inputs and uses a case statement to determine the 7-segment code for the pressed key. The DC motor module uses a PWM signal to control motor speed based on key inputs. The stepper motor module shifts a 4-bit pattern to the output pins to rotate the motor in different directions set by a direction input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views

Interface Verilog Code

The document contains Verilog code for interfacing different modules like a 7-segment display, LCD display, hex keypad, DC motor, and stepper motor. The 7-segment display module uses a case statement to set the output pins for each digit from 0-9 and some additional codes. The LCD module initializes and controls the LCD pins. The hex keypad module reads the row and column inputs and uses a case statement to determine the 7-segment code for the pressed key. The DC motor module uses a PWM signal to control motor speed based on key inputs. The stepper motor module shifts a 4-bit pattern to the output pins to rotate the motor in different directions set by a direction input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 21

Interfacing programs

Verilog code seven segment

module display(clk, q, s);


input clk;
output reg [6:0] q;
output reg [5:0] s;
reg[3:0] i;
always@(posedge clk)
begin
if(i==4'b0000)
s=6'b000000;
case(i)
0:q=7'b0111111;
1:q=7'b0000110;
2: q=7'b1011011;
3:q=7'b1001111;
4: q=7'b1100110;
5: q=7'b1101101;
6: q=7'b1111101;
7: q=7'b0000111;
8: q=7'b1111111;
9: q=7'b1100111;
10: q=7'b1110111;
11: q=7'b1111100;
12: q=7'b0111001;
13: q=7'b1011110;
14: q=7'b1111001;
15: q=7'b1110001;

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

hex key pad

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 @(posedge clk)


begin
dclk=dclk+1'b1;
end

always @( posedge dclk[11])


begin
col={col[2:0],col[3]};
disp_sel= 6'b000000;
end

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;

output reg [3:0] output1;

reg [8:0] cnt=9'b000000000;

reg [15:0] delay=16'b0000000000000000;

reg iclk;

reg [3:0] seq=4'b0011;

always @(posedge clk)

begin

delay=delay+1;

iclk=delay[8];

end

always @(posedge iclk)

begin
if(cnt<=9'b110010000)

begin

// seq={seq[0],seq[3:1]}; // rotate clockwise

seq={seq [2:0], seq [3]}; // rotate anticlockwise

output1=seq;

cnt=cnt+1;

end

else

begin

output1=4'b1111;

end

end

endmodule

#PACE: Start of PACE I/O Pin Assignments

NET "clk" LOC = "p80" ;

NET "output1<0>" LOC = "p138" ;

NET "output1<1>" LOC = "p132" ;

NET "output1<2>" LOC = "p131" ;

NET "output1<3>" LOC = "p125" ;


module dc_mot(clk,reset, pwm, rly, keys,dir);
input clk,reset,dir;
output [1:0] pwm;
output rly;
input [3:0] keys;
reg [1:0] pwm;
reg [16:0] div;
reg tick;
reg [7:0] counter= 0;
reg [7:0] duty_cycle;
always @ (posedge clk)
begin
div = div +1'b1;
tick= keys[3]& keys[2] & keys[1]& keys[0];
end
always @ (negedge tick)
begin
case(keys)
4'b1110: begin duty_cycle = 255;end
4'b1101: begin duty_cycle = 192;end
4'b1011: begin duty_cycle = 128;end
4'b0111: begin duty_cycle = 64;end
default: begin duty_cycle = 64;end
endcase
end
always @ (posedge div[12])
begin
if (reset==1'b1)
begin
counter = 8'b00000000;
pwm = 2'b01;
end
else
begin
counter=counter+1’b1;
if (counter <= duty_cycle)
pwm[1] = 1'b1;
else
pwm[1] = 1'b0;
end
end
assign rly = dir;
endmodule

UCF file(User constraint File)


NET "CLK" LOC = "p80" ;
NET "DIR" LOC = "p51";
NET "pwm<0>" LOC = "p138" ;
NET "pwm<1>" LOC = "p132" ;
NET "RESET" LOC = "p181" ;
NET "rly" LOC = "p131" ;
NET "Keys<0>" LOC = "p108" ;
NET "Keys<1>" LOC = "p107" ;
NET "Keys<2>" LOC = "p102" ;
NET "Keys<3>" LOC = "p101" ;

Stepper motor

Write a Verilog code to control the speed and direction of the stepper motor

module stepper(clk, dir, d_out);


input clk, dir;
output [3:0] d_out;
reg [3:0] d_out;
reg [15:0] clk_div;
reg clk_int;
reg [3:0] shift_reg = 4'b1001;
integer i;
always@(posedge clk)
clk_div = clk_div + 1;

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

UCF FOR STEPPER MOTOR:

NET "clk" LOC = "p80";


NET "dir" LOC = "p51";
NET "d_out<0>" LOC = "p131";
NET "d_out<1>"LOC = "p125";
NET "d_out<2>"LOC = "p124";
NET "d_out<3>" LOC = "p123";

Second method
Main program
module step_motor(
input clk,
input rst,
input dir,
input en,
output [3:0] signal_out
);

// Wire to connect the clock signal


// that controls the speed that the motor
// steps from the clock divider to the
// state machine.
wire new_clk;

// Clock Divider to take the on-board clock


// to the desired frequency.
clock_div clock_Div(
.clk(clk),
.rst(rst),
.new_clk(new_clk)
);

// The state machine that controls which


// signal on the stepper motor is high.
step_driver control(
.rst(rst),
.dir(dir),
.clk(new_clk),
.en(en),
.signal(signal_out)
);

endmodule

module step_driver(
input rst,
input dir,
input clk,
input en,
output reg [3:0] signal
);

// local parameters that hold the values of


// each of the states. This way the states
// can be referenced by name.
localparam sig4 = 3'b001;
localparam sig3 = 3'b011;
localparam sig2 = 3'b010;
localparam sig1 = 3'b110;
localparam sig0 = 3'b000;

// register values to hold the values


// of the present and next states.
reg [2:0] present_state, next_state;

// run when the present state, direction


// or enable signals change.
always @ (present_state, dir, en)
begin
// Based on the present state
// do something.
case(present_state)
// If the state is sig4, the state where
// the fourth signal is held high.
sig4:
begin
// If direction is 0 and enable is high
// the next state is sig3. If direction
// is high and enable is high
// next state is sig1. If enable is low
// next state is sig0.
if (dir == 1'b0 && en == 1'b1)
next_state = sig3;
else if (dir == 1'b1 && en == 1'b1)
next_state = sig1;
else
next_state = sig0;
end
sig3:
begin
// If direction is 0 and enable is high
// the next state is sig2. If direction
// is high and enable is high
// next state is sig4. If enable is low
// next state is sig0.
if (dir == 1'b0&& en == 1'b1)
next_state = sig2;
else if (dir == 1'b1 && en == 1'b1)
next_state = sig4;
else
next_state = sig0;
end
sig2:
begin
// If direction is 0 and enable is high
// the next state is sig1. If direction
// is high and enable is high
// next state is sig3. If enable is low
// next state is sig0.
if (dir == 1'b0&& en == 1'b1)
next_state = sig1;
else if (dir == 1'b1 && en == 1'b1)
next_state = sig3;
else
next_state = sig0;
end
sig1:
begin
// If direction is 0 and enable is high
// the next state is sig4. If direction
// is high and enable is high
// next state is sig2. If enable is low
// next state is sig0.
if (dir == 1'b0&& en == 1'b1)
next_state = sig4;
else if (dir == 1'b1 && en == 1'b1)
next_state = sig2;
else
next_state = sig0;
end
sig0:
begin
// If enable is high
// the next state is sig1.
// If enable is low
// next state is sig0.
if (en == 1'b1)
next_state = sig1;
else
next_state = sig0;
end
default:
next_state = sig0;
endcase
end

// State register that passes the next


// state value to the present state
// on the positive edge of clock
// or reset.
always @ (posedge clk, posedge rst)
begin
if (rst == 1'b1)
present_state = sig0;
else
present_state = next_state;
end

// 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;

// Count value that counts to define_speed


reg [25:0] count;

// Run on the positive edge of the clk and rst signals


always @ (posedge(clk),posedge(rst))
begin
// When rst is high set count and new_clk to 0
if (rst == 1'b1)
begin
count = 26'b0;
new_clk = 1'b0;
end
// When the count has reached the constant
// reset count and toggle the output clock
else if (count == define_speed)
begin
count = 26'b0;
new_clk = ~new_clk;
end
// increment the clock and keep the output clock
// the same when the constant hasn't been reached
else
begin
count = count + 1'b1;
new_clk = new_clk;
end
end
endmodule

#PACE: Start of PACE I/O Pin Assignments


NET "clk" LOC = "p80" ;
NET "dir" LOC = "p52" ;
NET "en" LOC = "p51" ;
NET "rst" LOC = "p180" ;
NET "signal_out<0>" LOC = "p131" ;
NET "signal_out<1>" LOC = "p125" ;
NET "signal_out<2>" LOC = "p124" ;
NET "signal_out<3>" LOC = "p123" ;

Write a Verilog code to generate the following waveforms.

a. Square wave

module square_wave(clk, reset, dac_out);


input clk, reset;
output [7:0] dac_out;
reg [7:0] dac_out;
reg [7:0] counter = 8'b00000000;
reg [3:0]temp=3'b0;
reg enable=1'b0;
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 = 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

module tri_wave(clk, reset, dac_out);


input clk, reset;
output [7:0] dac_out;
reg [7:0] dac_out;
reg [7:0] counter;
reg [3:0] temp = 3'b0;
reg enable = 1'b0;

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

module saw_wave(clk, reset, dac_out);


input clk, reset;
output [7:0] dac_out;
reg [7:0] dac_out;
reg [7:0] counter;
reg [3:0]temp = 3'b0;
always@(posedge clk)
temp = temp + 1;

always@(posedge temp[3])
begin
if (reset)
counter = 8'b00000000;
else
counter = counter+1;
dac_out = counter;
end
endmodule

d. Sine wave

module sine_wave(clk, reset, dac_out);


input clk, reset;
output [7:0] dac_out;
reg [7:0] dac_out;
reg [7:0] sine [0:71];
reg [3:0] temp = 3'b0;
integer i;

always@(posedge clk)
temp = temp + 1;

always@( posedge temp[3])


begin
if (reset)
begin
sine[0]= 128; // 128 + 128 sinθ
sine[1] = 139;
sine[2] = 150;
sine[3] = 161;
sine[4] = 171;
sine[5] = 182;
sine[6] = 192;
sine[7] = 201;
sine[8] = 210;
sine[9] = 218;
sine[10] = 226;
sine[11] = 232;
sine[12] = 238;
sine[13] = 244;
sine[14] = 248;
sine[15] = 251;
sine[16] = 254;
sine[17] = 255;
sine[18] = 255;
sine[19] = 255;
sine[20] = 254;
sine[21] = 251;
sine[22] = 248;
sine[23] = 244;
sine[24] = 238;
sine[25] = 232;
sine[26] = 226;
sine[27] = 218;
sine[28] = 210;
sine[29] = 201;
sine[30] = 192;
sine[31] = 182;
sine[32] = 171;
sine[33] = 161;
sine[34] = 150;
sine[35] = 139;
sine[36] = 128;
sine[37] = 116;
sine[38] = 105;
sine[39] = 94;
sine[40] = 84;
sine[41] = 73;
sine[42] = 64;
sine[43] = 55;
sine[44] = 45;
sine[45] = 37;
sine[46] = 29;
sine[47] = 23;
sine[48] = 17;
sine[49] = 11;
sine[50] = 7;
sine[51] = 4;
sine[52] = 1;
sine[53] = 0;
sine[54] = 0;
sine[55] = 0;
sine[56] = 1;
sine[57] = 4;
sine[58] = 7;
sine[59] = 11;
sine[60] = 17;
sine[61] = 23;
sine[62] = 29;
sine[63] = 37;
sine[64] = 45;
sine[65] = 55;
sine[66] = 64;
sine[67] = 73;
sine[68] = 84;
sine[69] = 94;
sine[70] = 105;
sine[71] = 116;
end
else
begin
dac_out = sine[i];
i = i+ 1;
if(i == 72)
i = 0;
end
end
endmodule

UCF FOR DAC:

NET "clk" LOC= "p80";


NET "dac_out<0>" LOC= "p114";
NET "dac_out<1>" LOC= "p109";
NET "dac_out<2>" LOC= "p108";
NET "dac_out<3>" LOC= "p107";
NET "dac_out<4>" LOC= "p102";
NET "dac_out<5>" LOC= "p101";
NET "dac_out<6>" LOC= "p100";
NET "dac_out<7>" LOC= "p97";
NET "reset" LOC= "p181";

Elevator

module ele( r_floor, r_door,r_callfloor, r_finish, o_floor, o_door, o_elevator


);
input o_door, r_callfloor;
output reg [3:0]r_floor;
output reg r_door, r_finish;
input [3:0]o_floor;
output reg [1:0] o_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

You might also like