# Generating system interrupts with analogWrite

**URL:** <https://community.particle.io/t/generating-system-interrupts-with-analogwrite/45007>\
**Category:** Firmware\
**Created:** [November 5, 2018, 9:12pm UTC](https://community.particle.io/t/generating-system-interrupts-with-analogwrite/45007 "2018-11-05T21:12:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrferrar](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@mrferrar](https://community.particle.io/u/mrferrar)\
**Post date:** [November 5, 2018, 9:12pm UTC](https://community.particle.io/t/generating-system-interrupts-with-analogwrite/45007/1 "2018-11-05T21:12:06Z")

</div>

Hello,

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?

Thanks

---

<div class="post-metadata">

**Author:** ![mrferrar](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@mrferrar](https://community.particle.io/u/mrferrar)\
**Post date:** [November 6, 2018, 2:33am UTC](https://community.particle.io/t/generating-system-interrupts-with-analogwrite/45007/2 "2018-11-06T02:33:23Z")

</div>

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.

```auto
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;
    NVIC_Init(&NVIC_InitStructure);
    addITConfig(A5);
    attachSystemInterrupt(SysInterrupt_TIM3_IRQ, Wiring_TIM3_Interrupt_Handler_override);

```
