SPI and DAC

Kecked

Member
Ok so I am basing what I know about SPI off how to make a max 7219 led driver work. I am now trying to use a dac to produce a voltage based on an input from an adc. I looked over wests tutorial and find it complex from what I think I should be able to do so here is what I tried. It didn't work because I am not sure how to read the cut sheet and determine the register to address to send data.

I think all you need to make spi work (using 28x2) is to hook up the clk line, the DIN line and the CS line. My dac doesn't have LDAC. I am using a for next loop to make data for the dac and pulsout to load the data via cs. I could not find a register to put with the data to send so I just put the same variable as the data and that produces a ramp as expected but one that repeats many times as the dac counts up. So what is wrong with this approach provided I find the appropriate register value to pair with the data. IF its not a register than the appropriate top byte that provides the correct command. This is on a max5241. I tried making the cs line high and low via the let pin command and also via the pulseout command thinking that maybe cs had to stsay open longer than just a pulse. Both give the same result.


Code:
#picaxe 28x2
setfreq m16
let adcsetup = 0


		Let dirsA=%00010000
		Let dirsB=%11111111
		Let dirsC=%11111111
hspisetup spimode00,spifast
main:
	

	
	for w0=0 to 10000 step 1
	let pinb.0=0
	hspiout (W0,W0)
	let pinb.0=1
	next w0
 

hippy

Technical Support
Staff member
hspiout (W0,W0)

You cannot send 16-bit words via SPI only 8-bit bytes. You'd likely have to send the component parts of 'w0', that is 'b0' and 'b1', so perhaps ...

hspiout (b1,b0)
 

Kecked

Member
turned out that in this case I had to send a multiplier command that defined how it deals with the reference volatge and then the data so is was hspiout(00000000,00000001), hspiout(b1,b0) but it does work. I think I'll try the shiftout method next just to see how that works but I'll have to figure out how to make the clk along side it. Maybe hpwm as a burst might work. Second thought is to setup hspi and use the clk generated but sent the data from another pin other than MOSI voia shiftout synced on clk. Will be an interesting experiment. Reason for this is so I can use better dacs with higher resolution.
 

westaust55

Moderator
HSPI commands use a dedicated internal hardware comms module which also defines specific PICAXE pins for data in, data out, and clock.
That command group should tentatively be capable of use for most SPI protocol components.

If there is a specific reason to use an alternative set of pins (for example also using I2C comms) then the X1and X2 parts have inbuilt firmware based SHIFTOUT/SPIOUT and SHIFTIN/SPIIN commands available.
 
Last edited:

Kecked

Member
max5541

View attachment MAX5541.pdfthats cuz I'm a bone head it's a max5541. I just chose it because it is 16 bit and was a part in vsm with spi. No particular design yet for it so I could change. What I really wanted was the ability to have 65000 or so steps that I could use to get good enough resolution to make all 88 notes from 0-5v as 1/2V/octave. That's 0.0415V/step or about 555 per step. I then double the 0-5v signalvia lm324 for 0-10 1v/oct. I know it works with a max551 because that is how my midi dac works someone else built. I want to be able to as we discussed use two pots to set note and octave and then send a value to the dac to produce the required voltage. I'm just trying to learn how to use a dac. BUT. Since hspiout only lets me use 8 bit words I have to send it b1,bo which is no big deal but the dac seems to receive that as the command plus 8 bit value so I'm stuck to 255 values. Thus now I need to implement spi via shiftout commands and for next bit bangs which I have been avoiding cuz I don't understand how to do it. I read over Wets tutorial and just got a headache. Not his fault I'm just not there yet.
 

hippy

Technical Support
Staff member
Since hspiout only lets me use 8 bit words I have to send it b1,bo which is no big deal but the dac seems to receive that as the command plus 8 bit value so I'm stuck to 255 values.
According to the datasheet there is no 'command', just 16-bit data to send to the DAC. Set CS high on program start, then to send 16-bit data, set CS low, clock out 16-bits MSB first, set CS high.

Perhaps post your VSM design and code.
 

Kecked

Member
I think I'll dump the dac and go midi. That looks a lot simpler. Since I already have a midi DAC.... I tried Hspiout(b1,b0) with a pulse out and setting cs low before the command and after. The dac doesn't respond. It only responds if I HSPIout (b0,b0) so that the upper and lower bytes match or hspiout(B0,1). I will post as asked since I might want to figure this out for something else later.
I looked witha scope and the clk, din, and cs all look as they shoud so I don't know why. What I called a command was settting the 1 in hspiout(b0,1).

Also when I set the hspiout(b1,b0) it looks like it counts to 255 and recycles so I wonder if this is an 8 bit dac after all. Certainly behaves like it. BTW: Hippy people talk about your website and archive for a bunch of stuff but all the links seem dead. Can you point me to it. Many thanks.
 
Top