Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
Margining.ino
Go to the documentation of this file.
1 /*!
2 Code for datasheet application - Margining a power supply with the
3 LTC2645 Quad 12-/10-/8-Bit PWM to VOUT DACs with 10ppm/C Reference
4 @verbatim
5 
6 NOTES
7  Setup:
8  Set the terminal baud rate to 115200 and select the newline terminator.
9  Equipment required is a precision voltage source (null box) and a precision voltmeter (to monitor voltage source).
10  No external power supply is required.
11  Ensure JP1 is installed in the default position from the factory.
12 
13 USER INPUT DATA FORMAT:
14  decimal : 1024
15  hex : 0x400
16  octal : 02000 (leading 0 "zero")
17  binary : B10000000000
18  float : 1024.0
19 
20 @endverbatim
21 
22 http://www.linear.com/product/LTC2645
23 
24 http://www.linear.com/product/LTC2645
25 
26 
27 Copyright 2018(c) Analog Devices, Inc.
28 
29 All rights reserved.
30 
31 Redistribution and use in source and binary forms, with or without
32 modification, are permitted provided that the following conditions are met:
33  - Redistributions of source code must retain the above copyright
34  notice, this list of conditions and the following disclaimer.
35  - Redistributions in binary form must reproduce the above copyright
36  notice, this list of conditions and the following disclaimer in
37  the documentation and/or other materials provided with the
38  distribution.
39  - Neither the name of Analog Devices, Inc. nor the names of its
40  contributors may be used to endorse or promote products derived
41  from this software without specific prior written permission.
42  - The use of this software may or may not infringe the patent rights
43  of one or more patent holders. This license does not release you
44  from the requirement that you obtain separate licenses from these
45  patent holders to use this software.
46  - Use of the software either in source or binary form, must be run
47  on or directly connected to an Analog Devices Inc. component.
48 
49 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
50 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
51 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
53 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
54 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
55 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
57 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
58 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60 
61 /*! @file
62  @ingroup LTC2645
63 */
64 
65 #include <Arduino.h>
66 #include <stdint.h>
67 #include "Linduino.h"
68 #include "UserInterface.h"
69 #include "math.h"
70 
71 // Pin Map
72 const uint8_t DAC_A = 9; //! 16-bit PWM Pin
73 const uint8_t DAC_B = 10; //! 16-bit PWM Pin
74 
75 // Function Decoration
76 void print_title(); // Print the title block
77 void print_prompt(); // Prompt the user for an input command
78 void init_16_bit_PWM(); // Initializes Pin 9 and 10 for 16-bit PWM
79 void pwm_16_bit(uint8_t pin, uint16_t duty); // Sets the 16-bit PWM
80 
81 //! Initialize Linduino
82 void setup()
83 {
84  Serial.begin(115200); // Initialize the serial port to the PC
85  init_16_bit_PWM(); // Initialize the 16-bit PWM
86 
87 
88  // Initialize toggle pin
89  pinMode(DAC_A ,OUTPUT);
90 
91  print_title();
92  print_prompt(0);
93 }
94 
95 //! Repeats Linduino loop
96 void loop()
97 {
98  int16_t user_command;
99  static int8_t selected_dac = 0; // The selected DAC to be updated (0=A, 1=B ... 3=D). Initialized to "A".
100 
101  if (Serial.available()) // Check for user input
102  {
103 
104  user_command = read_int(); // Read the user command
105  Serial.println(user_command);
106  Serial.flush();
107 
108  switch (user_command)
109  {
110  case 1:
111  pwm_16_bit(DAC_A, 0); // Sets the PWM to no Margin
112  break;
113  case 2:
114  // +5 % Margin
115  pwm_16_bit(DAC_A, 10480); // Sets the PWM to +5 % Margin
116  break;
117  case 3:
118  // -5 % Margin
119  pwm_16_bit(DAC_A, 31450); // Sets the PWM to -5 % Margin
120  break;
121  case 4:
122  // +10 % Margin
123  pwm_16_bit(DAC_A, 16); // Sets the PWM to +10 % Margin
124  break;
125  case 5:
126  // -10 % Margin
127  pwm_16_bit(DAC_A, 41933); // Sets the PWM to -10 % Margin
128  break;
129  default:
130  Serial.println("Incorrect Option");
131  break;
132  }
133  Serial.println("\n*****************************************");
134  print_prompt(selected_dac);
135  }
136 }
137 
138 // Function Definition
139 
140 //! Prints the title block when program first starts.
142 {
143  Serial.println();
144  Serial.println(F("*****************************************************************"));
145  Serial.println(F("* DCxxxxx Demonstration Program *"));
146  Serial.println(F("* *"));
147  Serial.println(F("* This program demonstrates how to implement a PWM signal *"));
148  Serial.println(F("* to set the LTC2645 DACs to a margining circuit . *"));
149  Serial.println(F("* *"));
150  Serial.println(F("* Set the baud rate to 115200 and select the newline terminator.*"));
151  Serial.println(F("* *"));
152  Serial.println(F("*****************************************************************"));
153 }
154 
155 
156 //! Prints main menu.
157 void print_prompt(int16_t selected_dac)
158 {
159  Serial.println(F("\nCommand Summary:"));
160  Serial.println(F(" 1-No Margining/High Impedance"));
161  Serial.println(F(" 2- +5 % Margin"));
162  Serial.println(F(" 3- -5 % Margin"));
163  Serial.println(F(" 4- +10 % Margin"));
164  Serial.println(F(" 5- -10 % Margin"));
165 
166  Serial.print(F(" Selected DAC: "));
167  Serial.println((char) (selected_dac + 0x41));
168  Serial.print(F("Enter a command:"));
169  Serial.flush();
170 }
171 
172 //! Initializes Pin 9 and 10 for 16-bit PWM
174 {
175  pinMode(DAC_A, OUTPUT); // Set Pin 9 as output
176  pinMode(DAC_B, OUTPUT); // Set Pin 10 as output
177 
178 
179  // Enable timer1 for PWM, Phase Correct mode for pin 9 and 10.
180  // Use IRC1 as top, no prescalar (~122Hz),
181  // Negative Edge Triggered
182  TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11);
183  TCCR1B = _BV(WGM13) | _BV(CS10);
184  TCCR1C = 0x0;
185  ICR1 = 0xFFFF; // Set Timer for 16-bit
186 
187  // The following code should be uncommented if Fast PWM Mode
188  // is desired
189  /*
190  // Enable timer1 for Fast PWM mode for pin 9 and 10.
191  // Use IRC1 as top, no prescalar (~244Hz),
192  // Negative Edge Triggered
193  TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11);
194  TCCR1B = _BV(WGM13)| _BV(WGM12) | _BV(CS10);
195  TCCR1C = 0x0;
196  ICR1 = 0xFFFF; // Set Timer for 16-bit
197  */
198 
199  // Set to 0% duty cycle
200  OCR1A = 0x0;
201  OCR1B = 0x0;
202 }
203 
204 //! Sets duty cycle for 16-bit PWM.
205 void pwm_16_bit(uint8_t pin, uint16_t duty)
206 {
207  if (pin == 9)
208  OCR1A = duty; // Pin 9
209  else
210  OCR1B = duty; // Pin 10
211 }
const uint8_t DAC_B
16-bit PWM Pin
Definition: Margining.ino:73
unsigned char user_command
static void pwm_16_bit(uint8_t pin, uint16_t duty)
Sets duty cycle for 16-bit PWM.
Definition: Margining.ino:205
Header File for Linduino Libraries and Demo Code.
static void setup()
Initialize Linduino.
Definition: Margining.ino:82
int32_t read_int()
static void loop()
Repeats Linduino loop.
Definition: Margining.ino:96
static void print_title()
16-bit PWM Pin
Definition: Margining.ino:141
const uint8_t DAC_A
Definition: Margining.ino:72
static void init_16_bit_PWM()
Initializes Pin 9 and 10 for 16-bit PWM.
Definition: Margining.ino:173
static void print_prompt()