avr_piano/lcd.c

90 lines
2.0 KiB
C
Raw Normal View History

2024-04-17 14:07:35 -04:00
#include <avr/io.h>
2024-04-17 19:04:16 -04:00
#define F_CPU 16000000UL
2024-04-17 15:33:18 -04:00
#include <util/delay.h>
2024-04-17 14:07:35 -04:00
2024-04-17 15:33:18 -04:00
// Define LCD control pin connections
2024-04-17 19:04:16 -04:00
#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
2024-04-17 15:33:18 -04:00
// Define LCD data pin connections
2024-04-17 19:04:16 -04:00
#define LCD_DATA_PORT PORTD
#define LCD_DATA_DDR DDRD
2024-04-17 14:07:35 -04:00
2024-04-17 15:33:18 -04:00
// Function prototypes
void LCD_init();
void LCD_command(uint8_t cmd);
void LCD_write(uint8_t data);
void LCD_send(char* str);
2024-04-17 19:04:16 -04:00
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;
2024-04-17 15:33:18 -04:00
2024-04-17 19:04:16 -04:00
// Initialize LCD
LCD_init();
2024-04-17 15:33:18 -04:00
2024-04-17 19:04:16 -04:00
// Display "Hello, World!"
LCD_send("Hello, World!");
2024-04-17 15:33:18 -04:00
2024-04-17 19:04:16 -04:00
for (;;) {
// Your main code here
}
2024-04-17 15:33:18 -04:00
2024-04-17 19:04:16 -04:00
return 0;
2024-04-17 15:33:18 -04:00
}
// Initialize LCD
2024-04-17 19:04:16 -04:00
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
2024-04-17 14:07:35 -04:00
}
2024-04-17 15:33:18 -04:00
// Send command to LCD
2024-04-17 19:04:16 -04:00
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
2024-04-17 15:33:18 -04:00
}
// Write data to LCD
2024-04-17 19:04:16 -04:00
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
2024-04-17 15:33:18 -04:00
}
// Send string to LCD
2024-04-17 19:04:16 -04:00
void
LCD_send(char *str)
{
while (*str)
LCD_write(*str++);
2024-04-17 15:33:18 -04:00
}