Programming a D-flip flop in VHDL

Following are few ways to code a positively edge triggered D Flip Flop in VHDL.

Method 1:

library IEEE;
use IEEE.std_logic_1164.all;

entity dff1 is
port (D, Clock : in std_logic;
		Q , Qbar : out std_logic);
end entity dff1;

architecture dff_arch of dff1 is
begin
p0: process (Clock) is
begin
if (Clock = '1') then
Q <= D;
Qbar <= not D;
end if;
end process p0;
end architecture dff_arch;

Method 2:

library IEEE;
use IEEE.std_logic_1164.all;

entity dff1 is
port (D, Clock : in std_logic;
		Q , Qbar : out std_logic);
end entity dff1;

architecture dff_arch of dff1 is
begin
p0: process is
begin
wait until (CLock = '1');
Q <= D;
Qbar <= not D;
end process p0;
end architecture dff_arch;

Method 3:( The most correct design)

library IEEE;
use IEEE.std_logic_1164.all;

entity dff1 is
port (D, Clock : in std_logic;
		Q , Qbar : out std_logic);
end entity dff1;

architecture dff_arch of dff1 is 
begin

p0: process (Clock) is
variable state: std_logic;
begin

if rising_edge(Clock) then
state := D;
end if;
Q <= state;
Qbar <= not state;
end process p0; end architecture dff_arch;

Pin configuration is as follows:

1

Thank you.

Configuring DE0-Nano EPCS64 flash device.

Similar to most of the FPGA devices available in the market today, the DE0-Nano also uses SRAM cells to store the configuration data it requires to operate correctly. Since SRAM is a volatile memory it will lose all the data once we power down the device. So whenever we power up the device it will read the configuration data from some other non-volatile memory element and reprogram the FPGA fabric.

DE0-Nano has a flash device named as EPCS64. It has 64Mbits capacity. This blog post will teach you how to program the EPCS64 flash device so that you can save your program in the chip indefinitely (theoretically).

  • Generate the .sof file by compiling your program.
  • Goto File > Convert Programming Files
  • Under ‘output programming file’, select ‘JTAG indirect configuration file’ as the programming file type.’
  • Select ‘EPCS64’ as the configuration device.
  • Give a file name and path.
  • Under ‘Input files to convert’ click on ‘Flash loader’.
  • Then click on ‘Add device’ button at the right side of the window.
  • Select ‘EP4CE22’ under ‘Cyclone IV E’ and click ‘ok’.
  • Under ‘Input files to convert’ click on ‘SOF data’.
  • Then click on ‘Add file’ button at the right side of the window.
  • Select you generated ‘.sof’ file which is generally it is located in the ‘output_files’ folder.
  • Finally click on generate button.

(more…)

Simple bit-shift example in VHDL

Here is another simple VHDL code to carryout simple bit shifting operation. I decided to post simple VHDL codes in this blog so that any newbie who reads this will be able to find some examples to work on. Make sure that you assign correct pins for inputs and outputs as mentioned in the

https://unboxnbeyond.wordpress.com/2013/03/24/hello-world-de0-nano-with-vhdl/#more-143

‘A’ should be connected to a push button
‘B’ should be connected to LEDs.

VHDL code:

library IEEE;
use IEEE.numeric_std.all;
use IEEE.std_logic_1164.all;

entity bitshiftror is
port (A: in std_logic;
		B: out std_logic_vector(7 downto 0));
end entity bitshiftror;

architecture bitshiftor of bitshiftror is

signal B_out: BIT_VECTOR(7 downto 0) := (0 => '1', others => '0');
constant n: NATURAL := 1;

begin
aa: process (A) is
begin
	if((A xor '1') = '1') then
		B_out <= (B_out ror n);

		B <= to_stdlogicvector(B_out);				
	end if;
end process aa;
end architecture bitshiftor;

Thank you.

LM4f120: Using “buttons.h” for inbuilt buttons.

Last few days I spent a lot of time with the LM4F120 board just to get familiarized with the Stellarisware APIs. There are some documents available with the software which gives a good picture on the behavior of those APIs. Since TI discontinued Stellarisware you can find these documents under Tivaware section in TI.com.

Following is the way to implement ‘button.h’ in your code. Here, I will be using inbuilt buttons to turn on and off the LEDs in the circuit.

Code:

#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "drivers/buttons.h"

int main(void) {

unsigned char button_val;
unsigned char Delta;
unsigned char Rawstate;

SysCtlClockSet(SYSCTL_SYSDIV_5| SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE , GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);

ButtonsInit();

while(1){
SysCtlDelay(100000);

button_val = ButtonsPoll(&Delta, &Rawstate);

if(BUTTON_PRESSED(LEFT_BUTTON,button_val,Delta)){
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0xFF);
}

if(BUTTON_PRESSED(RIGHT_BUTTON,button_val,Delta)){
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0x00);
}
}

}

But unfortunately this code alone will not do the trick. You should strictly adhere to following steps in order to build this code. Otherwise you will receive “Unresolved variable …” errors.
(more…)

MSP430: Temperature stabilizer

It has been a while since I played with the MSP430G2 evaluation board. Recently one of my friends wanted me to build a temperature stabilizer for one of his projects. The requirement was to stabilize a water container at 50 Centigrades. I decided to use the MSP430 as it has the basic components needed for it. In this post I will be talking about the code I used in the MSP430.

I used MSP430G2553 micro controller to do all the processing,
I used LM35 temperature sensor to measure the temperature.

The application was straight forward. I had to connect the LM35 IC through the ADC of the MSP430 and process the signal.

Schematic:

Following is the connection diagram.

 060420131317

Logic:

Following is a state diagram of the problem.

060420131316

LM35:

According to the datasheet of the LM35 it works within 4V and 30V supply voltages. Since the MSP430 works at 3.3V I used a 9V battery to power up the LM35 IC. But the ground should be shared with the launchpad for this to work properly. It gives 10mV/C as the output in a linear scale.

Code:

The coding was done using the TIs code composer studio.

#include <MSP430.h>

/*
* main.c
*/

unsigned int value = 0;
unsigned int upperT = 180;
unsigned int lowerT = 160;

//The function to configure the ADC
void configureAdc(){
ADC10CTL0 = SREF_1 + ADC10SHT_0 + ADC10ON + ADC10IE + REFON;
ADC10CTL1 = INCH_5 + ADC10DIV_3;
ADC10AE0 |= BIT5;
}

// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR;

__interrupt void ADC10_ISR (void)
{
__bic_SR_register_on_exit(CPUOFF); // Return to active mode
}

int main(void) {
short flag = 1; //relay on
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

BCSCTL1 = CALBC1_1MHZ; //Clock calibration
DCOCTL = CALDCO_1MHZ;
BCSCTL2 &= ~(DIVS_3);

P1DIR |= BIT0 + BIT6; //GPIO selection
P1SEL |= BIT5;

P1OUT &= ~(BIT0 + BIT6); //LEDx off

configureAdc();
__enable_interrupt();

P1OUT |= BIT6; //LED1 ON

while(1){
__delay_cycles(2000000); //delay 2sec
ADC10CTL0 |= ENC + ADC10SC; //Enable conversion
__bis_SR_register(CPUOFF + GIE); //Low power mode with enabled interrupts
value = ADC10MEM; //ADC conversion

if(flag == 1){ //When relay is on
if (value>=upperT){ //Wait until T = 53C
P1OUT &= ~(BIT0 + BIT6);
P1OUT |= BIT0;
flag = 0; //relay off //Turn off the relay
}
}
else if(flag==0){ //When relay is off
if (value<=lowerT){ //Wait until T = 47C
P1OUT &= ~(BIT0 + BIT6);
P1OUT |= BIT6;
flag = 1; //relay on //Turn on the relay
}

}

}

return 0;
}

Hello world DE0-Nano with VHDL

Here i will be explaining how to write a very simple VHDL code to simulate an And gate with two inputs. In the following post I have mentioned about device families, pin assignments, compilation and programming the actual hardware. I will not repeat those information please go through this post if you have not.

Only difference here is that instead of adding Block diagram / Schematic to the project you should select File >New > VHDL file. Then you will get a window to type the code.

Following is the VHDL code,

entity VHDLturo is
port (A, B:in BIT;
Z : out BIT);
end VHDLturo;


architecture logicfunc of VHDLturo is
begin
Z <= A and B;
end architecture logicfunc;

(more…)

Hello world DE0-Nano

Last few weeks i have been going through lots of books on FPGAs and VHDL. Now it is the time to write the first VHDL program. But before even writing a VHDL program I decided to draw a simple schematic design and power it up through the DE0-Nano.

First you need to start QuartusII and start a new project. Select Cyclone IV E device family and EP4CE22F17C6 device. That is the hardware you have in the DE0-Nano. After you created the project, just go to File > New. Then select Block diagram / Schematic file.

Now you will see the window where you should draw the design. Click on schadd icon. Then type ‘and2’ in the Name text box. Now you can draw and gates in the design by simply clicking on it. For time being just include one And gate.

and2

(more…)

Testing and Configuring the Altera DE0 Nano FPGA

$(KGrHqV,!k8E+rCZTg-JBQDE4nujG!~~60_35Altera DE0 Nano contains a very good software/tool which actually is a control panel. It gives you an easy platform to make sure that the Development board is functioning properly. This blog post will have details on configuring the control panel and installing USB blaster drivers.
(more…)

FPGA starter kit comparison (BASYS2 or DE0-Nano)

The next device I am going to purchase is an FPGA development board. Since I am a newbie to FPGAs I wanted to buy a simple board to start my experiments on it. There are lots of FPGA boards available in e-bay for less cost. I don’t know why but my guts kept telling me to go for a board from a respected company and directly from them. So I did it. The money i could spare on these is 120$ max.

I searched for chips by the leading manufacturers of FPGAs, Xilinx and Altera. Then I found that Digilent and Terasic offers two entry level FPGA development boards, the BASYS2 and the DE0-Nano respectively; for chips by Xilinx and Altera respectively. Both these have an academic pricing (which i am entitled to) of 59$ and with shipping it will cost around 100$. In-fact BASYS2 with 250K gates (will explain later) 119$ and DE0-Nano is 112$.

First let’s look at the two boards as shown in their website,

Altera DE0-Nano is one on the left and the Basys™2 Spartan-3E is the one on the right.

de0_nano

BASYS2-top-400

To me the Digilent Basys2 looks better than DE0-Nano. It has all those gadgets in the board which makes it look better.

(more…)