We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
hi to all
i am started working on ad7705 16 bit ADC and my first experience on SPI communication. the follwing code i wrote to communicate AD7705 to AT89s52
/******************************************************************************************************/ #include <REGX51.H> #include<define.h> #include<intrins.h> /*******************************************************************************************************/ volatile unsigned int TimeTick1ms; char DispBuf[DGTS_ON_DISP]; int SPI_OUTDATA_BUFFER; sbit SPI_DIN = P1^4; // SPI D_IN sbit SPI_DOUT = P1^5; // SPI D_OUT sbit SPI_DRDY = P1^6; // SPI D_RDY sbit SPI_CLK = P1^7; // SPI CLK /*****************function declaration***************************/ void SPI_Write(char COMMAND_DATA); void SPI_Read(); void TMR_1(void)interrupt 3 { TR1 = STOP; TL1 = COUNT_LO_1mS; TH1 = COUNT_HI_1mS; TR1 = RUN; TimeTick1ms++; } void main(void) { char i; P0 = 0x3f; P1 = 0xff; P2 = 0xf8; P3 = 0xff; IE = 0x8A; TMOD = 0x11; /* T1 in 16-bit timer mode and T0 in 16-bit timer mode*/ TH1 = 0x63; TL1 = 0xBF; TR1 = RUN; V2F = 0; /**********************************************/ while(1) { SPI_Write(0x20); // Active Channel Ain1(+)/Ain1(-),Next Opraton Write to CLK REG. for(i=0;i<=10;i++) {_nop_();} SPI_Write(0x0C); // Master Clk Enable, 4.9512 MHz Clk , O/P Update rate 50Hz (20 mSec). for(i=0;i<=10;i++) {_nop_();} SPI_Write(0x28); // Active Channel Ain1(+)/Ain1(-),Next Opraton Read to CLK REG. for(i=0;i<=10;i++) {_nop_();} SPI_Write(0x10); // Active Channel Ain1(+)/Ain(-),Next Opraton Write to SETUP REG. for(i=0;i<=10;i++) {_nop_();} SPI_Write(0x40); // Gain = 1, Bipolar Mode, Buffer Off, Clear FSYNC, Perform Self Calibration. for(i=0;i<=10;i++) {_nop_();} // Clearing FSYNC bit Starts sampling. while(SPI_DRDY); //wait for DRDY to go LOW SPI_Write(0x38); // Active Channel Ain1(+)/Ain(-),Next Opraton Read From DATA REG. for(i=0;i<=10;i++) {_nop_();} SPI_Read(); for(i=0;i<=10;i++) {_nop_();} val = SPI_OUTDATA_BUFFER; V2F = enable; disp(val,Lable_No); } } /***********************************************************************/ void SPI_Write(char COMMAND_DATA) { char i; bit SPI_IN_BIT; for(i=0;i<=8;i++) { SPI_IN_BIT = (COMMAND_DATA&0x80);// bit isolation 7 times SPI_CLK = 0;// clk is low SPI_DIN = SPI_IN_BIT;// data on data din sends COMMAND_DATA <<= 1; _nop_(); SPI_CLK = 1;// clk is high } SPI_CLK = 1; // the clk line is high } /**********************************************************************/ void SPI_Read() { char i; for (i=0;i<=16;i++) { SPI_CLK = 0; SPI_OUTDATA_BUFFER = (SPI_OUTDATA_BUFFER | SPI_DOUT); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); SPI_CLK = 1; if(i < 16) {SPI_OUTDATA_BUFFER <<= 1;} } SPI_CLK = 1; }
As DIN and SCLK waveform i saw on the CRO that are proper i dont no where i am makeing mistake.
list of problem are as follow 1. i am not comfirm that AD7705 getting the data or not 2. is my spi routine is proper 3. after AD7705 configuraton DRDY high and it remains on that state 4. i am using 4 MHz crystal but as per data sheet they said to use 4.9152MHz or 2MHz.
help me to solve this
1) I do not know if the AD SPI engine is the same, but the CodeArchitect generated code for the LPC0xx series works for the sane SILabs derivatives and some other processors with just minor changes, try it. 2) I guess the AT89s52 is the slave and you will need to do one of two a) buy a megabottle of aspirin or b) replace the chip with one with hardware SPI. Bit-banged SPI masters are fairly easy, bit-banged SPI slaves are a true ***.
Erik