CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problems with spi and PIC18F818

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
spom



Joined: 12 Jul 2007
Posts: 32

View user's profile Send private message

Problems with spi and PIC18F818
PostPosted: Mon Nov 12, 2007 3:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 12, 2007 5:16 am     Reply with quote

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







PostPosted: Mon Nov 12, 2007 7:43 am     Reply with quote

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








PostPosted: Mon Nov 12, 2007 9:48 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 12, 2007 10:58 am     Reply with quote

The CCS driver is for the AD8400 only and ignores the pot-number.
See http://www.ccsinfo.com/forum/viewtopic.php?t=4532 for a modification so it uses the pot-number.
spom



Joined: 12 Jul 2007
Posts: 32

View user's profile Send private message

PostPosted: Mon Nov 12, 2007 11:28 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 12, 2007 1:29 pm     Reply with quote

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








PostPosted: Fri Dec 14, 2007 2:43 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 14, 2007 3:05 pm     Reply with quote

They allow you to setup the hardware SPI module for one of the
four possible SPI modes. See this webpage:
http://www.totalphase.com/support/articles/article03/#modes
Guest








PostPosted: Fri Dec 14, 2007 5:40 pm     Reply with quote

i have this timing on the slave:
h ttp://img152.imageshack.us/my.php?image=timingcs9.png
which mode should i use? SPI_MODE_1_1 ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 14, 2007 5:55 pm     Reply with quote

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:
Code:
SPI_MODE_0_1


Example:
Code:
setup_spi(SPI_MASTER | SPI_MODE_0_1 | SPI_CLK_DIV_16);
Guest








PostPosted: Fri Dec 14, 2007 6:18 pm     Reply with quote

what is SCLK idle state?

thats all what they show -> h ttp://img519.imageshack.us/my.php?image=timingry8.png
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 14, 2007 6:55 pm     Reply with quote

It doesn't say. Just try Mode 1.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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