Hi,
I'm trying to read a current with an ammeter click ( MIKROE2377 ) which is connected with an arduino mega on a arduino mega click shield ( the schemes are attached ) the shield is on the left of the click, i am able to read the current value using the AN pin corresponding to AO ( analog read ).
My problem is by using the SPI pins PB0 PB1 and PB3), I have seen that the MOSI pin isn't used in the click on the click scheme, I wonder how I could communicate with this device using the <SPI.h> library without being able to send it a value with SPI.transfer() function..
#include <SPI.h> #define AN A0
int samples=500;
unsigned long sum=0;
unsigned long mean;
int value;
int current;
int receivedVal=10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
pinMode(53,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
sum=0;
for(int i=0;i<samples;i++)
{
/*
AnalogRead part
value=analogRead(AN);
sum+=value;
delay(10);*/
SPI.transfer() does send out a byte as well as receive a byte. As the used ADC has 12 bit resolution you have to transfer 16 bits to get the value so you have to call SPI.transfer() twice. It doesn't matter that you cannot send data to the chip as it reacts on the pull-down of the CS signal.
pylon:
SPI.transfer() does send out a byte as well as receive a byte. As the used ADC has 12 bit resolution you have to transfer 16 bits to get the value so you have to call SPI.transfer() twice. It doesn't matter that you cannot send data to the chip as it reacts on the pull-down of the CS signal.
Thank you for you answer, I've tried with two calls but the result is the same, the print shows 0 even when I have 600 mA on the output..
#include <SPI.h>
#define AN A0
int samples=250;
unsigned long sum=0;
unsigned long mean;
int value;
int current;
byte incoming1;
byte incoming2;
uint16_t incoming;
int receivedVal=10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
pinMode(53,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
sum=0;
for(int i=0;i<samples;i++)
{
//AnalogRead part
//value=analogRead(AN);
//sum+=value;
//delay(10);
digitalWrite(53,LOW);
//incoming=SPI.transfer16(0);
incoming1=SPI.transfer(0);
incoming2=SPI.transfer(0);
digitalWrite(53,HIGH);
Serial.print(incoming1);
Serial.print(" ");
Serial.println(incoming2);
receivedVal=incoming1<<8 + incoming2;
sum+=receivedVal;;
}
mean=sum/samples;
current=map(mean,207,414,1,1000);
Serial.print(current);
Serial.println("mA");
delay(50);
}