This is part 3 of the tutorials and examples I have been trying to put together for the Altera Max II C-M240 CPLD Development Board included with HackerBoxes #12. Other than the board itself, I’m using the Linux version of Quartus Prime Lite version 16.0.2 to code, compile, and program the board.
- Part 1 – Tutorial covering the basics of project setup and basic schematic design.
- Part 2 – Tutorial covering lighting up onboard LEDs and buzzer with pushbuttons.
I’ve been playing with Verilog and the Altera EPM240T100C5N (the Altera CPLD chip included on the board) for about a week now, and I have to say I am really digging it. There have been quite a few moments where I just couldn’t get things to work the way I wanted them to, mostly due to my lack of experience, but also because it’s been a real pain to find the information I needed. If you’re looking for a quick example and basic explanation of blinking a LED, here goes:
Set up a New Project as explained in Part 1 of the tutorials. Once you have your project created, click on “File->New->Verilog HDL File”.
When you create the project, your entry for the top level entity and the module name used in the code must match!!! It won’t compile if they do not match. So if you use use a different name for your top level entity in your project, change the name of the module from “blink_leds” used in the code below to match the name you used for your top level entity during project setup.
In the text editor, use the following code:
// Verilog blinking LED code
// Be sure your top level entity and module name match!
module blink_leds(clk, led1, led2, led3);
reg [22:0] counter;
always @(posedge clk)
if(counter==5000000) counter<=0; else counter <= counter+1;
always @(posedge clk)
led1 <= ~led1;
led2 <= ~led2;
led3 <= ~led3;
A few important highlights here: One, I’d like to give a big thank you to Jean Nicolle at http://fpga4fun.com for providing such clear examples making it much easier for me to undetstand and apply his code to the M240 board. So, wherever you are Jean, thank you – much appreciated! 🙂
Two, we are using one input, PIN_12, for the clock, and three outputs for the onboard LEDS at PIN_58, PIN_56, and PIN_54, although you could use whatever pins you wanted. I chose these pins simply because they connect to the onboard LEDS.
Three, be sure you set your “Unused Pins” to a state like “input tri-stated”. Also, after you compile the code, use the “Pin Planner” to assign your pins as below, and then compile again:
Last, let me explain how to calculate the frequency. This is tied to the counter variable, which in the example above is a 23 bit value. Why a 23 bit value? I wanted the LEDs to flash at a frequency of 5 Hz or 5 times a second. I also wanted the duty cycle to be 50%, or said another way, I wanted the time the LEDs are on and off to be the same – equal time on and off.
To achieve this, I use a counter to count up to 5,000,000. The “5,000,000” number is arrived at because the global clock we are using is 50 MHz or 50,000,000 cycles per second. To reduce that speedy number down to 5 cycles per second with an equal on/off time, I use the formula below:
global clock frequency / desired frequency / 2 = counter
Applying the formula, we use 50,000,000 / 5 / 2 = 5,000,000. This means that 5,000,000 is our halfway point where we should change the state of the LED from on/off or vice-versa. So, 5,000,000 is the number our counter has to count to before it switches the LEDs.
One other point to consider is that we need to be sure we have a counter that can count high enough to reach 5,000,000 which is why I chose a 23 bit counter. Setting “counter” to 2^22 would only reach a max count of 4,194,304 which is too small, so we have to use 2^23 which is 8,388,608, plenty for our needs.
The only thing left to do is to program the board and enjoy watching your LEDs flash on and off. Try calculating some different frequencies for the LEDs by using different numbers for the counter. Just be sure to adjust the bit width of the counter accordingly. It’s that simple. 🙂