Generating system interrupts with analogWrite


I would like to use analogWrite to control a stepper motor, and generate a system interrupt from the same timer to track each step (this is currently done by connecting the output to another interrupt pin). attachSystemInterrupt seems to be what I’m looking for, but I cannot get the two to work together. Is there another method for accomplishing this internally?


For those interested, I was able to figure out a solution.
TIM_ITConfig is used by the tone() call but not analogWrite().
Adding this along with the NVIC setup enabled system interrupts to be generated when using analogWrite.

void addITConfig(uint8_t pin) {
    if(PIN_MAP[pin].timer_ch == TIM_Channel_1)
        //Since A4 and D3 share the same TIM3->Channel1, only one can work at a time
        if(pin == 14)
            PIN_MAP[3].timer_ccr = 0;
        else if(pin == 3)
            PIN_MAP[14].timer_ccr = 0;

        // Channel1 configuration
        TIM_ITConfig(PIN_MAP[pin].timer_peripheral, TIM_IT_CC1, ENABLE);
    else if(PIN_MAP[pin].timer_ch == TIM_Channel_2)
        //Since A5 and D2 share the same TIM3->Channel2, only one can work at a time
        if(pin == 15)
            PIN_MAP[2].timer_ccr = 0;
        else if(pin == 2)
            PIN_MAP[15].timer_ccr = 0;

        // Channel2 configuration
        TIM_ITConfig(PIN_MAP[pin].timer_peripheral, TIM_IT_CC2, ENABLE);
    else if(PIN_MAP[pin].timer_ch == TIM_Channel_3)
        // Channel3 configuration
        TIM_ITConfig(PIN_MAP[pin].timer_peripheral, TIM_IT_CC3, ENABLE);
    else if(PIN_MAP[pin].timer_ch == TIM_Channel_4)
        // Channel4 configuration
        TIM_ITConfig(PIN_MAP[pin].timer_peripheral, TIM_IT_CC4, ENABLE);

    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    attachSystemInterrupt(SysInterrupt_TIM3_IRQ, Wiring_TIM3_Interrupt_Handler_override);
1 Like