The present disclosure relates to an implantable pacemaker (1) configured to provide electrical pacing pulses to the heart (2) of a patient. The pacemaker (1) comprises a pulse generator (3) configured to generate said electrical pacing pulses, at least one pacing electrode (4) to apply said electrical pacing pulses to the heart (2), a sensing unit (5) configured to sense events of electrical activity of a ventricle (2a) of the heart (2), a sensor (6) configured to measure a signal relating to the patient, and a memory (7) configured to store values of a parameter (P). The pacemaker (1) is configured to be operated in a first mode to generate a reference curve (R) and to select a target range (T) of values of the parameter (P) corresponding to a desired range (D) of atrioventricular delays. The pacemaker (1) is further configured to be operated in a second mode for approaching the target range.