You have reached the legacy GHI Electronics, LLC website, for the new website please visit here. For the new forum please visit here.

This legacy website will be taken offline at the end of this year. If there is anything that you would like to archive and save for future reference please do so.

AD9850 Example by Bill_Wyo

Jul. 18, 2016   |   Snippet   |   Licensed as Apache 2.0   |   1156 views

Here is an example of using an AD9850 function generator. I have included the pin assignments for a Spider 1.0 and a Cerberus. You will have to include a reference to GHI.Pins. When I wrote this I was using a Cerbuino so there were only two sockets left after connecting the character display. As a result the control is performed using two buttons. One button selects the frequency to be added to the displayed frequency or selects Reset to zero out the displayed frequency. The second button writes the displayed frequency to the AD9850.

I have included the Gadgeteer diagram and a picture of the AD9850. I connected the 6 connectors on the top left of the AD9850 in the picture to a breakout module which can be inserted into a Y socket.

Here is a link to the AD9850 technical document.
http://www.analog.com/media/en/technical-documentation/data-sheets/AD9850.pdf

Comments or questions?   Discuss on the forum.



Author Version Date
Bill_Wyo 1 07/18 '16 at 10:58pm
1 — Source
  1. using System;
  2. using System.Collections;
  3. using System.Threading;
  4. using Microsoft.SPOT;
  5. using Microsoft.SPOT.Presentation;
  6. using Microsoft.SPOT.Presentation.Controls;
  7. using Microsoft.SPOT.Presentation.Media;
  8. using Microsoft.SPOT.Presentation.Shapes;
  9. using Microsoft.SPOT.Touch;
  10. using Microsoft.SPOT.Hardware;
  11.  
  12. using Gadgeteer.Networking;
  13. using GT = Gadgeteer;
  14. using GTM = Gadgeteer.Modules;
  15. using Gadgeteer.Modules.GHIElectronics;
  16. using GHI.Pins;
  17.  
  18. namespace Function_Generator
  19. {
  20. public partial class Program
  21. {
  22. static OutputPort frequpdate, reset, clkPin, dataPin;
  23. static double freqval = 1000;
  24. static string[] freqvals = new string[9] { "1", "10", "100", "1000", "10000", "100000", "1000000", "10000000", "Reset" };
  25. static int freqvalcounter = 0;
  26.  
  27. void ProgramStarted()
  28. {
  29. //Thesse pin definitions are for socket 4 on Spider 1.0
  30. frequpdate = new OutputPort(GHI.Pins.EMX.IO31, false);
  31. reset = new OutputPort(GHI.Pins.EMX.IO37, false);
  32. clkPin = new OutputPort(GHI.Pins.EMX.IO34, false);
  33. dataPin = new OutputPort(GHI.Pins.EMX.IO32, false);
  34.  
  35. //here is how the pins would be defined if using a Cerberus
  36. //frequpdate = new OutputPort(GHI.Pins.FEZCerberus.Socket6.Pin6, false);
  37. //reset = new OutputPort(GHI.Pins.FEZCerberus.Socket6.Pin4, false);
  38. //clkPin = new OutputPort(GHI.Pins.FEZCerberus.Socket6.Pin7, false);
  39. //dataPin = new OutputPort(GHI.Pins.FEZCerberus.Socket6.Pin5, false);
  40.  
  41. Thread thread = new Thread((new FunctionGenerator().SetFrequency));
  42. thread.Start();
  43. }
  44. public class FunctionGenerator
  45. {
  46. internal FunctionGenerator()
  47. {
  48. button.ButtonPressed += button_ButtonPressed;
  49. button2.ButtonPressed += button2_ButtonPressed;
  50.  
  51. reset.Write(false);
  52. Thread.Sleep(10);
  53. reset.Write(true);
  54. Thread.Sleep(10);
  55. reset.Write(false);
  56. }
  57.  
  58. void button2_ButtonPressed(GTM.GHIElectronics.Button sender, GTM.GHIElectronics.Button.ButtonState state)
  59. {
  60. ++freqvalcounter;
  61. if (freqvalcounter == 9) freqvalcounter = 0;
  62. }
  63.  
  64. void button_ButtonPressed(GTM.GHIElectronics.Button sender, GTM.GHIElectronics.Button.ButtonState state)
  65. {
  66. switch (freqvalcounter)
  67. {
  68. case 0:
  69. {
  70. freqval = freqval + 1;
  71. break;
  72. }
  73. case 1:
  74. {
  75. freqval = freqval + 10;
  76. break;
  77. }
  78. case 2:
  79. {
  80. freqval = freqval + 100;
  81. break;
  82. }
  83. case 3:
  84. {
  85. freqval = freqval + 1000;
  86. break;
  87. }
  88. case 4:
  89. {
  90. freqval = freqval + 10000;
  91. break;
  92. }
  93. case 5:
  94. {
  95. freqval = freqval + 100000;
  96. break;
  97. }
  98. case 6:
  99. {
  100. freqval = freqval + 1000000;
  101. break;
  102. }
  103. case 7:
  104. {
  105. freqval = freqval + 10000000;
  106. break;
  107. }
  108. case 8:
  109. {
  110. freqval = 0;
  111. break;
  112. }
  113. }
  114. }
  115. public void SetFrequency()
  116. {
  117. frequpdate.Write(false);
  118. frequpdate.Write(true);
  119. frequpdate.Write(false);
  120. while (true)
  121. {
  122. uint freq = (uint)freqval;
  123. float temp = freq * 4294.967296f / 125.0f;
  124. uint val = (uint)temp;
  125. byte[] data = new byte[5] { (byte)(val), (byte)(val >> 8), (byte)(val >> 16), (byte)(val >> 24), 0x0 };
  126. for (int j = 0; j < 5; ++j)
  127. {
  128. byte i = 0;
  129. int mask;
  130. while (i < 8)
  131. {
  132. mask = (1 << i);
  133. clkPin.Write(false);
  134. if (((int)data[j] & mask) == 0)
  135. dataPin.Write(false);
  136. else
  137. dataPin.Write(true);
  138. clkPin.Write(true);
  139. ++i;
  140. }
  141. }
  142. frequpdate.Write(false);
  143. Thread.Sleep(10);
  144. frequpdate.Write(true);
  145. Thread.Sleep(10);
  146. frequpdate.Write(false);
  147. PrintFrequency();
  148. Thread.Sleep(1000);
  149. //}
  150. }
  151. }
  152. private void PrintFrequency()
  153. {
  154. characterDisplay.SetCursorPosition(0, 0);
  155. characterDisplay.Print(" ");
  156. characterDisplay.SetCursorPosition(0, 0);
  157. characterDisplay.Print(freqval.ToString());
  158. characterDisplay.SetCursorPosition(1, 0);
  159. characterDisplay.Print(" ");
  160. characterDisplay.SetCursorPosition(1, 0);
  161. characterDisplay.Print(freqvals[freqvalcounter].ToString());
  162.  
  163. }
  164. }
  165. }
  166. }