commit f5713538fe12b57c286188a2bf13016628f55a92
parent 90d226a838a5c7f2d6ea86a529241b3f571f7130
Author: Vineet K <git@vineetk.net>
Date: Wed, 17 Apr 2024 19:04:16 -0400
make lcd.c compile
Diffstat:
| M | lcd.c | | | 104 | +++++++++++++++++++++++++++++++++++++++++++++---------------------------------- |
1 file changed, 59 insertions(+), 45 deletions(-)
diff --git a/lcd.c b/lcd.c
@@ -1,13 +1,18 @@
#include <avr/io.h>
+
+#define F_CPU 16000000UL
#include <util/delay.h>
// Define LCD control pin connections
-#define LCD_RS_PIN PB0
-#define LCD_RW_PIN PB1
-#define LCD_E_PIN PB2
+#define LCD_RS_PIN PB0
+#define LCD_RW_PIN PB1
+#define LCD_E_PIN PB2
+#define LCD_RS_PORT PORTB
+#define LCD_RW_PORT PORTB
+#define LCD_E_PORT PORTB
// Define LCD data pin connections
-#define LCD_DATA_PORT PORTD
-#define LCD_DATA_DDR DDRD
+#define LCD_DATA_PORT PORTD
+#define LCD_DATA_DDR DDRD
// Function prototypes
void LCD_init();
@@ -15,61 +20,70 @@ void LCD_command(uint8_t cmd);
void LCD_write(uint8_t data);
void LCD_send(char* str);
-int main() {
- // Set control pins as outputs
- DDRB |= (1 << LCD_RS_PIN) | (1 << LCD_RW_PIN) | (1 << LCD_E_PIN);
- // Set data pins as outputs
- LCD_DATA_DDR = 0xFF;
+int
+main()
+{
+ // Set control pins as outputs
+ DDRB |= (1 << LCD_RS_PIN) | (1 << LCD_RW_PIN) | (1 << LCD_E_PIN);
+ // Set data pins as outputs
+ LCD_DATA_DDR = 0xFF;
- // Initialize LCD
- LCD_init();
+ // Initialize LCD
+ LCD_init();
- // Display "Hello, World!"
- LCD_send("Hello, World!");
+ // Display "Hello, World!"
+ LCD_send("Hello, World!");
- while (1) {
- // Your main code here
- }
+ for (;;) {
+ // Your main code here
+ }
- return 0;
+ return 0;
}
// Initialize LCD
-void LCD_init() {
- _delay_ms(15); // Delay for power-on
- LCD_command(0x38); // Function Set: 8-bit data, 2-line display, 5x8 font
- LCD_command(0x0C); // Display ON, Cursor OFF, Blink OFF
- LCD_command(0x01); // Clear display
- _delay_ms(2); // Delay for Clear Display command
- LCD_command(0x06); // Entry Mode Set: Increment cursor, No display shift
+void
+LCD_init()
+{
+ _delay_ms(15); // Delay for power-on
+ LCD_command(0x38); // Function Set: 8-bit data, 2-line display, 5x8 font
+ LCD_command(0x0C); // Display ON, Cursor OFF, Blink OFF
+ LCD_command(0x01); // Clear display
+ _delay_ms(2); // Delay for Clear Display command
+ LCD_command(0x06); // Entry Mode Set: Increment cursor, No display shift
}
// Send command to LCD
-void LCD_command(uint8_t cmd) {
- LCD_RS_PORT &= ~(1 << LCD_RS_PIN); // Set RS low for command mode
- LCD_RW_PORT &= ~(1 << LCD_RW_PIN); // Set RW low for write mode
- LCD_DATA_PORT = cmd; // Send command to data port
- LCD_E_PORT |= (1 << LCD_E_PIN); // Enable LCD
- _delay_us(1); // Short delay
- LCD_E_PORT &= ~(1 << LCD_E_PIN); // Disable LCD
- _delay_us(100); // Delay for command execution
+void
+LCD_command(uint8_t cmd)
+{
+ LCD_RS_PORT &= ~(1 << LCD_RS_PIN); // Set RS low for command mode
+ LCD_RW_PORT &= ~(1 << LCD_RW_PIN); // Set RW low for write mode
+ LCD_DATA_PORT = cmd; // Send command to data port
+ LCD_E_PORT |= (1 << LCD_E_PIN); // Enable LCD
+ _delay_us(1); // Short delay
+ LCD_E_PORT &= ~(1 << LCD_E_PIN); // Disable LCD
+ _delay_us(100); // Delay for command execution
}
// Write data to LCD
-void LCD_write(uint8_t data) {
- LCD_RS_PORT |= (1 << LCD_RS_PIN); // Set RS high for data mode
- LCD_RW_PORT &= ~(1 << LCD_RW_PIN); // Set RW low for write mode
- LCD_DATA_PORT = data; // Send data to data port
- LCD_E_PORT |= (1 << LCD_E_PIN); // Enable LCD
- _delay_us(1); // Short delay
- LCD_E_PORT &= ~(1 << LCD_E_PIN); // Disable LCD
- _delay_us(100); // Delay for data execution
+void
+LCD_write(uint8_t data)
+{
+ LCD_RS_PORT |= (1 << LCD_RS_PIN); // Set RS high for data mode
+ LCD_RW_PORT &= ~(1 << LCD_RW_PIN); // Set RW low for write mode
+ LCD_DATA_PORT = data; // Send data to data port
+ LCD_E_PORT |= (1 << LCD_E_PIN); // Enable LCD
+ _delay_us(1); // Short delay
+ LCD_E_PORT &= ~(1 << LCD_E_PIN); // Disable LCD
+ _delay_us(100); // Delay for data execution
}
// Send string to LCD
-void LCD_send(char* str) {
- while (*str) {
- LCD_write(*str++);
- }
+void
+LCD_send(char *str)
+{
+ while (*str)
+ LCD_write(*str++);
}