#include <xc.h> #define _XTAL_FREQ 4000000
// LCD Connections #define RS RB4 #define EN RB5 #define D4 RB0 #define D5 RB1 #define D6 RB2 #define D7 RB3
// Function Prototypes void Lcd_Init(); void Lcd_Command(unsigned char); void Lcd_Char(unsigned char); void Lcd_String(const char *); void Lcd_Set_Cursor(unsigned char, unsigned char); void Lcd_Clear(); void delay();
unsigned int pulse_count = 0;
void main(void) { TRISB = 0x00; // LCD PORT TRISC = 0xFF; // Input for IR receiver TRISA = 0xFF; // Sensor input PORTB = 0x00;
Lcd_Init();
Lcd_Set_Cursor(1, 1);
Lcd_String("Heart Rate:");
while (1) {
pulse_count = 0;
// Count pulses for 15 seconds (simulate)
for (int i = 0; i < 15000; i++) {
if (RC0 == 1) { // IR Pulse Detected
pulse_count++;
__delay_ms(20); // Debounce
}
}
unsigned int heart_rate = pulse_count * 4;
Lcd_Set_Cursor(2, 1);
Lcd_String("BPM: ");
if (heart_rate < 100)
Lcd_Char(' ');
if (heart_rate < 10)
Lcd_Char(' ');
Lcd_Char((heart_rate / 100) + 48);
Lcd_Char(((heart_rate / 10) % 10) + 48);
Lcd_Char((heart_rate % 10) + 48);
__delay_ms(1000);
Lcd_Clear();
Lcd_Set_Cursor(1, 1);
Lcd_String("Heart Rate:");
}
}
// LCD Functions void Lcd_Init() { Lcd_Command(0x02); Lcd_Command(0x28); Lcd_Command(0x0C); Lcd_Command(0x06); Lcd_Command(0x01); }
void Lcd_Command(unsigned char cmd) { RS = 0; D4 = (cmd >> 4) & 0x01; D5 = (cmd >> 5) & 0x01; D6 = (cmd >> 6) & 0x01; D7 = (cmd >> 7) & 0x01; EN = 1; __delay_ms(2); EN = 0;
D4 = (cmd >> 0) & 0x01;
D5 = (cmd >> 1) & 0x01;
D6 = (cmd >> 2) & 0x01;
D7 = (cmd >> 3) & 0x01;
EN = 1;
__delay_ms(2);
EN = 0;
}
void Lcd_Char(unsigned char data) { RS = 1; D4 = (data >> 4) & 0x01; D5 = (data >> 5) & 0x01; D6 = (data >> 6) & 0x01; D7 = (data >> 7) & 0x01; EN = 1; __delay_ms(2); EN = 0;
D4 = (data >> 0) & 0x01;
D5 = (data >> 1) & 0x01;
D6 = (data >> 2) & 0x01;
D7 = (data >> 3) & 0x01;
EN = 1;
__delay_ms(2);
EN = 0;
}
void Lcd_String(const char *str) { while (*str != '\0') { Lcd_Char(*str); str++; } }
void Lcd_Set_Cursor(unsigned char row, unsigned char column) { if (row == 1) Lcd_Command(0x80 + column - 1); else if (row == 2) Lcd_Command(0xC0 + column - 1); }
void Lcd_Clear() { Lcd_Command(0x01); __delay_ms(2); }