Problem Playing Audio (8-bit DAC)

Go To Last Post
20 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Guys,
The amount of time I've solved a problem to do with AVR by reading this forum is amazing but now a problem eludes me and I think I must make my first post.

I have an ATMEGA32 hooked up with a tlc7524 DAC via PORTA
For now I just want to play some audio samples through it at 8-bit/8 Khz.

If I understand correctly I just convert the audio I want to play into an 8 bit unsigned wav at 8khz and strip the audio samples out of it and put it in my code.

I set up the timer to trigger every 125us (8000hz)

DACTimerInit:


	LDI Temp,	0x0A		; Set Timer Mode
	OUT TCCR0,	Temp

	LDI Temp,	0x02		; Enable  Timer Interupts
	OUT TIMSK,  Temp

	LDI Temp,	0xF8 		; Set timer to correct cycles	
	OUT OCR0,	r16	

	RET

The simulator shows that this timer interrupt does indeed run every 125us exactly:

timerHandle:	

	IN	Temp,	PortD
	ORI	Temp,	0x04		; Set bit 4 (ie DAC latch)
	OUT PortD,	Temp

	NOP						;Wait 4 cycles for DAC latch just to be sure
	NOP
	NOP
	NOP

	ANDI Temp,	0xFB		; Clear bit 4 (ie DAC latch)
	OUT PortD,	Temp
	

LPM	 temp, 	Z+			; Get next byte from ROM and OUTput it to PORTA
	OUT  PORTA,	temp


;......Some code to ensure looping snipped.......

RETI

The simulator runs perfectly it presents each byte of data to PORTA and sets the latch bit at 8000 Hz.

Alas when I run it on my circuit I get a really strange noise.

I recorded the noise and took a picture of the waveforms.:

1) Original Sound
Waveform 1

2) The noise I recorded off my circuit at 8 Khz
Waveform 2

3) As the noise coming off my DAC was twice as long as the original so I halved the timer to see what would happen. It sounds pretty much the same.

Waveform 3

I'll post the part of the schematic I'm trying to get working here. Sorry its a bit messy but I think you can see if I've done something terribly wrong.
Schematic
Haha I there just a random floating GND there, just ignore that.

Does anyone recognize the sound? It definitely follows a pattern.

Thanks for any help,
Sean

Edit: Also if you zoom in on the output waveforms you can see that it keep dipping up and down almost like a square wave. Very weird....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Signed samples into an unsigned DAC?

XOR each sample with 80h to make it unsigned.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I told audacity to save the wav as unsigned but it is the beta version and not to be trusted. Thanks for the XOR tip I'll try that

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmmmm it sounds the same.

I just wrote a little program that switches all the data pins on and off every 5 seconds.

When I measure the output of the DAC it goes between 2 and 4 volts as the data pins turn on and off I would have thought it should go between 0-5v and when I read the voltage from the op-amp output to gnd it just stays at 2v. There must be something horribly wrong with the DAC/Amp part of the circuit, so I suppose this is more of a general electronics question now, rather than a UC one.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The opamp will need both positive and negative voltage rails.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So instead of connecting the neg terminal to ground I should have connected it to a negative supply?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You also have your nWR pin polarity wrong. You keep the signal low most of the time, meaning the latch is transparent most of the time, responding to changes on data bus.

So you should keep it high most of the time, make it low first in the interrupt, wait a bit, set it high, update data.

Or update data, make WR low, wait, and set WR high.

Edit: and are you sure when you use LPM, you have the high/low bytes in correct order in the flash word?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Write a test prog that outputs 0 thru 255 in a loop. Should generate a sawtooth! Good for scoping thru the signal path.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Creating a sawtooth, now that's an idea. I did that and looked in the simulator and also on some leds on another debug board I have and it's definitely looping from 0-255 but all I get is this crazy waveform:

Also good pickup on nWR being the wrong polarity, that's now fixed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also I just put a cap on the output now to make it AC so the waveform is now centered around 0, but still a weird mess.

I also connected directly up to the DAC output and used my soundcard preamp and I got this. Could it be that my DAC is blown?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I assume the dac outputs 0-5V, then it goes thru an opamp to get converted to -5 to +5 or some such. I think you should definitely see the sawtooth on the output of the dac.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
So instead of connecting the neg terminal to ground I should have connected it to a negative supply?

Yes! I gather you're using the circuit from the datasheet, one input of the op-amp is connected to 0v(gnd) - without a negative supply, the opamp wont work as expected.

Bob - the dac is current output. The op-amp converts it to a voltage.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmmm yeh, I found this example which I wish I found before I built the pcb, he mentions that you should use the "voltage-mode" where you run the opamp backwards with vref as an output, this has a positive output that you can amplify easier.

You're right in saying that I used the diagram from the datasheet so is there anyway I can salvage this? Is there an opamp I can get that makes it's own -12v supply or something I can connect that can bias it or something. The pcb is already made so it going to be a pain to hack on wires directly to pins but it's my own fault for not researching how op-amps work enough. This is what happens when a programmer tries electronics!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can steal the -7V from the max232 (pin 7?)

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The example site uses an audio amp chip. Which ever way you go will require mods. Why did you not just use a spi voltage output dac? The 7524 is old school!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Throw away the opamp at all. Use a reversed DAC connection as shown in your and many other examples.

Or even better solution - throw away the DAC and use PWM instead. 8 bit PWM @8 ksps can be easily achieved.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't know if this is what you want to hear but my opinion is forget 8-bit parallel DAC and use something designed for audio applications instead. Like TDA1545, TDA1543 or other I2S like DACs. It's easy to handle these buses with SPI channel and it's actually pretty fast with AVR. For example 44100Hz 16-bit stereo playback from internal RAM FIFO buffer could be done within some 30% of CPU time at 16MHz. Of course you can't reach better quality than 8-bit source data but once you'll apply some effects like volume modulation or digital filtering then 16-bit output should give you better SNR. For example if you'll apply 25% volume to 8-bit stream then the noise level will be 4 times higher compared to the signal amplitude. But with 16-bit output valid data bits will be just "moved" to lower bits of 16-bit audio sample ...
I'm using this in my audio projects for years and I really have no idea why should I degrade sound using 8-bit output is these DACs are relatively cheap (or can be recycled from some PC junk like old CDROMs). But of course I have no idea what kind of application you are trying to make. If it's just playback of 8-bit PCM then 8-bit PWM seems to be good enough ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The application was simply a board that when I reset it it played the next audio clip in the ROM. I first made it with some SMD parts and then it wasn't working but I couldn't work out whether it was my bad job with the fiddly soldier. I was on a deadline so I decided to do things simply with DIP chips but the only DIP chips I could find with my local supplier didn't have SPI.

I was in such a rush to get the pcb sent away I didn't check the opamp schematic properly and if I made it again today it would be much better. I don't have a deadline anymore so I'm going to play around with it.

The easiest option seems to be PWM although the OC1/2 pins are used by things I can't modify. Would I be able to soft PWM fast enough to produce audio?

Otherwise I will fiddle with the DAC by snipping pins and soldiering patch wires (ewww)

Thanks guys for all your help, the responses were really helpful.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

leavingentropia wrote:
Would I be able to soft PWM fast enough to produce audio?

Easily. Set up a timer interrupt at 64x your sample rate and produce a soft PWM within the timer ISR - it shouldn't take more than a dozen instructions.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@leavingentropia

Re: your Eagle schematic in your first post, you'll get a cleaner schematic if you do the following:

(menu) View -> Display/Hide Layers

In the dialog box, un-set the #93 layer (so its white, instead of blue) and see if that looks less cluttered.