Created on: 13 March 2013
Another easy thing to do in VHDL is to route I/O pins from a microcontroller through a CPLD to connect the pins to peripherals.
In this tutorial, a switch and two LEDs that are interfaced to a CPLD are connected to a microcontroller that is also connected to the CPLD. VHDL is used to connect the switch and LEDs to the microcontroller.
The microcontroller runs software to read the switch and control the LEDs.
Connections between an ATtiny2313 microcontroller and CPLD are made as shown in the diagram below.
On the home made Xilinx CPLD board, the above connections are made by connecting wires between the connector on the microcontroller and the connector on the CPLD. The photo below shows the three green wires connecting microcontroller to the CPLD.
The VHDL code for connecting the switch and two LEDs to the microcontroller pins is shown here.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity IO_redirect_top is
Port ( LED : out STD_LOGIC_VECTOR (1 downto 0); -- LEDs on CPLD
BUTTON : in STD_LOGIC; -- button on CPLD
U_LED : in STD_LOGIC_VECTOR (1 downto 0); -- connection to uC
U_SW : out STD_LOGIC); -- connection to uC
end IO_redirect_top;
architecture Behavioral of IO_redirect_top is
begin
-- connect microcontroller pins that drive LEDs to LEDs
LED <= U_LED;
-- connect push button switch to microcontroller input
U_SW <= BUTTON;
end Behavioral;
This code is pretty straight forward and is nothing new if you have been following the VHDL CPLD course. The LEDs and push button switch that are interfaced to the CPLD are named LED and BUTTON. The microcontroller pins that will connect to the LEDs are called U_LED and the microcontroller pin that will connect to the push button switch is called U_SW.
The VHDL code simply connects the signals from the LEDs to the microcontroller LED signals. The push button switch signal is connected to the microcontroller switch signal.
The software used on the ATtiny2313 microcontroller to read the switch and control the LEDs is listed below.
#include <avr/io.h>
void Delay(volatile unsigned int);
void ChangeDelay(volatile unsigned int*);
int main(void)
{
volatile unsigned int del_val = 40000;
// LEDs are redirected to pins PB2 and PB3
DDRB |= ((1 << PB2) | (1 << PB3));
// push button switch redirected to PB1
DDRB &= ~(1 << PB1);
while(1)
{
// switch one LED on / other LED off
PORTB &= ~(1 << PB2); // switch PD0 LED off
PORTB |= (1 << PB3); // switch PD5 LED on
Delay(del_val);
ChangeDelay(&del_val);
PORTB &= ~(1 << PB3); // switch PD5 LED off
PORTB |= (1 << PB2); // switch PD0 LED on
Delay(del_val);
ChangeDelay(&del_val);
}
}
void Delay(volatile unsigned int del)
{
while(del--);
}
void ChangeDelay(volatile unsigned int *del)
{
if (PINB & (1 << PB1)) { // check switch state
*del = 10000;
}
else {
*del = 40000;
}
}
The program reads the push button switch and then changes the speed at which the LEDs flash, depending on if the switch is open or closed.