Vhdl Program For 8 Bit Up Down Counter Using Microcontroller
Created on: 8 February 2012
A ring counter is simply a shift register that feeds the last bit of the shift register into the first bit of the shift register.
Vhdl Program For 8 Bit Up Down Counter Using Microcontroller. The Z80 CPU is an 8-bit based microprocessor. It was introduced by Zilog in 1976 as the startup company's first product. The Z80 was conceived by Federico Faggin in. Johnson digital counter circuit diagram using D flip flop 7. The Johnson digital counter or Twisted Ring. VHDL 8-bit counter. Questions tagged vhdl counter xilinx or ask your. The gear shifter where you have one trigger for the up shift and another for down shift?
In this part of the VHDL CPLD course, a ring counter is written in VHDL and then implemented on a CPLD. This tutorial is also used to demonstrate the use of the VHDL ror and rol operators.
Although not used in a ring counter, the related VHDL srl and sll operators which are also related to shifting are also demonstrated.
Ring Counter Operation
In a digital circuit, a ring counter is normally implemented using D type flip-flops in the same way as the shift register from the previous tutorial.
The difference between the shift register and the ring counter is that the ring counter feeds the Q output of the last flip-flop into the D input of the first flip-flop. The first flip-flop also has a PRESET input in order to store a logic 1 in the first flip-flop at start-up. After introducing the 1 into the first flip-flop, the ring counter shifts the data in the shift register to the right continuously. Because the output of the last flip-flop is wired to the input of the first flip-flop, the 1 is continuously rotated through all the flip-flops – thus it is called a ring counter.
The image below shows the operation of an 8-bit ring counter as implemented in VHDL in this tutorial.
Operation of a Ring Counter
This video shows the ring counter in operation:
Ring Counter in VHDL Code
The code below is a modified version of one of the shift registers from the previous tutorial.
Initializing the Shift Register
The shift register (shift_reg) is first initialized to set the MSB in the shift register with this code:
This writes 1000-0000b to shift_reg.
In the code, the shift register is wired to the LEDs so that each bit in the shift register can be seen on the LEDs interfaced to the CPLD (8 bits in the register interface to 8 LEDs):
Because the LEDs on the home built CPLD board are wired as current sinking, initializing shift_reg (and therefore the LEDs) with 1000-0000b will switch the MSB LED off and all the other LEDs on. This line of code is therefore commented out and the first bit is cleared with all the other bits set:
Now 0111-1111b is written to shift_reg.
Ring Counter Operation
Instead of feeding data into the shift register on one end and shifting the data out of the shift register on the other end, the ring counter code shifts the data in the shift register to the right, but also shifts the last bit (bit 0) back to the first bit (bit 7) thus completing the ring:
Like the shift register in tutorial 11, the rest of the code in the VHDL process shifts every bit to the right.
Amazon.co.uk
Implementing the Ring Counter using ror
The VHDL ror (rotate right logical) operator can be used to rotate data to the right, rather than just shifting it. Using the ror operator on a register implements a ring counter.
The following code shows how to use the VHDL ror operator in a ring counter.
Using ror
Vhdl Program For 8 Bit Up Down Counter Using Microcontroller Windows 10
The first thing to note is that ror can't operate on the STD_LOGIC_VECTOR data type. The shift register shift_reg has to be defined using the BIT_VECTOR data type instead.
Because we want to wire the bits in shift_reg to the LEDs, we need to make LED of the BIT_VECTOR data type as well:
Which is in the entity part of our VHDL file:
If the two vectors were not of the same type, then we would not be able to do this:

The synthesis step of compiling the VHDL code would give an error.
The ror operator works by rotating x to the right by n number of bits and stores the result in y:
VHDL Shift Operators
There are six shift operators in VHDL:
Vhdl Program For 8 Bit Up Down Counter Using Microcontroller Keyboard
- rol – rotate left logical
- ror – rotate right logical
- sll – shift left logical
- srl – shift right logical
- sla – shift left arithmetic
- sra – shift right arithmetic
Four of these operators have been included in the above code listing (the ring_counter_ror_top code) to demonstrate their operation. Uncomment any one of the statements containing a shift operator and comment all of the others out. Compile and load the code to a CPLD board to see the operators working.
The sll and srl operators shift the data in the shift register out of the shift register and shift 0's in on the other end. The 0's will switch the LEDs on when using the home built CPLD board.
VHDL Data Types
We have now seen that there is more than one data type in VHDL. The VHDL code so far in this course has used the STD_LOGIC_VECTOR data type (for more than one bit), the STD_LOGIC data type (for a single bit) and now the BIT_VECTOR data type.
In the next tutorial in this series, we will look at VHDL data types in more detail.
Please enable JavaScript to view the comments powered by Disqus.
VHDL CPLD Course
