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); 
}
suha
1,77214 silver badges17 bronze badges
asked Jun 17, 2012 at 14:09
\$\endgroup\$
1
  • \$\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\$ Commented Jun 18, 2012 at 8:44

1 Answer 1

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.

answered Feb 18, 2013 at 9:06
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.