A cardiac rhythm management device includes a dual-chamber pacemaker especially designed for treating congestive heart failure and a defibrillator as a back-up measure. The device incorporates a programmed microcontroller which is operative to adjust the AV delay of the pacemaker to a minimum or maximum length consistent with optimal cardiac function. The cardiac stimulator includes apparatus for sensing cardiac function from cycle-to-cycle and determining whether incremental changes made to the AV delay interval enhances or worsens the measured cardiac function. On each iterative cycle, the AV delay interval is changed, either incremented or decremented, until a cross-over point is reached in which it is noted that the cardiac function ceases to improve and this is followed with a further incremental adjustment in an opposite direction to compensate for any overshoot. By providing a cardiac defibrillator in combination with the pacing device with closed-loop control of the AV interval, incidences of sudden death due to lethal arrhythmias is reduced.