|
|
View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
dac_mcp4822 |
Posted: Thu Mar 15, 2012 1:55 pm |
|
|
Hello!
First, excuse my bad english!
Here is my problem. I'm trying to drive DAC (creation of the microchip). It is a 12bit D/A converter. The remain four bits (to 16) are configuration bits.
For example: 15bit - select dac(a|b), 14bit unusable(don't care) 13bit-coefficient (coef.*U_reference) , 12bit - dac_enable. So I tried to set these bits to logic '1', and then remaining 12 data bits. Unfortunately, without success. I developed my code to this level.
Code: |
#include <18F2520.h>
#use delay(internal=8000000,RESTART_WDT)
#FUSES WDT,WDT128, NOPUT,NOBROWNOUT, NOMCLR, NOLVP, NOPROTECT
#use rs232(baud=9600,xmit=PIN_B0,rcv=PIN_B2)
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define sel_dac sent.word,15
#define gain sent.word,13
#define dac_enable sent.word,12
#define PIN_CS pin_C4 //chip select
#define PROG_EXE pin_A0 //indicate start of executing program
union combine{
int8 part[2];
int16 word;
}sent;
MCP_4822_set(){
for(sent.word=0;sent.word<0xfff;sent.word++){
bit_set(sel_dac);
bit_set(gain);
bit_set(dac_enable);
spi_write(sent.part[1]);
spi_write(sent.part[0]);
delay_ms(10);
}
}
void main()
{
setup_spi(spi_master|spi_mode_0|SPI_CLK_DIV_64);
while (true)
{
mcp_4822_set();
}
}
|
The chip send only configuration bits (15,13 and 12) and none data. I tried with the #bit function, but the result was the same :(
I hope you understand my problem, and maybe someone is going to help me. Thx in advance! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Fri Mar 16, 2012 2:31 am |
|
|
Your for loop in MCP_4822_set, which I can't see any real point to, has two things which confuse what's going on.
First is the use of the union. Unions often cause problems as its not easy for us humans to connect one view of it with the others. I try to avoid unions whereever possible. They can be useful in some very occasional cases, but this is not one of them.
The second thing is using macros for gain, dac_enable and sel_dac. It "changes" the apparent functionality, by obscuring its actual function, of bit_set(). Changes it so much so that I went looking for your definition of it in your code.
The result of these is that you are changing the loop variable in the loop in ways that significantly change it. It won't count from 1 to 0xFFF, it will count until any of the bit_set()s change the upper bits and then immediately exit. This is because the word form of the union INCLUDES the bits you are setting in the macros. Is this really what you wanted? I doubt it.
One of the links points to a thread with my code for this part. Take a look at it.
RF Developer |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Fri Mar 16, 2012 4:46 am |
|
|
I want to increase DAC output voltage from minimum to maximum Vreference. Thats why i used for loop. When i configure setting bits like dac_sel, gain and dac_enable, inside the loop they are not implemented. The shifted out data is only increasing 12data bits from 0x00 to 0xfff and none of 12, 14, and 15 bits occurs.
Thank you! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Fri Mar 16, 2012 4:59 am |
|
|
I disagree about the union, the code here is clear, fast and easy to understand.
However agree wholeheartedly about the defines. The problem is that you are changing these bits in the count value, which then means the count will immediately terminate...
Hence the outer loop will then be executed again and again, just sending a count of '0'.
The key would be to separate things:
Code: |
#define sel_gain_enable (0b1011000000000000)
MCP_4822_set(){
int16 icount;
for(icount=0;icount<0xfff;icount++){
sent.word=icount | sel_gain_enable; // set the three extra bits needed
spi_write(sent.part[1]);
spi_write(sent.part[0]);
delay_ms(10);
}
}
|
This way the number used for the count, is independant of the value being sent.
Best Wishes |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Fri Mar 16, 2012 11:43 am |
|
|
OMG. How couldn't came on my mind about the ORing function.
It's so simple. Thank you very much guys. |
|
|
|
|
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
|