element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Get uno sketch to work on Mega2560
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 1 reply
  • Subscribers 401 subscribers
  • Views 431 views
  • Users 0 members are here
  • help
  • arduino_development_environment
  • arduino
Related

Get uno sketch to work on Mega2560

Former Member
Former Member over 11 years ago

Hello, I am new to Arduino but have made a successful digital frequency.

My problem is that it works extremely well in Uno, I then tried the Mega2560 which works with the exception of the rotary encoder, which on the Uno was connected to pins 2 & 3. tried another encoder but same results. I need more I/p and o/p which the 2560 has.

I have not changed the sketch at all. My question is if the interrupt pins on 2 & 3 don't seem to work on the 2560, which pins are compatible?   I attach a section of sketch involved

Many thanks to those who can help me.

// Include the library code

#include <LiquidCrystal.h>

#include <rotary.h>

#include <EEPROM.h>

 

 

//Setup some items

#define W_CLK 8   // Pin 8 - connect to AD9850 module word load clock pin (CLK)

#define FQ_UD 9   // Pin 9 - connect to freq update pin (FQ)

#define DATA 10   // Pin 10 - connect to serial data load pin (DATA)

#define RESET 11  // Pin 11 - connect to reset pin (RST)

#define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }

Rotary r = Rotary(2,3); // sets the pins the rotary encoder uses.  Must be interrupt pins.

LiquidCrystal lcd(12, 13, 7, 6, 5, 4); // I used an odd pin combination because I need pin 2 and 3 for the interrupts.

int_fast32_t rrx=7200000; // Starting frequency of VFO

int_fast32_t rx2=1; // variable to hold the updated frequency

int_fast32_t increment = 10; // starting VFO update increment in HZ.

int buttonstate = 0;

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 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!

 

 

 

 

void setup() {

pinMode(A0,INPUT); // Connect to a button that goes to GND on push

digitalWrite(A0,HIGH);

lcd.begin(16, 2);

PCICR |= (1 << PCIE2);

PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);

sei();

pinMode(FQ_UD, OUTPUT);

pinMode(W_CLK, OUTPUT);

pinMode(DATA, OUTPUT);

pinMode(RESET, OUTPUT);

pulseHigh(RESET);

pulseHigh(W_CLK);

pulseHigh(FQ_UD);  // this pulse enables serial mode on the AD9850 - Datasheet page 12.

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(); 

lcd.setCursor(6, 0);

lcd.print("Antenna Analyzer");

lcd.setCursor(6, 1);

lcd.print("Roger Ashley M0HNE);

 

}

}

 

 

void loop() {

if (rx != rx2){   

showFreq();

sendFrequency(rx);

rx2 = rx;

}

 

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();

}

}  

}

 

 

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

}

}

 

 

 

// frequency calc from datasheet page 8 = <sys clock> * <frequency tuning word>/2^32

void sendFrequency(double frequency) { 

int32_t freq = frequency * 4294967295/125000000;  // note 125 MHz clock on 9850.  You can make 'slight' tuning variations here by adjusting the clock frequency.

for (int b=0; b<4; b++, freq>>=8) {

tfr_byte(freq & 0xFF);

}

tfr_byte(0x000);   // Final control byte, all 0 for 9850 chip

pulseHigh(FQ_UD);  // Done!  Should see output

}

// transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line

void tfr_byte(byte data)

{

for (int i=0; i<8; i++, data>>=1) {

digitalWrite(DATA, data & 0x01);

pulseHigh(W_CLK);   //after each bit sent, CLK is pulsed high

}

}

 

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

};

  • Sign in to reply
  • Cancel
  • crosseyejack
    0 crosseyejack over 11 years ago

    Don't have a 2560 to hand but I would put it down to these lines here:-

    Rotary r = Rotary(2,3);

    PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);

    The code is trying to enable the interrupts that are normally used on the ATMega328/168 (Used in the Uno) but these pins are different on the Arduino Mega 2560. As I don't have a Arduino Mega 2560 to hand, you will have to have a look at http://arduino.cc/en/uploads/Hacking/PinMap2560big.png and remap the pins yourself and give it a try.


    Dan.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube