import lcd.c from lab 10

This commit is contained in:
Vineet K 2024-04-20 15:42:35 -04:00 committed by Shokara Kou
parent ae4f92b170
commit 3f358c19e5

192
lcd.c
View File

@ -1,89 +1,127 @@
#include <avr/io.h>
#include <avr/io.h> //standard AVR header
#define F_CPU 16000000UL
#include <util/delay.h>
#define F_CPU 16000000UL // THE CPU FREQUENCY
#include <util/delay.h> //delay header
// Define LCD control pin connections
#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_DPRT PORTD //LCD DATA PORT
#define LCD_DDDR DDRD //LCD DATA DDR
#define LCD_DPIN PIND //LCD DATA PIN
#define LCD_CPRT PORTB //LCD COMMANDS PORT
#define LCD_CDDR DDRB //LCD COMMANDS DDR
#define LCD_CPIN PINB //LCD COMMANDS PIN
#define LCD_RS 0 //LCD RS
#define LCD_EN 1 //LCD EN
// Function prototypes
void LCD_init();
void LCD_command(uint8_t cmd);
void LCD_write(uint8_t data);
void LCD_send(char* str);
void lcd_putValue(unsigned char val){
LCD_DPRT &= 0x0F;
LCD_DPRT |= (val&0xF0); //send cmnd to data port
LCD_CPRT |= (1<<LCD_EN); //EN = 1 for H-to-L pulse
_delay_us(1); //wait to make enable wide
LCD_CPRT &= ~ (1<<LCD_EN); //EN = 0 for H-to-L pulse
_delay_us(100); //wait to make enable wide
int
main()
LCD_DPRT &= 0x0F;
LCD_DPRT |= val<<4; //send cmnd to data port
LCD_CPRT |= (1<<LCD_EN); //EN = 1 for H-to-L pulse
_delay_us(1); //wait to make enable wide
LCD_CPRT &= ~ (1<<LCD_EN); //EN = 0 for H-to-L pulse
_delay_us(100); //wait to make enable wide
}
//*******************************************************
void lcdCommand( unsigned char cmnd ){
LCD_CPRT &= ~ (1<<LCD_RS); //RS = 0 for command
lcd_putValue(cmnd);
}
//*******************************************************
void lcdData( unsigned char data )
{
// 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;
LCD_CPRT |= (1<<LCD_RS); //RS = 1 for data
lcd_putValue(data);
}
// Initialize LCD
LCD_init();
void lcd_clear()
{
lcdCommand(0x01);
_delay_us(1700);
}
// Display "Hello, World!"
LCD_send("Hello, World!");
void lcd_showCursor()
{
lcdCommand(0x0E);
_delay_us(50);
}
for (;;) {
// Your main code here
void lcd_hideCursor()
{
lcdCommand(0x0C);
_delay_us(50);
}
//*******************************************************
void lcd_init()
{
LCD_DDDR |= 0xF0;
LCD_CDDR |= (1<<LCD_RS)|(1<<LCD_EN);
LCD_CPRT &=~(1<<LCD_EN); //LCD_EN = 0
_delay_us(2000); //wait for init.
lcdCommand(0x33); //send $33 for init.
lcdCommand(0x32); //send $32 for init
lcdCommand(0x28); //init. LCD 2 line,5*7 matrix
lcdCommand(0x0e); //display on, cursor on
lcdCommand(0x06); //shift cursor right
lcd_clear();
}
//*******************************************************
void lcd_gotoxy(unsigned char x, unsigned char y)
{
unsigned char firstCharAdr[]={0x80,0xC0,0x94,0xD4};//Table 12-4
lcdCommand(firstCharAdr[y-1] + x - 1);
_delay_us(100);
}
//*******************************************************
void lcd_print( char * str )
{
unsigned char i = 0;
while(str[i] != 0) //while it is not end of string
{
lcdData(str[i]);
i++ ;
}
}
//*******************************************************
int main(void)
{
lcd_init();
lcd_hideCursor();
while(1)
{
lcd_clear();
lcd_gotoxy(1,1);
lcd_print("The world is but");
lcd_gotoxy(1,2);
lcd_print("one country");
lcd_showCursor();
_delay_ms(2500);
lcd_clear();
lcd_gotoxy(1,1);
lcd_print("and mankind its");
lcd_gotoxy(1,2);
lcd_print("citizens.");
_delay_ms(3500);
}
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
}
// 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
}
// 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
}
// Send string to LCD
void
LCD_send(char *str)
{
while (*str)
LCD_write(*str++);
}