Categories
Statistics
Flag Counter
Since 08.08.2014
Counts only, if "DNT = disabled".

Your IP is 44.211.31.134
ec2-44-211-31-134.compute-1.
Info
Valid HTML 4.01 Transitional Creative Commons Lizenzvertrag
rss
เราจะทำแบบวิศวกรผู้ยิ่งใหญ่
We love the King
24. July 2024
Your valuable opinion :
4 stars

Avg. 4.14 from 28 votes.



Arduino-Code-Snippets.php    19771 Bytes    04-06-2024 19:36:27


Arduino Genuino Code Snippets


My personal "cloud" storage



As the title suggests this is a collection of self written code to be copy-pasted into future projects. It is likely possible that smarter solutions exist elsewhere ...






#1 HEXDUMP OF AN EEPROM



Double click on code to select ...


/* ////////////////////////////////////////////////////////////////// 
HEXDUMP OF AN EEPROM
https://www.changpuak.ch/electronics/Arduino-Code-Snippets.php
////////////////////////////////////////////////////////////////// */

// Import library for I2C / TWI devices
#include <Wire.h>

// Define eeprom address
const byte I2CADRESSE = 0x50 ;

// In Setup add
Wire.begin();
Serial.begin(9600);  // because data is send to console

// And here comes the function ...
void ReadEeprom()
{
byte rdata ;
  for (int address = 0; address < 512; address+=8) 
  {  
    if ( address < 16 ) Serial.print("0");
    if ( address < 0x100 ) Serial.print("0");
    Serial.print(address,HEX);  
    Serial.print("\t"); 
    // One row, HEX
    for (int add = 0; add < 8; add++) 
    { 
    Wire.beginTransmission(I2CADRESSE); 
    Wire.write((address+add) >> 8);   // MSB
    Wire.write((address+add) & 0xFF); // LSB    
    Wire.endTransmission();  
    Wire.requestFrom(I2CADRESSE,1);
    if (Wire.available()) rdata = Wire.read();
    if (rdata<16) Serial.print("0");  
    Serial.print(rdata, HEX);
    Serial.print(" ");
    }
    Serial.print("\t");
    // One row, ASCII
    for (int add = 0; add < 8; add++) 
    { 
    Wire.beginTransmission(I2CADRESSE);  
    Wire.write((address+add) >> 8);   // MSB
    Wire.write((address+add) & 0xFF); // LSB    
    Wire.endTransmission();  
    Wire.requestFrom(I2CADRESSE,1);
    if (Wire.available()) rdata = Wire.read();
    if (rdata < 32) 
    {
    Serial.print(".");
    }
    else if (rdata > 126) 
       {
       Serial.print(".");
       }
       else
          {
          Serial.print(char(rdata));
          }
    Serial.print(" ");
    }
    Serial.println();  
  } 
  Serial.print("\n");  

Serial.println("\nO.K.\n\n");
}

// ////////////////////////////////////////////////////////////////// 
// END OF FILE.
// ////////////////////////////////////////////////////////////////// 




#2 SERIAL MENUE / COMMAND PARSER



Double click on code to select ...


// ///////////////////////////////////////////////////////////// 
//
//  A MENUE FOR THE SERIAL COMMUNICATION
//  BY ALEXANDER C. FRANK, (C) 2017
//
// ///////////////////////////////////////////////////////////// 

char Buffy[20] ;                 // holds User Input from Serial
byte byteRead ;

// ///////////////////////////////////////////////////////////// 
// Clears the Input Buffer
// ///////////////////////////////////////////////////////////// 
void InitBuffy()
{
  for (int i=0; i<20; i++)
  Buffy[i] = 32 ;   // SPACE
}

// ///////////////////////////////////////////////////////////// 
// Read User Input from Serial
// ///////////////////////////////////////////////////////////// 
void ReadUserInput()
{
  int pointer = 0 ;
  boolean ende = false ;
  while ( ( Serial.available() ) || ( ende == false ) ) 
      {
      byteRead = Serial.read();
      if ( byteRead == 10 ){ ende = true ; }
      if ( byteRead < 255 )		   // YES, ALWAYS TRUE :-)
        { 
        Buffy[pointer] = byteRead ; 
        if ( pointer < 19 ) pointer++ ;
        }
      }
}

// ///////////////////////////////////////////////////////////// 
// Prints a list of Options
// ///////////////////////////////////////////////////////////// 
void print_options()
{
  Serial.print("ARDUINO CONFIGURATION MENUE\n");
  Serial.print("---------------------------\n");
  Serial.print("[0] Execute Function Zero\n");
  Serial.print("[1] Execute Function One\n");
  Serial.print("---------------------------\n");
  Serial.print(">>> PRESS KEY ...\n");
}


void setup() 
{
  Serial.begin(9600);
  print_options();
}

void loop()
{
    InitBuffy();
    ReadUserInput();
    int user_command = Buffy[0] ;               
    
    switch (user_command)
    {
      case 48 : /* 0 */  
        Serial.println("\nFunction '0'.\n"); 
        print_options(); 
        break;
      case 49 : /* 1 */  
        Serial.println("\nFunction '1'.\n"); 
        print_options(); 
        break;
      default:
        Serial.println("\nOOOPS - Invalid Input.\n");
        print_options();
    }
    delay(200);  
}

// ///////////////////////////////////////////////////////////// 
// END OF FILE.
// ///////////////////////////////////////////////////////////// 




#3 PROGRAMMING THE ADF4360-9



Double click on code to select ...


/* ///////////////////////////////////////////////////////////// 
ARDUINO/Genuino (DUE) Test/Demo Sketch for ADF 4360-9
Software Version 2.0, 
11.09.2017, Alexander C. Frank
NOTE THAT THE FREQUENCY RANGE IS DETERMINED BY THE INDUCTORS !!!
(We used 270 nH, suiteable for a frequency around 100 MHz)
LOOPFILTER:
C=2.7nF
R=3k,C=18nF
R=100R
C=18nF
Kvco = 2MHz/V
Phase margin = 45 degree
Icp = 0.93 mA
Filter BW = 7.58 kHz
If the tuning voltage is near 0 V your N-value is too big
If the tuning voltage is near 3.3 V your N-value is too small
//////////////////////////////////////////////////////////////*/

const int crystal = 100 ;         // MHz
const int phasecomp = 1 ;         // MHz
unsigned int frequency = 100;    // MHz
const int LE_AD4360 = A0 ; 
const int DAT_AD4360 = A2 ;
const int CLK_AD4360 = A1 ;
// CONTROL LATCH
const int power_down = B00 ;      // CE is 1 >> NORMAL OPERATION
const int chargepump1 = B010;     // 0.93 mA
const int chargepump2 = B010;
const int output_power = B11 ;          // MAXIMUM :-)
const int mute_till_lock = B0 ;         // DISABLED
const int charge_pump_out = B0 ;        // NORMAL
const int phase_detect_polarity = B1 ;  // POSITIVE
const int div_out_control = B110 ;      // MUX = A COUNTER OUTPUT
const int core_power_level = B01 ;      // RECOMMENDED 5mA
// N-COUNTER LATCH
const int charge_pump_used = B1 ;       // USE chargepump2
// R-COUNTER
unsigned int band_select_clock = B11 ;  // DATASHEET RECOMMENDS 8
const int lock_detect_precision = B1 ;  // FIVE CYCLES
const int anti_backlash_pulse_width = B11;  // 3 ns


void setup()
{
   Serial.begin(9600);
   pinMode(LE_AD4360, OUTPUT);
   pinMode(DAT_AD4360, OUTPUT);
   pinMode(CLK_AD4360, OUTPUT);
   digitalWrite(LE_AD4360, LOW);
   digitalWrite(DAT_AD4360, LOW);
   digitalWrite(CLK_AD4360, LOW);
}

void write_r_counter_latch()
{
  unsigned long data = 0x000000;
  unsigned int control_bits = B01 ;
  unsigned long r_counter = crystal / phasecomp;
  if (r_counter < 1) r_counter = 1 ;
  if (r_counter > 16383) r_counter = 16383 ;
  data = (band_select_clock << 20);
  data = data | (lock_detect_precision << 18);
  data = data | (anti_backlash_pulse_width << 16);
  data = data | (r_counter << 2);
  data = data | control_bits ; 
  Serial.print("R:"); Serial.println(data,HEX) ;
  digitalWrite(LE_AD4360, LOW);
  shiftOut(DAT_AD4360, CLK_AD4360, MSBFIRST, (data >> 16) );
  shiftOut(DAT_AD4360, CLK_AD4360, MSBFIRST, (data >> 8) );
  shiftOut(DAT_AD4360, CLK_AD4360, MSBFIRST, (data) );
  digitalWrite(LE_AD4360, HIGH); delay(1);
  digitalWrite(LE_AD4360, LOW);
}

void write_control_latch()
{
  unsigned long data = 0x000000;
  unsigned int control_bits = B00 ;
  data = (power_down << 20);
  data = data | (chargepump2 << 17);
  data = data | (chargepump1 << 14);
  data = data | (output_power << 12);
  data = data | (mute_till_lock << 11 );
  data = data | (charge_pump_used << 10 );
  data = data | (charge_pump_out << 9 ) ;
  data = data | (phase_detect_polarity << 8 );
  data = data | (div_out_control << 5 );
  data = data | (core_power_level << 2 );
  data = data | control_bits ;
  Serial.print("C:"); Serial.println(data,HEX) ;
  digitalWrite(LE_AD4360, LOW);
  shiftOut(DAT_AD4360, CLK_AD4360, MSBFIRST, (data >> 16) );
  shiftOut(DAT_AD4360, CLK_AD4360, MSBFIRST, (data >> 8) );
  shiftOut(DAT_AD4360, CLK_AD4360, MSBFIRST, (data) );
  digitalWrite(LE_AD4360, HIGH); delay(1);
  digitalWrite(LE_AD4360, LOW);
}

void write_n_counter_latch(unsigned int vco_freq, unsigned int div_out)
{
  unsigned long data = 0x000000 ;
  unsigned int control_bits = B10 ;
  unsigned long b_counter = vco_freq / phasecomp;
  if (b_counter < 3) b_counter = 3 ;
  if (b_counter > 8191) b_counter = 8191 ;
  if (div_out < 2) div_out = 2;
  if (div_out > 31) div_out = 31;
  data = (charge_pump_used << 21);
  data = data | (b_counter << 8);
  data = data | (div_out << 2);
  data = data | control_bits ;
  Serial.print("N:"); Serial.println(data,HEX) ;
  digitalWrite(LE_AD4360, LOW);
  shiftOut(DAT_AD4360, CLK_AD4360, MSBFIRST, (data >> 16) );
  shiftOut(DAT_AD4360, CLK_AD4360, MSBFIRST, (data >> 8) );
  shiftOut(DAT_AD4360, CLK_AD4360, MSBFIRST, (data) );
  digitalWrite(LE_AD4360, HIGH); delay(1);
  digitalWrite(LE_AD4360, LOW);
}
  
void loop()
{
  delay(100);
  write_r_counter_latch();
  delay(10);
  write_control_latch();
  delay(10);
  write_n_counter_latch(frequency,25);
  Serial.println("\n\n") ;
  delay(1000);
  
}


// ///////////////////////////////////////////////////////////// 
// END OF FILE.
// ///////////////////////////////////////////////////////////// 



ADF4360-9 Memory Map

MEMORY MAP OF THE ADF4360-9. DRAWING COURTESY OF ANALOG DEVICES.

The correct write sequence is as follows:
1. R Counter Latch
2. Control Latch
3. N Counter Latch




#4 DISPLAY SERIAL MESSAGE ON LCD



Double click on code to select ...


// ///////////////////////////////////////////////////////////// 
// ARDUINO RECEIVES SERIAL COMMANDS AND DISPLAYS THEM
// ON AN LCD (AS PORT MAYST BE BLOCKED BY A PHYTON SCRIPT)
// ///////////////////////////////////////////////////////////// 

#include <LiquidCrystal.h>

const int rs = 9, en = 8, d4 = 6, d5 = 5, d6 = 4, d7 = A5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() 
{
  Serial.begin(9600); 
  
  lcd.begin(16, 2);
  lcd.print("HELLO WORLD");
  delay(5000) ;
}

void loop() 
{
  // when characters arrive over the serial port...
  if (Serial.available()) 
  {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) 
    {
      byte data ;
      data = Serial.read() ;
      // display each ASCII character to the LCD
      if((data >= 32)&&(data <= 176)) lcd.write(data);
    }
  }
}


// ///////////////////////////////////////////////////////////// 
// END OF FILE.
// ///////////////////////////////////////////////////////////// 




#5 ROTARY ENCODER WITH INTERRUPT PINS



Double click on code to select ...


// ///////////////////////////////////////////////////////////// 
// ROTARY ENCODER WITH INTERRUPT PINS
// ISR SETS A FLAG
// ///////////////////////////////////////////////////////////// 

// ROTARY ENCODER
const int RotaryEncoder1 = A0 ;   // PRESSED
const int RotaryEncoder2 = 2 ;
const int RotaryEncoder3 = 3 ;
volatile boolean LEFT = false ;
volatile boolean RIGHT = false ;

// /////////////////////////////////////////////////////////////////////
// S E T U P
// /////////////////////////////////////////////////////////////////////

void setup() 
{
  pinMode(RotaryEncoder1, INPUT_PULLUP);
  pinMode(RotaryEncoder2, INPUT_PULLUP);
  pinMode(RotaryEncoder3, INPUT_PULLUP);
  // YELLOW
  attachInterrupt(digitalPinToInterrupt(RotaryEncoder2), 
			RotaryEncoderISR2, FALLING);		// REMOVE LINE BREAK
  // GREEN
  attachInterrupt(digitalPinToInterrupt(RotaryEncoder3), 
			RotaryEncoderISR3, FALLING);		// REMOVE LINE BREAK
}

// /////////////////////////////////////////////////////////////
// M A I N L O O P
// /////////////////////////////////////////////////////////////

void loop() 
{
  // EVALUATE ROTARY ENCODER
  if (LEFT)
  {
    noInterrupts() ;
    //
	// DO SOMETHING
	//
    LEFT = false ;
    RIGHT = false ;
    interrupts() ;
  }

  if (RIGHT)
  {
    noInterrupts() ;
    //
	// DO SOMETHING
	//
    LEFT = false ;
    RIGHT = false ;
    interrupts() ;
  }
  delay(9) ;
  // EVERYTHING ELSE GOES HERE
  
}

// /////////////////////////////////////////////////////////////
// INTERRUPT SERVICE ROUTINES
// /////////////////////////////////////////////////////////////

void RotaryEncoderISR2()
{
  // YELLOW
  LEFT = false ;
  RIGHT = false ;
  byte autre = digitalRead(RotaryEncoder3) ;
  if (autre > 0) LEFT = true ;
  if (autre < 1) RIGHT = true ;
}

void RotaryEncoderISR3()
{
  // GREEN
  LEFT = false ;
  RIGHT = false ;
  byte autre = digitalRead(RotaryEncoder2) ;
  if (autre > 0) RIGHT = true ;
  if (autre < 1) LEFT = true ;
}


// /////////////////////////////////////////////////////////////
// END OF FILE.
// /////////////////////////////////////////////////////////////




#6 SENDING NOT A MULTIPLE OF 8 BITS, E.G. 21 BITS



Double click on code to select ...


// ///////////////////////////////////////
void Write_ADF_4118(unsigned long PayLoad)
// ///////////////////////////////////////

{
  unsigned long Pointer ; 
  
  digitalWrite(REF_LE, LOW) ;
  for(int i = 21 ; i > 0 ; i--)
  {
    // DATA
    Pointer = 1 << (i-1) ;
    if((Pointer & PayLoad) > 0 ) digitalWrite(REF_DAT, HIGH) ;
    else digitalWrite(REF_DAT, LOW) ;
    // CLOCK
    digitalWrite(REF_CLK, HIGH) ;
    digitalWrite(REF_CLK, LOW) ;
  }
  digitalWrite(REF_LE, HIGH) ;
 }





✈ Share your thoughts



The webmaster does not read these comments regularely. Urgent questions should be send via email. Ads or links to completely uncorrelated things will be removed.


Your Browser says that you allow tracking. Mayst we suggest that you check that DNT thing ?

 
t1 = 6586 d

t2 = 291 ms

★ ★ ★  Copyright © 2006 - 2024 by changpuak.ch  ★ ★ ★

Impressum