0% found this document useful (0 votes)
14 views9 pages

LCD and MAX30100 with HC-05 Bluetooth Code

I want you can subscribe for

Uploaded by

h210595v
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

LCD and MAX30100 with HC-05 Bluetooth Code

I want you can subscribe for

Uploaded by

h210595v
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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!

Common questions

Powered by AI

Matching the baud rate between the microcontroller and the HC-05 module is crucial for ensuring reliable serial communication. A mismatch in baud rates can lead to data loss, errors, or corruption during transmission or reception, as the timing for sending and receiving bits would not align properly. The UART initialization in the mikroC code sets the baud rate to 9600, a standard rate that ensures compatibility and synchronization between the transmitting and receiving ends, thereby maintaining data integrity and communication stability .

The code maintains data integrity during transmission by using UART communication, which is initialized at a baud rate of 9600. The SendDataToBluetooth function transmits data character-by-character from the data buffer to the Bluetooth module, ensuring each byte is correctly relayed. This method of transmission minimizes the chance of data corruption, as each character is sent in a controlled, sequential manner .

To extend the functionality of the mikroC code, one could implement an additional feature to log sensor data over time, perhaps by integrating an SD card for storage. Another modification could be adding a feature to alarm or notify when certain threshold values are exceeded, such as a high IR level that might indicate an anomaly. Furthermore, enhancing the Bluetooth module setup to support pairing with multiple devices or enabling encryption for secured data transmission could be valuable for increased usability and security .

The Delay_ms() function is used to introduce a delay during the LCD initialization process. This delay is crucial to allow sufficient time for the LCD to power up and for the commands to be executed properly. Timing is critical when interfacing with LCDs to ensure that the initialization sequence is followed correctly, which helps prevent command overlap and ensures accurate display configuration .

The LCD display is initialized through a sequence of commands in the Lcd_Init function. This setup begins with setting PORTB as output for LCD control. The LCD is powered up and initialized in a 4-bit mode with specific commands (0x33, 0x32, and 0x28) to configure it for two lines with a 5x7 dot matrix. The display is turned on with the cursor off (0x0C), and the display is cleared (0x01). These commands ensure the LCD is ready for displaying data .

The code reads Red and IR data from the MAX30100 sensor using the I2C communication protocol. Each type of data is read as two bytes, representing the MSB and LSB. These two bytes are then combined into a single integer and converted into a string format using the sprintf function. This formatted string, which contains the Red and IR values, is stored in a data buffer that is subsequently used for transmission over Bluetooth via the SendDataToBluetooth function .

The code manages repetitive tasks within an infinite while loop in the main function. This loop continuously performs the sequence of reading sensor data with ReadMAX30100, sending the data to the Bluetooth module using SendDataToBluetooth, and displaying it on the LCD with Lcd_Out. By structuring these operations in a loop, the code ensures continuous monitoring and display, facilitating real-time data processing and visualization .

The code uses the Lcd_Out function to determine the position on the LCD where the text should appear. Depending on the row (either 1 or 2) and column number, it adjusts the cursor position using the appropriate command. For the first line, it adds the column offset to 0x80, and for the second line, to 0xC0. This approach uses the fact that LCD DDRAM addresses start at these base addresses for the respective lines, ensuring text is accurately placed .

Adapting the code to a different microcontroller platform may present challenges related to differences in peripheral interfaces, such as variations in I2C or UART implementations. The pin configuration and port definitions might need adjustments to match the new hardware. Additionally, differences in the compiler or development environment, such as required libraries and language syntax, may necessitate significant refactoring of the code. Ensuring that the specific timing functions and delays are compatible with the new platform is also crucial for maintaining reliable operation .

Initializing the I2C communication is crucial because it sets up the protocol for data exchange between the microcontroller and the MAX30100 sensor. The I2C communication allows for the sensor to be polled by the microcontroller, enabling data acquisition of the Red and IR values necessary for subsequent processing and transmission. Without this initialization, the microcontroller would be unable to communicate with the sensor, leading to a failure in obtaining and utilizing sensor data .

You might also like