An implantable pacemaker configured to provide electrical pacing pulses to the heart of a patient, wherein the pacemaker comprises: - a pulse generator configured to generate said electrical pacing pulses, - at least one pacing electrode to apply said electrical pacing pulses to the heart, - a sensing unit configured to sense events of electrical activity of a ventricle of the heart, wherein the respective event corresponds to a contraction of the ventricle, - a sensor configured to measure a signal relating to the patient, and - a memory configured to store values of a parameter, wherein - the pacemaker is configured to be operated in a first mode, wherein in the first mode, the pacemaker is configured to apply electrical pacing pulses generated by the pulse generator to the ventricle via the at least one pacing electrode unless the sensing unit senses an event, and to determine from said signal for each applied electrical pacing pulse or sensed event a value of a parameter that varies with the atrioventricular delay of the heart, wherein the pacemaker is configured to store the values of the parameter in the memory, and wherein the pacemaker is further configured to generate in the first mode a reference curve which associates each determined value of the parameter with an atrioventricular delay value, characterised in that the pacemaker is further configured to select a target range of values of the parameter corresponding to a desired range of atrioventricular delays, and - the pacemaker is further configured to be operated in a second mode, wherein in the second mode, the pacemaker is configured to determine a value of the parameter for every heartbeat of the patient, wherein when the respective value of the parameter determined in the second mode for a heartbeat is outside the target range, the pacemaker is configured to adjust the timing of an electrical pacing pulse for the next heartbeat such that a determined value of the parameter for a succeeding heartbeat approaches the target range. - a pulse generator configured to generate said electrical pacing pulses, - at least one pacing electrode to apply said electrical pacing pulses to the heart, - a sensing unit configured to sense events of electrical activity of a ventricle of the heart, wherein the respective event corresponds to a contraction of the ventricle, - a sensor configured to measure a signal relating to the patient, and - a memory configured to store values of a parameter, wherein - the pacemaker is configured to be operated in a first mode, wherein in the first mode, the pacemaker is configured to apply electrical pacing pulses generated by the pulse generator to the ventricle via the at least one pacing electrode unless the sensing unit senses an event, and to determine from said signal for each applied electrical pacing pulse or sensed event a value of a parameter that varies with the atrioventricular delay of the heart, wherein the pacemaker is configured to store the values of the parameter in the memory, and wherein the pacemaker is further configured to generate in the first mode a reference curve which associates each determined value of the parameter with an atrioventricular delay value, characterised in that the pacemaker is further configured to select a target range of values of the parameter corresponding to a desired range of atrioventricular delays, and - the pacemaker is further configured to be operated in a second mode, wherein in the second mode, the pacemaker is configured to determine a value of the parameter for every heartbeat of the patient, wherein when the respective value of the parameter determined in the second mode for a heartbeat is outside the target range, the pacemaker is configured to adjust the timing of an electrical pacing pulse for the next heartbeat such that a determined value of the parameter for a succeeding heartbeat approaches the target range.