Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- LGT8F328P DAC example
- Generates a slightly complex analog signal.
- First, it generates a full ramp pulse then a sine
- wave, a half ramp, and last a small square. All at
- a fairly low frequency (1.8khz and less) but they
- are generated at different rates making it a little
- harder to trigger on with a scope.
- A good way to test the hold-off on your scope :)
- Scott Beasley (2021) No Copyright.
- */
- #include <Arduino.h>
- #include <avr/power.h>
- #define SAMPLES 33
- // Sine wave sample voltages
- byte table[SAMPLES] = {
- 0x80, 0x98, 0xb0, 0xc6, 0xda, 0xea, 0xf5, 0xfd, 0xff,
- 0xfd, 0xf5, 0xea, 0xda, 0xc6, 0xb0, 0x98, 0x80, 0x67,
- 0x4f, 0x39, 0x25, 0x15, 0x0a, 0x02, 0x00, 0x02, 0x0a,
- 0x15, 0x25, 0x39, 0x4f, 0x67, 0x80
- };
- void setup ( )
- {
- clock_prescale_set (clock_div_1); // Run at 32Mhz!
- analogReference (INTERNAL2V56); // 2.56v ref
- pinMode (4, ANALOG); // Set Pin 4 for DAC use.
- DAL0 = 0; // Set the DAC pin to 0v
- }
- void loop ( )
- {
- // Keep this out of the "Arduino" flow for
- // smoother wave generation.
- // Remove the while if you need the serial monitor.
- while (1) {
- // Ramp up
- for (byte i = 0; i < 255; i++) {
- DAL0 = i;
- delayMicroseconds (5);
- }
- // Ramp down
- for (byte i = 255; i > 0; i--) {
- DAL0 = i;
- delayMicroseconds (5);
- }
- // Sine wave first
- for (byte i = 0; i < SAMPLES - 8; i++) {
- DAL0 = table[i]; // Write out value to the dac
- delayMicroseconds (15);
- }
- // Ramp up
- for (byte i = 0; i < 128; i++) {
- DAL0 = i;
- delayMicroseconds (5);
- }
- // Ramp down
- for (byte i = 128; i > 0; i--) {
- DAL0 = i;
- delayMicroseconds (5);
- }
- DAL0 = 190;
- delayMicroseconds (450);
- DAL0 = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment