I WANT TO USE ARDUINO AND COMMUNICATE WITH 4 DEVICES:
DDS AD9835
DIGIPOT AD5204
TWO DAC08
I AM USING LABVIEW VISA WRITE FUNCTIONS AND PASSING HEX STRINGS TO ARDUINO
FOR PROG IN ARDUINO THESE HEX STRINGS ARE CONVERTED TO INTEGER USING htoi file which i have created.
then it stored in a variable and actions are to be performed.
upon reception of hex strings from labview proper actions must bee taken
but noting happens
and plus if anythin is passed through serial monitor there r no changes
my mega 2560 is good n so is my hardware
whats wrong i m not able to see..
can somebody help??
this is really urgent
#include <SPI.h> // to begin serial communication with digipot
#include <AD9835.h>// a libray found from net for DDS AD 9835 to generate sinewaves of desired frequencies.
#include "htoi.h" // created in order to to convert the hex strings into int from 00 to 255 these hex codes are recvd from labview visa functio and are user selectable
int frequency[11] = {
125,250,500,750,1000,1500,2000,3000,4000,6000,8000}; // these are the set of frequencies which DDS will generate upon selection from labview
int noiseintensity[8] = {
0,31,63,95,127,161,192,223}; //codes for 1st DAC08 user selectable from labview
int toneintensity[8] = {
0,31,63,95,127,161,192,223}; //codes for 2nd DAC08 user selectable from labview
/*pin 8 = direct connection
pin 9 = 40dB attenuation
pin 10 = 80dB attenuation for noise*/ // at 8,9,10 relays are attached. amongst these any one will be selected from labview selection is performed
int noiserelay[3] = {
8,9,10};
/*pin 11 = direct connection
pin 12 = 40dB attenuation
pin 13 = 80dB attenuation for tone*/ //at 11,12,13 relays are attached. amongst these any one will be selected from labview selection is performed
int tonerelay[3] = {
11,12,13};
/*pin 14 = Tone Right relay
pin 15 = Noise Right relay
pin 16 = Tone left relay
pin 17 = Noise left relay*/at14,15,16,17 relays are attached. amongst these any one will be selected from labview selection is performed
int righttone = 14;
int rightnoise = 15;
int lefttone = 16;
int leftnoise = 17;
int a;
AD9835 dds(
7, // FSYNC
3, // SCLK
2, // SDATA
6, // FSEL
5, // PSEL1
4, // PSEL0
50000000 // hzMasterClockFrequency (50MHz)
);
int z = 0;
void setup()
{
for(int x = 0; x<3; x++)
{
pinMode(tonerelay[x], OUTPUT);
pinMode(noiserelay[x], OUTPUT);
}
pinMode(righttone, OUTPUT);
pinMode(rightnoise, OUTPUT);
pinMode(lefttone, OUTPUT);
pinMode(leftnoise, OUTPUT);
DDRC = 0xff; //tone DAC pins
DDRA = 0xff; //noise DAC pins
Serial.begin(9600);
dds.begin();
}
void loop()
{
char mystring[4];
while(Serial.available())
{
char check = Serial.read();
if(check == 'K')
{
{
Serial.print("Ready");
while(Serial.available())
{
for(int i = 0;i<4; i++)
{
mystring[i] = Serial.read();
delay(50);
// delay to avoid overwrite previous value
}
if(mystring[0]== '*' && mystring[3]== '#')
{
a = htoi(mystring[1], mystring[2]);
compare(a);
}
//this loop checks wheather data only between two special chaarcters is recvd and garbage is not recvd
}
}
}
}
}
void compare(int a) // this function upon reception of data. frequency codes are between 20 and 30 and so on as explained so different functions are created.
{
if(a >= 20 && a <=30)
{
freq(a);
}
else if(a >= 40 && a <= 64)
{
tone_intensity(a);
}
else if(a >= 70 && a <= 94)
{
noise_intensity(a);
}
else if(a >= 100 && a <= 102)
{
noiseselect(a);
}
else if(a >= 103 && a <= 105)
{
toneselect(a);
}
}
// these conditions of relay selection are correct but at the pins the hih signals are not seen. 0v is seen instead of 5v.
void freq(int a)
{
a = a-20; // this is done so that frequncy is selected from array declared in beginning amongst 11 frequncies and further codes are from library dds ad9835.
dds.setFrequencyHz(0, frequency[a]);
dds.selectFrequencyRegister(0);
// Without modulation the choice of phase offset does not matter,
// but we set it to zero for good measure.
dds.setPhaseDeg(0,0);
dds.selectPhaseRegister(0);
// Finally, we turn on the IC.
dds.enable();
}
/*tonerelay[0]: pin 11 = direct connection
tonerelay[1]: pin 12 = 40dB attenuation
tonerelay[2]: pin 13 = 80dB attenuation for tone*/
void tone_intensity(int a)
{
a = a-40; // this is done to seelct the element from array of DAC codes declared earlier
if(a >= 0 && a <= 7)
{
digitalWrite(tonerelay[0], 0);
digitalWrite(tonerelay[1], 0);
digitalWrite(tonerelay[2], 1);
PORTC = toneintensity[a];
}
else if(a >= 8 && a <= 15)
{
digitalWrite(tonerelay[0], 0);
digitalWrite(tonerelay[2], 0);
digitalWrite(tonerelay[1], 1);
a = a - 8;
PORTC = toneintensity[a];
}
else if(a >= 16 && a <= 24)
{
digitalWrite(tonerelay[1], 0);
digitalWrite(tonerelay[2], 0);
digitalWrite(tonerelay[0], 1);
a = a - 16;
PORTC = toneintensity[a];
}
}
/*noiserelay[0]: pin 8 = direct connection
noiserelay[1]: pin 9 = 40dB attenuation
noiserelay[2]: pin 10 = 80dB attenuation for noise*/
void noise_intensity(int a)
{
a = a- 70;
if(a >= 0 && a <= 7)
{
digitalWrite(noiserelay[0], 0);
digitalWrite(noiserelay[1], 0);
digitalWrite(noiserelay[2], 1);
PORTA = noiseintensity[a];
}
else if(a >= 8 && a <= 15)
{
digitalWrite(noiserelay[0], 0);
digitalWrite(noiserelay[2], 0);
digitalWrite(noiserelay[1], 1);
a = a - 8;
PORTA = noiseintensity[a];
}
else if(a >= 16 && a <= 24)
{
digitalWrite(noiserelay[2], 0);
digitalWrite(noiserelay[1], 0);
digitalWrite(noiserelay[0], 1);
a = a-16;
PORTA = noiseintensity[a];
}
}
/*pin 14 = Tone Right relay: righttone
pin 15 = Noise Right relay: rightnoise
pin 16 = Tone left relay: lefttone
pin 17 = Noise left relay: leftnoise*/
void noiseselect(int a)
{
if (a == 100)
{
digitalWrite(rightnoise, LOW);
digitalWrite(leftnoise, LOW);
}
else if(a == 101)
{
digitalWrite(leftnoise, LOW);
digitalWrite(rightnoise, HIGH);
}
else if(a == 102)
{
digitalWrite(rightnoise, LOW);
digitalWrite(leftnoise, HIGH);
}
}
void toneselect(int a)
{
if (a == 103)
{
digitalWrite(righttone, LOW);
digitalWrite(lefttone, LOW);
}
else if(a == 104 && rightnoise == 0)
{
digitalWrite(lefttone, LOW);
digitalWrite(righttone, HIGH);
delay(1000);
digitalWrite(righttone, LOW);
}
else if(a == 105 && leftnoise == 0)
{
digitalWrite(righttone, LOW);
digitalWrite(lefttone, HIGH);
delay(1000);
digitalWrite(lefttone, LOW);
}// these relay selections logic is correct but it doeno make the pins of aruino high.
}
//THE HTOI FILE IS: // the file which converts the recvd hex string into int since char cant be compared int can be.this is e.g.(164= 160+4) like wise FF=255= 240+15 as per this code and likewise.
int ten(char tens)
{
switch(tens)
{
case '0':
return 0;
case '1':
return 16;
case '2':
return 32;
case '3':
return 48;
case '4':
return 64;
case '5':
return 80;
case '6':
return 96;
case '7':
return 112;
case '8':
return 128;
case '9':
return 134;
case 'A':
return 160;
case 'B':
return 176;
case 'C':
return 192;
case 'D':
return 208;
case 'E':
return 224;
case 'F':
return 240;
}
}
int unit(char units)
{
switch(units)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'A':
return 10;
case 'B':
return 11;
case 'C':
return 12;
case 'D':
return 13;
case 'E':
return 14;
case 'F':
return 15;
}
}
int htoi(char tens, char units)
{
int a = ten(tens);
int b = unit(units);
int sum = a + b;
return sum; // return sum because that is the actual final code.
}
MY DIGIPOT PROG IS NOT YET ADDED. THAT I WILL DO ONCE THIS SOLVED.
regards
vanashree