Heart Rate Monitor Code for LCD Display
Heart Rate Monitor Code for LCD Display
Timer0 plays a critical role by providing time-based interruptions every 2 milliseconds to facilitate the continuous sampling of the pulse signal. The Timer0 is configured using `T0CON = 0xC5` to control the operation of Timer0 with pre-scaler settings, followed by initializing `TMR0L = 0x06` to ensure timely recurring interrupts. During each interrupt, the heart rate sensor samples the signal, updates the sample counter, checks conditions for pulse detection, and dynamically adjusts the threshold. By ensuring periodic execution, Timer0 allows real-time monitoring of heart signal data to accurately measure and respond to changes in pulse rate .
Configuration of the PIC18F452 microcontroller encompasses both hardware and software settings. Hardware setup involves connecting the pulse sensor to analog pin RA0 (pulsePin = 0), using a pin (RD13) to blink an LED at each detected heartbeat (blinkPin = 13), and initializing the LCD on port D for outputs. Software configuration involves setting up the initial values for signal processing variables, configuring the ADC with `ADC_Init()`, and establishing Timer0 to interrupt every 2ms using `InitTimer0()`. The initialization also requires setting up oscillator control for frequency scaling and ensuring the ADC operates correctly with specific `ANSEL` register settings for each port .
Initialization involves several key steps such as setting up the oscillator control (OSCCON) to adjust clock frequency, configuring analog ports (ANSx registers) to ensure proper analog signal input and digital output, and initializing Timer0 for time-sensitive operations. The Liquid Crystal Display (LCD) is also initialized and prepared to display introductory messages, while critical pulse detection variables are initialized to track heart signal data accurately. Lastly, ADC and interrupts are configured to work in tandem, accommodating real-time signal processing before entering the main operation loop where data acquisition and processing occur .
The rate array stores the last ten inter-beat interval (IBI) values, providing a basis for calculating an accurate heart rate. Every time a new IBI is calculated following a heartbeat detection, the oldest value in the rate array is replaced with the latest IBI value, and the running total is updated by removing the oldest and adding the newest IBI. This running total is divided by ten to yield an average IBI, which stabilizes short-term variability in BPM calculation, resulting in a realistic BPM that accounts for fluctuations across multiple cardiac cycles .
The system detects a pulse by monitoring changes in signal amplitude in relation to a dynamically adjusted threshold. When the incoming analog signal exceeds the threshold while satisfying the conditions of non-high frequency noise and sufficient time since the last beat, it sets a Pulse flag indicating pulse detection. The threshold is adjusted dynamically after detecting the beat. If the signal surpasses the threshold and is associated with the rising section of a pulse wave, the Pulse flag is engaged. Once the pulse has passed and the signal decreases, the amplitude is computed as the difference between the peak (P_cnt) and the trough values. The new threshold is then set as halfway between the amplitude and trough, i.e., the amplitude is divided by two and added to the trough. This ensures the threshold adapts to the signal's dynamic range for accurate pulse detection .
The IBI, representing the duration in milliseconds between successive heartbeats, is a crucial measure for determining the heart rate. Initially seeded with 600 milliseconds, the IBI is dynamically updated with each beat detection, being the elapsed time between the current and last beat, aiding in calculating BPM. Maintaining accurate IBI measurements ensures realistic BPM outputs. The system calculates an average IBI using a running total of the last ten IBI values, stored within an array, facilitating a responsive and accurate heart rate analysis by smoothing short-term fluctuations .
The LCD serves as an interface between the heart rate monitoring system and the user, displaying initialization messages and real-time pulse data. Upon startup, it presents the message 'ELECTRONIC HEART RATE MONITOR,' positioned at the first row and scrolls leftward. After initialization, the LCD shows 'HEART RATE (BPM)' on the first row. On the second row, it displays the calculated BPM value obtained by converting it from integer to a string format, ensuring clarity and user engagement by updating every two seconds as new pulse information is available .
The system calculates the BPM by averaging the intervals between heartbeats, known as inter-beat intervals (IBI). Initially, it seeds the rate array with the IBI value from the first two detected beats. For the first detected beat, the system discards the IBI as it is unreliable. During subsequent pulses, it keeps a running total of the last ten IBI values. By dividing the accumulated total by ten, it computes the average IBI, from which the BPM is calculated as 60000 divided by the average IBI, ensuring accuracy over time. This initialization helps stabilize BPM readings by accounting for initial fluctuations .
The system detects prolonged periods without a heartbeat—defined as more than 2.5 seconds—by checking if `N_cnt` exceeds 2500. In these cases, it resets critical variables to their default values: the threshold (`Thresh`), peak count (`P_cnt`), and trough (`Trough`) are all set to 512. This reset helps eliminate potential noise-induced errors and stabilizes the system upon return of a detectable heartbeat. The re-establishment to default values ensures a quick reset of conditions that could otherwise mislead readings, so effective recovery from noise and preparation for pulse resumption occur within the system .
The system avoids false positive detections due to noise by implementing various threshold and timing checks. It sets an initial threshold at 512 and adjusts dynamically based on real-time amplitude analyses between peaks and troughs to differentiate valid pulses from noise. Furthermore, signals are cross-verified against conditions like minimized acceptable time intervals between beats (greater than one-fifth of the IBI multiplied by three). Additionally, a noise prevention mechanism is embedded to disregard any signals that do not align with a detected range stipulated by `N_cnt > 250`—a time period to prevent noise mimicking a heartbeat .