Setting SPI with 20MHz clock

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Guy's

 

I'm working with an ATMEGA328PB and an MCP2518FD (Canbus)

 

The 328PB has two SPI's 0,1 and I'm using the latter

 

The datasheet for the MCP2518FD says it will run at 20MHz and I'm using an external clock at the same speed.  I presume therefore that I don't need to set a clock divider?  If so how do I do this?

 

Should SPR1 be removed in the SPI enable?

 

My Initialisation is below

 

regards

 

DDRC |=  (1 << PIN_SCK_1) ; //Set SCK as output

DDRC &= ~(1<< PIN_MISO_1); // Set MISO as input

 

DDRE |= (1 << PIN_SS_1) | (1 << PIN_MOSI_1); // Set Slave select for Can bus as output

 

// SPI Enable, Master mode

SPCR1 |= (1 << SPE) | (1 << MSTR)| (1<<SPR1);

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Did you see:

 

 

As I assume "SPR1" in your code is basically "1" then you appear to be selecting an F_CPU/64 clock. If you want to the SPI to run as fast as it can (F_CPU/4) surely you don't actually want to set any SPR bits?

 

BTW as the fastest you can run the 328PB appears to be 20MHz then obviously you are never going to achieve more than 5MHz SCK so you are well within in the limits of the CAN interface.

Last Edited: Tue. Jan 19, 2021 - 11:16 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you,

 

Does that mean that a clock divider of 4 is explicit  if nothing is set?  I was under the impression that I could run SCK at 20MHz but couldn't find a way of doing that

 

regards

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Apologies I forgot about SPI2X. That means the limit is /2 not /4 so for a 20MHz F_CPU you can achieve 10MHz SCK.

 

That is:

Last Edited: Tue. Jan 19, 2021 - 12:19 PM