├── LICENSE ├── README.md ├── antuino.pdf ├── antuino_analyzer_27mhz_v2.ino └── si5351.ino /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # antuino 2 | Arduino+Si5351 = Antenna/Network Analyzer 3 | -------------------------------------------------------------------------------- /antuino.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afarhan/antuino-old/79e6acfa6fcd2759faf1845d07586c5375928d90/antuino.pdf -------------------------------------------------------------------------------- /antuino_analyzer_27mhz_v2.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | //#define XTAL_FREQ 24996900l; // Frequency of Quartz-Oszillator 6 | uint32_t xtal_freq_calibrated = 27000000l; 7 | #define MASTER_CAL 0 8 | #define LAST_FREQ 4 9 | #define OPEN_HF 8 10 | #define OPEN_VHF 12 11 | #define OPEN_UHF 16 12 | 13 | #define SI_CLK0_CONTROL 16 // Register definitions 14 | #define SI_CLK1_CONTROL 17 15 | #define SI_CLK2_CONTROL 18 16 | 17 | 18 | unsigned long f = 10000000l; 19 | #define IF_FREQ (24991000l) 20 | #define MODE_ANTENNA_ANALYZER 0 21 | #define MODE_MEASUREMENT_RX 1 22 | #define MODE_NETWORK_ANALYZER 2 23 | unsigned long mode = MODE_ANTENNA_ANALYZER; 24 | 25 | LiquidCrystal lcd(8,9,10,11,12,13); 26 | char b[32], c[32], buff[32], serial_in[32]; 27 | int return_loss; 28 | unsigned long frequency = 10000000l; 29 | unsigned long fromFrequency=14150000; 30 | unsigned long toFrequency=30000000; 31 | //int openReading = 93; // in dbm 32 | int openHF = 96; 33 | int openVHF = 96; 34 | int openUHF = 68; 35 | 36 | int dbmOffset = -114; 37 | 38 | int menuOn = 0; 39 | unsigned long timeOut = 0; 40 | 41 | /* for reading and writing from serial port */ 42 | unsigned char serial_in_count = 0; 43 | 44 | void active_delay(int delay_by){ 45 | unsigned long timeStart = millis(); 46 | 47 | while (millis() - timeStart <= delay_by) { 48 | //Background Work 49 | } 50 | } 51 | 52 | int calibrateClock(){ 53 | int knob = 0; 54 | int32_t prev_calibration; 55 | 56 | 57 | //keep clear of any previous button press 58 | while (btnDown()) 59 | active_delay(100); 60 | active_delay(100); 61 | 62 | prev_calibration = xtal_freq_calibrated; 63 | xtal_freq_calibrated = 27000000l; 64 | 65 | si5351aSetFrequency_clk1(10000000l); 66 | ltoa(xtal_freq_calibrated - 27000000l, c, 10); 67 | printLine2(c); 68 | 69 | while (!btnDown()) 70 | { 71 | knob = enc_read(); 72 | 73 | if (knob > 0) 74 | xtal_freq_calibrated += 10; 75 | else if (knob < 0) 76 | xtal_freq_calibrated -= 10; 77 | else 78 | continue; //don't update the frequency or the display 79 | 80 | si5351aSetFrequency_clk1(10000000l); 81 | 82 | ltoa(xtal_freq_calibrated - 27000000l, c, 10); 83 | printLine2(c); 84 | } 85 | 86 | printLine2("Calibration set!"); 87 | EEPROM.put(MASTER_CAL, xtal_freq_calibrated); 88 | 89 | while(btnDown()) 90 | active_delay(50); 91 | active_delay(100); 92 | } 93 | 94 | 95 | #define ENC_A (A0) 96 | #define ENC_B (A1) 97 | #define FBUTTON (A2) 98 | #define BACK_LIGHT (A3) 99 | #define DBM_READING (A6) 100 | 101 | 102 | const int PROGMEM vswr[] = { 103 | 999, 104 | 174, 105 | 87, 106 | 58, 107 | 44, 108 | 35, 109 | 30, 110 | 26, 111 | 23, 112 | 21, 113 | 19, 114 | 18, 115 | 17, 116 | 16, 117 | 15, 118 | 14, 119 | 14, 120 | 13, 121 | 13, 122 | 12, 123 | 12, 124 | 12, 125 | 12, 126 | 11, 127 | 11, 128 | 11, 129 | 11, 130 | 1, 131 | 10, 132 | 10, 133 | 10 134 | }; 135 | 136 | void printLine1(char *c){ 137 | lcd.setCursor(0, 0); 138 | lcd.print(c); 139 | } 140 | 141 | void printLine2(char *c){ 142 | lcd.setCursor(0, 1); 143 | lcd.print(c); 144 | } 145 | 146 | 147 | 148 | int enc_prev_state = 3; 149 | 150 | //returns true if the button is pressed 151 | int btnDown(){ 152 | if (digitalRead(FBUTTON) == HIGH) 153 | return 0; 154 | else 155 | return 1; 156 | } 157 | 158 | void resetTimer(){ 159 | //push the timer to the next 160 | timeOut = millis() + 10000l; 161 | digitalWrite(BACK_LIGHT, HIGH); 162 | } 163 | 164 | 165 | void checkTimeout(){ 166 | unsigned long last_freq; 167 | 168 | if (timeOut > millis()) 169 | return; 170 | digitalWrite(BACK_LIGHT, LOW); 171 | EEPROM.get(LAST_FREQ, last_freq); 172 | if (last_freq != frequency) 173 | EEPROM.put(LAST_FREQ, frequency); 174 | } 175 | 176 | byte enc_state (void) { 177 | return (analogRead(ENC_A) > 500 ? 1 : 0) + (analogRead(ENC_B) > 500 ? 2: 0); 178 | } 179 | 180 | int enc_read(void) { 181 | int result = 0; 182 | byte newState; 183 | int enc_speed = 0; 184 | 185 | long stop_by = millis() + 50; 186 | 187 | while (millis() < stop_by) { // check if the previous state was stable 188 | newState = enc_state(); // Get current state 189 | 190 | if (newState != enc_prev_state) 191 | delay (1); 192 | 193 | if (enc_state() != newState || newState == enc_prev_state) 194 | continue; 195 | //these transitions point to the encoder being rotated anti-clockwise 196 | if ((enc_prev_state == 0 && newState == 2) || 197 | (enc_prev_state == 2 && newState == 3) || 198 | (enc_prev_state == 3 && newState == 1) || 199 | (enc_prev_state == 1 && newState == 0)){ 200 | result--; 201 | } 202 | //these transitions point o the enccoder being rotated clockwise 203 | if ((enc_prev_state == 0 && newState == 1) || 204 | (enc_prev_state == 1 && newState == 3) || 205 | (enc_prev_state == 3 && newState == 2) || 206 | (enc_prev_state == 2 && newState == 0)){ 207 | result++; 208 | } 209 | enc_prev_state = newState; // Record state for next pulse interpretation 210 | enc_speed++; 211 | active_delay(1); 212 | } 213 | return(result); 214 | } 215 | 216 | // this builds up the top line of the display with frequency and mode 217 | void updateDisplay() { 218 | int vswr_reading; 219 | // tks Jack Purdum W8TEE 220 | // replaced fsprint commmands by str commands for code size reduction 221 | 222 | memset(c, 0, sizeof(c)); 223 | memset(b, 0, sizeof(b)); 224 | 225 | ultoa(frequency, b, DEC); 226 | 227 | 228 | //one mhz digit if less than 10 M, two digits if more 229 | 230 | if (frequency >= 100000000l){ 231 | strncat(c, b, 3); 232 | strcat(c, "."); 233 | strncat(c, &b[3], 3); 234 | strcat(c, "."); 235 | strncat(c, &b[6], 3); 236 | } 237 | else if (frequency >= 10000000l){ 238 | strcpy(c, " "); 239 | strncat(c, b, 2); 240 | strcat(c, "."); 241 | strncat(c, &b[2], 3); 242 | strcat(c, "."); 243 | strncat(c, &b[5], 3); 244 | } 245 | else { 246 | strcpy(c, " "); 247 | strncat(c, b, 1); 248 | strcat(c, "."); 249 | strncat(c, &b[1], 3); 250 | strcat(c, "."); 251 | strncat(c, &b[4], 3); 252 | } 253 | if (mode == MODE_ANTENNA_ANALYZER) 254 | strcat(c, " ANT"); 255 | else if (mode == MODE_MEASUREMENT_RX) 256 | strcat(c, " MRX"); 257 | else if (mode == MODE_NETWORK_ANALYZER) 258 | strcat(c, " SNA"); 259 | printLine1(c); 260 | 261 | if (mode == MODE_ANTENNA_ANALYZER){ 262 | return_loss = openReading(frequency) - analogRead(DBM_READING)/5; 263 | if (return_loss > 30) 264 | return_loss = 30; 265 | if (return_loss < 0) 266 | return_loss = 0; 267 | 268 | vswr_reading = pgm_read_word_near(vswr + return_loss); 269 | sprintf (c, "%ddb VSWR=%d.%01d", return_loss, vswr_reading/10, vswr_reading%10); 270 | }else if (mode == MODE_MEASUREMENT_RX){ 271 | sprintf(c, "%d dbm ", analogRead(DBM_READING)/5 + dbmOffset); 272 | } 273 | else if (mode == MODE_NETWORK_ANALYZER) { 274 | sprintf(c, "%d dbm ", analogRead(DBM_READING)/5 + dbmOffset); 275 | } 276 | printLine2(c); 277 | } 278 | 279 | 280 | long prev_freq = 0; 281 | void takeReading(long newfreq){ 282 | long local_osc; 283 | 284 | if (newfreq < 20000l) 285 | newfreq = 20000l; 286 | if (newfreq < 150000000l) 287 | { 288 | if (newfreq < 50000000l) 289 | local_osc = newfreq + IF_FREQ; 290 | else 291 | local_osc = newfreq - IF_FREQ; 292 | } else { 293 | newfreq = newfreq / 3; 294 | local_osc = newfreq - IF_FREQ/3; 295 | } 296 | 297 | if (prev_freq != newfreq){ 298 | switch(mode){ 299 | case MODE_MEASUREMENT_RX: 300 | si5351aSetFrequency_clk2(local_osc); 301 | break; 302 | case MODE_NETWORK_ANALYZER: 303 | si5351aSetFrequency_clk2(local_osc); 304 | si5351aSetFrequency_clk0(newfreq); 305 | Serial.print(local_osc); 306 | Serial.print(' '); 307 | Serial.println(newfreq); 308 | break; 309 | default: 310 | si5351aSetFrequency_clk2(local_osc); 311 | si5351aSetFrequency_clk1(newfreq); 312 | } 313 | prev_freq = newfreq; 314 | } 315 | } 316 | 317 | void setup() { 318 | lcd.begin(16, 2); 319 | Wire.begin(); 320 | Serial.begin(9600); 321 | Serial.flush(); 322 | Serial.println("i Antuino v1.01"); 323 | analogReference(DEFAULT); 324 | 325 | unsigned long last_freq = 0; 326 | EEPROM.get(MASTER_CAL, xtal_freq_calibrated); 327 | EEPROM.get(LAST_FREQ, last_freq); 328 | EEPROM.get(OPEN_HF, openHF); 329 | EEPROM.get(OPEN_VHF, openVHF); 330 | EEPROM.get(OPEN_UHF, openUHF); 331 | 332 | if (0< last_freq && last_freq < 500000000l) 333 | frequency = last_freq; 334 | 335 | if (xtal_freq_calibrated < 26900000l || xtal_freq_calibrated > 27100000l) 336 | xtal_freq_calibrated = 27000000l; 337 | 338 | pinMode(ENC_A, INPUT_PULLUP); 339 | pinMode(ENC_B, INPUT_PULLUP); 340 | pinMode(FBUTTON, INPUT_PULLUP); 341 | pinMode(BACK_LIGHT, OUTPUT); 342 | 343 | digitalWrite(BACK_LIGHT, LOW); 344 | printLine1("Antuino v1.1"); 345 | delay(2000); 346 | digitalWrite(BACK_LIGHT, HIGH); 347 | 348 | if (btnDown()){ 349 | calibrateClock(); 350 | } 351 | 352 | si5351aOutputOff(SI_CLK0_CONTROL); 353 | takeReading(frequency); 354 | updateDisplay(); 355 | } 356 | 357 | int menuBand(int btn){ 358 | int knob = 0; 359 | int band, prev = 0, r; 360 | unsigned long offset; 361 | unsigned long prev_freq; 362 | 363 | 364 | if (!btn){ 365 | printLine2("Band Select \x7E"); 366 | return; 367 | } 368 | 369 | printLine2("Band Select: "); 370 | //wait for the button menu select button to be lifted) 371 | while (btnDown()) 372 | active_delay(50); 373 | active_delay(50); 374 | 375 | while(!btnDown()){ 376 | 377 | prev_freq = frequency; 378 | knob = enc_read(); 379 | checkTimeout(); 380 | 381 | if (knob != 0){ 382 | 383 | resetTimer(); 384 | 385 | if (knob < 0 && frequency > 3000000l) 386 | frequency -= 250000l; 387 | if (knob > 0 && frequency < 500000000l) 388 | frequency += 250000l; 389 | 390 | if (prev_freq <= 150000000l && frequency > 150000000l) 391 | frequency = 350000000l; 392 | if (prev_freq >= 350000000l && frequency < 350000000l) 393 | frequency = 149999000l; 394 | 395 | 396 | takeReading(frequency); 397 | updateDisplay(); 398 | printLine2("Band Select"); 399 | } 400 | else{ 401 | r = analogRead(DBM_READING); 402 | if (r != prev){ 403 | takeReading(frequency); 404 | updateDisplay(); 405 | prev = r; 406 | } 407 | } 408 | active_delay(20); 409 | } 410 | 411 | menuOn = 0; 412 | while(btnDown()) 413 | active_delay(50); 414 | active_delay(50); 415 | 416 | printLine2(""); 417 | updateDisplay(); 418 | } 419 | 420 | 421 | int tuningClicks = 0; 422 | int tuningSpeed = 0; 423 | void doTuning(){ 424 | int s; 425 | unsigned long prev_freq; 426 | 427 | s = enc_read(); 428 | 429 | if (s < 0 && tuningClicks > 0) 430 | tuningClicks = s; 431 | else if (s > 0 && tuningClicks < 0) 432 | tuningClicks = s; 433 | else 434 | tuningClicks += s; 435 | 436 | tuningSpeed = (tuningSpeed * 4 + s)/5; 437 | //Serial.print(tuningSpeed); 438 | //Serial.print(","); 439 | //Serial.println(tuningClicks); 440 | if (s != 0){ 441 | resetTimer(); 442 | prev_freq = frequency; 443 | 444 | if (s > 4 && tuningClicks > 100) 445 | frequency += 200000l; 446 | else if (s > 2) 447 | frequency += 1000l; 448 | else if (s > 0) 449 | frequency += 20l; 450 | else if (s > -2) 451 | frequency -= 20l; 452 | else if (s > -4) 453 | frequency -= 1000l; 454 | else if (tuningClicks < -100) 455 | frequency -= 200000l; 456 | 457 | takeReading(frequency); 458 | updateDisplay(); 459 | } 460 | } 461 | 462 | void doTuning2(){ 463 | int s; 464 | unsigned long prev_freq; 465 | 466 | s = enc_read(); 467 | 468 | if (s < 0 && tuningClicks > 0) 469 | tuningClicks = s; 470 | else if (s > 0 && tuningClicks < 0) 471 | tuningClicks = s; 472 | else 473 | tuningClicks += s; 474 | 475 | tuningSpeed = (tuningSpeed * 4 + s)/5; 476 | // Serial.print(tuningSpeed); 477 | // Serial.print(","); 478 | // Serial.println(tuningClicks); 479 | if (s != 0){ 480 | resetTimer(); 481 | prev_freq = frequency; 482 | 483 | if (tuningSpeed >= 5 && tuningClicks > 100) 484 | frequency += 10000000l; 485 | else if (tuningSpeed== 4 && tuningClicks > 100) 486 | frequency += 1000000l; 487 | else if (tuningSpeed == 3) 488 | frequency += 100000l; 489 | else if (tuningSpeed == 2) 490 | frequency += 10000l; 491 | else if (tuningSpeed == 1) 492 | frequency += 1000l; 493 | else if (tuningSpeed == 0 && s > 0) 494 | frequency += 100l; 495 | else if (tuningSpeed == 0 && s < 0) 496 | frequency -= 100l; 497 | else if (tuningSpeed == -1) 498 | frequency -= 1000l; 499 | else if (tuningSpeed == -2) 500 | frequency -= 10000; 501 | else if (tuningSpeed == -3) 502 | frequency -= 100000l; 503 | else if (tuningSpeed== -4 && tuningClicks < -100 && frequency > 1000000l) 504 | frequency -= 1000000l; 505 | else if (tuningSpeed <= -5 && tuningClicks < -100 && frequency > 10000000l) 506 | frequency -= 10000000l; 507 | 508 | if (frequency < 100000l) 509 | frequency = 100000l; 510 | if (frequency > 450000000l) 511 | frequency = 450000000l; 512 | if (tuningSpeed < 0 && frequency < 350000000l && frequency > 150000000l) 513 | frequency = 149999000l; 514 | if (tuningSpeed > 0 && frequency > 150000000l && frequency < 350000000l) 515 | frequency = 350000000l; 516 | takeReading(frequency); 517 | updateDisplay(); 518 | } 519 | } 520 | 521 | 522 | 523 | int menuSelectAntAnalyzer(int btn){ 524 | if (!btn){ 525 | printLine2("Ant. Analyzer \x7E"); 526 | } 527 | else { 528 | mode = MODE_ANTENNA_ANALYZER; 529 | printLine2("Ant. Analyzer! "); 530 | active_delay(500); 531 | printLine2(""); 532 | menuOn = 0; 533 | 534 | //switch off just the tracking source 535 | si5351aOutputOff(SI_CLK0_CONTROL); 536 | takeReading(frequency); 537 | updateDisplay(); 538 | } 539 | } 540 | 541 | int readOpen(unsigned long f){ 542 | int i, r; 543 | 544 | takeReading(f); 545 | delay(100); 546 | r = 0; 547 | for (i = 0; i < 10; i++){ 548 | r += analogRead(DBM_READING)/5; 549 | delay(50); 550 | } 551 | sprintf(b, "%ld: %d ", f, r/10); 552 | printLine2(b); 553 | delay(1000); 554 | 555 | return r/10; 556 | } 557 | 558 | int menuCalibrate2(int btn){ 559 | if (!btn){ 560 | printLine2("Calibrate SWR \x7E"); 561 | } 562 | else { 563 | printLine1("Disconnect ant &"); 564 | printLine2("Click to Start.."); 565 | 566 | //wait for the button to be raised up 567 | while(btnDown()) 568 | active_delay(50); 569 | active_delay(50); //debounce 570 | 571 | //wait for a button down 572 | while(!btnDown()) 573 | active_delay(50); 574 | 575 | printLine1("Calibrating....."); 576 | 577 | int i, r; 578 | mode = MODE_ANTENNA_ANALYZER; 579 | printLine2(""); 580 | delay(100); 581 | r = readOpen(20000000l); 582 | EEPROM.put(OPEN_HF, r); 583 | r = readOpen(140000000l); 584 | EEPROM.put(OPEN_VHF, r); 585 | r = readOpen(440000000l); EEPROM.put(OPEN_UHF, r); 586 | 587 | menuOn = 0; 588 | 589 | printLine1("Calibrating....."); 590 | printLine2("Done! "); 591 | delay(1000); 592 | 593 | //switch off just the tracking source 594 | si5351aOutputOff(SI_CLK0_CONTROL); 595 | takeReading(frequency); 596 | updateDisplay(); 597 | } 598 | } 599 | 600 | 601 | 602 | int menuSelectMeasurementRx(int btn){ 603 | if (!btn){ 604 | printLine2("Measurement RX \x7E"); 605 | } 606 | else { 607 | mode = MODE_MEASUREMENT_RX; 608 | printLine2("Measurement RX!"); 609 | active_delay(500); 610 | printLine2(""); 611 | menuOn = 0; 612 | 613 | //only allow the local oscillator to work 614 | si5351aOutputOff(SI_CLK0_CONTROL); 615 | si5351aOutputOff(SI_CLK1_CONTROL); 616 | takeReading(frequency); 617 | updateDisplay(); 618 | } 619 | } 620 | 621 | int menuSelectNetworkAnalyzer(int btn){ 622 | if (!btn){ 623 | printLine2("SNA \x7E"); 624 | } 625 | else { 626 | mode = MODE_NETWORK_ANALYZER; 627 | printLine2("SNA! "); 628 | active_delay(500); 629 | printLine2(""); 630 | menuOn = 0; 631 | 632 | //switch off the clock2 that drives the return loss bridge 633 | si5351aOutputOff(SI_CLK1_CONTROL); 634 | takeReading(frequency); 635 | updateDisplay(); 636 | } 637 | } 638 | 639 | void menuExit(int btn){ 640 | 641 | if (!btn){ 642 | printLine2("Exit Menu \x7E"); 643 | } 644 | else{ 645 | printLine2("Exiting..."); 646 | active_delay(500); 647 | printLine2(""); 648 | updateDisplay(); 649 | menuOn = 0; 650 | } 651 | } 652 | 653 | void doMenu(){ 654 | int select=0, i,btnState; 655 | 656 | //wait for the button to be raised up 657 | while(btnDown()) 658 | active_delay(50); 659 | active_delay(50); //debounce 660 | 661 | menuOn = 2; 662 | 663 | while (menuOn){ 664 | i = enc_read(); 665 | btnState = btnDown(); 666 | 667 | checkTimeout(); 668 | 669 | if (i != 0) 670 | resetTimer(); 671 | 672 | if (select + i < 60) 673 | select += i; 674 | 675 | if (i < 0 && select - i >= 0) 676 | select += i; //caught ya, i is already -ve here, so you add it 677 | 678 | if (select < 10) 679 | menuBand(btnState); 680 | else if (select < 20) 681 | menuSelectAntAnalyzer(btnState); 682 | else if (select < 30) 683 | menuSelectMeasurementRx(btnState); 684 | else if (select < 40) 685 | menuSelectNetworkAnalyzer(btnState); 686 | else if (select < 50) 687 | menuCalibrate2(btnState); 688 | else 689 | menuExit(btnState); 690 | } 691 | 692 | //debounce the button 693 | while(btnDown()) 694 | active_delay(50); 695 | active_delay(50); 696 | } 697 | 698 | void checkButton(){ 699 | int i, t1, t2, knob, new_knob; 700 | 701 | //only if the button is pressed 702 | if (!btnDown()) 703 | return; 704 | active_delay(50); 705 | if (!btnDown()) //debounce 706 | return; 707 | 708 | doMenu(); 709 | //wait for the button to go up again 710 | while(btnDown()) 711 | active_delay(10); 712 | active_delay(50);//debounce 713 | } 714 | 715 | int openReading(unsigned long f){ 716 | if (f < 60000000l) 717 | return openHF; 718 | else if (f < 150000000l) 719 | return openVHF; 720 | else 721 | return openUHF; 722 | } 723 | 724 | void readDetector(unsigned long f){ 725 | int i = openReading(f) - analogRead(DBM_READING)/5; 726 | sprintf(c, "d%d\n", i); 727 | Serial.write(c); 728 | } 729 | 730 | void doSweep(){ 731 | unsigned long x, stepSize; 732 | int reading, vswr_reading; 733 | 734 | stepSize = (toFrequency - fromFrequency) / 300; 735 | Serial.write("begin\n"); 736 | for (x = fromFrequency; x < toFrequency; x = x + stepSize){ 737 | takeReading(x); 738 | delay(10); 739 | reading = openReading(x) - analogRead(DBM_READING)/5; 740 | if (mode == MODE_ANTENNA_ANALYZER){ 741 | if (reading < 0) 742 | reading = 0; 743 | vswr_reading = pgm_read_word_near(vswr + reading); 744 | sprintf (c, "r:%ld:%d:%d\n", x, reading, vswr_reading); 745 | }else 746 | sprintf(c, "r:%ld:%d\n", x, analogRead(DBM_READING)/5 + dbmOffset); 747 | Serial.write(c); 748 | } 749 | Serial.write("end\n"); 750 | } 751 | 752 | char *readNumber(char *p, unsigned long *number){ 753 | *number = 0; 754 | 755 | sprintf(c, "#%s", p); 756 | while (*p){ 757 | char c = *p; 758 | if ('0' <= c && c <= '9') 759 | *number = (*number * 10) + c - '0'; 760 | else 761 | break; 762 | p++; 763 | } 764 | return p; 765 | } 766 | 767 | char *skipWhitespace(char *p){ 768 | while (*p && (*p == ' ' || *p == ',')) 769 | p++; 770 | return p; 771 | } 772 | 773 | /* command 'h' */ 774 | void sendStatus(){ 775 | Serial.write("helo v1\n"); 776 | sprintf(c, "from %ld\n", fromFrequency); 777 | Serial.write(c); 778 | 779 | sprintf(c, "to %ld\n", toFrequency); 780 | Serial.write(c); 781 | 782 | sprintf(c, "mode %ld\n", mode); 783 | Serial.write(c); 784 | 785 | } 786 | 787 | void parseCommand(char *line){ 788 | unsigned long param = 0; 789 | char *p = line; 790 | char command; 791 | 792 | while (*p){ 793 | p = skipWhitespace(p); 794 | command = *p++; 795 | 796 | switch (command){ 797 | case 'f' : //from - start frequency 798 | p = readNumber(p, &fromFrequency); 799 | takeReading(fromFrequency); 800 | break; 801 | case 'm': 802 | p = readNumber(p, &mode); 803 | updateDisplay(); 804 | break; 805 | case 't': 806 | p = readNumber(p, &toFrequency); 807 | break; 808 | case 'v': 809 | sendStatus(); 810 | break; 811 | case 'g': 812 | doSweep(); 813 | break; 814 | case 'r': 815 | readDetector(frequency); 816 | break; 817 | case 'i': /* identifies itself */ 818 | Serial.write("iAntuino 1.1\n"); 819 | break; 820 | } 821 | } /* end of the while loop */ 822 | } 823 | 824 | void acceptCommand(){ 825 | int inbyte = 0; 826 | inbyte = Serial.read(); 827 | 828 | if (inbyte == '\n'){ 829 | parseCommand(serial_in); 830 | serial_in_count = 0; 831 | return; 832 | } 833 | 834 | if (serial_in_count < sizeof(serial_in)){ 835 | serial_in[serial_in_count] = inbyte; 836 | serial_in_count++; 837 | serial_in[serial_in_count] = 0; 838 | } 839 | } 840 | 841 | int prev = 0; 842 | void loop() { 843 | 844 | doTuning2(); 845 | checkButton(); 846 | 847 | checkTimeout(); 848 | if (Serial.available()>0) 849 | acceptCommand(); 850 | 851 | delay(50); 852 | int r = analogRead(DBM_READING); 853 | if (r != prev){ 854 | takeReading(frequency); 855 | updateDisplay(); 856 | prev = r; 857 | } 858 | } 859 | 860 | 861 | -------------------------------------------------------------------------------- /si5351.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SI_CLK0_CONTROL 16 // Register definitions 4 | #define SI_CLK1_CONTROL 17 5 | #define SI_CLK2_CONTROL 18 6 | #define SI_SYNTH_PLL_A 26 7 | #define SI_SYNTH_PLL_B 34 8 | #define SI_SYNTH_MS_0 42 9 | #define SI_SYNTH_MS_1 50 10 | #define SI_SYNTH_MS_2 58 11 | #define SI_PLL_RESET 177 12 | 13 | #define SI_R_DIV_1 0b00000000 // R-division ratio definitions 14 | #define SI_R_DIV_2 0b00010000 15 | #define SI_R_DIV_4 0b00100000 16 | #define SI_R_DIV_8 0b00110000 17 | #define SI_R_DIV_16 0b01000000 18 | #define SI_R_DIV_32 0b01010000 19 | #define SI_R_DIV_64 0b01100000 20 | #define SI_R_DIV_128 0b01110000 21 | 22 | #define SI_CLK_SRC_PLL_A 0b00000000 23 | #define SI_CLK_SRC_PLL_B 0b00100000 24 | 25 | void i2cSendRegister (byte regist, byte value){ // Writes "byte" into "regist" of Si5351a via I2C 26 | Wire.beginTransmission(96); // Starts transmission as master to slave 96, which is the 27 | // I2C address of the Si5351a (see Si5351a datasheet) 28 | Wire.write(regist); // Writes a byte containing the number of the register 29 | Wire.write(value); // Writes a byte containing the value to be written in the register 30 | Wire.endTransmission(); // Sends the data and ends the transmission 31 | } 32 | 33 | void si5351aOutputOff(uint8_t clk) 34 | { 35 | //i2c_init(); 36 | 37 | i2cSendRegister(clk, 0x80); // Refer to SiLabs AN619 to see bit values - 0x80 turns off the output stage 38 | 39 | //i2c_exit(); 40 | } 41 | 42 | 43 | void setupPLL(uint8_t pll, uint8_t mult, uint32_t num, uint32_t denom) 44 | { 45 | uint32_t P1; // PLL config register P1 46 | uint32_t P2; // PLL config register P2 47 | uint32_t P3; // PLL config register P3 48 | 49 | P1 = (uint32_t)(128 * ((float)num / (float)denom)); 50 | P1 = (uint32_t)(128 * (uint32_t)(mult) + P1 - 512); 51 | P2 = (uint32_t)(128 * ((float)num / (float)denom)); 52 | P2 = (uint32_t)(128 * num - denom * P2); 53 | P3 = denom; 54 | 55 | i2cSendRegister(pll + 0, (P3 & 0x0000FF00) >> 8); 56 | i2cSendRegister(pll + 1, (P3 & 0x000000FF)); 57 | i2cSendRegister(pll + 2, (P1 & 0x00030000) >> 16); 58 | i2cSendRegister(pll + 3, (P1 & 0x0000FF00) >> 8); 59 | i2cSendRegister(pll + 4, (P1 & 0x000000FF)); 60 | i2cSendRegister(pll + 5, ((P3 & 0x000F0000) >> 12) | ((P2 & 0x000F0000) >> 16)); 61 | i2cSendRegister(pll + 6, (P2 & 0x0000FF00) >> 8); 62 | i2cSendRegister(pll + 7, (P2 & 0x000000FF)); 63 | } 64 | 65 | // 66 | // Set up MultiSynth with integer divider and R divider 67 | // R divider is the bit value which is OR'ed onto the appropriate register, it is a #define in si5351a.h 68 | // 69 | void setupMultisynth(uint8_t synth, uint32_t divider, uint8_t rDiv) 70 | { 71 | uint32_t P1; // Synth config register P1 72 | uint32_t P2; // Synth config register P2 73 | uint32_t P3; // Synth config register P3 74 | 75 | P1 = 128 * divider - 512; 76 | P2 = 0; // P2 = 0, P3 = 1 forces an integer value for the divider 77 | P3 = 1; 78 | 79 | i2cSendRegister(synth + 0, (P3 & 0x0000FF00) >> 8); 80 | i2cSendRegister(synth + 1, (P3 & 0x000000FF)); 81 | i2cSendRegister(synth + 2, ((P1 & 0x00030000) >> 16) | rDiv); 82 | i2cSendRegister(synth + 3, (P1 & 0x0000FF00) >> 8); 83 | i2cSendRegister(synth + 4, (P1 & 0x000000FF)); 84 | i2cSendRegister(synth + 5, ((P3 & 0x000F0000) >> 12) | ((P2 & 0x000F0000) >> 16)); 85 | i2cSendRegister(synth + 6, (P2 & 0x0000FF00) >> 8); 86 | i2cSendRegister(synth + 7, (P2 & 0x000000FF)); 87 | } 88 | 89 | // This example sets up PLL A 90 | // and MultiSynth 0 91 | // and produces the output on CLK0 92 | // 93 | void si5351aSetFrequency(uint32_t frequency) 94 | { 95 | uint32_t pllFreq; 96 | uint32_t xtalFreq = xtal_freq_calibrated; 97 | uint32_t l; 98 | float f; 99 | uint8_t mult; 100 | uint32_t num; 101 | uint32_t denom; 102 | uint32_t divider; 103 | 104 | divider = 900000000 / frequency;// Calculate the division ratio. 900,000,000 is the maximum internal 105 | // PLL frequency: 900MHz 106 | if (divider % 2) divider--; // Ensure an even integer division ratio 107 | 108 | pllFreq = divider * frequency; // Calculate the pllFrequency: the divider * desired output frequency 109 | 110 | mult = pllFreq / xtalFreq; // Determine the multiplier to get to the required pllFrequency 111 | l = pllFreq % xtalFreq; // It has three parts: 112 | f = l; // mult is an integer that must be in the range 15..90 113 | f *= 1048575; // num and denom are the fractional parts, the numerator and denominator 114 | f /= xtalFreq; // each is 20 bits (range 0..1048575) 115 | num = f; // the actual multiplier is mult + num / denom 116 | denom = 1048575; // For simplicity we set the denominator to the maximum 1048575 117 | 118 | // Set up PLL A with the calculated multiplication ratio 119 | setupPLL(SI_SYNTH_PLL_A, mult, num, denom); 120 | // Set up MultiSynth divider 0, with the calculated divider. 121 | // The final R division stage can divide by a power of two, from 1..128. 122 | // reprented by constants SI_R_DIV1 to SI_R_DIV128 (see si5351a.h header file) 123 | // If you want to output frequencies below 1MHz, you have to use the 124 | // final R division stage 125 | setupMultisynth(SI_SYNTH_MS_0, divider, SI_R_DIV_1); 126 | // Reset the PLL. This causes a glitch in the output. For small changes to 127 | // the parameters, you don't need to reset the PLL, and there is no glitch 128 | i2cSendRegister(SI_PLL_RESET, 0xA0); 129 | // Finally switch on the CLK0 output (0x4F) 130 | // and set the MultiSynth0 input to be PLL A 131 | i2cSendRegister(SI_CLK1_CONTROL, 0x4F | SI_CLK_SRC_PLL_A); 132 | } 133 | 134 | void si5351aSetFrequency_clk0(uint32_t frequency) 135 | { 136 | uint32_t pllFreq; 137 | uint32_t xtalFreq = xtal_freq_calibrated; 138 | uint32_t l; 139 | float f; 140 | uint8_t mult; 141 | uint32_t num; 142 | uint32_t denom; 143 | uint32_t divider; 144 | 145 | divider = 900000000 / frequency;// Calculate the division ratio. 900,000,000 is the maximum internal 146 | // PLL frequency: 900MHz 147 | if (divider % 2) divider--; // Ensure an even integer division ratio 148 | 149 | pllFreq = divider * frequency; // Calculate the pllFrequency: the divider * desired output frequency 150 | //sprintf(buff,"pllFreq: %ld", (long)pllFreq); 151 | //Serial.println(buff); 152 | 153 | mult = (pllFreq / xtalFreq); // Determine the multiplier to get to the required pllFrequency 154 | l = pllFreq % xtalFreq; // It has three parts: 155 | f = l; // mult is an integer that must be in the range 15..90 156 | f *= 1048575; // num and denom are the fractional parts, the numerator and denominator 157 | f /= xtalFreq; // each is 20 bits (range 0..1048575) 158 | num = f; // the actual multiplier is mult + num / denom 159 | denom = 1048575; // For simplicity we set the denominator to the maximum 1048575 160 | 161 | // Set up PLL B with the calculated multiplication ratio 162 | setupPLL(SI_SYNTH_PLL_A, mult, num, denom); 163 | // Set up MultiSynth divider 0, with the calculated divider. 164 | // The final R division stage can divide by a power of two, from 1..128. 165 | // reprented by constants SI_R_DIV1 to SI_R_DIV128 (see si5351a.h header file) 166 | // If you want to output frequencies below 1MHz, you have to use the 167 | // final R division stage 168 | setupMultisynth(SI_SYNTH_MS_0, divider, SI_R_DIV_1); 169 | // Reset the PLL. This causes a glitch in the output. For small changes to 170 | // the parameters, you don't need to reset the PLL, and there is no glitch 171 | i2cSendRegister(SI_PLL_RESET, 0xA0); 172 | // Finally switch on the CLK2 output (0x4F) 173 | // and set the MultiSynth0 input to be PLL A 174 | i2cSendRegister(SI_CLK0_CONTROL, 0x4F | SI_CLK_SRC_PLL_A); 175 | } 176 | 177 | void si5351aSetFrequency_clk1(uint32_t frequency) 178 | { 179 | uint32_t pllFreq; 180 | uint32_t xtalFreq = xtal_freq_calibrated; 181 | uint32_t l; 182 | float f; 183 | uint8_t mult; 184 | uint32_t num; 185 | uint32_t denom; 186 | uint32_t divider; 187 | 188 | divider = 900000000 / frequency;// Calculate the division ratio. 900,000,000 is the maximum internal 189 | // PLL frequency: 900MHz 190 | if (divider % 2) divider--; // Ensure an even integer division ratio 191 | 192 | pllFreq = divider * frequency; // Calculate the pllFrequency: the divider * desired output frequency 193 | 194 | mult = (pllFreq / xtalFreq); // Determine the multiplier to get to the required pllFrequency 195 | l = pllFreq % xtalFreq; // It has three parts: 196 | f = l; // mult is an integer that must be in the range 15..90 197 | f *= 1048575; // num and denom are the fractional parts, the numerator and denominator 198 | f /= xtalFreq; // each is 20 bits (range 0..1048575) 199 | num = f; // the actual multiplier is mult + num / denom 200 | denom = 1048575; // For simplicity we set the denominator to the maximum 1048575 201 | 202 | // Set up PLL A with the calculated multiplication ratio 203 | setupPLL(SI_SYNTH_PLL_A, mult, num, denom); 204 | // Set up MultiSynth divider 0, with the calculated divider. 205 | // The final R division stage can divide by a power of two, from 1..128. 206 | // reprented by constants SI_R_DIV1 to SI_R_DIV128 (see si5351a.h header file) 207 | // If you want to output frequencies below 1MHz, you have to use the 208 | // final R division stage 209 | setupMultisynth(SI_SYNTH_MS_1, divider, SI_R_DIV_1); 210 | // Reset the PLL. This causes a glitch in the output. For small changes to 211 | // the parameters, you don't need to reset the PLL, and there is no glitch 212 | i2cSendRegister(SI_PLL_RESET, 0xA0); 213 | // Finally switch on the CLK0 output (0x4F) 214 | // and set the MultiSynth0 input to be PLL A 215 | i2cSendRegister(SI_CLK1_CONTROL, 0x4F | SI_CLK_SRC_PLL_A); 216 | } 217 | 218 | 219 | void si5351aSetFrequency_clk2(uint32_t frequency) 220 | { 221 | uint32_t pllFreq; 222 | uint32_t xtalFreq = xtal_freq_calibrated; 223 | uint32_t l; 224 | float f; 225 | uint8_t mult; 226 | uint32_t num; 227 | uint32_t denom; 228 | uint32_t divider; 229 | 230 | divider = 900000000 / frequency;// Calculate the division ratio. 900,000,000 is the maximum internal 231 | // PLL frequency: 900MHz 232 | if (divider % 2) divider--; // Ensure an even integer division ratio 233 | 234 | pllFreq = divider * frequency; // Calculate the pllFrequency: the divider * desired output frequency 235 | //sprintf(buff,"pllFreq: %ld", (long)pllFreq); 236 | //Serial.println(buff); 237 | 238 | mult = (pllFreq / xtalFreq); // Determine the multiplier to get to the required pllFrequency 239 | l = pllFreq % xtalFreq; // It has three parts: 240 | f = l; // mult is an integer that must be in the range 15..90 241 | f *= 1048575; // num and denom are the fractional parts, the numerator and denominator 242 | f /= xtalFreq; // each is 20 bits (range 0..1048575) 243 | num = f; // the actual multiplier is mult + num / denom 244 | denom = 1048575; // For simplicity we set the denominator to the maximum 1048575 245 | 246 | // Set up PLL B with the calculated multiplication ratio 247 | setupPLL(SI_SYNTH_PLL_B, mult, num, denom); 248 | // Set up MultiSynth divider 0, with the calculated divider. 249 | // The final R division stage can divide by a power of two, from 1..128. 250 | // reprented by constants SI_R_DIV1 to SI_R_DIV128 (see si5351a.h header file) 251 | // If you want to output frequencies below 1MHz, you have to use the 252 | // final R division stage 253 | setupMultisynth(SI_SYNTH_MS_2, divider, SI_R_DIV_1); 254 | // Reset the PLL. This causes a glitch in the output. For small changes to 255 | // the parameters, you don't need to reset the PLL, and there is no glitch 256 | i2cSendRegister(SI_PLL_RESET, 0xA0); 257 | // Finally switch on the CLK2 output (0x4F) 258 | // and set the MultiSynth0 input to be PLL A 259 | i2cSendRegister(SI_CLK2_CONTROL, 0x4F | SI_CLK_SRC_PLL_B); 260 | } 261 | 262 | --------------------------------------------------------------------------------