mega32u2 max spi does not look right.

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

 

 

I set my spi to max but not achieving what I was hoping for.

 

    SPCR = (1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPR1)|(0<<DORD)|(0<<CPHA)|(0<<CPOL);
    SPSR = (1<<SPI2X);//double speed

 

I'm seeing 15us per byte.

 

Doesn't the doubleR set spi to 8MHZ (F_CPU/2)  when running a 16MHz clock? Was assuming I'd get closer to 1usec per byte. Maybe my math is just bad.

 

 

Last Edited: Wed. Apr 8, 2020 - 12:49 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have included SP1 twice. Not sure what the second one will do (I'll bet it does nothing). According to the spec sheet, that SHOULD give you Fclk/2. Then, you ask for double speed! Bet it won't do that!

 

Does it give an expected clock rate if you make the clock prescaler for a larger ratio, like 1/128? Do you know that the clock is actually running at the frequency you think it is?

 

Jim

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

ka7ehk wrote:
You have included SP1 twice.

 

Here:

SPCR = (1<<MSTR) | (1<<SPR1) | (0<<SPR0) | (0<<SPR1) | (0<<DORD) | (0<<CPHA) | (0<<CPOL);

 

 

Not sure what the second one will do (I'll bet it does nothing)

Indeed it will do nothing - as it's a zero!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Apr 8, 2020 - 01:22 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ah good catch that was doing it, removing that SPR1 made it clock to 2us per byte, and then adding back the doubler actually worked 1us per byte! The slave device respond as it should.

Last Edited: Wed. Apr 8, 2020 - 01:29 AM