Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Muppetry with Interrupts on: October 31, 2013, 09:55:04 am
OK, thought so and understood.

I only added that out of desperation. Here is the code that (still) isn't working:

Code:
#define ledPin 13

void setup()
{
  Serial.begin(9600);
 
  pinMode(ledPin, OUTPUT);

// initialize Timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;

OCR1A = 31250; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
}

ISR(Timer1_COMPA_vect) // timer compare interrupt service routine
{

  digitalWrite(ledPin, digitalRead(ledPin) ^ 1); // toggle LED pin
}

void loop()
{
  Serial.println("Inside Main Loop");
  delay(20000);
}

Thanks for any help....

Mark
2  Using Arduino / Programming Questions / Re: Muppetry with Interrupts on: October 31, 2013, 09:36:55 am
Could you perhaps elaborate a little?
3  Using Arduino / Programming Questions / Muppetry with Interrupts on: October 31, 2013, 09:21:04 am
Hi Gang

I'm posting in the help that someone can offer me some assistance with the basics of getting timer based interrupts working on the Mega2560 board.

My longer term aim is to have Timer0 (8 bit) counting an external 32.768KHz clock and dividing it by 256 through calling the ISR every 256 external pulses (128 times per second).

However, due to some spectacular Muppetry, I can't get even a basic ISR to run. Here's the code I would like help with:

Code:
#define ledPin 13

void setup()
{
  Serial.begin(9600);
 
  pinMode(ledPin, OUTPUT);

// initialize Timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;

OCR1A = 31250; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
}

ISR(Timer1_COMPA_vect) // timer compare interrupt service routine
{
  Serial.println("Inside ISR");
  digitalWrite(ledPin, digitalRead(ledPin) ^ 1); // toggle LED pin
}

void loop()
{
  Serial.println("Inside Main Loop");
  delay(20000);
}


All I see in the serial monitor is the text output from the main loop and no activity on pin 13.

Can anyone (gently) point out what's going on here?

Thank you,

Mark.
4  General Category / General Discussion / Outline Arduino Images on: November 06, 2012, 01:32:38 am
Hi Guys

I'm trying to find an image like this one:

http://arduino.cc/en/uploads/Tutorial/Arduino_bb.png

but for the mega. Is there a library or something I cant find with this kind of stuff in it?

Thanks,

Mark.
5  Using Arduino / Programming Questions / Re: -ve long storage in EEPROM on: June 07, 2012, 10:32:55 am
Thank you so much!

I have actually spotted why my code didnt work (typical after 3 days staring at it!) but the solutions offered are so much simpler. It's the complexity of mine that had tripped me up.

Thanks again.

Mark.
6  Using Arduino / Programming Questions / -ve long storage in EEPROM on: June 07, 2012, 10:00:25 am
Guys

Been lurking a while, but new to Arduino in general.

I'm developing a Ham Radion WSPR beacon using Arduino and I have everything working just fine, except....

I need to have a Calibration Factor to get my Arduino interfaced DDS exactly on frequency and I am trying to store and read back that value from EEPROM. It works fine for +ve numbers but not -ve. I've seen the tutorials on EEPROM writing but nothing I have seen has helped with -ve numbers.

I have tried including a bit test for the value read back, but this doesn't work either:

if (CalFactor >> 31 ==1) { CalFactor = CalFactor - (pow(2,32)-1); }

Can any kind soul here help with this connundrum of mine?

Here's the bit of code I can't get to function with -ve numbers, the value read back for a write of -2 is 288,741,374:

Code:
#include <EEPROM.h>
#include <math.h>

long CalFactor;
byte temp;
int j;
int i;

void setup()
{

   Serial.begin(9600);        // connect to the serial port

   CalFactor = -2;

   //Writes CalFactor to address 50 + 3 bytes of EEprom
   for (j=0; j<4; j++)  
   { // Step through 4 bytes to make 32 bit word
      for (i=0; i<8; i++)
      { // Step through 8 bit to make 1 byte
        bitWrite(temp,i,(bitRead(CalFactor,j*i+i))); // Assemble bits to make 1 byte
      }
      EEPROM.write(50+j,temp); // Write byte to EEprom
   }
  
   Serial.println("Written to EERPROM ");
   Serial.println(CalFactor);
   Serial.println(CalFactor,BIN);

   CalFactor = 0; // set all bits to zero
  
   // Read back from EEPROM
   for(j=0; j<4; j++)
   {  // Step through 4 bytes to make 32 bit word
      temp = EEPROM.read(50+j); // Get bytes 50 through 53
      for (i=0; i<8; i++)
      { // Set through 8 bit to make 1 byte
         bitWrite(CalFactor,i*j+i,(bitRead(temp,i))); // Assemble bits to make 32 bit word
      }
   }
  
   if (CalFactor >> 31 == 1)
   {
      Serial.println("1st Bit is 1");
//      CalFactor = CalFactor - (pow(2,32)-1);
   }
  
   Serial.println("Read from EERPROM");
   Serial.println(CalFactor);
   Serial.println(CalFactor,BIN);
}

void loop()
{  
}


Moderator edit: [code] [/code] tags added.
Pages: [1]