Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- const int chipSelectPin = 10; // chip select, the MAX5354 updates output only after chip selct
- // is taken high again !
- byte highbyte;
- byte lowbyte;
- byte highbyteVolt[4]; // write the data for SPI
- byte lowbyteVolt[4]; // write the data for SPI
- byte highbyteUpdate = B01000000; // updating DAC output from DAC input register
- byte lowbyteUpdate = B00000000; // updating DAC output from DAC input register
- int i = 0; // loop counter
- long sineWave = 0;
- unsigned long currentTime = 0;
- unsigned long currentMicros = 0;
- unsigned long previousMicros = 0;
- unsigned long periodT = 3000000; // 21 = 48 kHz 42 = 24 kHz
- void setup() {
- pinMode(chipSelectPin, OUTPUT);
- // initialize SPI:
- SPI.begin();
- Serial.begin(9600);
- // For the 48 kHz updating rate it is too slow to write data and update on separate commands.
- // Control bit C2 C1 C0 for update and D9 to D0 for voltage value
- // C2 C1 C0 =
- highbyteVolt[3] = B00011101; // 3,0 V on 10 bit DAC (3/3,3)1024 = 11 1010 0011
- lowbyteVolt[3] = B00011000; // 3,0 V on 10 bit DAC
- highbyteVolt[2] = B00010011; // 2,0 V on 10 bit DAC (2/3,3)1024 = 10 0110 1101
- lowbyteVolt[2] = B01101000; // 2,0 V on 10 bit DAC
- highbyteVolt[1] = B00001001; // 1,0 V on 10 bit DAC (1/3,3)1024 = 01 0011 0110
- lowbyteVolt[1] = B10110000; // 1,0 V on 10 bit DAC
- highbyteVolt[0] = B00000000; // 0,0 V on 10 bit DAC (0/3,3)1024 = 00 0000 0000
- lowbyteVolt[0] = B00000000; // 0,0 V on 10 bit DAC
- }
- void loop()
- {
- currentMicros = micros();
- if(currentMicros - previousMicros > periodT)
- {
- i = i+1;
- if(i == 4)
- {
- i = 0;
- }
- previousMicros = micros();
- //Metodi 2
- /* for(byte i = 0; i < 32++){
- upperByte = i;
- for(int j = 0; j < 256; j++){
- lowerbyte = j << 3;
- }
- }*/
- //Metodi 1
- /* for(uint16_t i = 0; i < 1024; i++){
- uint16_t s = 1*sin(2*PI*1*i+2);
- byte sineUpper = s >> 5;
- byte sineLower = s << 3;
- byte upperbyte = i >> 5;
- byte lowerbyte = i << 3;*/
- int calcPinPoint(int a) {
- return (1024/2)*sin(e*PI/100+512);
- }
- for (int i=0; i< 1024; i++) {
- int value = calcPinPoint(i);
- byte upperByte = value >> 5;
- byte lowerByte = value << 3;
- Serial.print("UpperByte: ");
- Serial.print(upperbyte, BIN);
- Serial.print("\tLowerByte: ");
- Serial.print(lowerbyte, BIN);
- Serial.println();
- //delay(100);
- digitalWrite(chipSelectPin, LOW);
- //SPI.transfer(sineUpper);
- //SPI.transfer(sineLower);
- SPI.transfer(upperbyte);
- SPI.transfer(lowerbyte);
- digitalWrite(chipSelectPin, HIGH);
- delayMicroseconds(2);
- }
- /*
- */
- }
- /* // Write to DAC input register without updating
- // the DAC output register.
- // take the CS pin low to select the chip:
- digitalWrite(chipSelectPin, LOW);
- // send in the value in two bytes via SPI:
- SPI.transfer(highbyteVolt[i]);
- SPI.transfer(lowbyteVolt[i]);
- // take the CS pin high to de-select the chip:
- digitalWrite(chipSelectPin, HIGH);*/
- /* No need for separate update
- // Update DAC output
- // take the CS pin low to select the chip:
- digitalWrite(chipSelectPin, LOW);
- // send in the value in two bytes via SPI:
- SPI.transfer(highbyteUpdate);
- SPI.transfer(lowbyteUpdate);
- // take the CS pin high to de-select the chip:
- digitalWrite(chipSelectPin, HIGH);
- */
- }
- //delay(1); Delay just for debugging if something goes wrong.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement