make lcd.c compile

This commit is contained in:
Vineet K 2024-04-17 19:04:16 -04:00 committed by Shokara Kou
parent 90d226a838
commit f5713538fe

30
lcd.c
View File

@ -1,10 +1,15 @@
#include <avr/io.h> #include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h> #include <util/delay.h>
// Define LCD control pin connections // Define LCD control pin connections
#define LCD_RS_PIN PB0 #define LCD_RS_PIN PB0
#define LCD_RW_PIN PB1 #define LCD_RW_PIN PB1
#define LCD_E_PIN PB2 #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 pin connections
#define LCD_DATA_PORT PORTD #define LCD_DATA_PORT PORTD
#define LCD_DATA_DDR DDRD #define LCD_DATA_DDR DDRD
@ -15,7 +20,9 @@ void LCD_command(uint8_t cmd);
void LCD_write(uint8_t data); void LCD_write(uint8_t data);
void LCD_send(char* str); void LCD_send(char* str);
int main() { int
main()
{
// Set control pins as outputs // Set control pins as outputs
DDRB |= (1 << LCD_RS_PIN) | (1 << LCD_RW_PIN) | (1 << LCD_E_PIN); DDRB |= (1 << LCD_RS_PIN) | (1 << LCD_RW_PIN) | (1 << LCD_E_PIN);
// Set data pins as outputs // Set data pins as outputs
@ -27,7 +34,7 @@ int main() {
// Display "Hello, World!" // Display "Hello, World!"
LCD_send("Hello, World!"); LCD_send("Hello, World!");
while (1) { for (;;) {
// Your main code here // Your main code here
} }
@ -35,7 +42,9 @@ int main() {
} }
// Initialize LCD // Initialize LCD
void LCD_init() { void
LCD_init()
{
_delay_ms(15); // Delay for power-on _delay_ms(15); // Delay for power-on
LCD_command(0x38); // Function Set: 8-bit data, 2-line display, 5x8 font LCD_command(0x38); // Function Set: 8-bit data, 2-line display, 5x8 font
LCD_command(0x0C); // Display ON, Cursor OFF, Blink OFF LCD_command(0x0C); // Display ON, Cursor OFF, Blink OFF
@ -45,7 +54,9 @@ void LCD_init() {
} }
// Send command to LCD // Send command to LCD
void LCD_command(uint8_t cmd) { void
LCD_command(uint8_t cmd)
{
LCD_RS_PORT &= ~(1 << LCD_RS_PIN); // Set RS low for command mode 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_RW_PORT &= ~(1 << LCD_RW_PIN); // Set RW low for write mode
LCD_DATA_PORT = cmd; // Send command to data port LCD_DATA_PORT = cmd; // Send command to data port
@ -56,7 +67,9 @@ void LCD_command(uint8_t cmd) {
} }
// Write data to LCD // Write data to LCD
void LCD_write(uint8_t data) { void
LCD_write(uint8_t data)
{
LCD_RS_PORT |= (1 << LCD_RS_PIN); // Set RS high for data mode 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_RW_PORT &= ~(1 << LCD_RW_PIN); // Set RW low for write mode
LCD_DATA_PORT = data; // Send data to data port LCD_DATA_PORT = data; // Send data to data port
@ -67,9 +80,10 @@ void LCD_write(uint8_t data) {
} }
// Send string to LCD // Send string to LCD
void LCD_send(char* str) { void
while (*str) { LCD_send(char *str)
{
while (*str)
LCD_write(*str++); LCD_write(*str++);
}
} }