diff options
| author | Joseph Bryant <jbryant0653@floridapoly.edu> | 2024-04-17 15:33:18 -0400 |
|---|---|---|
| committer | Joseph Bryant <jbryant0653@floridapoly.edu> | 2024-04-17 15:33:18 -0400 |
| commit | 90d226a838a5c7f2d6ea86a529241b3f571f7130 (patch) | |
| tree | 51a5296999a88c2cbe7b6f073a6e5a8b42effc35 /lcd.c | |
| parent | 9f6c3500159cd828d633345d1fb39250b4e5669b (diff) | |
Garbage LCD Code
Diffstat (limited to 'lcd.c')
| -rw-r--r-- | lcd.c | 74 |
1 files changed, 71 insertions, 3 deletions
| @@ -1,7 +1,75 @@ | |||
| 1 | #include <avr/io.h> | 1 | #include <avr/io.h> |
| 2 | #include <util/delay.h> | ||
| 2 | 3 | ||
| 3 | int | 4 | // Define LCD control pin connections |
| 4 | main() | 5 | #define LCD_RS_PIN PB0 |
| 5 | { | 6 | #define LCD_RW_PIN PB1 |
| 7 | #define LCD_E_PIN PB2 | ||
| 8 | // Define LCD data pin connections | ||
| 9 | #define LCD_DATA_PORT PORTD | ||
| 10 | #define LCD_DATA_DDR DDRD | ||
| 6 | 11 | ||
| 12 | // Function prototypes | ||
| 13 | void LCD_init(); | ||
| 14 | void LCD_command(uint8_t cmd); | ||
| 15 | void LCD_write(uint8_t data); | ||
| 16 | void LCD_send(char* str); | ||
| 17 | |||
| 18 | int main() { | ||
| 19 | // Set control pins as outputs | ||
| 20 | DDRB |= (1 << LCD_RS_PIN) | (1 << LCD_RW_PIN) | (1 << LCD_E_PIN); | ||
| 21 | // Set data pins as outputs | ||
| 22 | LCD_DATA_DDR = 0xFF; | ||
| 23 | |||
| 24 | // Initialize LCD | ||
| 25 | LCD_init(); | ||
| 26 | |||
| 27 | // Display "Hello, World!" | ||
| 28 | LCD_send("Hello, World!"); | ||
| 29 | |||
| 30 | while (1) { | ||
| 31 | // Your main code here | ||
| 32 | } | ||
| 33 | |||
| 34 | return 0; | ||
| 35 | } | ||
| 36 | |||
| 37 | // Initialize LCD | ||
| 38 | void LCD_init() { | ||
| 39 | _delay_ms(15); // Delay for power-on | ||
| 40 | LCD_command(0x38); // Function Set: 8-bit data, 2-line display, 5x8 font | ||
| 41 | LCD_command(0x0C); // Display ON, Cursor OFF, Blink OFF | ||
| 42 | LCD_command(0x01); // Clear display | ||
| 43 | _delay_ms(2); // Delay for Clear Display command | ||
| 44 | LCD_command(0x06); // Entry Mode Set: Increment cursor, No display shift | ||
| 7 | } | 45 | } |
| 46 | |||
| 47 | // Send command to LCD | ||
| 48 | void LCD_command(uint8_t cmd) { | ||
| 49 | LCD_RS_PORT &= ~(1 << LCD_RS_PIN); // Set RS low for command mode | ||
| 50 | LCD_RW_PORT &= ~(1 << LCD_RW_PIN); // Set RW low for write mode | ||
| 51 | LCD_DATA_PORT = cmd; // Send command to data port | ||
| 52 | LCD_E_PORT |= (1 << LCD_E_PIN); // Enable LCD | ||
| 53 | _delay_us(1); // Short delay | ||
| 54 | LCD_E_PORT &= ~(1 << LCD_E_PIN); // Disable LCD | ||
| 55 | _delay_us(100); // Delay for command execution | ||
| 56 | } | ||
| 57 | |||
| 58 | // Write data to LCD | ||
| 59 | void LCD_write(uint8_t data) { | ||
| 60 | LCD_RS_PORT |= (1 << LCD_RS_PIN); // Set RS high for data mode | ||
| 61 | LCD_RW_PORT &= ~(1 << LCD_RW_PIN); // Set RW low for write mode | ||
| 62 | LCD_DATA_PORT = data; // Send data to data port | ||
| 63 | LCD_E_PORT |= (1 << LCD_E_PIN); // Enable LCD | ||
| 64 | _delay_us(1); // Short delay | ||
| 65 | LCD_E_PORT &= ~(1 << LCD_E_PIN); // Disable LCD | ||
| 66 | _delay_us(100); // Delay for data execution | ||
| 67 | } | ||
| 68 | |||
| 69 | // Send string to LCD | ||
| 70 | void LCD_send(char* str) { | ||
| 71 | while (*str) { | ||
| 72 | LCD_write(*str++); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
