PIC18F4685 SPI hardware and 9th bit;

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
MR2010
Posts: 109
Joined: 06 Jun 2010 13:28

PIC18F4685 SPI hardware and 9th bit;

#1 Post by MR2010 » 29 Mar 2012 23:25

Hello ;
i'm trying to send 9 bits using spi1 hardware,it works on pic16f877a but not on pic18f4685

this function working good on pic16f877a

Code: Select all

void WriteSpiData(  char Dat)



{
           SSPCON= 0;     //DISABLE SPI

                  PORTC.B5 = 1;     //SEND THE 9TH BIT
                  PORTC.B3 = 1;
                  PORTC.B3 = 0;

           SSPSTAT = 0b11000000;
           SSPCON = 0b10100000;   //ENABLE SPI
           SSPBUF = (dat&0xFF);
}

this one aint working for pic18f4685

Code: Select all

void WriteSpiData(char Dat)
{
 
                            SSPCON1.SSPEN = 0;
                  LATC.B5 = 1;     //SEND THE 9TH BIT
                  LATC.B3 = 1;
                  LATC.B3 = 0;

                     SSPSTAT = 0b11000000;
                    SSPCON1 = 0b10100010;
               SSPBUF =(dat&0xFF);
}
Last edited by MR2010 on 31 Mar 2012 01:22, edited 1 time in total.

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: PIC18F4685 SPI hardware and 9th bit;

#2 Post by janko.kaljevic » 30 Mar 2012 12:15

Hello,

Please check it on scope. This should work.
Also I do not have experience with 9bit SPI and I am afraid that I will not be very helpful.

Best regards.

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: PIC18F4685 SPI hardware and 9th bit;

#3 Post by Mince-n-Tatties » 30 Mar 2012 14:43

MR2010 wrote:

Code: Select all

void WriteSpiData(  char Dat)



{
           SSPCON= 0;     //DISABLE SPI

                  LATC.B5 = 1;     //SEND THE 9TH BIT
                  LATC.B3 = 1;
                  LATC.B3 = 0;

           SSPSTAT = 0b11000000;
           SSPCON = 0b10100000;   //ENABLE SPI
           SSPBUF = (dat&0xFF);
}
the above code cannot work on a 16F877A, there is no LATch on that pic.

i dont have time right now to look at the ASM and pic data sheets in-depth for this issue, but here is a first step try...

force the tris bits to output on the 4685

Code: Select all

void WriteSpiData(char dat)
{

    #ifdef P16F877A
          SSPCON= 0;     //DISABLE SPI
          PORTC.B5 = 1;     //SEND THE 9TH BIT
          PORTC.B3 = 1;
          PORTC.B3 = 0;

          SSPSTAT = 0b11000000;
          SSPCON = 0b10100000;   //ENABLE SPI
          SSPBUF = (dat&0xFF);

    #endif

    #ifdef P18F4685
     
          SSPCON1.SSPEN = 0;
          TRISC.B5 = 0;    // force to output mode
          TRISC.B3 = 0;    // force to output mode
          LATC.B5 = 1;     //SEND THE 9TH BIT
          LATC.B3 = 1;
          LATC.B3 = 0;

          SSPSTAT = 0b11000000;
          SSPCON1 = 0b10100010;
          SSPBUF =(dat&0xFF);
          
    #endif
}
Last edited by Mince-n-Tatties on 30 Mar 2012 14:52, edited 1 time in total.
Best Regards

Mince

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: PIC18F4685 SPI hardware and 9th bit;

#4 Post by Mince-n-Tatties » 30 Mar 2012 14:50

to the mE team...

is it expected that there is such a diff in compile time between 18F and 16F for this simple project?

Code: Select all

void WriteSpiData(char dat)
{

    #ifdef P16F877A
          SSPCON= 0;     //DISABLE SPI
          PORTC.B5 = 1;     //SEND THE 9TH BIT
          PORTC.B3 = 1;
          PORTC.B3 = 0;

          SSPSTAT = 0b11000000;
          SSPCON = 0b10100000;   //ENABLE SPI
          SSPBUF = (dat&0xFF);

    #endif

    #ifdef P18F4685
     
          SSPCON1.SSPEN = 0;
          TRISC.B5 = 0;    // force to output mode
          TRISC.B3 = 0;    // force to output mode
          LATC.B5 = 1;     //SEND THE 9TH BIT
          LATC.B3 = 1;
          LATC.B3 = 0;

          SSPSTAT = 0b11000000;
          SSPCON1 = 0b10100010;
          SSPBUF =(dat&0xFF);
          
    #endif
}




void main() {

   //SPI1_Init();                           // Initialize SPI module


   WriteSpiData(0xa) ;

}
compile data for 16F877A
16F877A.jpg
16F877A.jpg (104.07 KiB) Viewed 3092 times
Compile data for 18F4685
18F4685.jpg
18F4685.jpg (100.31 KiB) Viewed 3092 times
Best Regards

Mince

MR2010
Posts: 109
Joined: 06 Jun 2010 13:28

Re: PIC18F4685 SPI hardware and 9th bit;

#5 Post by MR2010 » 31 Mar 2012 00:36

Thank you for help i will give it a try ,about writing LATC for pic 16 was mistak of copy and past ,and about using mC spi library will never work bcz its slow and limited, anyway this code works on pic16 good but i wanted more speed and more space thats why im trying to use pic18;
Thanks everybody ;

MR2010
Posts: 109
Joined: 06 Jun 2010 13:28

Re: PIC18F4685 SPI hardware and 9th bit;

#6 Post by MR2010 » 31 Mar 2012 01:40

Hi ;
back again i tryed this but nothings .

Code: Select all

  void WriteSpiData(  char Dat)
  {

          SSPCON1.SSPEN = 0;
          TRISC.B5 = 0;    // force to output mode
          TRISC.B3 = 0;    // force to output mode
          LATC.B5 = 1;     //SEND THE 9TH BIT
          LATC.B3 = 1;
          LATC.B3 = 0;

          SSPSTAT = 0b11000000;
          SSPCON1 = 0b10100000;
          SSPBUF =(dat&0xFF);

  }

but spi soft like this works ,the problem is slow ;

Code: Select all


#define  SCK   LATC.B3
#define  SDIN  LATC.B5

void WriteSpiData(unsigned char dat)
{
      unsigned char x = 8;


                SDIN  = 1;
                SCK = 1;
                SCK = 0;

        while ( x--)
                {
                        SDIN = Dat >> 7 & 0x01;
                        SCK = 1;
                        SCK = 0;
                        Dat = Dat << 1;
                }
}
Attachments
and here is 4685 spi master waveform
and here is 4685 spi master waveform
SPI MODE WAVEFORM.jpg (96.34 KiB) Viewed 3081 times
here is a picture of how i want it to work
here is a picture of how i want it to work
spi timing.jpg (30.61 KiB) Viewed 3081 times

MR2010
Posts: 109
Joined: 06 Jun 2010 13:28

Re: PIC18F4685 SPI hardware and 9th bit;

#7 Post by MR2010 » 31 Mar 2012 23:31

to mE team ;

i used the logic analizer and the problem is mC compiler has bug in sending data on raising and falling edge ,
the problem in the CLK doesnt works as normal spi in mC library

anyone has any idea why and how to sort this out
Attachments
the problem in mC.jpg
the problem in mC.jpg (95.8 KiB) Viewed 3057 times

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

Re: PIC18F4685 SPI hardware and 9th bit;

#8 Post by Mince-n-Tatties » 01 Apr 2012 11:12

MR2010 wrote:to mE team ;

i used the logic analizer and the problem is mC compiler has bug in sending data on raising and falling edge ,
the problem in the CLK doesnt works as normal spi in mC library

anyone has any idea why and how to sort this out
it is true that the default spi_init library function will set CKP = 0 & CKE = 1. this is not a bug it is 1 of 4 SPI config settings.

however i am further confused by your comment, because you also override the SSPCON and SSPSTAT with the exact same settings CKP = 0 & CKE = 1 in both the code blocks you posted. You have also changed the data sample option SMP, dont know why as this is for input data.

did you actually post the code your are using?

you have two options,

1. drive SSPCON(1) and SSPSTAT correctly as required by you via direct write to the registers. Read the device data sheet
2. use the library SPIx_Init_Advanced function. Read the help file for directions on how to use.

this is what the SPI_Init library does in both cases (877A & 4685)

Code: Select all

TRISC5_bit = 0;
TRISC3_bit = 0;
TRISC4_bit = 1;

SSPCON(1) = 0b00000000;
SSPSTAT = 0b01000000;
SSPCON(1) = 0b00100000;
which equates to clock as output, sdo as output, sdi as input, CKE = 1, CKP = 0, SSPEN = 1.

what compiler version are you using? 5.40 is the current release.
Best Regards

Mince



jtemples
Posts: 258
Joined: 22 Jan 2012 05:46

Re: PIC18F4685 SPI hardware and 9th bit;

#11 Post by jtemples » 01 Apr 2012 21:58

the hardware spi is structured to send 8 bits. so it is tough to have a fully-hardware-implemented 9-bit solution.

a few alternatives:

1) many spi slaves allow 8+ bits to be sent. they either take the last few bits (in your case last 9 bits) or they discard the the last few bits. so if your slave works that way, you can pad your data and send them via the hardware spi.

for example, if your chip takes just the last 9 bits, something like this would work for you: spi_send(0x01); spi_send(dat); will send 0x01:dat, which is effectively identical to what you are trying to do.

2) use software spi: you can send as many bits as you want.

MR2010
Posts: 109
Joined: 06 Jun 2010 13:28

Re: PIC18F4685 SPI hardware and 9th bit;

#12 Post by MR2010 » 01 Apr 2012 22:12

jtemples wrote:the hardware spi is structured to send 8 bits. so it is tough to have a fully-hardware-implemented 9-bit solution.

a few alternatives:

1) many spi slaves allow 8+ bits to be sent. they either take the last few bits (in your case last 9 bits) or they discard the the last few bits. so if your slave works that way, you can pad your data and send them via the hardware spi.

for example, if your chip takes just the last 9 bits, something like this would work for you: spi_send(0x01); spi_send(dat); will send 0x01:dat, which is effectively identical to what you are trying to do.

2) use software spi: you can send as many bits as you want.
Thank you jtemples .

my hardware target needs 1st 0 bit for C then 8 bits of data or 1 bit for D then 8bits of data,the device im trying to drive doesnt discard any data ;
if you look above to the timming picture you will understand what im trying to do, the problem im having is the clock pin doesnt output the correct signals when is set on SKE = 1 and SKP = 0; but otherwise the clock out pin works well ;
for using soft spi seems to work slow even PLL enabled , my code has huge of data to be sent in short time thats why i'm trying to use spi hardware ;

Cheers;
Attachments
target device
target device
spi timing.jpg (30.61 KiB) Viewed 3029 times

jtemples
Posts: 258
Joined: 22 Jan 2012 05:46

Re: PIC18F4685 SPI hardware and 9th bit;

#13 Post by jtemples » 01 Apr 2012 23:03

if your device is what I think it is, there are a few issues with your routines (even the "working" one).

1) the datasheet suggests that SDA is sampled into the controller on the rising edge of SCK, not falling edge. SDO is on the falling edge of SCK.
2) SCK has to be low on the falling edge of SCE.

so if I were to write your code, it would be something like this:

//configure spi to send data on rising edge, and sck idles low (ckp=0, cke=0)
spi_init(void) {
//...
set sck/sdo as output;
}

spi_write(unsigned char dat) {
set sspen; //turn on sspen - take control of spi pins
sspbuf=dat; //send the data
}

device_write(unsigned char dc, unsigned char dat) {
clear sck; //sck/sdo already set as output
if dc==command, clear sda; //assuming dc=0 -> command
else set sda; //assuming dc=1 -> data
clear sspen; //now sck and sda are under tris/port/lat control
set sck; //send the dc bit;
spi_write(dat); send the data byte;
}

you will handle sce separately.

I think that if you use two pins to handle the 9-bit data (1 pin to handle the dc bit and another fully dedicated to sdo), you can speed up the transmission considerably - no turning on / off the spi module. you however need to use 1 - 2 resistors if you want to go down that route.

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: PIC18F4685 SPI hardware and 9th bit;

#14 Post by janko.kaljevic » 02 Apr 2012 15:59

Hello,

@Mince
These compilation times are very strange.
I have tested it on more computers and compiling times were much less than yours.

On my computer
P16F877A - 94ms
P18F4685 - 187ms

It takes more time for PIC18F but really it should not take that much time.

Best regards.

MR2010
Posts: 109
Joined: 06 Jun 2010 13:28

Re: PIC18F4685 SPI hardware and 9th bit;

#15 Post by MR2010 » 03 Apr 2012 18:04

Thanks jtemples .

still trying everythings no way spi clock problem;

Cheers;

Post Reply

Return to “mikroC PRO for PIC General”