Hey I am interfacing 16 bit adc using SPI with my arduino duemilanove. ADC has two channels. Now I dont want to sent data read by arduino directly as serial transmission is turning to be slow. So i want to store data in SRAM as I read data for 1sec and after that I want to send the data collected to serial port. This way I can also know amount of bytes given by ADC in 1sec. I think Timers with interrupts might solve my problem but I have no clue where to begin. ?
thanks
SRAM is limited to 2K in total so you are limited as to what you can store.
The millis timer function will help you keep track of elapse time.
Make a record of the time before you start collecting data, count the number of samples you get and see how many you have got when the current time minus the start time exceeds a second.
So there is no need to use timers and interrupts?
I have used Timers that generates interrupts while using atmega 8/16
?
So there is no need to use timers and interrupts?
Correct.
I have used Timers that generates interrupts while using atmega 8/16
That's fine, if you want to do things at specific intervals. Reading from the ADC as fast as possible does not mean doing things at fixed intervals. It means doing it again as soon as possible.
Hey I used Millis function and checked the counter to see how many samples I am getting actually but problem is it does does display 485 then 505 505 on serial monitor. It then dispays nothing.
I mean it since its infinite loop it should keep on displaying close to 500 right?
Does it mean my arduino is hanging or crashing due to limited RAM?
Does it mean my arduino is hanging or crashing due to limited RAM?
Maybe. Or there is a problem with your code.
I actually checked those 500 values. thsoe are not correct. I am using 16 bit adc with 2 channels. Problem I think is
I scan 1 channel
print data
delay(100)
scan other channel
print data
It works fine but as soon as I remove delay values displayed are not correct. I cant afford this delay as I need atleast 500 samples per sec
Still haven't seen any code...
aashishsharma:
Its SPI.transfer(8) actually. Interface is making some smiley for it
That is why there are the code tags that we ask posters to use.
Please read the how to use this forum sticky, that will tell you what to do.
You can modify that last post and make it readable.
here is my code
Sorry for previous mistake in posting
#include "SPI.h"
int ss=10;
unsigned int adcValue=0;
byte highByte;
byte lowByte;
long previousMillis=0;
long interval=1000;
unsigned int adcValue1[500];
unsigned int k=0;
void setup()
{
pinMode(ss, OUTPUT); // setting SS pin as output
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV2);
Serial.begin(115200);
}
void loop()
{
//series of command bit
digitalWrite(ss,LOW); // Selecting chip by making CS low
SPI.transfer(32); //command for comm reg to select ch1 and write to clock register
SPI.transfer(167); //command for clock reg to set 2,4576Mhz
SPI.transfer(16); //command for comm reg to write setup register
SPI.transfer(68); //command for setup reg to self calibration,unipolar,unbuffered
char DataNotReady = 128;
while(DataNotReady) // wait for end of conversion
{
SPI.transfer(8);//command for comm reg to read (dec 8)
DataNotReady =SPI.transfer(0); // Read comm register
DataNotReady &= 0x80; //Hexadecimal of 128
}
SPI.transfer(33); //command for comm register to select ch2 and write to clock register
SPI.transfer(167); // command for clock register to set 2.4576 Mhz
SPI.transfer(17); //command for the comm register to write setup for channel 2
SPI.transfer(68); //command for set up register to self calibrate,unipolar,unbuffered
char DataNotReady1 = 128;
while(DataNotReady1) // wait for end of conversion
{
SPI.transfer(9);//command for comm reg to read (dec 8)
DataNotReady1 =SPI.transfer(0); // Read comm register
DataNotReady1 &= 0x80; //Hexadecimal of 128
}
while(1)
{ for(int ch=1; ch<=2;ch++)
{
char DataNotReady = 128;
while(DataNotReady) // wait for end of conversion
{ if(ch==1)
{
SPI.transfer(8);//command for comm reg to read (dec 8)
}
else if(ch==2)
{ SPI.transfer(9);
}
DataNotReady =SPI.transfer(0); // Read comm register
DataNotReady &= 0x80; //Hexadecimal of 128
}
//read 16bit of data ADC
highByte = 0;
lowByte = 0;
if(ch==1)
{
SPI.transfer(56); // inform the adc that you will be reading the data register in the next operation
}
else if(ch==2)
{ SPI.transfer(57);
}
highByte = SPI.transfer(0);
lowByte = SPI.transfer(0);
adcValue = highByte << 8;
adcValue = adcValue | lowByte;
adcValue1[k]=adcValue;
k++;
unsigned long currentMillis= millis();
if(currentMillis- previousMillis > interval)
{ Serial.println(k,DEC);
previousMillis=currentMillis;
for(int i=0; i<500;i++)
Serial.println(adcValue1[i],DEC);
}
// Serial.print("analog value =");
// Serial.println(adcValue, DEC);
// Serial.print('\n');
//delay(1000);
}
}
}