View previous topic :: View next topic |
Author |
Message |
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
Why AD7706 and AD7715 drivers use software SPI? |
Posted: Tue Dec 18, 2007 7:53 am |
|
|
Just curious: why AD7706 and AD7715 drivers, which come with the IDE (v3.240), use software SPI and not hardware SPI?
Cheers,
- Nick _________________ Read the label, before opening a can of worms. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Tue Dec 18, 2007 9:02 am |
|
|
My guess is that a lot of PICs don't have SPI hardware. If you do it in software it is easier to adapt to any PIC. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue Dec 18, 2007 9:11 am |
|
|
I'm not sure but
it might be that a common "developers kit" that has these
chips on it happens to use the software spi,... thus the drivers
are written around them. Do you know of any kit boards that
have the AD7706 or AD7715 on them?? |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Tue Dec 18, 2007 9:54 am |
|
|
Today I have to get the readings from AD7718 into a PIC18LF4520, and AD7718 is an SPI part. I want to use SPI on the PIC. The registers of AD7718 look similar to those of AD7706, and I was hoping that I could reuse the driver.
My first try is to see what I get, when I read an empty SPI bus. I’ve physically removed all SPI peripherals. I have no pull-ups. For some reason spi_read() consistently returns 0x87. I was expecting 0x00, 0xFF or random noise. Does anyone know why I’m consistently getting 0x87?
Does anyone have sample code for AD7718 or AD7706, (they share the datasheet)?
P.S. I’m not a big fan of SPI – I prefer to use I2C whenever I can. _________________ Read the label, before opening a can of worms. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Sat Dec 22, 2007 5:45 am |
|
|
kender wrote: | My first try is to see what I get, when I read an empty SPI bus. I’ve physically removed all SPI peripherals. I have no pull-ups. For some reason spi_read() consistently returns 0x87. I was expecting 0x00, 0xFF or random noise. Does anyone know why I’m consistently getting 0x87? |
Changing from spi_read() to spi_read(0) solved this problem. _________________ Read the label, before opening a can of worms. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Sat Dec 22, 2007 8:15 am |
|
|
To make sure that my SPI bus works, I'm reading a hard-wired product code from the AD7718. It should be 0x4X (the second nobble depends on the silicon version). Here's my code (the helper functions only drive the chip-select low and high and call spi_write and spi_read):
Code: |
spi_write_AD7718(0b01001111); // address the ID register for reading
fprintf(DBUG, "0x%2X \n\r", spi_read_byte_AD7718());
|
I'm getting:
Code: |
0x43
0x00
0x00
0x00
0x43
0x00
0x00
and so on
|
Every 4th reading is the correct code. The rest are zeros. Where do all the zeros come from? _________________ Read the label, before opening a can of worms. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Dec 22, 2007 2:15 pm |
|
|
1. Post a short but complete test program.
2. Post your compiler version.
3. Post the list of connections between the PIC and the AD7718. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Tue Dec 25, 2007 5:46 pm |
|
|
PCM programmer wrote: | 1. Post a short but complete test program. |
Code: |
#include <18F4520.h>
#device ICD=TRUE
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV25 //Brownout reset at 2.5V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES DEBUG //Debug mode for use with ICD
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(clock=5068800)
#use rs232(baud=115200, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stream=BT)
#use rs232(DEBUGGER, xmit=PIN_B5, stream=DBUG)
|
Code: | void
spi_write_AD7718(int8 iByte)
// PURPOSE: 1 byte to AD7718
{
output_low(ADC_CS);
spi_write(iByte);
output_high(ADC_CS);
}
int8
spi_read_byte_AD7718()
{
int8 iRet = 0;
output_low(ADC_CS);
iRet = spi_read(0);
output_high(ADC_CS);
return iRet;
}
void init_hw()
{
int8 iCh;
setup_adc_ports(NO_ANALOGS | VSS_VDD);
setup_adc(ADC_OFF | ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_64);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED | T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(VREF_LOW | -2);
setup_low_volt_detect(FALSE);
setup_oscillator(FALSE);
}
void main()
{
init_hw();
while (1)
{
delay_ms(200);
spi_write_AD7718(0b01001111); // address the ID register for reading
fprintf(DBUG, "0x%2X \n\r", spi_read_byte_AD7718());
}
}
|
PCM programmer wrote: | 2. Post your compiler version. |
PCWH v3.240
PCM programmer wrote: | 3. Post the list of connections between the PIC and the AD7718. |
Here it is:
Code: |
PIC AD7718
SDO (43) DIN (24)
SDI (42) DOUT (23)
SCLK (37) SCLK (20)
D7 (5) CS# (21)
|
_________________ Read the label, before opening a can of worms. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 25, 2007 6:27 pm |
|
|
Quote: | #FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18) |
I can't really look at your code until tomorrow but one quick question
is, are you using an LVP programmer ? 99% of all programmers are
High Voltage programmers (ICD2, ICD-U40, etc). If you're using a
normal programmer, then you need to change that fuse to NOLVP. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Tue Dec 25, 2007 7:35 pm |
|
|
PCM programmer wrote: | Quote: | #FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18) |
I can't really look at your code until tomorrow but one quick question
is, are you using an LVP programmer ? 99% of all programmers are
High Voltage programmers (ICD2, ICD-U40, etc). If you're using a
normal programmer, then you need to change that fuse to NOLVP. |
If you could look at my code tomorrow, I'd really appreciate it.
I have ICD-U40, and I'm using high-voltage programming. I'll change the fuse to NOLVP, although I don't have any apparent problems downloading into the PIC.
Have a great Christmas! _________________ Read the label, before opening a can of worms.
Last edited by kender on Wed Dec 26, 2007 2:52 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 26, 2007 2:03 pm |
|
|
Quote: | setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_64); |
The SPI mode is wrong. In the statement above, you have selected
SPI Mode 1. You can tell this by looking at the table below:
Code: |
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
|
Look at page 11 in the AD7718 data sheet:
http://www.analog.com/UploadedFiles/Data_Sheets/AD7708_7718.pdf
It has two timing diagrams the show the relationship between SCLK and
data. You can determine the SPI mode from these diagrams. There
are only four possible modes. The important things to notice are:
1. The SCLK signal is at a high level when it's inactive.
2. The rising edge of SCLK occurs in the middle of each data cell.
Look at the timing diagrams down at the bottom of this page:
http://elm-chan.org/docs/spi_e.html
It shows the four SPI modes. The diagram that matches page 11
of the AD7718 data sheet is the one for SPI Mode 3.
You need to modify your setup_spi() statement to use the constant
for Mode 3. Paste the four #define statements into your program,
so they are above main(). Then delete SPI_L_TO_H from setup_spi()
and put in SPI_MODE_3 instead.
--------
Some other things --
Quote: | setup_vref(VREF_LOW | -2); |
This is a bug in the CCS Wizard. There is no legal "-2" parameter.
Just delete that line.
Quote: | #use delay(clock=5068800)
#use rs232(baud=115200, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stream=BT) |
This baud rate won't work with your crystal frequency. Go to this web
page and download the PicBaud program.
http://www.micromagicsystems.com/picbaud/4523812801
(This website plays a sound when you go to it).
Put in 5.0688 for the frequency, and 115200 for the baud rate. With
BRGH = 1, you get 8.33% error from the ideal baud rate. That's not
good enough. You won't be able to communicate with a PC. It needs
to be less than 2%, and the lower the better. |
|
|
|