An implantable pacemaker is configured to provide electrical pacing pulses to the heart of a patient. The pacemaker has a pulse generator configured to generate the electrical pacing pulses, at least one pacing electrode to apply the electrical pacing pulses to the heart, a sensing unit configured to sense events of electrical activity of a ventricle of the heart, a sensor configured to measure a signal relating to the patient, and a memory configured to store values of a parameter. The pacemaker is configured to be operated in a first mode to generate a reference curve and to select a target range of values of the parameter corresponding to a desired range of atrioventricular delays. The pacemaker is further configured to be operated in a second mode for approaching the target range.