using USART as SPI on mega32u4

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

I'm currently using the main SPI for communication to a device but also would like to use a second SPI for communication to a nrf24 module. Looking for a tutorial out there but coming up a blank though was able to assemble this.
 

unsigned char Spi_read(unsigned char data1, unsigned char data2)
{
    UCSR1A = _BV(TXC1);              // Clear the Transmit complete flag, all other bits should be written 0.
    SPI_PORT_SS &= ~_BV(SPI_BIT_SS); // Pull SS low to select the SPI device.
    UDR1 = data1                      // Begin transmission of first byte.
    UDR1 = data2;                     // Continue transmission with second byte.
    while ( !(UCSR1A & _BV(TXC1)) ); // Check we've finished, by waiting for Transmit complete flag.
    SPI_PORT_SS |= _BV(SPI_BIT_SS);  // Pull SS high to deselect the SPI device.
    //return what?
}

void Init(void)
{
    SPI_PORT_DIR_SS |= SPI_BIT_SS;   // Set SS as output pin.
    SPI_PORT_SS |= SPI_BIT_SS;       // Pull SS high to deselect the SPI device.
    UBRR1 = 0x0000;
    DDRD |= _BV(PD4);                // Setting the XCK1 port pin as output, enables USART SPI master mode (this pin for ATmega1284p)
    UCSR1C = _BV(UMSEL11) | _BV(UMSEL10) | _BV(UCSZ10) | _BV(UCPOL1);
                                     // Set USART SPI mode of operation and SPI data mode 1,1. UCPHA1 = UCSZ10
    UCSR1B = _BV(TXEN1);             // Enable transmitter. Enable the Tx (also disable the Rx, and the rest of the interrupt enable bits set to 0 too).
                                     // Set baud rate. IMPORTANT: The Baud Rate must be set after the Transmitter is enabled.
    UBRR1 = 0x0000;                  // Where maximum speed of FCPU/2 = 0x0000

}

Anyone know of a good write up on how to set up my USART in SPI mode with some explanation of the bits. I could have my function wrong but I'm not seeing the data return register. This is all very different then a normal SPI set up on a mega, also I can not find a good answer as to were the PINS live on the mega32u4 chip. Since USART are PORD 2 and 3 I would assume those are for miso and mosi, and SS / reset I could simply use any available pin. Then the question would be where is the clock?

 

 

Last Edited: Sat. Jan 4, 2020 - 07:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ahh crap, just found this...

https://www.avrfreaks.net/forum/...

Guessing the chip is the same sort of thing.