module lab5(
input logic quadA,
input logic quadB,
output logic [6:0] seven_segment,
output logic [2:0] sel,
output logic [7:0] q_sig,
input logic reset_button,
output reg en2,
output reg en3,
output reg midpin,
output logic pwm_out,
output logic quad_button1,
input logic quad_button2,
input inclk0_sig
);
/////Clocks Generated from PLL
logic c0_sig;
logic c1_sig;
logic c2_sig;
logic [10:0] address_sig;
PLL PLL_inst (
.inclk0 ( inclk0_sig ),
.c0 ( c0_sig ),
//10 KHz Clock
.c1 ( c1_sig ),
// 4 KHz Clock
.c2 ( c2_sig )
// 16.77216 MHz Clock
);
Rom_Sin Rom_Sin_inst (
.address ( address_sig ),
.clock ( clock_sig ),
.q ( q_sig )
);
wire clock_sig = c2_sig;
assign en2 = 1'b0;
assign midpin = 1'b0;
assign pwm_out = 1'b0;
assign quad_button1 = 1'b0;
logic [3:0] muxo;
logic [3:0] present_state;
logic [3:0] next_state;
logic [3:0] hundreds_digit;
logic [3:0] tens_digit;
logic [3:0] thousands_digit;
logic [3:0] ones_digit;
logic [13:0] count;
logic [23:0] Add_out;
logic [23:0] DQ_out;
////Logic needed for quad encoders
logic quadA_steady1;
logic quadB_steady2;
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;
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;
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;
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
<=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
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
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
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;
// 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;
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
////////////////////////////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

More Related Content

PPT
PDF
it's only abuse if it crashes
PDF
Алексей Кутумов, Coroutines everywhere
ODP
Rust言語紹介
PDF
Rust: код может быть одновременно безопасным и быстрым, Степан Кольцов
TXT
Snake.c
PPTX
Tokyo APAC Groundbreakers tour - The Complete Java Developer
it's only abuse if it crashes
Алексей Кутумов, Coroutines everywhere
Rust言語紹介
Rust: код может быть одновременно безопасным и быстрым, Степан Кольцов
Snake.c
Tokyo APAC Groundbreakers tour - The Complete Java Developer

What's hot (20)

PDF
Rust concurrency tutorial 2015 12-02
PPT
Verilog code
PDF
Rust Mozlando Tutorial
PPT
为什么 rust-lang 吸引我?
PDF
Debugging TV Frame 0x09
PPTX
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
PDF
Perl 5.10 on OSDC.tw 2009
PDF
Cilk Plus Parallel Reduction
PDF
C++ L07-Struct
PPTX
C++ Lambda and concurrency
KEY
20110424 action scriptを使わないflash勉強会
DOCX
Oops pramming with examples
PDF
Seu primeiro loop com Python AsyncIO - TDC 2016
PPTX
Linked list
PPT
Евгений Крутько, Многопоточные вычисления, современный подход.
ODP
Ravada VDI Eslibre
PDF
Ethereum virtual machine for Developers Part 1
PDF
Asterisk: PVS-Studio Takes Up Telephony
PDF
Ugly code
PPTX
The Node.js Event Loop: Not So Single Threaded
Rust concurrency tutorial 2015 12-02
Verilog code
Rust Mozlando Tutorial
为什么 rust-lang 吸引我?
Debugging TV Frame 0x09
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Perl 5.10 on OSDC.tw 2009
Cilk Plus Parallel Reduction
C++ L07-Struct
C++ Lambda and concurrency
20110424 action scriptを使わないflash勉強会
Oops pramming with examples
Seu primeiro loop com Python AsyncIO - TDC 2016
Linked list
Евгений Крутько, Многопоточные вычисления, современный подход.
Ravada VDI Eslibre
Ethereum virtual machine for Developers Part 1
Asterisk: PVS-Studio Takes Up Telephony
Ugly code
The Node.js Event Loop: Not So Single Threaded
Ad

Viewers also liked (6)

PDF
Design of 17-Bit Audio Band Delta-Sigma Analog to Digital Converter
PDF
Ece593 project1 chien_chun_yao_and_karthikvel_rathinavel
TXT
Digital Voltmeter displaying voltage level on a seven segment display and com...
PDF
Ece 523 project – fully differential two stage telescopic op amp
PDF
ECE 626 project report Switched Capacitor
PDF
Two stage folded cascode op amp design in Cadence
Design of 17-Bit Audio Band Delta-Sigma Analog to Digital Converter
Ece593 project1 chien_chun_yao_and_karthikvel_rathinavel
Digital Voltmeter displaying voltage level on a seven segment display and com...
Ece 523 project – fully differential two stage telescopic op amp
ECE 626 project report Switched Capacitor
Two stage folded cascode op amp design in Cadence
Ad

Similar to Sine Wave Generator with controllable frequency displayed on a seven segment board using FPGA Board (20)

PDF
To designing counters using verilog code
PDF
I have written the code but cannot complete the assignment please help.pdf
PDF
ARM 7 LPC 2148 lecture
PDF
Pwm wave
PDF
PWM wave generator using microcontroller
PDF
Mdp plus 2.1
PDF
Open bot
PPT
ch5_additional.ppt
DOCX
Direct analog
PDF
PDF
OpenBot-Code
PDF
The IoT Academy IoT Training Arduino Part 3 programming
PDF
include ltiostreamgt include ltstringgt include .pdf
TXT
Senior design project code for PPG
PDF
DomCode 2015 - Abusing phones to make the internet of things
PDF
Musical Machines and Flapping Phones
DOC
GreyCount
PDF
Verilog_Examples (1).pdf
PDF
Solution Manual for Data Structures and Algorithm Analysis in C++, 4/E 4th Ed...
PDF
Actor Concurrency
To designing counters using verilog code
I have written the code but cannot complete the assignment please help.pdf
ARM 7 LPC 2148 lecture
Pwm wave
PWM wave generator using microcontroller
Mdp plus 2.1
Open bot
ch5_additional.ppt
Direct analog
OpenBot-Code
The IoT Academy IoT Training Arduino Part 3 programming
include ltiostreamgt include ltstringgt include .pdf
Senior design project code for PPG
DomCode 2015 - Abusing phones to make the internet of things
Musical Machines and Flapping Phones
GreyCount
Verilog_Examples (1).pdf
Solution Manual for Data Structures and Algorithm Analysis in C++, 4/E 4th Ed...
Actor Concurrency

More from Karthik Rathinavel (7)

PDF
Low Noise Amplifier at 2 GHz using the transistor NE85639 in ADS
PDF
Ece523 folded cascode design
PDF
Differntial Input to Single Ended Output, Two stage Op-amp
PDF
Continuous Low Pass Filter Realization using Cascaded stages of Tow-Thomas Bi...
DOCX
Transmitting Digital Signal through Light Pulses
PDF
Project Report
PPTX
Project presentation
Low Noise Amplifier at 2 GHz using the transistor NE85639 in ADS
Ece523 folded cascode design
Differntial Input to Single Ended Output, Two stage Op-amp
Continuous Low Pass Filter Realization using Cascaded stages of Tow-Thomas Bi...
Transmitting Digital Signal through Light Pulses
Project Report
Project presentation

Recently uploaded (20)

PDF
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
PPT
UNIT-I Machine Learning Essentials for 2nd years
PPTX
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
PDF
VTU IOT LAB MANUAL (BCS701) Computer science and Engineering
PDF
IAE-V2500 Engine for Airbus Family 319/320
PPTX
Solar energy pdf of gitam songa hemant k
PDF
Mechanics of materials week 2 rajeshwari
PPTX
Module1.pptxrjkeieuekwkwoowkemehehehrjrjrj
PDF
THE PEDAGOGICAL NEXUS IN TEACHING ELECTRICITY CONCEPTS IN THE GRADE 9 NATURAL...
PPT
Basics Of Pump types, Details, and working principles.
PPTX
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
PPTX
22ME926Introduction to Business Intelligence and Analytics, Advanced Integrat...
PDF
Performance, energy consumption and costs: a comparative analysis of automati...
PDF
25AF1191PC303 MODULE-1 CHAIN SURVEYING SEMESTER III SURVEYING
PDF
electrical machines course file-anna university
PDF
AIGA 012_04 Cleaning of equipment for oxygen service_reformat Jan 12.pdf
PDF
ASPEN PLUS USER GUIDE - PROCESS SIMULATIONS
PPTX
Software-Development-Life-Cycle-SDLC.pptx
PDF
Software defined netwoks is useful to learn NFV and virtual Lans
PPTX
Unit IILATHEACCESSORSANDATTACHMENTS.pptx
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
UNIT-I Machine Learning Essentials for 2nd years
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
VTU IOT LAB MANUAL (BCS701) Computer science and Engineering
IAE-V2500 Engine for Airbus Family 319/320
Solar energy pdf of gitam songa hemant k
Mechanics of materials week 2 rajeshwari
Module1.pptxrjkeieuekwkwoowkemehehehrjrjrj
THE PEDAGOGICAL NEXUS IN TEACHING ELECTRICITY CONCEPTS IN THE GRADE 9 NATURAL...
Basics Of Pump types, Details, and working principles.
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
22ME926Introduction to Business Intelligence and Analytics, Advanced Integrat...
Performance, energy consumption and costs: a comparative analysis of automati...
25AF1191PC303 MODULE-1 CHAIN SURVEYING SEMESTER III SURVEYING
electrical machines course file-anna university
AIGA 012_04 Cleaning of equipment for oxygen service_reformat Jan 12.pdf
ASPEN PLUS USER GUIDE - PROCESS SIMULATIONS
Software-Development-Life-Cycle-SDLC.pptx
Software defined netwoks is useful to learn NFV and virtual Lans
Unit IILATHEACCESSORSANDATTACHMENTS.pptx

Sine Wave Generator with controllable frequency displayed on a seven segment board using FPGA Board

  • 1. module lab5( input logic quadA, input logic quadB, output logic [6:0] seven_segment, output logic [2:0] sel, output logic [7:0] q_sig, input logic reset_button, output reg en2, output reg en3, output reg midpin, output logic pwm_out, output logic quad_button1, input logic quad_button2, input inclk0_sig ); /////Clocks Generated from PLL logic c0_sig; logic c1_sig; logic c2_sig; logic [10:0] address_sig; PLL PLL_inst ( .inclk0 ( inclk0_sig ), .c0 ( c0_sig ), //10 KHz Clock .c1 ( c1_sig ), // 4 KHz Clock .c2 ( c2_sig ) // 16.77216 MHz Clock ); Rom_Sin Rom_Sin_inst ( .address ( address_sig ), .clock ( clock_sig ), .q ( q_sig ) ); wire clock_sig = c2_sig; assign en2 = 1'b0; assign midpin = 1'b0; assign pwm_out = 1'b0; assign quad_button1 = 1'b0; logic [3:0] muxo; logic [3:0] present_state; logic [3:0] next_state; logic [3:0] hundreds_digit; logic [3:0] tens_digit; logic [3:0] thousands_digit; logic [3:0] ones_digit; logic [13:0] count; logic [23:0] Add_out; logic [23:0] DQ_out; ////Logic needed for quad encoders logic quadA_steady1; logic quadB_steady2;
  • 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