Cos'è ElectroYou | Login Iscriviti

ElectroYou - la comunità dei professionisti del mondo elettrico

Libreria rotary con problemi su arduino mega

Progetti, interfacciamento, discussioni varie su questa piattaforma.

Moderatori: Foto UtenteWALTERmwp, Foto Utentexyz

0
voti

[1] Libreria rotary con problemi su arduino mega

Messaggioda Foto Utenteaduri » 12 feb 2023, 22:36

Buongiorno a tutti,
devo usare questa libreria per encoder che uso con successo su arduino 1 con questo sw (l'originale e' qui: www.ad7c.com) per gestire un menu di comando per un dds, lo vorrei usare con arduino mega ma non va.
Ho subito pensato ai pins 2 e 3 di interrupt HW e guardando il datasheet del mega ho provato i pins 18 e 19, poi A11 e A12 che corrisponto ai PCINT19 e PCINT20 (definiti in libreria) ma continua a non andare.

Avete qualche idea?

Allego codice:

Codice: Seleziona tutto
/*
Main code by Richard Visokey AD7C - www.ad7c.com
Revision 2.0 - November 6th, 2013
used and midified by IZ1QXJ Antonio
*/

// Include the library code
#include <LiquidCrystal_I2C.h>
#include <rotary.h>
#include <EEPROM.h>

//Setup some items

#define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
LiquidCrystal_I2C lcd(0x3f, 16, 2);//0x3f o 0x27

int_fast32_t rx=7150000; // Base (starting) frequency of VFO.  This only loads once.  To force load again see ForceFreq variable below.
int_fast32_t rx2=1; // variable to hold the updated frequency
int_fast32_t increment = 10; // starting VFO update increment in Hz.!!!!!!!!!
int_fast32_t iffreq = 4192000; // Intermedite Frequency - Amount to subtract (-) from base frequency. ********************************************
int buttonstate = 0;
int buttonstate2 = 0;
int GoIF = 1;
String hertz = "10 Hz";
int  hertzPosition = 5;
byte ones,tens,hundreds,thousands,tenthousands,hundredthousands,millions ;  //Placeholders
String freq; // string to hold the frequency
int_fast32_t timepassed = millis(); // int to hold the arduino miilis since startup
int memstatus = 1;  // value to notify if memory is current or old. 0=old, 1=current.

int f0=4;int f1=5; int f2=6; int f4=7; // BCD 100Hz
int ForceFreq = 1;  // Change this to 0 after you upload and run a working sketch to activate the EEPROM memory.  YOU MUST PUT THIS BACK TO 0 AND UPLOAD THE SKETCH AGAIN AFTER STARTING FREQUENCY IS SET!

// Rotary(Encoder Pin 1, Encoder Pin 2, Button Pin)
Rotary r = Rotary(2, 3, A0);

void setup() {
  pinMode(A0,INPUT); // Connect to a button that goes to GND on push
// pinMode(A5,INPUT); // IF sense **********************************************
  digitalWrite(A0,HIGH);
  //digitalWrite(A5,HIGH);
  lcd.begin();
   lcd.backlight(); //accende la retroilluminazione
  PCICR |= (1 << PCIE2);
  PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
  sei();
  Serial.begin(9600);
lcd.setCursor(hertzPosition,1);   
  lcd.print(hertz);

  // Load the stored frequency 
  if (ForceFreq == 0) {
    freq = String(EEPROM.read(0))+String(EEPROM.read(1))+String(EEPROM.read(2))+String(EEPROM.read(3))+String(EEPROM.read(4))+String(EEPROM.read(5))+String(EEPROM.read(6));
    rx = freq.toInt(); 
  }
}

void loop() {   
  // Update the display and frequency if the new Freq NEQ the old Freq 
  if (rx != rx2){   
        showFreq();
//        sendFrequency(rx);

  Serial.println(rx);
delay(500);
        rx2 = rx;       

      }
     

       
  // Rotate through the rate of tuning as you hold down the button
  buttonstate = digitalRead(A0);
  if(buttonstate == LOW) {
        setincrement();       
    };
   

    // Write the frequency to memory if not stored and 2 seconds have passed since the last frequency change.
    if(memstatus == 0){   
      if(timepassed+2000 < millis()){
        storeMEM();
        }
      } 
   
}
 
// Interrupt routine to catch the rotary encoder
ISR(PCINT2_vect) {
  unsigned char result = r.process();
  if (result) {   
    if (result == DIR_CW){rx=rx+increment;}
    else {rx=rx-increment;};       
      if (rx >=30000000){rx=rx2;}; // UPPER VFO LIMIT
      if (rx <=1000000){rx=rx2;}; // LOWER VFO LIMIT
  }
}

void setincrement(){
  if(increment == 10){increment = 50; hertz = "50 Hz"; hertzPosition=5;}
  else if (increment == 50){increment = 100;  hertz = "100 Hz"; hertzPosition=4;}
  else if (increment == 100){increment = 500; hertz="500 Hz"; hertzPosition=4;}
  else if (increment == 500){increment = 1000; hertz="1 kHz"; hertzPosition=6;}
  else if (increment == 1000){increment = 2500; hertz="2.5 kHz"; hertzPosition=4;}
  else if (increment == 2500){increment = 5000; hertz="5 kHz"; hertzPosition=6;}
  else if (increment == 5000){increment = 10000; hertz="10 kHz"; hertzPosition=5;}
  else if (increment == 10000){increment = 100000; hertz="100 kHz"; hertzPosition=4;}
  else if (increment == 100000){increment = 1000000; hertz="1 MHz"; hertzPosition=6;} 
  else{increment = 10; hertz = "10 Hz"; hertzPosition=5;}; 
   lcd.setCursor(0,1);
   lcd.print("                ");
   lcd.setCursor(hertzPosition,1);
   lcd.print(hertz);
   delay(250); // Adjust this delay to speed up/slow down the button menu scroll speed.
};

void showFreq(){
    millions = int(rx/1000000);
    hundredthousands = ((rx/100000)%10);
    tenthousands = ((rx/10000)%10);
    thousands = ((rx/1000)%10);
    hundreds = ((rx/100)%10);
    tens = ((rx/10)%10);
    ones = ((rx/1)%10);
    lcd.setCursor(0,0);
    lcd.print("                ");
   if (millions > 9){lcd.setCursor(1,0);}
   else{lcd.setCursor(2,0);}
    lcd.print(millions);
    lcd.print(".");
    lcd.print(hundredthousands);
    lcd.print(tenthousands);
    lcd.print(thousands);
    lcd.print(".");
    lcd.print(hundreds);
    lcd.print(tens);
    lcd.print(ones);
    lcd.print(" MHz  ");
    timepassed = millis();
    memstatus = 0; // Trigger memory write
};

void storeMEM(){
  //Write each frequency section to a EPROM slot.  Yes, it's cheating but it works!
   EEPROM.write(0,millions);
   EEPROM.write(1,hundredthousands);
   EEPROM.write(2,tenthousands);
   EEPROM.write(3,thousands);
   EEPROM.write(4,hundreds);       
   EEPROM.write(5,tens);
   EEPROM.write(6,ones);   
   memstatus = 1;  // Let program know memory has been written
};



la libreria e' qui:
https://github.com/brianlow/Rotary

ciao
Antonio
Avatar utente
Foto Utenteaduri
435 1 5 11
Expert
Expert
 
Messaggi: 937
Iscritto il: 29 giu 2005, 23:13
Località: Genova

0
voti

[2] Re: Libreria rotary con problemi su arduino mega

Messaggioda Foto Utenteaduri » 16 feb 2023, 12:39

Buongiorno a tutti,
ho fatto un passo avanti scoprendo che con gli esempi della libreria l'encoder con l'arduino mega lavora esattamente come l'arduino 1, il problema e' sul settaggio dei registri degli interrupt e pins relativi in uso.
Codice: Seleziona tutto
PCICR |= (1 << PCIE2);
  PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
  sei();



devo studiarmi le differenze del mega vs UNO.

Se qualcuno ci e' gia' passato e vuole darmi due dritte e' ben accetto.

saluti
Antonio
Avatar utente
Foto Utenteaduri
435 1 5 11
Expert
Expert
 
Messaggi: 937
Iscritto il: 29 giu 2005, 23:13
Località: Genova

0
voti

[3] Re: Libreria rotary con problemi su arduino mega

Messaggioda Foto Utentewimatech » 15 mar 2023, 12:50

perché non usi 2 e 3 anche sul mega?
Anche il mega ha gli interrupt su quei pin

Giusto per farti un idea prima di andare a toccare i registri hai provato uno sketch di test semplicemente dichiarando l'encoder come da esempi e stampando a monitor e eventualmente poi agire in profondità sui registri?

Codice: Seleziona tutto
/*
* Example using the Rotary library, dumping integers to the serial
* port. The integers increment or decrement depending on the direction
* of rotation.
*
* This example uses interrupts rather than polling.
*/

#include <Rotary.h>

// Rotary encoder is wired with the common to ground and the two
// outputs to pins 2 and 3.
Rotary rotary = Rotary(2, 3);

// Counter that will be incremented or decremented by rotation.
int counter = 0;

void setup() {
  Serial.begin(57600);
  attachInterrupt(0, rotate, CHANGE);
  attachInterrupt(1, rotate, CHANGE);
}

void loop() {
}

// rotate is called anytime the rotary inputs change state.
void rotate() {
  unsigned char result = rotary.process();
  if (result == DIR_CW) {
    counter++;
    Serial.println(counter);
  } else if (result == DIR_CCW) {
    counter--;
    Serial.println(counter);
  }
}
Avatar utente
Foto Utentewimatech
50 2 7
Frequentatore
Frequentatore
 
Messaggi: 240
Iscritto il: 24 feb 2022, 9:34


Torna a Arduino

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite