Using SPI Library to read and write to ADC registers ?

General discussion on mikroPascal.
Post Reply
Author
Message
amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

Using SPI Library to read and write to ADC registers ?

#1 Post by amfortas » 29 Apr 2014 23:35

Hii

I wish to write0x02 to an ADC register at 97h and then check the result by reading the contents at read address 17h using the SPI Library;

I tried
Chip_Select:=0
SPI_Write (0x17)
SPI_Write (0x02)
Chip_Select:=1

then

Chip_Select:=0
SPI_Write (0x17)
Result:= SPI_Read (0x00) {dummy byte}
Chip_Select:=1;

but this returns 0. I must be doing something wrong !

Has anyone a sample routine for writing and reading device registers ?

Very many thanks,

Colin

User avatar
petar.timotijevic
mikroElektronika team
Posts: 1739
Joined: 19 Feb 2014 13:46
Location: Serbia
Contact:

Re: Using SPI Library to read and write to ADC registers ?

#2 Post by petar.timotijevic » 30 Apr 2014 13:43

Hi,

Please see ADC Click example code.

ADC Click
http://www.mikroe.com/click/adc/


Best regards,
Peter

amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

Re: Using SPI Library to read and write to ADC registers ?

#3 Post by amfortas » 30 Apr 2014 16:42

Thanks Peter,

That should help.

Best regards,

Colin

amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

Re: Using SPI Library to read and write to ADC registers ?

#4 Post by amfortas » 01 May 2014 10:55

Hi Further to my earlier posting, I am actually trying to make sense of an Intersil ISL26104 which complicates matters by having different read and write configrations.

Very confusing and so far beyond my nearly 80 yr old grey matter !


I have extracted these cofigurations
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Reading data

Address Writing
SCK Idle Low
SMP=0
Bit Transmission SDO MIDDLE of Bit - Rising edge of clock
Next Bit SDO falling edge of clock
Address typically 0x17


Getting Register Content

Dummy byte Transmission SDO Middle of Bit - Rising edge of clock
SDI Data
SMP=1
SDI INPUT SDI Middle of bit - Falling edge of clock
Next Bit SDI rising edge of clock

------------------------------------------------------------------------------------
All data is 8 bit.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Writing Data to Registers

SCK Idle Low
SMP=0
Bit Transmission SDO Middle of Bit - Rising Edge of clock (Both Address and Data)
Address MSB=1 (Typically 0x97)

-----------------------------------------------------------------------------------

What specifically confuses me is the apparent need to re-init between the addressing phase when reading device registers.

SPI1_Init to Middle- High to Low +++
Chip_Select:=0;
SPI1_Write(Address);

SPI1 Init to Middle - Low to high ***
Res:=SPI1_Read(Dummy byte);
Chip_Select:=1;

I have connected my EasyPic6 Port C to the hardware with the four SPI lines functioning correctly under scope examination.

Do these Pascal Pro codings look like the correct way to make sense out of the strange, but otherwise superb ISL 261024? (They say it has a simple SPI interface !).

Many thanks,

Colin

amfortas
Posts: 212
Joined: 18 Feb 2010 18:10

Re: Using SPI Library to read and write to ADC registers ?

#5 Post by amfortas » 14 May 2014 00:14

The eventual solution for the ISL 26104 ADC was rather simple. I used a strange
mix of inline ASM to access the Microchip SPI registers yet gain easy access to variables.

Procedure Read_Register(AD,Dummy:Byte);

var R1:byte;
S:string[5];

begin
SSPSTAT:=0x40; { 0 1 0 0 0 0 0 0 }
{ SMP = 0, CKE = 1}
SSPCON1:=0x22; { 0 0 1 0 0 0 1 0 }
{ CKP Idle Low. Clock Fosc/6}
SSPBUF:=AD; { Load Register Address Byte }
ASM
bcf Chip_Select { Chip_Select =0 }
Char1: btfss SSPSTAT,BF { Check Address in SPI Buffer }
goto Char1
movf SSPBUF,W { If SPI Buffer full then discard }
end;
SSPSTAT:=0x40; { 1 1 0 0 0 0 0 0 }
SSPBUF:=Dummy; { Load Register Data Byte }
ASM
Char2: btfss SSPSTAT,BF { Check Data in SPI Buffer }
goto Char2
movf SSPBUF,W { If SPI Buffer full then }
movwf _Res1 { pass Data to Res1 }
{ bsf Chip_Select { Chip_Select=1 Transaction complete }
end;
Delay_uS(50);
Chip_Select:=1;
ByteToStr(Res1,S);
LCD_OUT(2,3,S);
end;

Function Get_Register(AD,Dummy:Byte): Byte;
var R1:byte;
begin
SSPSTAT:=0x40; { 0 1 0 0 0 0 0 0 }
{ SMP = 0, CKE = 1}
SSPCON1:=0x22; { 0 0 1 0 0 0 1 0 }
{ CKP Idle Low. Clock Fosc/6}
SSPBUF:=AD; { Load Register Address Byte }
ASM
bcf Chip_Select { Chip_Select =0 }
Char1: btfss SSPSTAT,BF { Check Address in SPI Buffer }
goto Char1
movf SSPBUF,W { If SPI Buffer full then discard }
end;
SSPBUF:=Dummy; { Load Register Data Byte }
ASM
Char2: btfss SSPSTAT,BF { Check Data in SPI Buffer }
goto Char2
movf SSPBUF,W { If SPI Buffer full then }
movwf _Res1 { pass Data to Res1 }
end;
Delay_uS(50);
Chip_Select:=1;

Result:=Res1;
end;



Procedure Write_Register(Ad,Dat:Byte);

begin
SSPSTAT:=0x40; { 0 1 0 0 0 0 0 0 }
{ SMP = 0, CKE = 1}
SSPCON1:=0x22; { 0 0 1 0 0 0 1 0 }
{ CKP Idle Low. Clock Fosc/6}
SSPBUF:=Ad; { Load Register Address Byte }
ASM
bcf Chip_Select { Chip_Select =0 }
Char1: btfss SSPSTAT,BF { Check Address in SPI Buffer }
goto Char1
movf SSPBUF,W { If SPI Buffer full then discard }
end;
SSPBUF:=Dat; { Load Register Data to send }
ASM
Char2: btfss SSPSTAT,BF
goto Char2
movf SSPBUF,W
end;
Delay_uS(50);
Chip_Select:=1;
end;

Thanks for your help Peter. Kick started my thinking !


Amfortas

Post Reply

Return to “mikroPascal General”