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); }