|
63 | 63 | /// tim = pyb.Timer(4) # create a timer object using timer 4 |
64 | 64 | /// tim.init(mode=Timer.PERIODIC) # initialize it in periodic mode |
65 | 65 | /// tim_ch = tim.channel(Timer.A, freq=2) # configure channel A at a frequency of 2Hz |
66 | | -/// tim_ch.callback(handler=lambda t:led.toggle()) # toggle a led on every cycle of the timer |
| 66 | +/// tim_ch.callback(handler=lambda t:led.toggle()) # toggle a LED on every cycle of the timer |
67 | 67 | /// |
68 | 68 | /// Further examples: |
69 | 69 | /// |
@@ -765,12 +765,14 @@ STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *po |
765 | 765 | break; |
766 | 766 | case TIMER_CFG_A_CAP_COUNT: |
767 | 767 | ch->timer->intflags |= TIMER_CAPA_MATCH << shift; |
| 768 | + // set the match value and make 1 the minimum |
| 769 | + MAP_TimerMatchSet(ch->timer->timer, ch->channel, MAX(1, args[3].u_int)); |
768 | 770 | break; |
769 | 771 | case TIMER_CFG_A_CAP_TIME: |
770 | 772 | ch->timer->intflags |= TIMER_CAPA_EVENT << shift; |
771 | 773 | break; |
772 | 774 | case TIMER_CFG_A_PWM: |
773 | | - // special case for the match interrupt |
| 775 | + // special case for the PWM match interrupt |
774 | 776 | ch->timer->intflags |= ((ch->channel & TIMER_A) == TIMER_A) ? TIMER_TIMA_MATCH : TIMER_TIMB_MATCH; |
775 | 777 | break; |
776 | 778 | default: |
@@ -833,18 +835,6 @@ STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *po |
833 | 835 | // create the callback |
834 | 836 | _callback = mpcallback_new (ch, args[1].u_obj, &pyb_timer_channel_cb_methods); |
835 | 837 |
|
836 | | - // get the value if given |
837 | | - uint32_t c_value = MAX(0, args[3].u_int); |
838 | | - ch->duty_cycle = MIN(100, c_value); |
839 | | - |
840 | | - // reload the timer |
841 | | - uint32_t period_c; |
842 | | - uint32_t match; |
843 | | - compute_prescaler_period_and_match_value(ch, &period_c, &match); |
844 | | - MAP_TimerLoadSet(ch->timer->timer, ch->channel, period_c); |
845 | | - // set the appropiate match value |
846 | | - MAP_TimerMatchSet(ch->timer->timer, ch->channel, (_config == TIMER_CFG_A_PWM) ? match : c_value); |
847 | | - |
848 | 838 | // enable the callback before returning |
849 | 839 | pyb_timer_channel_callback_enable(ch); |
850 | 840 | } |
|
0 commit comments