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