CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

16bit SPI_Write Question
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
logical



Joined: 21 Dec 2009
Posts: 57
Location: SouthPort, UK

View user's profile Send private message Visit poster's website

16bit SPI_Write Question
PostPosted: Tue Feb 02, 2010 4:18 pm     Reply with quote

Hi All,

Just after a bit of quick advice, I am trying to use a 16BIT Spi Function, rather than sending out 2x8bit chunks, I was wondering if it was possible to create an Unsigned INT16 then use that for the SPI_Write Command.
Basically SPI_WRITE(Unsigned INT16 Variable).

Compiler 4.093

Thanks

Chris

p.s sorry if this has been discussed before, had a look round with the search but could not find anything relating to this.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 02, 2010 5:39 pm     Reply with quote

Post your PIC. We don't remember your PIC from previous posts.

Also post if you are trying to do hardware, or software SPI.
logical



Joined: 21 Dec 2009
Posts: 57
Location: SouthPort, UK

View user's profile Send private message Visit poster's website

PostPosted: Tue Feb 02, 2010 6:21 pm     Reply with quote

PCM programmer wrote:
Post your PIC. We don't remember your PIC from previous posts.

Also post if you are trying to do hardware, or software SPI.


Thanks PCM as always, shame you not local as owe you a fair few drinks.

Pic Wise, 24HJ128GP504 or 18f4550 both obviously using Hardware SPI, this is totally a different project to the others I was working on, another thanks for that, with your (point in the right direction) I managed to complete the SH1101A Driver which works perfectly.

This one is for a new toy, full colour Densitron OLED with the SEPS525 Driver, I actually have one written by someone else in CCS (open source) BUT it is not for SPI sadly.
Let me know if that is of interest to you for you to play with.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 02, 2010 6:44 pm     Reply with quote

The 24HJ has the capability to transmit 16-bit data. The SPI module
can be configured for 8 or 16-bit mode. It doesn't look like CCS
supports 16-bit mode. I don't have the PCD compiler, so I can't test this.

But you can easily create an spi_write16() routine that will write two
sequential bytes. Example:
Code:

#include <18F4550.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

// SPI mode definitions for 16F and 18F PICs (not for PIC24).
#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)

void spi_write16(int16 data)
{
spi_write(make8(data, 1));   // Send MSB
spi_write(make8(data, 0));   // Send LSB
}

//======================================
void main(void)
{
spi_write16(0x1234);

while(1);
}
logical



Joined: 21 Dec 2009
Posts: 57
Location: SouthPort, UK

View user's profile Send private message Visit poster's website

PostPosted: Tue Feb 02, 2010 7:09 pm     Reply with quote

PCM programmer wrote:
The 24HJ has the capability to transmit 16-bit data. The SPI module
can be configured for 8 or 16-bit mode. It doesn't look like CCS
supports 16-bit mode. I don't have the PCD compiler, so I can't test this.

But you can easily create an spi_write16() routine that will write two
sequential bytes. Example:
Code:

#include <18F4550.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

// SPI mode definitions for 16F and 18F PICs (not for PIC24).
#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)

void spi_write16(int16 data)
{
spi_write(make8(data, 1));   // Send MSB
spi_write(make8(data, 0));   // Send LSB
}

//======================================
void main(void)
{
spi_write16(0x1234);

while(1);
}


Thanks again, i will do exactly that, once the screen is up and running I will have a go with 16bit SPI to see if I can send in one, I will also then post the results here as it may be useful to others if they come accross the same thing.
Will verify it with a scope as the board is almost completed.

Regards

Chris
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Feb 03, 2010 12:53 am     Reply with quote

In my opinion, the advantage of using 16-Bit SPI transfer is more or less meaningless, if you don't need to transmit
large amounts of data at maximum SPI speed. Don't know, if this is the case in your application. That's the reason,
why I wasn't motivated yet to try it.

Generally, when using spi_write() and spi_read() together, you should be aware, that a succeeding spi_read()
possibly fails, because spi_write() doesn't wait for the SPI transaction to finish. A simple workaround is to use
spi_read() only.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 03, 2010 1:26 am     Reply with quote

You mean that with regard to the PCD compiler.
logical



Joined: 21 Dec 2009
Posts: 57
Location: SouthPort, UK

View user's profile Send private message Visit poster's website

PostPosted: Wed Feb 03, 2010 12:47 pm     Reply with quote

Now I think I am finally losing it, I am getting 7clocks (tested on oscilloscope) when sending an 8bit SPI Write, thanks to PCM I hunted through the software and found the 16bit SPI Mode (SPI_MODE_16B) which works apart from one fact, that is sending 14clocks out on a 16bit Write.

At a total loss on this one, basically one missing clock per 8 which eliminates the trigger on the Scope, brand new PIC, no shorted tracks, proper filter capacitors within a few mm of the Micro and powered from a PicKit2 at 3.3v (also tested), no PLL and just for getting started, using a decent 4MHZ Crystal Oscillator.

To top it off, absolutely no data on the SPI Data Out Port.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Feb 03, 2010 4:46 pm     Reply with quote

Quote:
You mean that with regard to the PCD compiler.

Yes, of course. It's a special PCD issue.

Quote:
I am getting 7clocks (tested on oscilloscope) when sending an 8bit SPI Write
...
To top it off, absolutely no data on the SPI Data Out Port.

Sounds very dubious. You may want to share an example program that reproduces this waveforms.
logical



Joined: 21 Dec 2009
Posts: 57
Location: SouthPort, UK

View user's profile Send private message Visit poster's website

PostPosted: Wed Feb 03, 2010 4:53 pm     Reply with quote

FvM wrote:
Quote:
You mean that with regard to the PCD compiler.

Yes, of course. It's a special PCD issue.

Quote:
I am getting 7clocks (tested on oscilloscope) when sending an 8bit SPI Write
...
To top it off, absolutely no data on the SPI Data Out Port.

Sounds very dubious. You may want to share an example program that reproduces this waveforms.


I have tried creating the most basic code just to try and get a regular 8 bits out of the SPI Port, as you will see by the lines commented out, have just about ripped my hair out over this Shocked
When I disable the Write_Spi and uncomment the output_high/_low lines + the setup_spi2, the pin B3 works absolutely fine, the scope has markers to measure the waveform pulse width and confirms that to be correct too, have spent quite a few days on this before posting here to avoid wasting anyones time.

Chris

Code:
void main()
{
  //setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
  setup_spi2(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
  #pin_select SCK1OUT=PIN_B12
  #pin_select SDO1=PIN_B13
  #pin_select SDI1=PIN_B11
  //The following port is for the OLED
  #pin_select SCK2OUT=PIN_B2
  #pin_select SDO2=PIN_B3
  #pin_select SDI2=PIN_C0
  ///////////////////////////////////
   setup_wdt(WDT_OFF);
   setup_timer1(TMR_DISABLED);
 //  delay_ms(500);
//   oled_init();
   int8 temp;
   while (true){
   spi_write2(0xff);
   //output_high(pin_b3);
   //delay_ms(50);
   //output_low(pin_b3);
   //delay_ms(50);
   delay_us(200);
   }
}
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Feb 03, 2010 5:24 pm     Reply with quote

I also forgot to ask about the actual PIC24. Is it 24HJ128GP504 mentioned above? It may be the case, that #pin_select is not fully working in V4.093.

Also, what is exactly your observation with the below code? 7 or 8 clock pulses? SDO2 (PIN_B3) sending SPI data or not?


Last edited by FvM on Wed Feb 03, 2010 5:28 pm; edited 1 time in total
logical



Joined: 21 Dec 2009
Posts: 57
Location: SouthPort, UK

View user's profile Send private message Visit poster's website

PostPosted: Wed Feb 03, 2010 5:25 pm     Reply with quote

FvM wrote:
I also forgot to ask about the actual PIC24. Is it 24HJ128GP504 mentioned above?

Also, what is exactly your observation with the below code? 7 or 8 clock pulses? SDO2 (PIN_B3) sending SPI data or not?


Sure is 24HJ128GP504

No Data at all and 7 clock pulses.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Feb 03, 2010 5:40 pm     Reply with quote

At first look, I don't see a problem. Also #pin_select for SDO2 seems to be coded corrcectly by PCD V4.093.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Feb 04, 2010 12:28 am     Reply with quote

As an additional remark, with your code, you most likely see SDO permanent at "1". You may want to
send a pattern != 0xFF for test. I didn't see an explanation for getting 7 clocks only. Also reported silicon
errata don't point in this direction. By sending an alternating bit pattern and using different SPI modes, you
should be able to see, which clock bit is missing.

I would also try different system clock speeds and SPI clock dividers. You should also check with spi_read().
logical



Joined: 21 Dec 2009
Posts: 57
Location: SouthPort, UK

View user's profile Send private message Visit poster's website

PostPosted: Thu Feb 04, 2010 7:32 am     Reply with quote

FvM wrote:
As an additional remark, with your code, you most likely see SDO permanent at "1". You may want to
send a pattern != 0xFF for test. I didn't see an explanation for getting 7 clocks only. Also reported silicon
errata don't point in this direction. By sending an alternating bit pattern and using different SPI modes, you
should be able to see, which clock bit is missing.

I would also try different system clock speeds and SPI clock dividers. You should also check with spi_read().


Great thanks, will have a try with that and post the results.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group