|
|
View previous topic :: View next topic |
Author |
Message |
Rob_975
Joined: 14 Aug 2012 Posts: 16
|
DAC7562 12bit SPI DAC programming problems. |
Posted: Tue Dec 17, 2013 3:09 pm |
|
|
Hi there.
I'm having hard times to menage the DAC7562 DAC via SPI.
I use a PIC18F67K22@48MHz and the SPI bus is working (oscilloscope tested).
I use the SPI PIC hardware pins: 34=clk, 35=data in, 36=data out.
first, the SPI configuration.
Code: | #use spi(SPI1, FORCE_HW) |
It's a 50MHz DAC, thus I will not use the SPI_CLK_DIV_... on the SPI setup:
Code: | setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_XMIT_H_TO_L); |
The datasheet says:
I assume that the comunications setup is correct.
Then, according to these
I'm doing:
Code: | output_low(PIN_C6); //7562 data flow enable
spi_write(0x1ffff); // set output to max voltage
output_high(PIN_C6); //end of data transmission |
The Schematic:
The DAC will not change its outputs at all.
Help me guys, i'm stuck. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Tue Dec 17, 2013 3:52 pm |
|
|
Several things....
spi_write, sends just 8bits. Look at the manual...
Get rid of the setup_spi. #use spi, and setup_spi, are _alternatives_ and should not be used together.
With #use SPI, you use spi_xfer to send data.
This can be set to send longer word lengths, in the #use spi.
Data is clocked on the falling edge, so 'mode 1'
So, syntax is:
Code: |
#use SPI(MASTER, MODE=1, FORCE_HW, BITS=24) //defaults to maximum speed
//Then to send
int32 dummy;
output_low(PIN_C6);
dummy=spi_xfer(0x1ffff0,24);
output_high(PIN_C6);
|
Now, notice I read the return. Reason is that unless you do, the spi write, or spi xfer calls will _return_ when the last byte is still in transmission. Since we need to wait for the data to send, reading the return ensures that the call waits until the transfer is complete.
Then 'bits' in the xfer, says how many bits to actually send in the transaction. 'bits' in the #use, specifies the maximum to be used.
Then notice I'm sending 1FFFF0. There are four not used bits required at the end. 24bits is 6 hex digits, not five....
Best Wishes |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Wed Dec 18, 2013 2:55 am |
|
|
You must also initialise the DAC by setting the gains and enabling the reference, etc. before it'll generate any voltages. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Wed Dec 18, 2013 3:01 am |
|
|
Hurrah, somebody who looked further into the data sheet than me.
Best Wishes |
|
|
Rob_975
Joined: 14 Aug 2012 Posts: 16
|
|
Posted: Fri Dec 20, 2013 11:06 am |
|
|
Perfect, it works.
Just a little more thing.
For me the easiest way to write the 24bit word is in binary (because with the registers table I can easily set the proper bit):
Code: | dac=spi_xfer(id,0b000000001100000000000000 ,24); //set out_A=7.522V |
but if I write it divided according to the register table:
Code: | dac=spi_xfer(id,0b 00 000 000 110000000000 0000 ,24); |
the compiler shows up an "expecting a closed paren" error.
why?
Notice that being a total newbie, maybe I'm doing this in a bizarre way.
Is there is a better method to the the job?
Lastingly, I think I better learn how to manage bits with bitwise ops right?
Thanks. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Fri Dec 20, 2013 2:52 pm |
|
|
comment: as to the error , you can't have spaces in the 0bxxxx format.
'space' is NOT a valid binary character, only '0' and '1' are
same holds true for any 'format',
0x1234 is good; 0x12 3 4 is bad.
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Sat Dec 21, 2013 1:50 am |
|
|
As a comment, 'better way', I'd say was not to use values in the command at all.
#DEFINE them.
Think for a moment about things like #use SPI. You don't set them up with commands like:
#use spi(something=0b110010000)
instead the compiler authors have defined for you, nice constants that as their 'name', also give a hint to what is being done. So:
#define SETA_722volts 0x00C000
Then really, you should try to get used to reading hex. It is far easier to 'not' make a mistake about positioning, since effectively the hex value is just 4bit groups of binary digits, so:
0x00C000 = 0000 0000 1100 0000 0000 0000
which is effectively what you are trying to do.
Then the other thing is to combine values together (as you see in several of the CCS constants, with |).
#define SETA_volts 0x000000
#define SETB_volts 0x010000
#define V752 (0b110000000000<<4)
Then you can set the value with:
dac=spi_xfer(id,SETA_volts|V752 ,24); //set out_A=7.522V
Now the V752, is defined as the 12bit value 'placed' where it needs to be (four bits left).
Key here is that things like this are solved at _compile_ time, not in the chip itself.
Generally putting long numbers for things like register patterns directly 'in' the code, is a fairly 'sure' sign of relatively inexperienced programming.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|