• Designed a Sine Wave Generator Hardware, whose frequency could be controlled using a quadrature encoder.
• Coded the FPGA board in System Verilog to display a count (going from 0 to 9999) on to a seven segment board. This count that was displayed, was same as the frequency of the sine wave generated.
• Incorporated a brightness control feature for all the digits being displayed. This was done by changing the duty cycle using a push button for the PWM control.
• Included an additional feature of increasing the count and the frequency by tens, hundreds or thousands, instead of increasing by just one.
2. logic quadA_steady1_past;
logic quadB_steady2_past;
wire count_enable = quadA_steady1 ^ quadA_steady1_past ^ quadB_steady2 ^
quadB_steady2_past;
wire count_rotation_direction = quadA_steady1 ^ quadB_steady2_past;
/////////////////////////Debouncer for QuadA///////////////////
logic hold_something1;
logic hold_something_else1;
logic [3:0] Switch_count1;
always_ff@(posedge inclk0_sig)
begin
hold_something1 <= quadA;
hold_something_else1 <= hold_something1;
end
always_ff@(posedge inclk0_sig)
if(quadA_steady1==hold_something_else1)
Switch_count1 <= 0;
else
begin
Switch_count1 <= Switch_count1 + 1; //Incrementing counter1 for
switching
if(Switch_count1 == 9)
quadA_steady1 <= ~quadA_steady1;
end
///////////////////////////////End of Quad A Debouncer//////////
/////////////////////////Debouncer for QuadB/////////////////////
logic hold_something2;
logic hold_something_else2;
logic [3:0] Switch_count2;
always_ff@(posedge inclk0_sig)
begin
hold_something2 <= quadB;
hold_something_else2 <= hold_something2;
end
always_ff@(posedge inclk0_sig)
if(quadB_steady2==hold_something_else2)
Switch_count2 <= 0;
else
begin
Switch_count2 <= Switch_count2 + 1; //Incrementing counter2 for
switching
if(Switch_count2 == 9)
quadB_steady2 <= ~quadB_steady2;
end
////////////////////////End of QuadB Debouncer///////////////////
/////////////////////////Debouncer for quad_button/////////////////////
logic hold_something3;
logic hold_something_else3;
3. logic [3:0] Switch_count3;
logic quad_button2_steady;
always_ff@(posedge inclk0_sig)
begin
hold_something3 <= quad_button2;
hold_something_else3 <= hold_something3;
end
always_ff@(posedge inclk0_sig)
if(quad_button2_steady==hold_something_else3)
Switch_count3 <= 0;
else
begin
Switch_count3 <= Switch_count3 + 1; //Incrementing counter2 for
switching
if(Switch_count3 == 9)
quad_button2_steady <= ~quad_button2_steady;
end
////////////////////////End of quad_button Debouncer///////////////////
///Making a new clock of 2HZ Frequency using 4 kHz Clock
logic [11:0] number;
logic newclock;
always_ff@(posedge c1_sig) begin
number <= number+1;
if (number< 2000)
newclock <= 1;
else if (number >= 2000 && number < 4000)
newclock <= 0;
else
number <=0;
end
////////////////
//////////////////////////////Mode of Incrementing////
logic [1:0] alpha;
logic [10:0] a_count;
logic tenblock_inc;
logic hundredblock_inc;
logic thousandblock_inc;
//assign alpha = 2'b01;
always_ff@(posedge newclock) begin
if (!quad_button2_steady)
alpha <= alpha+1;
end
always_comb begin
if (alpha ==0)
begin
a_count = 1;
tenblock_inc = 0;
hundredblock_inc = 0;
thousandblock_inc = 0;
4. end
else if (alpha ==1)
begin
a_count = 10;
tenblock_inc = 1;
hundredblock_inc = 0;
thousandblock_inc = 0;
end
else if (alpha ==2)
begin
a_count = 100;
tenblock_inc = 0;
hundredblock_inc = 1;
thousandblock_inc = 0;
end
else ///// (alpha ==3)
begin
a_count = 1000;
tenblock_inc = 0;
hundredblock_inc = 0;
thousandblock_inc = 1;
end
end
//////////////////////////////////End of Mode Incrementing////////////////////
//////////////////////////////Quad
Encoder//////////////////////////////////////////
always_ff@(posedge inclk0_sig)
begin
quadA_steady1_past <= quadA_steady1;
quadB_steady2_past <= quadB_steady2;
end
always_ff@(posedge inclk0_sig or negedge reset_button)
begin
if (!reset_button)
count <=1000;
else
begin
if(count_enable)//////////////////////////////Either Increment or
decrement count
begin
if(count_rotation_direction)
begin
if (count<9999)
count <=
count+a_count;/////////////////////////Increment Count
else
count <= count;
end
else ////////////////////////////////////////decrement count
begin
if (count>0)
count <= count-a_count;
else
count <= 0;
5. end
end
end
end
/////////////////////////////Separating
digits/////////////////////////////////////
always_ff@(posedge inclk0_sig or negedge reset_button)
begin
if (!reset_button)
begin
ones_digit <=0;
tens_digit <=0;
hundreds_digit <=0;
thousands_digit <=1;
end
else
begin
if(count_enable)//////////////////////////////Either Increment or
decrement
begin
if(count_rotation_direction)
//Clockwise Rotation
begin
if (ones_digit ==9 && tens_digit ==9 && hundreds_digit ==9 &&
thousands_digit ==9)
begin
ones_digit <=9;
tens_digit <=9;
hundreds_digit <=9;
thousands_digit <=9;
end
else
/////////Increment Digits Normally
begin
if (tenblock_inc == 0 && hundredblock_inc == 0 &&
thousandblock_inc == 0)
begin
if (ones_digit <9)
ones_digit <= ones_digit+1;
else
// Ones digit is 9
begin
ones_digit <= 0;
if (tens_digit <9)
tens_digit <= tens_digit+1;
else
//Tens digit is 9
begin
tens_digit <= 0;
if (hundreds_digit <9)
hundreds_digit <=
hundreds_digit+1;
else
//Hundreds digit is 9
begin
hundreds_digit <= 0;
if (thousands_digit <9)
thousands_digit
6. <=thousands_digit+1;
else
//Thousands digit is 9
thousands_digit <=0;
//Does'nt even happen
end
end
end
end
/////////////////Increment Digits by tens
else if (tenblock_inc == 1 && hundredblock_inc == 0 &&
thousandblock_inc == 0)
begin
if (tens_digit <9)
tens_digit <= tens_digit+1;
else
//Tens digit is 9
begin
tens_digit <= 0;
if (hundreds_digit <9)
hundreds_digit <= hundreds_digit+1;
else
//Hundreds digit is 9
begin
hundreds_digit <= 0;
if (thousands_digit <9)
thousands_digit
<=thousands_digit+1;
else
//Thousands digit is 9
thousands_digit <=9;
end
end
end
///////////////Increment Digts by Hundreds
else if (tenblock_inc == 0 && hundredblock_inc == 1 &&
thousandblock_inc == 0)
begin
if (hundreds_digit <9)
hundreds_digit <= hundreds_digit+1;
else
//Hundreds digit is 9
begin
hundreds_digit <= 0;
if (thousands_digit <9)
thousands_digit
<=thousands_digit+1;
else
//Thousands digit is 9
thousands_digit <=9;
end
end
///////////////////Increment by thousands
else //if (tenblock_inc == 0 && hundredblock_inc == 0 &&
thousandblock_inc == 1)
begin
if (thousands_digit <9)
thousands_digit <=thousands_digit+1;
else
//Thousands digit is 9
7. thousands_digit <=9;
end
end
end
/////////////////////////////////////////////////////Anticlockwise
Rotation
else
begin
if (ones_digit ==0 && tens_digit ==0 && hundreds_digit ==0 &&
thousands_digit ==0)
begin
ones_digit <=0;
tens_digit <=0;
hundreds_digit <=0;
thousands_digit <=0;
end
else
begin //////////////////////////Normal Decrement
if (tenblock_inc == 0 && hundredblock_inc == 0 &&
thousandblock_inc == 0)
begin
if (ones_digit >0 && ones_digit <10)
ones_digit <= ones_digit-1;
else
// Ones digit is 0
begin
ones_digit <=9;
if (tens_digit >0 && tens_digit <10)
tens_digit <= tens_digit-1;
else
//Tens digit is 0
begin
tens_digit<= 9;
if (hundreds_digit>0 && hundreds_digit
<10)
hundreds_digit <= hundreds_digit-
1;
else
//Hundreds Digit is 0
begin
hundreds_digit<=9;
if (thousands_digit >0 &&
thousands_digit <10)
thousands_digit <=
thousands_digit-1;
else
//Thousands Digit is 0
thousands_digit <= 9;
end
end
end
end
////////Decrement by tens
else if (tenblock_inc == 1 && hundredblock_inc == 0 &&
thousandblock_inc == 0)
begin
8. if (tens_digit >0 && tens_digit <10)
tens_digit <= tens_digit-1;
else
//Tens digit is 0
begin
tens_digit<= 9;
if (hundreds_digit>0 && hundreds_digit <10)
hundreds_digit <= hundreds_digit-
1;
else
//Hundreds Digit is 0
begin
hundreds_digit<=9;
if (thousands_digit >0 &&
thousands_digit <10)
thousands_digit <=
thousands_digit-1;
else
//Thousands Digit is 0
thousands_digit <= 0;
end
end
end
///Decrement by Hundred
else if (tenblock_inc == 0 && hundredblock_inc == 1 &&
thousandblock_inc == 0)
begin
if (hundreds_digit>0 && hundreds_digit <10)
hundreds_digit <= hundreds_digit-1;
else
//Hundreds Digit is 0
begin
hundreds_digit<=9;
if (thousands_digit >0 && thousands_digit
<10)
thousands_digit <= thousands_digit-1;
else
//Thousands Digit is 0
thousands_digit <= 0;
end
end
//////Decrement by Thousand
else if (tenblock_inc == 0 && hundredblock_inc == 0 &&
thousandblock_inc == 1)
begin
if (thousands_digit >0 && thousands_digit <10)
thousands_digit <= thousands_digit-1;
else
//Thousands Digit is 0
thousands_digit <= 0;
end
end
end
end
end
9. end
///////////////// State Machine present state becomes new state every clock
edge//////
always_ff@(posedge c0_sig)
present_state <= next_state;
///Saving the state
//Making it go to the next state. Incrementing present
state/////////////////////
always_comb
begin
unique case (present_state)
4'b0000 : next_state = 4'b0001;
//Ones Digit State
4'b0001 : next_state = 4'b0010;
//Idle State
4'b0010 : next_state = 4'b0011;
//Tens Digit State
4'b0011 : next_state = 4'b0100;
//Idle State
4'b0100 : next_state = 4'b0101;
//Hundreds Digit State
4'b0101 : next_state = 4'b0110;
//Idle State
4'b0110 : next_state = 4'b0111;
//Thousands State
4'b0111 : next_state = 4'b1000;
//Idle State
4'b1000 : next_state = 4'b0000;
//Circles Back
endcase
end
// MUX: Displaying Output of each of three digits; one after the other
always_comb
begin
if (present_state == 0)
begin
muxo = ones_digit;
// mux out is first digit
sel = 3'b000;
en3 = 1'b1;
end
else if (present_state == 1)
//Idle between States
begin
muxo = 9;
// mux out is random
sel = 3'b111;
//Nothing Happens
en3 = 1'b0;
end
else if (present_state == 2)
begin
muxo = tens_digit;
10. // mux out is tens digit
sel = 3'b001;
if (count < 10)
// Zero Supression for Tens Digit
en3 = 1'b0;
else
en3 = 1'b1;
end
else if (present_state == 3)
//Idle between States
begin
muxo = 9;
// mux out is random
sel = 3'b111;
//Nothing Happens
en3 = 1'b0;
end
else if (present_state == 4)
begin
muxo = hundreds_digit; //
mux out is hundreds digit
sel = 3'b011;
if (count < 100)
// Zero Supression for Hundreds Digit
en3 = 1'b0;
else
en3 = 1'b1;
end
else if (present_state == 5)
//Idle State
begin
muxo = 9;
// mux out is random
sel = 3'b111;
//Nothing Happens
en3 = 1'b0;
end
else if (present_state == 6)
begin
muxo = thousands_digit; //
mux out is thousands digit
sel = 3'b100;
if (count < 1000)
// Zero Supression for Thousands Digit
en3 = 1'b0;
else
en3 = 1'b1;
end
else if (present_state == 7)
//Idle State
begin
muxo = 9;
// mux out is random
sel = 3'b111;
//Nothing Happens
en3 = 1'b0;
11. end
else
begin
muxo = 9;
//Doesn't Matter
sel = 3'b011;
//Going nowhere
en3 = 1'b0;
//Also Output is disabled
end
end
///////////////////////bcd to seven segment
decoder///////////////////////////////
always_comb begin
casez (muxo) //gfedcba
4'b0000 : seven_segment = 7'b1000000; //seven
segment display of 0
4'b0001 : seven_segment = 7'b1111001; //seven
segment display of 1
4'b0010 : seven_segment = 7'b0100100; //seven
segment display of 2
4'b0011 : seven_segment = 7'b0110000; //seven
segment display of 3
4'b0100 : seven_segment = 7'b0011001; //seven
segment display of 4
4'b0101 : seven_segment = 7'b0010010; //seven
segment display of 5
4'b0110 : seven_segment = 7'b0000010; //seven
segment display of 6
4'b0111 : seven_segment = 7'b1111000; //seven
segment display of 7
4'b1000 : seven_segment = 7'b0000000; //seven
segment display of 8
4'b1001 : seven_segment = 7'b0010000; //seven
segment display of 9
4'b1111 : seven_segment = 7'b0000000; //Doesn't
Matter Not Going to Display
default : seven_segment = 7'b1000000; //seven
segment displaying 0
endcase
end
///////////////////////////////Sine Wave
Generator/////////////////////////////////
///24 bit Adder
always_comb begin
Add_out = count + DQ_out; ////Using Count
Instead of Individual Digits
end
///DQ Flip Flop
always_ff@(posedge c2_sig) begin
DQ_out <= Add_out;
end
////Extracting the higher order bits for address assignment
always_comb begin
address_sig[10:0] = DQ_out[23:13];
end
12. ////////////////////////////End of Sine Generator///////////
///////////////////////////PWM Brightness
Control/////////////////////////////////////
/////////////////////Begining of first counter block_A with 4 MHz clock
always_ff@(posedge c1_sig)
begin
cout_A <= cout_A +4'b0001;
end
////////////////////////////End of first counter blockA with 4MHz Clock
///////////////////////////Second Counter, counts when button is pushed
always_ff @(posedge newclock)
begin
if (push_steady == 0)
cout_B <= cout_B +1;
else
cout_B <= cout_B;
end
///////////////////////////////End of second counter
///////////////////////////////Comparing the two counts
always_comb
begin
if (cout_A >= cout_B)
pwm_out = 1'b1;
else
pwm_out = 1'b0;
end
//////////////////////////////////////End of PWM Brightness
Control////////////////
endmodule