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

Setup Hardware SPI

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
vtrx



Joined: 11 Oct 2017
Posts: 100

View user's profile Send private message

Setup Hardware SPI
PostPosted: Fri Jul 16, 2021 7:45 pm     Reply with quote

To use SPI by hardware from 18f2550/4550, just declare it like this?

Code:
void main(void
 {
  ...
  setup_spi(SPI_MASTER|SPI_L_TO_H |SPI_XMIT_L_TO_H|SPI_CLK_DIV_16);
  ...


How do I change to 16bit to match this one?

Code:
#use SPI(DO=PIN_A0,DI=PIN_A3,CLK=PIN_A2,MODE=0,BAUD =270000,BITS=16,msb_first,SAMPLE_RISE,STREAM = max7912)


Using the configuration below, will i be using the hardware?

Code:
#use SPI (MASTER,SPI1,baud=1000000,MODE=0,BITS=16,msb_first)
PCM programmer



Joined: 06 Sep 2003
Posts: 21477

View user's profile Send private message

PostPosted: Sat Jul 17, 2021 10:52 am     Reply with quote

It should. To confirm it, look at the .LST file in symbolic mode.
You should see something like this:
Code:

.................... #use SPI (MASTER,SPI1,baud=1000000,MODE=0,BITS=16,msb_first)
0004:  MOVF   SSPBUF,W
0006:  MOVFF  ??65535+1,SSPBUF
000A:  RRCF   SSPSTAT,W
000C:  BTFSS  STATUS.C
000E:  GOTO   000A
0012:  MOVF   SSPBUF,W
0014:  MOVWF  @01
0016:  MOVFF  ??65535,SSPBUF
001A:  RRCF   SSPSTAT,W
001C:  BTFSS  STATUS.C
001E:  GOTO   001A
0022:  MOVFF  SSPBUF,00
0026:  GOTO   0054 (RETURN)

The code above is using the hardware SPI registers.
Ttelmah



Joined: 11 Mar 2010
Posts: 17318

View user's profile Send private message

PostPosted: Sun Jul 18, 2021 12:29 am     Reply with quote

You'll be using the hardware, but not on the pins listed.

SPI1 on the 2550/4550, uses pin B0 for SDI, C7 for SDO and B1 for SCK.

setup_spi, cannot do 16bit transfers.

If you read the manual for #use SPI, you will find no reference to
setup_spi, spi_read or spi_write.

setup_spi, is designed to be used with spi_read and spi_write, and is the
'older' way of setting up the SPI. It only supports the hardware, and therefore
8bit transfers.

#use SPI, is the later setup for SPI. It is designed to be used with spi_xfer,
not spi_read/write. This command brings with it the capability for up to
32bit transfers, by automatically performing multiple transfers.

So your #use SPI, will be using the hardware, on pins B0, C7 and B1,
but to perform 16bit transfers the code will have to change and use
spi_xfer, rather than spi_read/spi_write.
PCM programmer



Joined: 06 Sep 2003
Posts: 21477

View user's profile Send private message

PostPosted: Sun Jul 18, 2021 3:38 am     Reply with quote

I didn't post my source code for that listing. Here it is:
Code:

#include <18F4550.h>
#fuses HS, NOWDT 
#use delay(clock=20M)

#use SPI (MASTER,SPI1,baud=1000000,MODE=0,BITS=16,msb_first)

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

while(TRUE);
}
 

It does two sequential 8-bit transfers to get 16 bits.
Ttelmah



Joined: 11 Mar 2010
Posts: 17318

View user's profile Send private message

PostPosted: Sun Jul 18, 2021 4:11 am     Reply with quote

Yes. The key is that PCM correctly used the SPI_xfer... Very Happy

I read it PCM, that he is changing something in some existing code
that used setup_spi. Hence it is vital to realise that he needs to change
both the #use, and the actual I/O function used. Very Happy
vtrx



Joined: 11 Oct 2017
Posts: 100

View user's profile Send private message

PostPosted: Wed Jul 21, 2021 9:21 am     Reply with quote

Thanks for the answers.
Both 'setup_spi' and "#use SPI (MASTER,SPI1..." use the hardware, but i'm using "setup_spi" because I can choose the speed following the datasheet(4,16 and 64), using "#use SPI (MASTER..." ,i don't know if the speed is that shown in BAUD.

Here are the changes that were needed:

Code:
void main(void
 {
  ...
  setup_spi(SPI_MASTER|SPI_L_TO_H |SPI_XMIT_L_TO_H|SPI_CLK_DIV_16);
  ...


Code:
void sendData(unsigned int16 data)
{
  output_low(CS);
  spi_write(make8(data,1));
  spi_write(make8(data,0));
  output_high(CS);
}
.....
void skip_data()
{
  spi_write(0x00);
  spi_write(0x00);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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