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 support@ccsinfo.com

AD7856 using PIC SPI

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



Joined: 21 Sep 2010
Posts: 3
Location: Bangalore

View user's profile Send private message

AD7856 using PIC SPI
PostPosted: Wed Sep 22, 2010 12:40 am     Reply with quote

Hello,

I need help for ADC converter (14bit). This is my first time using external ADC as well as SPI. I have never used SPI before, so please go easy on me.

Please note I want to trigger CONVST by changing the Control bit rather than sending a pulse.

Here is my small test program.
Code:

void Ad_7856_Write()
{
   
   AD7856_SYNC = HIGH;
   DelayUs(1);
   AD7856_SYNC = LOW;
   DelayUs(1);
   
   Spi_Write(0xE5);
   Spi_Write(0xc0);

   DelayUs(1);
   AD7856_SYNC = HIGH;
   DelayUs(1);
}
   
void Ad_7856_Read()
{
   unsigned short Dig_Data=0;
   unsigned char Udata=0,Ldata=0;

   AD7856_SYNC = HIGH;
   DelayUs(1);
   
   AD7856_SYNC = LOW;

   Udata = Spi_Read();
   Ldata = Spi_Read();
   
   AD7856_SYNC = HIGH;
   DelayUs(1);
}


void init_Spi()
{
         /**
        *Initialise the SPI bus on the PIC
        */
   
   /*cke=1,ckp=0 rising edge*/
   SSPSTAT =0b11000000;   //smp=1,cke=1,ckp=0 rising edge
    SSPCON = 0b00100000;   //WCOL=0,sspov=0,sspen=1,ckp=0,Fosc/4

    SSPIF=0;                     //clear SPI interrupt flag
    SSPIE=1;                     //enable SPI interrupt
    POR=1;
 }


void Spi_Write( unsigned char spi_byte)
{
    char dummy=0x00;
    if(WCOL) WCOL = 0;            //for no collision
    BF=0;                   
    SSPIF=0;
    SSPEN=1;
    SSPBUF = spi_byte;
   DelayMs(1);
    while(!BF){}
    if(SSPOV!=0)SSPOV=0;         //overflow
    dummy=SSPBUF;                //receive dummy byte
   DelayMs(1);
}

unsigned char Spi_Read()
{
    char data;
    if(WCOL) WCOL = 0;
    BF=0;
    SSPIF=0;
    SSPEN=1;
   
    SSPBUF = 0xFF;              // Send dummy byte.
   DelayMs(1);
   
    while(!BF)
   {
   }
    if(SSPOV!=0)SSPOV=0;
    data = SSPBUF;
   DelayMs(1);
    return data;
}

Here my expected status is E5C0, but the result is not that when I read the AD7856 and also I have doubt about my spi read and write.
Can anybody help me?
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Sep 22, 2010 7:32 am     Reply with quote

why not use spi_read and spi_write functions included in CCS?


Code:
   Udata = Spi_Read();
   Ldata = Spi_Read();


you should send a 0x00 as a dummy to generate the clock


generaly the procedure is: (pseudo code time)

Code:
set chip select pin high/low  //depending on target chip logic
spi_write(address);           // send the address of what you want to read or write
value=spi_read(0x00);      // send dummy byte to read data from address sent above
set chip select pin low/ high //depending on target chip logic


note that SPI takes 4 lines... chip select, data in, data out, clk.

there is really not much more to it than that....

note the above mentioned logic is for single byte reads only.


to write you do the same with one minor change:

Code:
set chip select pin high/low  //depending on target chip logic
spi_write(address);           // send the address of what you want to read or write
spi_write(databyte);        // send dummy byte to WRITE data to address sent above
set chip select pin low/ high //depending on target chip logic



..... ccs has already made spi functions....

if your chip has hardware SPI then its pretty simple....


hope that helps ...

gabriel.
_________________
CCS PCM 5.078 & CCS PCH 5.093
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 22, 2010 2:31 pm     Reply with quote

Quote:
void Ad_7856_Write()
{

AD7856_SYNC = HIGH;
DelayUs(1);
AD7856_SYNC = LOW;
DelayUs(1);

Spi_Write(0xE5);
Spi_Write(0xc0);

DelayUs(1);
AD7856_SYNC = HIGH;
DelayUs(1);
}

This code is written for the HiTech compiler. You should ask questions
about your code in one of their forums (maybe in the PicMicro forum):
http://forum.htsoft.com/all/ubbthreads.php

Microchip also has a Hi-Tech forum:
http://www.microchip.com/forums/f231.aspx
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Sep 22, 2010 2:34 pm     Reply with quote

... I knew something was off...

thanks PCM...

g.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Mailar



Joined: 21 Sep 2010
Posts: 3
Location: Bangalore

View user's profile Send private message

AD7856 using PIC SPI
PostPosted: Sat Sep 25, 2010 5:06 am     Reply with quote

thanks for your reply..!
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