View previous topic :: View next topic |
Author |
Message |
Mailar
Joined: 21 Sep 2010 Posts: 3 Location: Bangalore
|
AD7856 using PIC SPI |
Posted: Wed Sep 22, 2010 12:40 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Sep 22, 2010 7:32 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 22, 2010 2:31 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Sep 22, 2010 2:34 pm |
|
|
... I knew something was off...
thanks PCM...
g. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
![](templates/subSilver/images/spacer.gif) |
Mailar
Joined: 21 Sep 2010 Posts: 3 Location: Bangalore
|
AD7856 using PIC SPI |
Posted: Sat Sep 25, 2010 5:06 am |
|
|
thanks for your reply..! |
|
![](templates/subSilver/images/spacer.gif) |
|