Electronic – How to generate sound in VHDL

audiofpgasoundvhdlwaveform

I'm new to FPGA and VHDL. I'm working on lab practical and for the practical we were already given file that has an I2C codec and the basics for the codec registers were already done but I found the given files were too complicated and I couldn't understand it; they were things like slave and masters. So I decided to follow an online tutorial.

I am using a DE0-Nano board. I want to make three different waves (sound) so I followed an online tutorial to make sine wave, but when I tested it on my board nothing happened, and I didn't get any sound. Is there something missing on my code or did I assign to the wrong pins?
Here is the documentation for the board just in case you need it.

I would also appreciate if you can also tell me about any source that will help me understand I2C and the slave/master.

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;   
entity sinewave is
port (
      clk :in  std_logic; -- I tried connecting it to PIN_B14 & PIN_R8
      dataout : out integer range -128 to 127 -- connect to PIN_A9
      );
end sinewave;

architecture Audio of sinewave is
  signal i : integer range 0 to 30:=0;
  type memory_type is array (0 to 29) of integer range -128 to 127; 
  signal sine : memory_type := (0,16,31,45,58,67,74,77,77,74,67,58,45,31,16,0,
-16,-31,-45,-58,-67,-74,-77,-77,-74,-67,-58,-45,-31,-16);

begin

process(clk)
begin
  if(rising_edge(clk)) then     
    dataout <= sine(i);
    i <= i+ 1;
    if(i = 29) then
      i <= 0;
    end if;
  end if;
end process;

end Audio;

Best Answer

You've got a whole lot of things mixed up:

  • First off, the DE0 Nano has an ADC, not a DAC. It converts analog voltages to digital values, not the other way around.

  • You can't write an integer to a single pin, it doesn't make any sense. A GPIO pin can be either a zero or a one. To output a parallel signal, you need to declare a std_logic_vector and assign a set of pins.

  • R8 is the input of a 50MHz clock. You'll probably want to divide that down quite a bit before you try to generate an audio frequency sine wave.

  • Even if the ADC was actually a DAC (it's not), you can't simply assign numbers to the pins it's connected to. You need to read the datasheet to understand how to communicate with a device. For the ADC, here's the datasheet, note that it communicates via a serial data stream (compatible with SPI).

  • I2C and audio don't really mix. You might be thinking of I2S, which is digital, serial, often multichannel, audio. I2C is typically too slow/not appropriate for audio. If you want to learn more about I2C, the Wikipedia page is pretty straightforward. If you want to learn about I2S, read up on I2S, and then get yourself an I2S DAC.

So, with all this in mind: if you want to output audio, instead of trying to use the non-existent DAC on the DE0 Nano, use PWM to output an analog voltage. You can use a PLL to get a high-frequency PWM frequency so that it can be very easily filtered (simple RC LPF) to get a passable audio signal.

Related Topic