Pages: [1]   Go Down
Author Topic: Function generator using AD9835 with Raspberry Pi  (Read 1803 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a breakout board for AD9835 by sparkfun. I am trying to interface it with a Raspberry Pi. Since the chip has 5V SPI, I am using a level shifter. All other pins work fine, but when i connect the MOSI pin of RPi to the input of level shifter, it inverts the MOSI logic. Gives HIGH on a 0 and LOW on a 1. Why is this happening?

To solve this I connect the MOSI pin directly to the ad9835 SDATA pin, no matter whatever frequency word i send(32 bit word), the frequency is stuck on 5MHz. Is it because MOSI level is 3.3v and its not getting detected?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PS.: the same code works perfect on arduino.
      I am using arduPi library in rpi.


::::::The Code::::::

void setup() {

 
  Serial.begin(9600);

  SPI.begin();
  SPI.setDataMode(SPI_MODE1);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV64);
  SPIwrite(0xF8, 0x00);
  AD9835write(10000);
}



void loop(){
}


void AD9835write(unsigned long frequency) {

  unsigned long temp = 0;
    temp = 0xFFFFFFFF/50000000*frequency;
  SPIwrite(0xC0, 0x00);
  
  SPIwrite(0x90, 0x00);
  SPIwrite(0x33, ((temp & 0xFF000000) >> 24));
  SPIwrite(0x22, ((temp & 0x00FF0000) >> 16));
  SPIwrite(0x31, ((temp & 0x0000FF00) >> 8  ));
  SPIwrite(0x20, ((temp & 0x000000FF)));
    SPIwrite(0x50, 0x00);

}
void SPIwrite(int byte1, int byte2) {

  digitalWrite(SlaveSelectPin, LOW);
  SPI.transfer(byte1);
  SPI.transfer(byte2);
  digitalWrite(SlaveSelectPin,HIGH);
}



« Last Edit: December 23, 2012, 05:15:12 pm by siddharthsharma52 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46215
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have an Arduino problem you want to talk about?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the code works for arduino, but not for raspberry pi.
Logged

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1189
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
PS.: the same code works perfect on arduino.

thats cool then  ( this is an Arduino forum.......... )
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

:\
Logged

Global Moderator
0
Offline Offline
Newbie
*****
Karma: 0
Posts: 40
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might ask this question on a Raspberry Pi forum or mailing list. There may be some esoteric behavior of the SPI port on the Pi that we Arduino users are less likely to know about.

Good luck!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did. waiting for replies.

I have been stuck on this problem for more than a week now! The library i use makes arduino codes compatible on raspberry pi. I have no idea why the MOSI pin is behaving this way!
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12482
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
To solve this I connect the MOSI pin directly to the ad9835 SDATA pin, no matter whatever frequency word i send(32 bit word), the frequency is stuck on 5MHz.
so obvious this is not the solution.

The source you provided does compile (after an #include and creating a global int for slaveselect) on my 1.0 but as I have no ad9835 I connet test it further.
Do you have the link to the datasheet you used? of the ad9835 ?
There might be a bug in the arduPi library which does "flip" the MOSI ?
Have you got other SPI based sketches running properly?


Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The source you provided does compile (after an #include and creating a global int for slaveselect) on my 1.0 but as I have no ad9835 I connet test it further.

The source code i uploaded was is the arduino code. for raspberry pi i removed the # define SlaveSelectPin, actually it does not make a difference because the slaveselect pin is fixed on the pi.

Even I thought the ardupi may have some bugs. but i tested it for other basic SPI, and SPI works fine!
I have used a npn transistor as inverter to get the original MOSI signal back and the signal now is correct. Now i am getting correct signals of MOSI,SCLK,SS. So basically all signals are as required, but still no ouput. I am guessing the problem might be with the frequency of SPI. will try all permutations and combinations. Hope it works. Will update,
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here the llink to AD9835 datasheet ::  http://www.analog.com/static/imported-files/data_sheets/AD9835.pdf
Logged

Pages: [1]   Go Up
Jump to: