Now that we’ve created the PWM module, we can instantiate it within in the mojo_top module! In Verilog, instantiating a module within another allows you to use the sub-module’s functionality within the higher-level module one or more times without having to copy its code. In our case, we can create as many PWM signals as we would like to either drive different LEDs or even connect to output pins on the Mojo! To configure a PWM signal to light up the 8th LED on the Mojo, we can append the line
PWM my_pwm(.clk(clk), .rst(rst), .duty(8’b01000000), .sig_drv(led));
The first word in the line is the name of the module that we want to instantiate, PWM. The next string is the name that we are going to give this instance of the module within our higher-level module. This helps identify different instances of the same PWM module if we choose to instantiate multiple copies.
Inside the parentheses that follow the module name, we use the .<module_signal_name>(signal_name) format to assign signals in the higher-level module (signal_name) to the corresponding signals in the sub-module (module_signal_name).
If we want to vary the duty cycle of the PWM signal, all we have to do is change the value passed into the duty cycle parameter. If we want to change the output signal being driven, we merely change the signal passed as a parameter to .sig_drv .
Your completed mojo_top module should look something like this:
To upload this code to the Mojo board, follow the same steps as before: generate the programming file in ISE, load the Mojo Loader application, and load the .bin file to the Mojo.
Congratulations, you’ve implemented hardware PWM on the Mojo! To experiment further, try creating multiple hardware PWM signals and supply them with different duty cycle parameters! Can you modify your code so that the duty cycle value can be input to the Mojo with some toggle switches?
We hope you enjoyed your first FPGA projects! Check back soon for more FPGA and microcontroller tutorials!
Binary Arithmetic: https://www.tutorialspoint.com/computer_logical_organization/binary_arithmetic.htm
Binary Representation of Numbers: https://www.swarthmore.edu/NatSci/echeeve1/Ref/BinaryMath/NumSys.html
W13 Rahul Iyer