Sure!
Below is a complete example of interfacing an LCD, MAX30100 sensor, and HC-05 Bluetooth
module using mikroC. This code includes all necessary initializations, functions, and comments for
clarity.
▎Complete MikroC Code
#include <built_in.h>
#define MAX30100_ADDRESS 0x57 // I2C address for MAX30100
#define LCD_RS LATB0
#define LCD_EN LATB1
#define LCD_D4 LATB2
#define LCD_D5 LATB3
#define LCD_D6 LATB4
#define LCD_D7 LATB5
// Function Prototypes
void InitI2C();
void InitUART();
void InitMAX30100();
void ReadMAX30100();
void SendDataToBluetooth(unsigned char *data);
void Lcd_Init();
void Lcd_Cmd(unsigned char cmd);
void Lcd_Char(unsigned char data);
void Lcd_Out(unsigned char row, unsigned char col, unsigned char *text);
void Lcd_Clear();
char dataBuffer[50]; // Buffer for sending data over Bluetooth
void main() {
// Initialize peripherals
InitI2C();
InitUART();
Lcd_Init();
InitMAX30100();
while(1) {
// Read data from MAX30100
ReadMAX30100();
// Send data to Bluetooth
SendDataToBluetooth(dataBuffer);
// Display on LCD
Lcd_Clear();
Lcd_Out(1, 1, dataBuffer);
Delay_ms(1000); // Wait for a second before next read
}
}
void InitI2C() {
// Initialize I2C for MAX30100
I2C1_Init(100000); // 100kHz I2C speed
void InitUART() {
// Initialize UART for Bluetooth communication
UART1_Init(9600); // 9600 baud rate
void InitMAX30100() {
// Initialize MAX30100 settings
I2C1_Start();
I2C1_Wr(MAX30100_ADDRESS); // Write address
I2C1_Wr(0x06); // Write to mode configuration register
I2C1_Wr(0x03); // Set to multi-mode, 400nA
I2C1_Stop();
void ReadMAX30100() {
unsigned char redData[2], irData[2];
// Read Red and IR data from MAX30100
I2C1_Start();
I2C1_Wr(MAX30100_ADDRESS); // Write address
I2C1_Wr(0x05); // Point to the FIFO data register
I2C1_Repeated_Start();
I2C1_Wr(MAX30100_ADDRESS | 0x01); // Read address
redData[0] = I2C1_Rd(1); // Read Red data (MSB)
redData[1] = I2C1_Rd(1); // Read Red data (LSB)
irData[0] = I2C1_Rd(1); // Read IR data (MSB)
irData[1] = I2C1_Rd(0); // Read IR data (LSB, send NACK)
I2C1_Stop();
// Convert the data to string format for Bluetooth transmission
sprintf(dataBuffer, "Red: %u, IR: %u", (redData[0] << 8) | redData[1], (irData[0] << 8) | irData[1]);
void SendDataToBluetooth(unsigned char *data) {
// Send data via UART to Bluetooth module
while (*data) {
UART1_Write(*data++);
void Lcd_Init() {
TRISB = 0; // Set PORTB as output for LCD control
Delay_ms(20); // Wait for LCD to power up
Lcd_Cmd(0x33); // Initialize LCD in 4-bit mode
Lcd_Cmd(0x32); // Initialize LCD in 4-bit mode again
Lcd_Cmd(0x28); // 2 lines, 5x7 matrix
Lcd_Cmd(0x0C); // Display on cursor off
Lcd_Cmd(0x01); // Clear display
Delay_ms(2);
void Lcd_Cmd(unsigned char cmd) {
LCD_RS = 0; // Command mode
LCD_D4 = (cmd >> 4) & 1;
LCD_D5 = (cmd >> 5) & 1;
LCD_D6 = (cmd >> 6) & 1;
LCD_D7 = (cmd >> 7) & 1;
LCD_EN = 1; // Enable pulse
Delay_us(500);
LCD_EN = 0;
LCD_D4 = cmd & 1;
LCD_D5 = (cmd >> 1) & 1;
LCD_D6 = (cmd >> 2) & 1;
LCD_D7 = (cmd >> 3) & 1;
LCD_EN = 1; // Enable pulse
Delay_us(500);
LCD_EN = 0;
void Lcd_Char(unsigned char data) {
LCD_RS = 1; // Data mode
LCD_D4 = (data >> 4) & 1;
LCD_D5 = (data >> 5) & 1;
LCD_D6 = (data >> 6) & 1;
LCD_D7 = (data >> 7) & 1;
LCD_EN = 1; // Enable pulse
Delay_us(500);
LCD_EN = 0;
LCD_D4 = data & 1;
LCD_D5 = (data >> 1) & 1;
LCD_D6 = (data >> 2) & 1;
LCD_D7 = (data >> 3) & 1;
LCD_EN = 1; // Enable pulse
Delay_us(500);
LCD_EN = 0;
}
void Lcd_Out(unsigned char row, unsigned char col, unsigned char *text) {
if(row == 1)
Lcd_Cmd(0x80 + col - 1); // Move cursor to first line
else if(row == 2)
Lcd_Cmd(0xC0 + col - 1); // Move cursor to second line
ChatGPT 4 | Midjourney | Gemini | Claude, [10/17/24, 5:07 PM]
while(*text) {
Lcd_Char(*text++);
void Lcd_Clear() {
Lcd_Cmd(0x01); // Clear display command
▎Explanation of Components:
• Initialization:
• I2C: Initializes the I2C communication for the MAX30100 sensor.
• UART: Initializes UART for communication with the HC-05 Bluetooth module.
• LCD: Initializes the LCD display.
• Reading Data:
• The ReadMAX30100 function reads the Red and IR values from the MAX30100 sensor and formats
them into a string.
• Sending Data:
• The SendDataToBluetooth function sends the formatted string via UART to the HC-05 module.
• LCD Functions:
• Functions like Lcd_Init, Lcd_Cmd, Lcd_Char, Lcd_Out, and Lcd_Clear handle the operations of the LCD
display.
▎Notes:
• Ensure that you have connected the components correctly in your Proteus simulation or hardware
setup.
• Adjust pin definitions as per your specific microcontroller and connections.
• Make sure you have the necessary libraries included based on your mikroC version.
Feel free to modify the code according to your specific needs!