// Include necessary header files
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define constants for the Max30100 oximeter
#define MAX30100_SDA_PIN RC4
#define MAX30100_SCL_PIN RC5
// Define constants for the HC05 Bluetooth module
#define HC05_TX_PIN RC6
#define HC05_RX_PIN RC7
// Define I2C address
#define MAX30100_ADDRESS 0xA0
// Define I2C clock speed
#define I2C_CLOCK_SPEED 400000
// Define LCD pins
sbit LCD_RS at RB0_bit;
sbit LCD_RW at RB1_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
// Function to initialize I2C
void initI2C() {
SSPADD = 0x09;
SSPSTAT &= ~(1 << SMP);
SSPCON1 |= (1 << SSPEN);
// Function to initialize MAX30100
void initMax30100() {
initI2C();
configMAX30100();
// Configure MAX30100 I2C registers
void configMAX30100() {
I2C1_Write(MAX30100_ADDRESS, 0x06, 0x02);
I2C1_Write(MAX30100_ADDRESS, 0x07, 0x03);
I2C1_Write(MAX30100_ADDRESS, 0x09, 0x04);
// Function to read pulse data from MAX30100 with error handling
int readPulseData() {
int pulseData = 0;
I2C1_Start();
if (I2C1_Write(MAX30100_ADDRESS) == 0) {
I2C1_Write(0x01);
I2C1_ReStart();
if (I2C1_Write(MAX30100_ADDRESS | 0x01) == 0) {
pulseData = I2C1_Read(0);
I2C1_Stop();
return pulseData;
I2C1_Stop();
return -1; // Indicate an error occurred
// Function to send pulse data to HC05 Bluetooth module
void sendPulseDataToBluetooth(int pulseData) {
char pulseDataStr[10];
IntToStr(pulseData, pulseDataStr);
UART1_Write_Text(pulseDataStr);
UART1_Write(0x0D);
UART1_Write(0x0A);
}
// Function to display pulse data on LCD
void displayPulseDataOnLCD(int pulseData) {
char pulseDataStr[10];
IntToStr(pulseData, pulseDataStr);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1, 1, "Pulse: ");
Lcd_Out(1, 7, pulseDataStr);
// Function to initialize LCD
void initLCD() {
TRISB &= ~(1 << LCD_RS);
TRISB &= ~(1 << LCD_RW);
TRISB &= ~(1 << LCD_EN);
TRISD &= ~(1 << LCD_D4);
TRISD &= ~(1 << LCD_D5);
TRISD &= ~(1 << LCD_D6);
TRISD &= ~(1 << LCD_D7);
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_TWO_LINE);
// Function to initialize UART1
void initUART1() {
TRISC &= ~(1 << HC05_TX_PIN);
TRISC &= ~(1 << HC05_RX_PIN);
UART1_Init(9600);
UART1_Enable();
UART1_DataBits(8);
UART1_StopBits(1);
UART1_Parity(NONE_PARITY);
void main() {
initMax30100();
initLCD();
initUART1();
while (1) {
int pulseData = readPulseData();
if (pulseData >= 0) { // Check for valid data
sendPulseDataToBluetooth(pulseData);
displayPulseDataOnLCD(pulseData);
} else {
// Handle error (e.g., display an error message on LCD)
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1, 1, "Error reading pulse data");
Delay_ms(100);
}