#include <stdint.h>
#include <stdio.h>
// Define register addresses for control and data ports
#define LCD_RS (*((volatile uint8_t *)0x40000007)) // Pin 7
#define LCD_EN (*((volatile uint8_t *)0x40000006)) // Pin 6
#define LCD_D4 (*((volatile uint8_t *)0x4000000C)) // Pin 12
#define LCD_D5 (*((volatile uint8_t *)0x4000000A)) // Pin 10
#define LCD_D6 (*((volatile uint8_t *)0x40000009)) // Pin 9
#define LCD_D7 (*((volatile uint8_t *)0x40000008)) // Pin 8
// Function to send data to LCD
void lcd_send_data(uint8_t data) {
LCD_RS = 1; // RS high for data
LCD_EN = 1; // Enable high
LCD_D4 = (data >> 4) & 0x01; // Send higher nibble
LCD_D5 = (data >> 5) & 0x01;
LCD_D6 = (data >> 6) & 0x01;
LCD_D7 = (data >> 7) & 0x01;
LCD_EN = 0; // Enable low
LCD_EN = 1; // Enable high
LCD_D4 = (data >> 0) & 0x01; // Send lower nibble
LCD_D5 = (data >> 1) & 0x01;
LCD_D6 = (data >> 2) & 0x01;
LCD_D7 = (data >> 3) & 0x01;
LCD_EN = 0; // Enable low
}
// Function to send command to LCD
void lcd_send_command(uint8_t command) {
LCD_RS = 0; // RS low for command
LCD_EN = 1; // Enable high
LCD_D4 = (command >> 4) & 0x01; // Send higher nibble
LCD_D5 = (command >> 5) & 0x01;
LCD_D6 = (command >> 6) & 0x01;
LCD_D7 = (command >> 7) & 0x01;
LCD_EN = 0; // Enable low
LCD_EN = 1; // Enable high
LCD_D4 = (command >> 0) & 0x01; // Send lower nibble
LCD_D5 = (command >> 1) & 0x01;
LCD_D6 = (command >> 2) & 0x01;
LCD_D7 = (command >> 3) & 0x01;
LCD_EN = 0; // Enable low
}
// Function to initialize LCD
void lcd_init() {
// Initialization sequence as per datasheet
lcd_send_command(0x33); // Initialize
lcd_send_command(0x32); // Set to 4-bit mode
lcd_send_command(0x28); // 2 line, 5x8 matrix
lcd_send_command(0x0C); // Display on, cursor off, blink off
lcd_send_command(0x01); // Clear display
lcd_send_command(0x06); // Increment cursor
}
// Function to print string on LCD
void lcd_print_string(char *str) {
while (*str) {
lcd_send_data(*str++);
}
}
// Function to print float on LCD
void lcd_print_float(float f) {
char buffer[16];
sprintf(buffer, "%.2f", f); // Format float with 2 decimal places
lcd_print_string(buffer);
}
int main() {
// Initialize LCD
lcd_init();
// Print "Temperature" at first line
lcd_send_command(0x80); // Set cursor to beginning of first line
lcd_print_string("Temperature");
// Print temperature value
lcd_send_command(0xC0); // Set cursor to beginning of second line
lcd_print_float(34); // Assuming initial temperature is 34
while (1) {
// Your loop code here
}
return 0;
}