![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
I need help setting up MCP41100 digital potentiometer. |
Posted: Wed Nov 14, 2007 6:00 am |
|
|
Hi all,
mcu: 16F628
PCM: 3.224
Iam trying to get the MCP41100 digital pot to work. I am using my own driver codes for this..
Code: |
#define SPI_SCK pin_a3
#define SPI_DOUT pin_a5
#define SPI_DIN pin_b0
#define MCP3208_CS pin_b3
#define MCP4822_CS pin_b4
#define MCP41100_CS pin_b5
void dpot_init()
{
output_high(MCP41100_CS);
}
void setup_dpot_write()
{
output_low(SPI_SCK);
output_low(MCP41100_CS);
output_low(SPI_SCK);
output_high(SPI_DIN);
delay_ms(2);
}
void dpot_data_out(int16 data_byte)
{
int i;
for (i=0;i<=15;i++)
{
if (bit_test(data_byte,i)==1)
output_high(SPI_DIN);
else
output_low(SPI_DIN);
output_high(SPI_SCK);
delay_ms(8);
output_low(SPI_SCK);
delay_ms(8);
}
delay_ms(8);
output_high(MCP41100_CS);
delay_ms(8);
}
void dpot_output(int command,int pot,int pot_data)
{
int i,b;
int16 dpot_word;
setup_dpot_write();
dpot_word=0;
if (command!=0)
{
bit_clear(dpot_word,0); //Don't care
bit_clear(dpot_word,1); //Don't care
if (bit_test(command,1)==1)
bit_set(dpot_word,2);
else
bit_clear(dpot_word,2);
if (bit_test(command,0)==1)
bit_set(dpot_word,3);
else
bit_clear(dpot_word,3);
bit_clear(dpot_word,4); //Don't care
bit_clear(dpot_word,5); //Don't care
if (bit_test(pot,1)==1) //Don't care for MCP41XXX devices
bit_set(dpot_word,6);
else
bit_clear(dpot_word,6);
if (bit_test(pot,0)==1) //Don't care for MCP41XXX devices
bit_set(dpot_word,7);
else
bit_clear(dpot_word,7);
b=15;
for (i=8;i<=15;i++)
{
if (bit_test(pot_data,b-8)==1)
bit_set(dpot_word,i);
else
bit_clear(dpot_word,i);
b--;
}
dpot_data_out(dpot_word);
}
}
|
I use the following code sequence.
Code: |
dpot_init(); //This is done when mcu is started (initialised)
dpot_output(1,2,51); ///Should set 20K (format: command,pot no,pot value)
|
As it can be seen in the define , I am using a DAC MCP4822 and an ADC MCP3208 all these devices (including the digital pot) are in the same SPI bus.
Except for the digital pot all the other devices work well.
Kindly help....
thanks
arunb |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Nov 14, 2007 6:52 am |
|
|
First a couple of comments.
Where I used to work, you would be fired on a project, if you 'named' a variable, that was 16bit, with a name including 'byte'. If you are writing code where the functions are a lot larger and more complex, and which may need maintenance by other people latter, it is vital that you don't give wrong cluse like this!...
Second, you have added some huge delays. The chip deals in times of tens of nSec. mSec, are a bit OTT!. Presumably this was done as part of the debugging, but the 'silly' thing is that the one place where a delay might be needed, is the one place you haven't got one!. The chip samples on the rising edge of the clock pulse, so it is worth ensuring that once the data bit is output, it is stable before raising this edge.
Third comment, you talk about having other devices on the same SPI bus. You are presumably not connecting the DOUT pin on this chip?. Unlike some chips, this one does not tri-state the output, when not selected.
Now for the reason it doesn't work. What _order_ are the bits meant to be sent to this chip?. What order are you sending them?.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
RE |
Posted: Wed Nov 14, 2007 9:49 am |
|
|
Hi,
Thank you for the comments, they are indeed useful.
You are quite right about the delay routine being not in the right place. I did not notice this until now.
I used the following settings.....(after initializing the device using dpot_init() ).
dpot_output(1,2,1)
Using the MPLAB simulator I got a value of 0x8048 in the dpot_word variable (used in dpot_output() function).
The contents of dpot_word are outputted bit 0 first...bit15 is the last bit to be outputted. (See dpot_data_out() function).
Hope this was helpful.
thanks
arunb |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Nov 14, 2007 10:58 am |
|
|
Yes, and what order does the 41100 expect to see the bits?.
It expects the data MSB first. You are outputting the word LSB first.
Code: |
void dpot_data_out(int16 data_byte) {
int i=15;
//Why fiddle around dropping CS in another function?.
output_low(MCP41100_CS);
do {
if (bit_test(data_byte,i)==1)
output_high(SPI_DIN);
else
output_low(SPI_DIN);
delay_cycles(1);
output_high(SPI_SCK);
delay_cycles(1);
output_low(SPI_SCK);
} while (i--)
delay_cycles;
output_high(MCP41100_CS);
}
void dpot_output(int command,int pot,int pot_data) {
int i,b;
int16 dpot_word;
dpot_word=command*4096;
dpot_word!=pot*256;
dpot.word!=pot_data;
dpot_data_out(dpot_word);
}
|
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
RE: |
Posted: Wed Nov 14, 2007 10:32 pm |
|
|
I finally got the device working.
After examining the data sheet for the MCP41XXX closely I found that only the P1 bit of the command word was don't care for the MCP41XXX device. I assumed (my mistake ) that both the bits P1 and P0 were don't care.
I believed that the MCP41XXX being a single pot device, did not require the pot. selection bits.
Thank you for the help.
thanks
arunb |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|