Go Down

Topic: Arduino Mega e SPI (Read 578 times) previous topic - next topic

edyorit

Hello everyone
I should interfacing my Arduino Mega2560 mcp4921 with a DAC that uses the SPI.
I found on web a code arduino and I have made ??some changes to the case, but I simulated all with Proteus and does not really work the DAC.
I checked power of both devices but nothing to do

Can anyone help me?

Thanks in advance

I enclose the following code written

Code: [Select]
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define INTERNAL1V1 2
#define INTERNAL2V56 3
#else
#define INTERNAL 3
#endif

// SPI Interface SS_PIN(PB2), SCK_PIN(PB5), MOSI_PIN(PB3), MISO_PIN
// Arduino Pin 10 13 11 12
// MCP4921 DAC SS SCK MOSI n/a
#include "SPI.h"
// ATmega328P ADC
int analogPin = 0; // analog input channel
// ADC analog input value
word sensorValue = 0; // equivalent to unsigned int
// Byte of data to output to DAC
byte data = 0;

int digitalPin=53;

void setup()
{
  //set pin(s) to input and output
  //pinMode(53, OUTPUT);
 
  SPI.begin(); // wake up the SPI bus.
  SPI.setBitOrder(MSBFIRST);
  pinMode(analogPin, INPUT);
  pinMode(digitalPin,OUTPUT);
 
  analogReference(INTERNAL2V56); //For 1280 and 2560
  //Serial.begin(9600);
  //pinMode(2, INPUT);
}

void loop() {
// read the value from the sensor:
sensorValue = analogRead(analogPin); // comment out this line to test DAC
// sensorValue = 0x0200; // 0x03FF = Vref, 0x0200 = 1/2 Vref
sensorValue = sensorValue << 2 ; // 10 bit ADC to 12-bit DAC word
// set SS pin low, beginning 16-bit (2 byte) data transfer to DAC
digitalWrite(digitalPin, LOW);
// send high byte
data = highByte(sensorValue);
data = 0b00001111 & data; // clear 4-bit command field (optional)
data = 0b00110000 | data; // 0=DACA, 0=buffered, 1=1x, 1=output enabledSpi.transfer(data); // alt: shiftOut(MOSI, SCK, MSBFIRST, data);
// send low byte
data = lowByte(sensorValue);
SPI.transfer(data); // alt: shiftOut(MOSI, SCK, MSBFIRST, data);
// set SS pin high, completing 16-bit transfer to DAC
digitalWrite(digitalPin, HIGH);
}

AWOL

Was there a question?
What do your debug prints tell you is happening?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

edyorit

I must receive digital string from pc by serial port and shift it to a dac (via spi)

In proteus this skecth doesn't work, can you help me to doing this project?

thanks in advance!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy