View previous topic :: View next topic |
Author |
Message |
spom
Joined: 12 Jul 2007 Posts: 32
|
Problems with spi and PIC18F818 |
Posted: Mon Nov 12, 2007 3:43 am |
|
|
Hi,
I use PIC18F818 (http://ww1.microchip.com/downloads/en/DeviceDoc/39598e.pdf) and the digital potentiometer AD8402 (http://www.analog.com/UploadedFiles/Data_Sheets/AD8400_8402_8403.pdf).
Now I have a problem with spi.
First I make this calibration:
Code: |
#include <16f818.h>
#use delay(clock=8000000)
#fuses NOWDT,HS,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP
#define SPI_MODE_0_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_0_1 (SPI_L_TO_H)
#define SPI_MODE_1_0 (SPI_H_TO_L)
#define SPI_MODE_1_1 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
void main()
{
setup_spi(spi_master |SPI_MODE_0_0 | spi_clk_div_64 | SPI_SAMPLE_AT_END );
|
Is the spi_mode right? I don't have any Osci to controll the Clock.
Then I try to write into the AD8402 with spi_write.
Is it right, that I can send only 8bits with this command?
But for the AD8402 I need 10bits.
For that can I use the spi_write twice?
First bit 1 and 2 and with the second command bit 3 to 10? Or the other way round?
Sorry for the stupid questions!
Thanks a lot. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Nov 12, 2007 5:16 am |
|
|
The hardware SPI unit of the PIC can only send data in 8-bit multiples. The new V4 compiler version can generate a software driven SPI master with other bit quantities.
Had you tried the search function of this forum you would have found the following post:
http://www.ccsinfo.com/forum/viewtopic.php?t=28500 |
|
|
Ttelmah Guest
|
|
Posted: Mon Nov 12, 2007 7:43 am |
|
|
However the 84xx, is happy with 8bit writes. The 'key' is the second paragraph down on page 22 of the data sheet. The 'last ten bits of the data word, are held when CS goes high'. Hence you can clock any amount of leading 'garbage' in, you just have to ensure that the last ten bits sent are the ones you want.
Data is sent on the SPI, MSB first, so what you do, is drop the CS line, then send one byte, containing the two address bits as it's two least significant bits initially, then send the 'value' byte second, then raise the CS line.
Best Wishes |
|
|
Guest
|
|
Posted: Mon Nov 12, 2007 9:48 am |
|
|
Thanks for your replies.
And of course: I use the PIC16F818
Now I changed my code to the following (by the way: I've never used drivers, so sorry for my stupid questions):
Code: |
#include <16f818.h>
#use delay(clock=8000000)
#include <AD8400.c>
#fuses NOWDT,HS,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP
void main()
{
init_pots();
delay_ms(50);
while(TRUE)
{
set_pot(1,44);
}
}
|
The code should just set the AD8402 to a value of about 220 Ohm between W2 and B2.
But it doesn't work.
Why?
Do I have to set SHDN and RS on a HIGH level as the datasheet says?
Is my PIN connection right?
_____AD8402__PIC16F818
CS __7 ______ B6
SDI _ 8 ______ B2
CLK _ 9 ______ B4
Thanks a lot! |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
|
spom
Joined: 12 Jul 2007 Posts: 32
|
|
Posted: Mon Nov 12, 2007 11:28 am |
|
|
I tried this change, but nothing happened.
Do I have to change the "defines" in the driver?
From
Code: | #define RST1 PIN_B0
#define CLK PIN_B1
#define DI PIN_B2
#define NUM_POTS 1 |
to
Code: |
#define RST1 PIN_B6
#define CLK PIN_B4
#define DI PIN_B2
#define NUM_POTS 1
|
? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 12, 2007 1:29 pm |
|
|
You should have these connections between the PIC and the AD8402:
Code: |
PIC pin AD8402 pin
RST1 \CS pin 7
CLK CLK pin 9
DI DI pin 8
|
Note that CCS calls the chip select pin "RST1". It goes to \CS on the
AD8402, as shown in the table above. (It does Not go to \RS).
In addition to those, you need to make these connections on the AD8402:
Code: |
\RS to Vdd
\SHDN to Vdd
|
Of course, you also need to connect Vdd to power, and AGND and DGND
to ground on the AD8402. |
|
|
Guest
|
|
Posted: Fri Dec 14, 2007 2:43 pm |
|
|
whats the difference between:
#define SPI_MODE_0_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_0_1 (SPI_L_TO_H)
#define SPI_MODE_1_0 (SPI_H_TO_L)
#define SPI_MODE_1_1 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Guest
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 14, 2007 5:55 pm |
|
|
No. It samples data on the negative edge of SCLK. Your timing diagram
doesn't show the level for the SCLK idle state. Some chips allow SCLK
to idle high or low. So you should use Mode 1 or Mode 2.
Try using Mode 1 first. Use this constant in your setup_spi() statement:
Example:
Code: | setup_spi(SPI_MASTER | SPI_MODE_0_1 | SPI_CLK_DIV_16); |
|
|
|
Guest
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 14, 2007 6:55 pm |
|
|
It doesn't say. Just try Mode 1. |
|
|
|