Creating a 1KHz clock O/P

This is written for a Mega2560 for demonstration purposes but should be adaptable to an ATTiny. It uses Timer1 to generate a 125Hz interrupt. In the ISR the current position of the POT is tracked. In the loop, the desired position variable is updated. When the two are not equal, the logic in the ISR ticks the pot as required. It'll go from 0 to 64 in 500mS.

/*
 * Sketch:  sketch_nov15c
 * Target:  Mega2560
 *
 */

const uint8_t pinUD = 2;
const uint8_t pinCLK = 3;
const uint8_t pinDebug = 4;

uint32_t
    timeNow;
static uint32_t
    timePot = 0ul;

volatile uint8_t
    desiredCnt = 0;
        
void setup() 
{
    //pins for U/nD and CLK
    pinMode( pinUD, OUTPUT );
    pinMode( pinCLK, OUTPUT );
    //for scoping
    pinMode( pinDebug, OUTPUT );

    //setup timer 1 to interrupt at 128Hz
    //  WGM 15; OCR1A is TOP
    //  /8 prescaler gives 2MHz ot 500nS tick
    //  128Hz is 7.8125mS
    //      7.8125mS / 500nS = 15625
    //      set OCR1A to 15625-1 
    TCCR1A = _BV(WGM11) | _BV(WGM10);
    TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS11);   //use /8 prescaler to give 2MHz clock
    OCR1A = 15624;
    //enable the timer1 compare interrupt
    TIMSK1 |=  _BV(OCIE1A); 

    //desired count is where we want the digital POT (DP) to be
    //we init desired to 0 and "current" in the ISR to 65 to ensure the 
    //DP is wound all the way to step '0' out of reset
    desiredCnt = 0;

}//setup

void loop() 
{
    //for demo, set to ramp pot up and down once per second
    timeNow = millis();
    if( (timeNow - timePot) >= 1000ul )
    {        
        timePot = timeNow;
        
        //when we modify desiredCnt, do so in a critical section
        noInterrupts();
        if( desiredCnt == 0 )
            desiredCnt = 64;
        else
            desiredCnt = 0;
        interrupts();
            
    }//if
    
}//loop

ISR( TIMER1_COMPA_vect )
{
    //initialize current position to something bigger
    //than 64. with desired set to zero we'll clock out
    //enough ticks to set the DP to a known state (0)
    static uint8_t
        currentCnt = 65;

    //if desired and current are not the same...
    if( desiredCnt != currentCnt )
    {
        //debug
        digitalWrite( pinDebug, HIGH );               
        //set the U/nD pin LOW if the desired position is less than the
        //current (iow, we want to count down)
        //otherwise, desired is higher than current so set it high
        digitalWrite( pinUD, (desiredCnt < currentCnt) ? LOW : HIGH );
        //pulse the clock line
        digitalWrite( pinCLK, HIGH );
        digitalWrite( pinCLK, LOW );
        //and bump the current position up or down
        currentCnt = currentCnt + ((desiredCnt < currentCnt) ? -1:1);
        
    }//if
    else
        //the current == desired so we do nothing but clear the
        //debug pin
        digitalWrite( pinDebug, LOW );
    
}//ISR timer1 compare