AVR SPI : Not working with UART enabled

Thread Starter

Athul S Nair

Joined Dec 24, 2016
23
Hello,
I'm trying to learn SPI protocol of AVR micro-controller AtMega168

I learned about polling method and this is the code
Code:
// this program enables SPI communication and
// Sets the AVR into Master mode


#include <avr/io.h>
#include <util/delay.h>
#include "USART.h"


int main (void)
{
    char data = 'A';
    //initUSART();
  
    //printString("MASTER\r\n");

    DDRB |= (1<<2)|(1<<3)|(1<<5);    // SCK, MOSI and SS as outputs
    DDRB &= ~(1<<4);                 // MISO as input

    SPCR |= (1<<MSTR);               // Set as Master
    SPCR |= (1<<SPR0)|(1<<SPR1);     // divided clock by 128
    SPCR |= (1<<SPE);                // Enable SPI
  
    while(1)
    {
        SPDR = data;                 // send the data
        while(!(SPSR & (1<<SPIF)));  // wait until transmission is complete
        //transmitByte(SPDR);
        //data = SPDR;
        _delay_ms(100);
        // if you have multiple slaves, this is where you want to switch
    }
}
Slave code

Code:
// this program enables SPI communication and
// Sets the AVR into Slave mode


#include <avr/io.h>
#include <util//delay.h>
#include "USART.h"


int main (void)
{
    initUSART();
    printString("Slave\r\n");
    char data;

    DDRB &= ~((1<<2)|(1<<3)|(1<<5));   // SCK, MOSI and SS as inputs
    DDRB |= (1<<4);                    // MISO as output

    SPCR &= ~(1<<MSTR);                // Set as slave
    SPCR |= (1<<SPR0)|(1<<SPR1);       // divide clock by 128
    SPCR |= (1<<SPE);                  // Enable SPI

    while(1)
    {
        while(!(SPSR & (1<<SPIF)));    // wait until all data is received
        data = SPDR;                   // hurray, we now have our data
        transmitByte(data);
        //_delay_ms(20);
    }
}
It has a problem,
If I uncomment these two lines from master code,
Code:
initUSART();
printString("MASTER\r\n");
Slave device won't display anything. I don;t have two Arduino boards so I'm using Proteus to simulate the code. In Proteus Serial terminal of Slave cursor just move forward and nothing displays

Also tried another code ,
Master : https://drive.google.com/drive/folders/1WOsMHF9srpg5Q3Afv7FEGhKyXRjUiMdt
Slave : https://drive.google.com/drive/folders/1wVgHuyIr5WdAD0s5Pny0U2znvkGqpUks

here uncommenting only initUSART() causes write collision error. Commenting out all UART related code solves it

Here's the link to USART library
https://drive.google.com/open?id=1qnzAkkWlIqIBm5IBzyj4gg_jKu8RTv7b

I tried to implement interrupt based data transfer, but not successful. I will add that code once I figured out what's happening here

Thanks
 

MrChips

Joined Oct 2, 2009
30,821
You are complicating things by trying to debug USART, SPI, and interrupts.
Test one thing at a time. I would test the USART first.

BTW, I generally do not use library functions. I like to have total control of the code.
It is not difficult to write your own low-level putc( ) and puts( ) functions.
 

Thread Starter

Athul S Nair

Joined Dec 24, 2016
23
You are complicating things by trying to debug USART, SPI, and interrupts.
Test one thing at a time. I would test the USART first.

BTW, I generally do not use library functions. I like to have total control of the code.
It is not difficult to write your own low-level putc( ) and puts( ) functions.
Okay, forget about Interrupts.
USART library actually works. I use same library for Slave code. which transmit received data to terminal. It has problem only with Master code

In some AVR variants the SPI and the UART share pins and function blocks. RTFDS carefully.
In atmega168/328 UART pins are PD0 & PD1
SPI pins are: PB2-5
 

Papabravo

Joined Feb 24, 2006
21,226
Okay, forget about Interrupts.
USART library actually works. I use same library for Slave code. which transmit received data to terminal. It has problem only with Master code



In atmega168/328 UART pins are PD0 & PD1
SPI pins are: PB2-5
At least we know the AVR variant we are talking about.
 

Thread Starter

Athul S Nair

Joined Dec 24, 2016
23
You are complicating things by trying to debug USART, SPI, and interrupts.
Test one thing at a time. I would test the USART first.

BTW, I generally do not use library functions. I like to have total control of the code.
It is not difficult to write your own low-level putc( ) and puts( ) functions.
Those libraries are written by me
 
Top