diff options
| author | Vineet K <git@vineetk.net> | 2024-04-20 15:42:35 -0400 |
|---|---|---|
| committer | Shokara Kou <kou@13f0.net> | 2024-04-20 15:42:35 -0400 |
| commit | 3f358c19e5fe1d48285c4a9b3930a942f2f866fe (patch) | |
| tree | 2b86f2e2b5c70d7e21d079d93bb457bc2321f380 | |
| parent | ae4f92b170a05c4c62c335aa17d64f5f95860010 (diff) | |
import lcd.c from lab 10
| -rw-r--r-- | lcd.c | 180 |
1 files changed, 109 insertions, 71 deletions
| @@ -1,89 +1,127 @@ | |||
| 1 | #include <avr/io.h> | 1 | #include <avr/io.h> //standard AVR header |
| 2 | |||
| 3 | #define F_CPU 16000000UL | ||
| 4 | #include <util/delay.h> | ||
| 5 | |||
| 6 | // Define LCD control pin connections | ||
| 7 | #define LCD_RS_PIN PB0 | ||
| 8 | #define LCD_RW_PIN PB1 | ||
| 9 | #define LCD_E_PIN PB2 | ||
| 10 | #define LCD_RS_PORT PORTB | ||
| 11 | #define LCD_RW_PORT PORTB | ||
| 12 | #define LCD_E_PORT PORTB | ||
| 13 | // Define LCD data pin connections | ||
| 14 | #define LCD_DATA_PORT PORTD | ||
| 15 | #define LCD_DATA_DDR DDRD | ||
| 16 | |||
| 17 | // Function prototypes | ||
| 18 | void LCD_init(); | ||
| 19 | void LCD_command(uint8_t cmd); | ||
| 20 | void LCD_write(uint8_t data); | ||
| 21 | void LCD_send(char* str); | ||
| 22 | |||
| 23 | int | ||
| 24 | main() | ||
| 25 | { | ||
| 26 | // Set control pins as outputs | ||
| 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; | ||
| 30 | 2 | ||
| 31 | // Initialize LCD | 3 | #define F_CPU 16000000UL // THE CPU FREQUENCY |
| 32 | LCD_init(); | 4 | #include <util/delay.h> //delay header |
| 33 | 5 | ||
| 34 | // Display "Hello, World!" | 6 | #define LCD_DPRT PORTD //LCD DATA PORT |
| 35 | LCD_send("Hello, World!"); | 7 | #define LCD_DDDR DDRD //LCD DATA DDR |
| 8 | #define LCD_DPIN PIND //LCD DATA PIN | ||
| 9 | #define LCD_CPRT PORTB //LCD COMMANDS PORT | ||
| 10 | #define LCD_CDDR DDRB //LCD COMMANDS DDR | ||
| 11 | #define LCD_CPIN PINB //LCD COMMANDS PIN | ||
| 12 | #define LCD_RS 0 //LCD RS | ||
| 13 | #define LCD_EN 1 //LCD EN | ||
| 36 | 14 | ||
| 37 | for (;;) { | 15 | void lcd_putValue(unsigned char val){ |
| 38 | // Your main code here | 16 | LCD_DPRT &= 0x0F; |
| 39 | } | 17 | LCD_DPRT |= (val&0xF0); //send cmnd to data port |
| 18 | LCD_CPRT |= (1<<LCD_EN); //EN = 1 for H-to-L pulse | ||
| 19 | _delay_us(1); //wait to make enable wide | ||
| 20 | LCD_CPRT &= ~ (1<<LCD_EN); //EN = 0 for H-to-L pulse | ||
| 21 | _delay_us(100); //wait to make enable wide | ||
| 40 | 22 | ||
| 41 | return 0; | 23 | LCD_DPRT &= 0x0F; |
| 24 | LCD_DPRT |= val<<4; //send cmnd to data port | ||
| 25 | LCD_CPRT |= (1<<LCD_EN); //EN = 1 for H-to-L pulse | ||
| 26 | _delay_us(1); //wait to make enable wide | ||
| 27 | LCD_CPRT &= ~ (1<<LCD_EN); //EN = 0 for H-to-L pulse | ||
| 28 | _delay_us(100); //wait to make enable wide | ||
| 42 | } | 29 | } |
| 43 | 30 | ||
| 44 | // Initialize LCD | 31 | //******************************************************* |
| 45 | void | 32 | void lcdCommand( unsigned char cmnd ){ |
| 46 | LCD_init() | 33 | LCD_CPRT &= ~ (1<<LCD_RS); //RS = 0 for command |
| 34 | lcd_putValue(cmnd); | ||
| 35 | } | ||
| 36 | |||
| 37 | //******************************************************* | ||
| 38 | void lcdData( unsigned char data ) | ||
| 47 | { | 39 | { |
| 48 | _delay_ms(15); // Delay for power-on | 40 | LCD_CPRT |= (1<<LCD_RS); //RS = 1 for data |
| 49 | LCD_command(0x38); // Function Set: 8-bit data, 2-line display, 5x8 font | 41 | lcd_putValue(data); |
| 50 | LCD_command(0x0C); // Display ON, Cursor OFF, Blink OFF | ||
| 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 | ||
| 54 | } | 42 | } |
| 55 | 43 | ||
| 56 | // Send command to LCD | 44 | void lcd_clear() |
| 57 | void | ||
| 58 | LCD_command(uint8_t cmd) | ||
| 59 | { | 45 | { |
| 60 | LCD_RS_PORT &= ~(1 << LCD_RS_PIN); // Set RS low for command mode | 46 | lcdCommand(0x01); |
| 61 | LCD_RW_PORT &= ~(1 << LCD_RW_PIN); // Set RW low for write mode | 47 | _delay_us(1700); |
| 62 | LCD_DATA_PORT = cmd; // Send command 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 command execution | ||
| 67 | } | 48 | } |
| 68 | 49 | ||
| 69 | // Write data to LCD | 50 | void lcd_showCursor() |
| 70 | void | ||
| 71 | LCD_write(uint8_t data) | ||
| 72 | { | 51 | { |
| 73 | LCD_RS_PORT |= (1 << LCD_RS_PIN); // Set RS high for data mode | 52 | lcdCommand(0x0E); |
| 74 | LCD_RW_PORT &= ~(1 << LCD_RW_PIN); // Set RW low for write mode | 53 | _delay_us(50); |
| 75 | LCD_DATA_PORT = data; // Send data to data port | ||
| 76 | LCD_E_PORT |= (1 << LCD_E_PIN); // Enable LCD | ||
| 77 | _delay_us(1); // Short delay | ||
| 78 | LCD_E_PORT &= ~(1 << LCD_E_PIN); // Disable LCD | ||
| 79 | _delay_us(100); // Delay for data execution | ||
| 80 | } | 54 | } |
| 81 | 55 | ||
| 82 | // Send string to LCD | 56 | void lcd_hideCursor() |
| 83 | void | ||
| 84 | LCD_send(char *str) | ||
| 85 | { | 57 | { |
| 86 | while (*str) | 58 | lcdCommand(0x0C); |
| 87 | LCD_write(*str++); | 59 | _delay_us(50); |
| 88 | } | 60 | } |
| 89 | 61 | ||
| 62 | //******************************************************* | ||
| 63 | void lcd_init() | ||
| 64 | { | ||
| 65 | LCD_DDDR |= 0xF0; | ||
| 66 | LCD_CDDR |= (1<<LCD_RS)|(1<<LCD_EN); | ||
| 67 | |||
| 68 | LCD_CPRT &=~(1<<LCD_EN); //LCD_EN = 0 | ||
| 69 | _delay_us(2000); //wait for init. | ||
| 70 | |||
| 71 | lcdCommand(0x33); //send $33 for init. | ||
| 72 | lcdCommand(0x32); //send $32 for init | ||
| 73 | lcdCommand(0x28); //init. LCD 2 line,5*7 matrix | ||
| 74 | lcdCommand(0x0e); //display on, cursor on | ||
| 75 | lcdCommand(0x06); //shift cursor right | ||
| 76 | |||
| 77 | lcd_clear(); | ||
| 78 | } | ||
| 79 | |||
| 80 | //******************************************************* | ||
| 81 | void lcd_gotoxy(unsigned char x, unsigned char y) | ||
| 82 | { | ||
| 83 | unsigned char firstCharAdr[]={0x80,0xC0,0x94,0xD4};//Table 12-4 | ||
| 84 | lcdCommand(firstCharAdr[y-1] + x - 1); | ||
| 85 | _delay_us(100); | ||
| 86 | } | ||
| 87 | |||
| 88 | //******************************************************* | ||
| 89 | void lcd_print( char * str ) | ||
| 90 | { | ||
| 91 | unsigned char i = 0; | ||
| 92 | |||
| 93 | while(str[i] != 0) //while it is not end of string | ||
| 94 | { | ||
| 95 | lcdData(str[i]); | ||
| 96 | i++ ; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | //******************************************************* | ||
| 101 | int main(void) | ||
| 102 | { | ||
| 103 | lcd_init(); | ||
| 104 | lcd_hideCursor(); | ||
| 105 | |||
| 106 | while(1) | ||
| 107 | { | ||
| 108 | lcd_clear(); | ||
| 109 | lcd_gotoxy(1,1); | ||
| 110 | lcd_print("The world is but"); | ||
| 111 | lcd_gotoxy(1,2); | ||
| 112 | lcd_print("one country"); | ||
| 113 | |||
| 114 | lcd_showCursor(); | ||
| 115 | _delay_ms(2500); | ||
| 116 | |||
| 117 | lcd_clear(); | ||
| 118 | |||
| 119 | lcd_gotoxy(1,1); | ||
| 120 | lcd_print("and mankind its"); | ||
| 121 | lcd_gotoxy(1,2); | ||
| 122 | lcd_print("citizens."); | ||
| 123 | _delay_ms(3500); | ||
| 124 | } | ||
| 125 | |||
| 126 | return 0; | ||
| 127 | } \ No newline at end of file | ||
