creat1001

LGT8F328P DAC example

Feb 8th, 2021
1,928
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 1 0
  1. /*
  2.     LGT8F328P DAC example
  3.  
  4.     Generates a slightly complex analog signal.
  5.  
  6.     First, it generates a full ramp pulse then a sine
  7.     wave, a half ramp, and last a small square. All at
  8.     a fairly low frequency (1.8khz and less) but they
  9.     are generated at different rates making it a little
  10.     harder to trigger on with a scope.
  11.  
  12.     A good way to test the hold-off on your scope :)
  13.  
  14.     Scott Beasley (2021) No Copyright.
  15. */
  16.  
  17. #include <Arduino.h>
  18. #include <avr/power.h>
  19.  
  20. #define SAMPLES 33
  21.  
  22. // Sine wave sample voltages
  23. byte table[SAMPLES] = {
  24.     0x80, 0x98, 0xb0, 0xc6, 0xda, 0xea, 0xf5, 0xfd, 0xff,
  25.     0xfd, 0xf5, 0xea, 0xda, 0xc6, 0xb0, 0x98, 0x80, 0x67,
  26.     0x4f, 0x39, 0x25, 0x15, 0x0a, 0x02, 0x00, 0x02, 0x0a,
  27.     0x15, 0x25, 0x39, 0x4f, 0x67, 0x80
  28. };
  29.  
  30. void setup ( )
  31. {
  32.     clock_prescale_set (clock_div_1); // Run at 32Mhz!
  33.  
  34.     analogReference (INTERNAL2V56); // 2.56v ref
  35.     pinMode (4, ANALOG); // Set Pin 4 for DAC use.
  36.  
  37.     DAL0 = 0; // Set the DAC pin to 0v
  38. }
  39.  
  40. void loop ( )
  41. {
  42.     // Keep this out of the "Arduino" flow for
  43.     // smoother wave generation.
  44.     // Remove the while if you need the serial monitor.
  45.     while (1) {
  46.         // Ramp up
  47.         for (byte i = 0; i < 255; i++) {
  48.             DAL0 = i;
  49.             delayMicroseconds (5);
  50.         }  
  51.  
  52.         // Ramp down
  53.         for (byte i = 255; i > 0; i--) {
  54.             DAL0 = i;
  55.             delayMicroseconds (5);
  56.         }        
  57.  
  58.         // Sine wave first
  59.         for (byte i = 0; i < SAMPLES - 8; i++) {
  60.             DAL0 = table[i]; // Write out value to the dac
  61.             delayMicroseconds (15);
  62.         }
  63.  
  64.         // Ramp up
  65.         for (byte i = 0; i < 128; i++) {
  66.             DAL0 = i;
  67.             delayMicroseconds (5);
  68.         }  
  69.  
  70.         // Ramp down
  71.         for (byte i = 128; i > 0; i--) {
  72.             DAL0 = i;
  73.             delayMicroseconds (5);
  74.         }
  75.  
  76.         DAL0 = 190;
  77.         delayMicroseconds (450);
  78.         DAL0 = 0;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment