View previous topic :: View next topic |
Author |
Message |
Setna
Joined: 24 Jun 2008 Posts: 10
|
Digital Pot Problem |
Posted: Tue Aug 19, 2008 8:50 am |
|
|
Hi,
I'm trying to control 4 AD5160 digital pots the PIC18F6722 development board. I have them daisy chained and trying to get them to just run a loop changing the resistance. Currently I have a voltmeter between the wiper and one end of the pot to measure the resistance. I can only get the resistance to show max and min values, nothing in the middle.
Here is the code:
Code: |
#include <18F6722.h>
#device ICD=TRUE
#include "LED.c"
#include "RudderServo.c"
#include "PID.c"
#include "Gyro.c"
#include "RightCamera.c"
#include "LeftCamera.c"
#include "Dpots.c"
#include <Calculation.h>
#include <stdlib.h>
#include <math.h>
#fuses HS,NOLVP,NOWDT
#use delay(clock=20000000)
int main()
{
int i = 0;
RED();
delay_ms(5000);
EnableSpi();
//delay_ms(1000);
LEDOFF();
GREEN();
while(TRUE)
{
output_low(C1); // C1 through C3 are defined CS pins
output_low(C2);
output_low(C3);
delay_us(20);
spi_write(i);
delay_us(20);
output_high(C1);
output_high(C2);
output_high(C3);
delay_ms(200);
i++;
if(i == 255)
i = 0;
}
return 0;
}
|
The pin definition and functions are in the .c file :
Code: |
#include "DPots.h"
#define C1 PIN_D1
#define C2 PIN_D2
#define C3 PIN_D3
void EnableSpi()
{
output_high(C1); //Configures the Digital Pots
output_high(C2);
output_high(C3);
setup_spi2(spi_master | spi_clk_div_16 | SPI_H_TO_L);
}
|
Thanks. |
|
|
Setna
Joined: 24 Jun 2008 Posts: 10
|
|
Posted: Tue Aug 19, 2008 10:05 am |
|
|
Found my error, simple typo in spi_write. Supposed to be spi_write2. |
|
|
Ttelmah Guest
|
|
Posted: Tue Aug 19, 2008 10:15 am |
|
|
Start by looking at the clock diagram for the chip, and the clock diagrams for the PIC. Note that the clock needs to start _low_, go high, and then the data is clocked on the _falling_ edge of the clock. This is not what is given by the SPI_H_TO_L selection. You need CKP=0, and CKE=0, which corresponds to SPI_L_TO_H. You can take the clock much higher (but 'sloth' shouldn't matter, and may be needed with the fairly high load of three chips). You don't need the delay after the write, or before sending the data. The setup time for these chips is a few _nSec_.
Triple check your connections. The data output from the SPI, will be on pin D4, with the clock on D6.
Best Wishes |
|
|
Setna
Joined: 24 Jun 2008 Posts: 10
|
|
Posted: Tue Aug 19, 2008 10:40 am |
|
|
Thanks very much Ttelmah. That fixed the problem. Also, I was wondering if you know of an SPI tutorial. I have only used it once before and have never completely understood the timing diagrams. If you know of one, it would be appreciated. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 19, 2008 12:12 pm |
|
|
Quote: | I was wondering if you know of an SPI tutorial | http://www.ccsinfo.com/forum/viewtopic.php?t=35393 |
|
|
|