interrupts and prescaler

I wish to use Timer1 to generate interrupts but require the use of the prescaler which is shared with Timer0. From what I can glean Timer0 is used in some way by Arduino. Is the prescaler used by Timer0 or is it freely available for me to use? My scheme is to simply add a fixed value to the compare register every time the compare equals the free running Timer1. Hopefully this will generate totally uniform and repetitive interrupt periods. (and run a bit of code in between) How many instructions will the compiler generate to carry out this addition? Is there any way I can view the assembled code or some way to see the hex file to be loaded into the processor? Maybe I could dissect out the instructions that way. As you can see this is all totally newbie speak so maybe someone will interrupt their routine (!) and provide a not too complex answer. Thanks all.

I'm not sure that interrupts, prescalers, and newbie belong in the same paragraph.

Perhaps you could explain why you want to generate interrupts in the first place. What are you trying to interrupt? What should that do when it is interrupted?

PaulS, I would like to generate a data stream with precise frequency. eg some data bits some sync bits and repeat continuously. I want to run at 2Khz so need ints every 250 microSecs. Hopefully I can do some small amount of house keeping after shifting the data bytes and the output pin has been toggled. I can see most of the code has to fit within the interrupt call with non critical stuff in the main loop. I am looking at all this from an assembly code point of view, with which I am more familiar. Maybe this is the wrong approach for C programming which I am not familiar with. Is there a way to view the code the compiler makes to try and get a feel for the size of code generated? Also, what kind of timing errors might the serial print command cause such an interrupt driven scheme? The arduino environment seems very friendly and a good choice for me as a C beginner but its hard to find much info on how it uses interrupt. Any pointers helpful.

We're getting closer...

What are you trying to drive with this "very precise frequency"?

There are tools which let you examine the hex file that is uploaded to the arduino. I'm not sure of the exact name, but avrdump, or something like that comes to mind.

Serial printing in an ISR is going to cause problems. Serial data transfer is a slow process. ISRs need to be fast, so interrupts are not missed, while you're busy writing data out.

I'm the last person to tell you anything (else) about interrupts. If you start at the beginning, explaining what you want to accomplish, I'm sure others can offer suggestions.

PaulS, I'm going to try to generate an LTC data stream for starters then modify it for my purposes. I'll have a look for avrdump in the morning. Just to clarify, any serial prints will be done occasionally outside the ISR. I was wondering if it uses interrupts, Timer1 (or any other) and how it might affect my ISR. and vice versa. Where does one find out about this stuff? All pointers appreciated.

Where does one find out about this stuff?

http://www.arduino.cc/playground/ and http://arduino.cc/en/Tutorial/HomePage

OK, have been reading all about interrupts. Thanks to forum for lots of info on them.
I have now got to this point and can generate a sample data stream, all in a small portion of the time available; about 10uSec out of 250uSec available so it seems worth continuing this route. Here is code if any one is interested.
Have found a possible BUG (maybe) with IDE. error message occurs ONLY if I try to make #defines the first lines of code.
Am I missing something?

// V0.1B Data streaming
// Platform; Arduino Mini 16Mhz
// IDE Arduino 0017
// V0.1A 27 Jan 2010 Set up 2usec interrupt and toogle pin 13
// V0.1B 28 Jan 2010 Biphase data streaming on pin 12; sync pulses on pin 13
//------------------------------------------------------------------------------
// #define dataPin 12 // VERY STRANGE! WILL GENERATE ERROR IF ACTIVE HERE
#define led 13
volatile byte temp1;
volatile byte toggle;
volatile byte data[9];
//#define led 13
#define dataPin 12 // BUT IS OK HERE!!
volatile byte i=0;
volatile byte j=0;
volatile byte buffer;
volatile byte mask;
volatile boolean dataBit=0;
//-------------------------------- set up ---------------------------------------
void setup(){
//set up timer 2 cotrol registers here
TCCR2A = B00000000; //select Normal Mode,freerunning counter, no connection to hardware pins
TCCR2B = B00000011; //normale, prescaler equals clock/32. With 16Mhz clock this give 2uSec clock to the TCNT2
OCR2A = 125; //initialise compare register for first interrupt
TIMSK2 = B00000010; //generate interrupt on output compare match A
pinMode(led,OUTPUT); // set DDR
pinMode(dataPin,OUTPUT);

data[1]=B00000000; // dummy data
data[0]=B00011001; // dummy data
buffer=data[0];
mask=B00000001;

}
//-------------------------------- interrupt service routine ---------------------
ISR (TIMER2_COMPA_vect){
OCR2A += 125; //add 250uSec increment to timer2
toggle = !toggle;
//digitalWrite(led,toggle); // nice stable 2Khz square wave here
digitalWrite(led,1); // start pulse for synching 'scope

digitalWrite(dataPin,dataBit); //write to output

if (i!=1) { // shift data only every second interrupt
i=1;
dataBit=!dataBit; // data bit must be toggledat every data read
temp1 =buffer & mask; //put zero or non zero depending on the state of the bit under the mask

mask=mask<<1; // shift the mask left 1 bit
if (mask==0) {
mask=B00000001; // reset the mask bit
j=j+1; // increment data pointer
if (j>1) {
j=0;
}
buffer=data[j]; // get the next data byte
}
}
else{
i=0;
if (temp1!=0) { // if bit to be sent is a 1 then toggle dataBit again on the next interrupt
dataBit=!dataBit;
}
}
digitalWrite(led,0); //end syn pulse. THe pulse length indicates the fraction used of the time available
// So far this code uses about 10uSec out of the 250uSec period. There is still
// space for a fair bit of code.

}
//--------------------------------------- main loop ------------------------------

void loop(){

}

The problem with #define as the first line of a pde has been discussed on the forum, recently.

I finally found this again.
Thank you, it was very helpful.
I am doing a RTC in Arduino Mega (Real Time Clock).
Using interrupts and other things.
(Lots of Serial.print statements for debug.)

Am still figuring what I can throw away in this, but here is code:

/* **************************************************************************/
/
B Conley, Circuitsville Engineering LLC 15 February 2010 */

/* **************************************************************************/
/
Real Time Clock application in Arduino Mega, using ATMega1280 /
/
Inputs: None (?) /
/
Outputs: Digital: D7 (PH4) Seconds blink LED /
/
D8 (PH5) Minutes blink LED /
/
D9 (PH6) Hours Blink LED /
/
Variables: Secs, Mins, Hrs /
/
Requirements: /
/
Set ORC1A register to 0xF424 to cause interrupt once per second /
/
Use Prescale of 256 (CS12:CS10 = 100, these TCCR1B LSBs) /
/
Set TCCR1B to bxxx0 1100 /
/
Set TCCR1A to bxxxx xx00 /
/
NOTES: /
/
Decimal 60 = b0011 1100 = 0x3C /
/
Decimal 59 = b0011 1011 = 0x3b /
/
Decimal 24 = b0001 1000 = 0x18 /
/
Decimal 23 = b0001 0111 = 0x17 /
/
***************************************************************************/
#define D7 7 /
Seconds, Green LED, PH4 /
#define D8 8 /
Minutes, Yellow LED, PH5 /
#define D9 9 /
Hours, Red LED, PH6 */
int Secs =0;
int Mins =0;
int Hrs =0;
int OutS =0;
int OutM =0;
int OutH =0;
byte IFMask = B00000010;
byte IFClrMask = B11111101;
byte result;
byte IFreset;

void setup()
{
Serial.begin(9600);
pinMode(D7,OUTPUT);
pinMode(D8,OUTPUT);
pinMode(D9,OUTPUT);
digitalWrite(D7,HIGH);
digitalWrite(D8,HIGH);
digitalWrite(D9,HIGH);
delay(3000);
digitalWrite(D9,LOW);
delay(3000);
digitalWrite(D8,LOW);
delay(3000);
digitalWrite(D7,LOW);
Secs =0;
Mins =57;
Hrs =0;
TCCR1A = B01000000;
TCCR1B = B00001100;
TIMSK1 = B00000010; /* Enables the Timer1 Compare A interrupt /
OCR1AH = 0xF4;
OCR1AL = 0x24;
} /
End Setup() /
/**************************************************************************/
/
timer1CompareFlag Function() An ISR (Interrupt Service Routine) /
/
Deals with Timer1A compare flag which should happen when one second is /
/
has passed /
/
Essential Function: /
/
Update seconds until seconds is >59 /
/
At Seconds >59 update Seconds, {reset to 00},(increment) Minutes /
/
Check if Minutes >59 if so update Minutes {reset to 00}, /
/
Increment hours /
/
Check Hours if >23 if so update Hours {reset to 00} /
/
Is actually an Interrupt Service Routine, so should be named & treated thus /
/**************************************************************************/
ISR (TIMER1_COMPA_vect){
noInterrupts();
//Serial.print("Beg ISR Seconds ");
//Serial.println(Secs,DEC);
Secs = Secs +1;
if (OutS == 0) /
Begin Blink Green (Seconds) LED /
{
digitalWrite(D7,LOW);
OutS = 1;
}
else
{
digitalWrite(D7,HIGH);
OutS = 0;
} /
End Blink Green LED /
if (Secs > 59)
{
//Serial.print("Inc Mins ISR Seconds ");
//Serial.println(Secs,DEC);
Secs = 0;
Mins += 1;
if (OutM == 0) /
Begin Blink Yellow (minutes) LED /
{
digitalWrite(D8,HIGH);
OutM = 1;
}
else
{
digitalWrite(D8,LOW);
OutM = 0;
} /
End Blink Yellow LED /
}
else
{
interrupts();
// Serial.print("Seconds ");
// Serial.println(Secs,DEC);
// Serial.print("Minutes ");
// Serial.println(Mins,DEC);
// Serial.print("Hours ");
// Serial.println(Hrs,DEC);
return;
} /
End Seconds If/Else /
if(Mins > 59)
{
Mins = 0;
Hrs += 1;
if (OutH == 0) /
Blink Hours (Red) LED /
{
digitalWrite(D9,HIGH);
OutH = 1;
}
else
{
digitalWrite(D9,LOW);
OutH = 0;
} /
End Hours (Red) LED Blink /
} /
End minutes If /
else
{
interrupts();
//Serial.print("End Ms ISR Seconds ");
//Serial.println(Secs,DEC);
//Serial.print("Minutes ");
//Serial.println(Mins,DEC);
//Serial.print("Hours ");
//Serial.println(Hrs,DEC);
return;
} /
End Minutes If/Else, end Minutes Else /
if(Hrs > 23)
{
Hrs = 0;
} /
End Hours If /
else
{
interrupts();
return;
} /
End Hours Else, end Hours IF/ELSE /
//Serial.print("End Hrs ISR Seconds ");
//Serial.println(Secs,DEC);
//Serial.print("Minutes ");
//Serial.println(Mins,DEC);
//Serial.print("Hours ");
//Serial.println(Hrs,DEC);
interrupts();
return;
} /
End function timer1CompareFlag() ISR /
void loop()
{
while(1){
interrupts();
Serial.print('B ‘, BYTE); // send a capital B
Serial.print(TIFR1,BIN);
Serial.print(’ Secs ‘, BYTE); // send seconds
Serial.print(Secs, DEC);
Serial.print(’ Mins ‘, BYTE); // send minutes
Serial.print(Mins, DEC);
Serial.print(’ Hrs ', BYTE); // send hours
Serial.println(Hrs, DEC);
result = TIFR1&IFMask;
if (result > 0){
Serial.println(‘Ever Go This Way?’, BYTE);
//timer1CompareFlag();
IFreset = TIFR1&IFClrMask; /
This should clear the Timer 1 Flag (OCF1A) /
TIFR1 = IFreset;
}
establishContact();
}
} /
End Loop() */

/* ***************************************************** /
/
Function establishContact() /
/
It sends out an ASCII character via USB to rcvr /
/
***************************************************** */

void establishContact() {
while (Serial.available() <= 0) {
Serial.print(‘A’, BYTE); // send a capital A
Serial.print(’ ‘,BYTE);
Serial.print(Secs,DEC);
Serial.print(’ ‘,BYTE);
Serial.print(Mins,DEC);
Serial.print(’ ',BYTE);
Serial.println(Hrs,DEC);
} /* End while loop /
return;
} /
End function establishContact() */