MikroC Code for-WPS Officehh

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Here's an example code for a pulse oximeter project using PIC16F877A, MAX30100, LCD 2x16

alphanumeric, and HC05 Bluetooth module:

```

#include <xc.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// Configuration bits

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)

#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)

#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)

#pragma config MCLRE = OFF // MCLR Pin Function Select bit (MCLR/VPP pin function is digital I/O)

#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

#pragma config BOREN = OFF // Brown-out Reset Enable bit (Brown-out reset disabled)

#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled)

#pragma config IESO = OFF // Internal/External Switchover bit (Internal/External switchover mode is
disabled)

#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-safe clock monitor disabled)

// MAX30100 I2C address

#define MAX30100_ADDRESS 0x57

// MAX30100 register addresses

#define MAX30100_REG_FIFO_WR_PTR 0x04


#define MAX30100_REG_FIFO_RD_PTR 0x05

#define MAX30100_REG_FIFO_DATA 0x06

#define MAX30100_REG_MODE_CONFIG 0x09

#define MAX30100_REG_SPO2_CONFIG 0x0A

#define MAX30100_REG_LED_CONFIG 0x0C

#define MAX30100_REG_TEMP_INT 0x1F

#define MAX30100_REG_TEMP_FRAC 0x20

#define MAX30100_REG_PROX_INT 0x21

// LCD pins

#define LCD_RS PORTBbits.RB0

#define LCD_RW PORTBbits.RB1

#define LCD_EN PORTBbits.RB2

#define LCD_DATA PORTD

// HC05 Bluetooth module pins

#define HC05_TX PORTBbits.RB4

#define HC05_RX PORTBbits.RB5

// Function prototypes

void init_lcd(void);

void lcd_write_char(char data);

void lcd_write_string(char *str);

void init_max30100(void);

void max30100_write_register(uint8_t reg, uint8_t data);


uint8_t max30100_read_register(uint8_t reg);

void init_hc05(void);

void hc05_write_data(char *data);

// Global variables

uint16_t ir_buffer[100]; // IR LED data buffer

uint16_t red_buffer[100]; // Red LED data buffer

uint8_t spo2; // SPO2 value

uint8_t heart_rate; // Heart rate value

void main(void) {

// Initialize oscillator

OSCCON = 0x70; // Set oscillator to 4MHz

// Initialize LCD

init_lcd();

// Initialize MAX30100

init_max30100();

// Initialize HC05 Bluetooth module

init_hc05();

while(1) {

// Read IR and Red LED data from MAX30100


for (uint8_t i = 0; i < 100; i++) {

ir_buffer[i] = max30100_read_register(MAX30100_REG_FIFO_DATA);

red_buffer[i] = max30100_read_register(MAX30100_REG_FIFO_DATA);

// Calculate SPO2 and heart rate

spo2 = calculate_spo2(ir_buffer, red_buffer);

heart_rate = calculate_heart_rate(ir_buffer);

// Display SPO2 and heart rate on LCD

lcd_write_string("SPO2: ");

lcd_write_char(spo2 + '0');

lcd_write_string(" HR: ");

lcd_write_char(heart_rate + '0');

// Send SPO2 and heart rate data to HC05 Bluetooth module

char data[10];

sprintf(data, "SPO2:%d HR:%d", spo2, heart_rate);

hc05_write_data(data);

// Wait for 1 second

__delay_ms(1000);

}
void init_lcd(void) {

// Initialize LCD pins

LCD_RS = 0;

LCD_RW = 0;

LCD_EN = 0;

LCD_DATA = 0;

// Initialize LCD

lcd_write_char(0x33); // Set 8-bit mode

lcd_write_char(0x32); // Set 4-bit mode

lcd_write_char(0x28); // Set 4-bit mode, 2 lines, 5x8 font

lcd_write_char(0x0C); // Turn on display, cursor off

lcd_write_char(0x01); // Clear display

```

You might also like