1
\$\begingroup\$

I am attempting to learn how to control an ATMega328 IC from my ATMega2560 Arduino using SPI. I have found the various tutorials that use digital potentiometers, but I have no digital potentiometers and all I want to learn to do is blink an LED on the ATMega328 from the code running on ATMega2560 via SPI.

Can anyone please provide a few lines of code to show me how this is done?

Here is my pin configuration (using Arduino pin names for both ICs):

  • ATMega2560 pin 50 (MISO) => ATMega328 pin 12 (MISO)
  • ATMega2560 pin 51 (MOSI) => ATMega328 pin 11 (MOSI)
  • ATMega2560 pin 52 (SCK) => ATMega328 pin 13 (SCK)
  • ATMega2560 pin 2 => ATMega328 pin 10 (SS)
  • ATMega2560 5V pin => ATMega328 Vcc pin
  • ATMega2560 GND pin => ATMega328 GND pin
  • ATMega328 pin 3 => LED that I would like to blink

Here is my current code:

#include "SPI.h" // necessary library

int SLAVESELECT = 2;
int DATAOUT = 51;
int DATAIN = 50;
int SPICLOCK = 52;

void setup()
{
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK, OUTPUT);
  pinMode(SLAVESELECT, OUTPUT);

  SPI.begin(); // wake up the SPI bus.
  SPI.setBitOrder(MSBFIRST);
}

void setValue(int pin, int value)
{
  digitalWrite(SLAVESELECT, LOW);
  SPI.transfer(pin); // send command byte
  SPI.transfer(value); // send value (0~255)
  digitalWrite(SLAVESELECT, HIGH);
}


void loop()
{
  setValue(0x11,255);
  delay(1000);
  setValue(0x11,0);
  delay(1000);   
}
\$\endgroup\$
  • \$\begingroup\$ This somewhat depends on what library you are using for the SPI slave. However, a typical code would look like this: 1. initialize SPI module as slave, 2. poll periodically for new messages or act on new messages on interrupt. \$\endgroup\$ – 0x6d64 Jun 18 '12 at 8:44
1
\$\begingroup\$

Although I am not very familiar with the Arduino, your sketch looks fine to me. But, there is something you are missing. ATMega328 is a microcontroller, just like your ATMega2560 on the Arduino. It is not an IC that is purposed to be controlled via SPI.

However, this does not mean that you cannot control this IC via SPI. Just like you did for your ATMega2560, you have to write code for ATMega328 to make it act like an "SPI Slave". Then, you will be able to make two microcontrollers speak SPI and they will communicate.

You have already configured ATMega2560 as an SPI Master, so your mission is half way complete. Now, you have to write code for ATMega328 and make it act like an SPI Slave. Then, on the slave side, you will check if master sends anything. And when master sends something to the slave, slave will check the bytes that you are sending, in this case 0x11 and 0x00 (and 0xFF for LED ON).

Learn how to write a sketch that configures ATMega328 as an SPI Slave, and learn how to receive data when it is configured as an SPI Slave. Then, you will check the data and act accordingly; for example, you will light up the LED when there is 0x11 and 0xFF code received.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.