
#define PUSHREGS() asm volatile ( \
"push r1  \n\t" \
"push r0  \n\t" \
"in r0, __SREG__ \n\t" \
"cli \n\t" \
"push r0  \n\t" \
"in r0 , 0x3b \n\t" \
"push r0 \n\t" \
"in r0 , 0x3c \n\t" \
"push r0 \n\t" \
"push r2  \n\t" \
"push r3  \n\t" \
"push r4  \n\t" \
"push r5  \n\t" \
"push r6  \n\t" \
"push r7  \n\t" \
"push r8  \n\t" \
"push r9  \n\t" \
"push r10 \n\t" \
"push r11 \n\t" \
"push r12 \n\t" \
"push r13 \n\t" \
"push r14 \n\t" \
"push r15 \n\t" \
"push r16 \n\t" \
"push r17 \n\t" \
"push r18 \n\t" \
"push r19 \n\t" \
"push r20 \n\t" \
"push r21 \n\t" \
"push r22 \n\t" \
"push r23 \n\t" \
"push r24 \n\t" \
"push r25 \n\t" \
"push r26 \n\t" \
"push r27 \n\t" \
"push r28 \n\t" \
"push r29 \n\t" \
"push r30 \n\t" \
"push r31 \n\t" \
)

#define POPREGS() asm volatile ( \
"pop r31 \n\t" \
"pop r30 \n\t" \
"pop r29 \n\t" \
"pop r28 \n\t" \
"pop r27 \n\t" \
"pop r26 \n\t" \
"pop r25 \n\t" \
"pop r24 \n\t" \
"pop r23 \n\t" \
"pop r22 \n\t" \
"pop r21 \n\t" \
"pop r20 \n\t" \
"pop r19 \n\t" \
"pop r18 \n\t" \
"pop r17 \n\t" \
"pop r16 \n\t" \
"pop r15 \n\t" \
"pop r14 \n\t" \
"pop r13 \n\t" \
"pop r12 \n\t" \
"pop r11 \n\t" \
"pop r10 \n\t" \
"pop r9  \n\t" \
"pop r8  \n\t" \
"pop r7  \n\t" \
"pop r6  \n\t" \
"pop r5  \n\t" \
"pop r4  \n\t" \
"pop r3  \n\t" \
"pop r2  \n\t" \
"pop r0  \n\t" \
"out 0x3c , r0 \n\t " \
"pop r0  \n\t" \
"out 0x3b , r0 \n\t " \
"pop r0  \n\t" \
"out __SREG__ , r0 \n\t " \
"pop r0  \n\t" \
"pop r1  \n\t" \
)

#define DI()   asm volatile ("cli")
#define EI()   asm volatile ("sei")
#define RETI() asm volatile ("reti")


#define ledPin 13
volatile int xx=0;






 
ISR(TIMER3_OVF_vect,ISR_NAKED)          // timer compare interrupt service routine
{
  PUSHREGS();
  TCNT3 =    65536-250;

  //PUSHREGS();
  xx++;
  digitalWrite(ledPin, digitalRead(ledPin) ^ 1);   // toggle LED pin
  //POPREGS();
  POPREGS();
  RETI();
}
void initTmr()
{

  // initialize timer1 
  noInterrupts();           // disable all interrupts
  TCCR3A = 0;
  TCCR3B = 0;
 
  /*
 prescaler in cs2 cs1 cs0
   0   0   0   none
   0   0   1   /1 == none
   0   1   0   /8
   0   1   1   /64
   1   0   0   /256
   1   0   1   /1024
   16MHz Arduino -> 16000000/256 = 62500 ticks/second
   -------------------------/64  = 250000 ticks/second !
   
   NB 16 bit counter so values >= 65535 is not working
   
   01msec timer /64 + 250 
   10msec timer /64 + 2500
   100msec timer /64 + 25000
   
   */
 
  //JDN 10 msec
 
  TCNT3 =    65536-250;
  TCCR3A = 0;
 // OCR1A = 2500;
  TCCR3B  = 0x03;//|= (1<<CS12);// (1 << WGM12) | 0x03;//(1<<CS12);//(0x03) ; // 0x03 == /64
  TIMSK3 |=  (1<<TOIE3);//(1 << OCIE1A);  // enable timer compare interrupt


  interrupts();             // enable all interrupts

}

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

int y=0;
void loop()
{ 
  
  int fl=0;
  //delay(500);
  noInterrupts();
  if (xx > 1000) {
     xx=0;
     fl = 1;
  }
  interrupts();
  if (fl) {
    y++;
    Serial.println(y);
  }
  // your program here...
}

