Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. #include <SPI.h>
  2.  
  3. const int chipSelectPin = 10; // chip select, the MAX5354 updates output only after chip selct
  4.  
  5.  
  6. // is taken high again !
  7.  
  8. byte highbyte;
  9. byte lowbyte;
  10. byte highbyteVolt[4]; // write the data for SPI
  11. byte lowbyteVolt[4]; // write the data for SPI
  12. byte highbyteUpdate = B01000000; // updating DAC output from DAC input register
  13. byte lowbyteUpdate = B00000000; // updating DAC output from DAC input register
  14.  
  15. int i = 0; // loop counter
  16.  
  17. long sineWave = 0;
  18. unsigned long currentTime = 0;
  19. unsigned long currentMicros = 0;
  20. unsigned long previousMicros = 0;
  21. unsigned long periodT = 3000000; // 21 = 48 kHz 42 = 24 kHz
  22.  
  23. void setup() {
  24. pinMode(chipSelectPin, OUTPUT);
  25. // initialize SPI:
  26. SPI.begin();
  27. Serial.begin(9600);
  28.  
  29.  
  30. // For the 48 kHz updating rate it is too slow to write data and update on separate commands.
  31. // Control bit C2 C1 C0 for update and D9 to D0 for voltage value
  32. // C2 C1 C0 =
  33. highbyteVolt[3] = B00011101; // 3,0 V on 10 bit DAC (3/3,3)1024 = 11 1010 0011
  34. lowbyteVolt[3] = B00011000; // 3,0 V on 10 bit DAC
  35. highbyteVolt[2] = B00010011; // 2,0 V on 10 bit DAC (2/3,3)1024 = 10 0110 1101
  36. lowbyteVolt[2] = B01101000; // 2,0 V on 10 bit DAC
  37. highbyteVolt[1] = B00001001; // 1,0 V on 10 bit DAC (1/3,3)1024 = 01 0011 0110
  38. lowbyteVolt[1] = B10110000; // 1,0 V on 10 bit DAC
  39. highbyteVolt[0] = B00000000; // 0,0 V on 10 bit DAC (0/3,3)1024 = 00 0000 0000
  40. lowbyteVolt[0] = B00000000; // 0,0 V on 10 bit DAC
  41. }
  42.  
  43. void loop()
  44. {
  45.  
  46.  
  47.  
  48.  
  49. currentMicros = micros();
  50.  
  51. if(currentMicros - previousMicros > periodT)
  52. {
  53. i = i+1;
  54. if(i == 4)
  55. {
  56. i = 0;
  57. }
  58. previousMicros = micros();
  59.  
  60. //Metodi 2
  61. /* for(byte i = 0; i < 32++){
  62. upperByte = i;
  63.  
  64. for(int j = 0; j < 256; j++){
  65. lowerbyte = j << 3;
  66.  
  67.  
  68. }
  69.  
  70. }*/
  71.  
  72. //Metodi 1
  73. /* for(uint16_t i = 0; i < 1024; i++){
  74. uint16_t s = 1*sin(2*PI*1*i+2);
  75.  
  76. byte sineUpper = s >> 5;
  77. byte sineLower = s << 3;
  78.  
  79. byte upperbyte = i >> 5;
  80. byte lowerbyte = i << 3;*/
  81.  
  82. int calcPinPoint(int a) {
  83. return (1024/2)*sin(e*PI/100+512);
  84. }
  85.  
  86.  
  87. for (int i=0; i< 1024; i++) {
  88. int value = calcPinPoint(i);
  89. byte upperByte = value >> 5;
  90. byte lowerByte = value << 3;
  91.  
  92.  
  93. Serial.print("UpperByte: ");
  94. Serial.print(upperbyte, BIN);
  95. Serial.print("\tLowerByte: ");
  96. Serial.print(lowerbyte, BIN);
  97. Serial.println();
  98. //delay(100);
  99. digitalWrite(chipSelectPin, LOW);
  100. //SPI.transfer(sineUpper);
  101. //SPI.transfer(sineLower);
  102. SPI.transfer(upperbyte);
  103. SPI.transfer(lowerbyte);
  104. digitalWrite(chipSelectPin, HIGH);
  105. delayMicroseconds(2);
  106.  
  107. }
  108.  
  109.  
  110. /*
  111. */
  112.  
  113. }
  114.  
  115. /* // Write to DAC input register without updating
  116. // the DAC output register.
  117. // take the CS pin low to select the chip:
  118. digitalWrite(chipSelectPin, LOW);
  119. // send in the value in two bytes via SPI:
  120. SPI.transfer(highbyteVolt[i]);
  121. SPI.transfer(lowbyteVolt[i]);
  122. // take the CS pin high to de-select the chip:
  123. digitalWrite(chipSelectPin, HIGH);*/
  124.  
  125. /* No need for separate update
  126. // Update DAC output
  127. // take the CS pin low to select the chip:
  128. digitalWrite(chipSelectPin, LOW);
  129. // send in the value in two bytes via SPI:
  130. SPI.transfer(highbyteUpdate);
  131. SPI.transfer(lowbyteUpdate);
  132. // take the CS pin high to de-select the chip:
  133. digitalWrite(chipSelectPin, HIGH);
  134. */
  135. }
  136.  
  137. //delay(1); Delay just for debugging if something goes wrong.
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement