just a square wave,
so all i would be doing is use play with the built in timers rather what i am doing? to achieve that 100khz pulse?
Once you set up the timer, the 100 kHz square wave will come out the pin an will continue until your sketch tells it to stop or you turn off the power.
The first step is to pick a timer. I will assume you are using an Arduino UNO. Since you are counting to less than 256 you can use Timer2 (the 8-bit timer). Timer0 is used for the millis() timer and should generally not be changed. Timer1 is a 16-bit timer that can count up to 65535. Several common libraries use Timer1 (like Servo, IRremote, and tone()) so if you can use Timer2 you should use it first.
There are a couple of ways to make a square wave: Toggle the output every 80 counts OR set the period of a PWM output to 160 counts and set the duty-cycle to 50% (80 cycles). The first one is easier.
Get a copy of the ATmega328P datasheet and read about Timer2. You will see that the two main registers to set are TCCR2A and TCCR2B (Timer/Counter Control Registers). Start by setting them both to 0:
TCCR2A = 0;
TCCR2B = 0;
Now look at the Waveform Generation Mode table for Timer2. You want the CTC mode (Mode 2). Since all of the TCCR bits have been set to 0 you only need to set one bit: WGM21 (Bit 1 of the WGM field of Timer2). Look at the TCCR registers and you will see that WGM21 is in TCCR2A.
TCCR2A |= (1 << WGM21);
To tell the timer how high to count before cycling back to 0 you set the ‘TOP’ value. The WGM table says to put the value in the OCR2A register for CTC mode.
OCR2A = 79; // There are 80 counts, 0 to 79
To tell the counter how fast to count you set the Clock Select bits. We want it to count full speed (16 MHz) so we just have to turn on CS20. You’ll find it in the TCCR2B register:
TCCR2B |= (1 << CS20);
The clock is off and running!
Now you want to tell the clock hardware to toggle an output pin each time the count hits TOP. You can use Pin 11 (OC2A) or Pin 3 (OC2B). Let’s assume Pin 3: OC2B. To tell the hardware to toggle, set the bits COM2B1 to 0 and COM2B0 to 1. Since we already set all the bits to 0 you only have to set COM2B0 which is in TCCR2A:
TCCR2A |= (1 << COM2B0);
That’s all you need. Put it all in setup() and put nothing in loop() and your 100 kHz signal will come out of Pin 3 whenever your sketch is running.