#include <Wire.h>
#include <math.h>
#define LCD_ADDR 0x27
volatile float result = 0.0f;
// Send data to the I2C LCD
void lcdWrite(uint8_t data) {
Wire.beginTransmission(LCD_ADDR);
Wire.write(data);
Wire.endTransmission();
}
// Send one nibble to the LCD
void lcdNibble(uint8_t data, uint8_t mode) {
uint8_t value = (data & 0xF0) | 0x08 | mode;
lcdWrite(value | 0x04);
delayMicroseconds(1);
lcdWrite(value);
delayMicroseconds(100);
}
// Send a full byte
void lcdSend(uint8_t data, uint8_t mode) {
lcdNibble(data, mode);
lcdNibble(data << 4, mode);
}
void lcdCommand(uint8_t command) {
lcdSend(command, 0);
}
void lcdPrint(const char *text) {
while (*text) {
lcdSend((uint8_t)*text++, 1);
}
}
// Initialize the LCD
void lcdInit() {
delay(100);
lcdNibble(0x30, 0);
delay(5);
lcdNibble(0x30, 0);
delay(5);
lcdNibble(0x30, 0);
delay(1);
lcdNibble(0x20, 0);
delay(1);
lcdCommand(0x28);
lcdCommand(0x08);
lcdCommand(0x01);
delay(5);
lcdCommand(0x06);
lcdCommand(0x0C);
}
void setup() {
Wire.setSDA(D14);
Wire.setSCL(D15);
Wire.begin();
lcdInit();
// Display student name
lcdCommand(0x80);
lcdPrint("Sharefh");
// Measure execution time
unsigned long startTime = millis();
for (int i = 0; i < 5000; i++) {
result += sqrtf((float)i) * sinf((float)i);
}
unsigned long duration = millis() - startTime;
// Display execution time
lcdCommand(0xC0);
lcdPrint("Time: ");
char buffer[12];
ultoa(duration, buffer, 10);
lcdPrint(buffer);
lcdPrint(" ms");
}
void loop() {
}Loading
st-nucleo-c031c6
st-nucleo-c031c6