diff options
| author | Joseph Bryant <jbryant0653@floridapoly.edu> | 2024-04-21 15:10:35 -0400 |
|---|---|---|
| committer | Joseph Bryant <jbryant0653@floridapoly.edu> | 2024-04-21 15:18:20 -0400 |
| commit | c2fe984ed4e6e0077747b1eb5ffa76e5a7c2fe5f (patch) | |
| tree | bc851cf16dea2357c045924f7f5b8f0243e775db /lcd.c | |
| parent | 3f358c19e5fe1d48285c4a9b3930a942f2f866fe (diff) | |
Full LCD setup with buttons A5 and A4 working!
Diffstat (limited to 'lcd.c')
| -rw-r--r-- | lcd.c | 209 |
1 files changed, 146 insertions, 63 deletions
| @@ -1,49 +1,60 @@ | |||
| 1 | #include <avr/io.h> //standard AVR header | 1 | #include <avr/io.h> //standard AVR header |
| 2 | 2 | #define F_CPU 16000000UL // THE CPU FREQUENCY | |
| 3 | #define F_CPU 16000000UL // THE CPU FREQUENCY | ||
| 4 | #include <util/delay.h> //delay header | 3 | #include <util/delay.h> //delay header |
| 5 | 4 | #include <avr/interrupt.h> //interrupt header | |
| 6 | #define LCD_DPRT PORTD //LCD DATA PORT | 5 | |
| 7 | #define LCD_DDDR DDRD //LCD DATA DDR | 6 | #define LCD_DPRT PORTD //LCD DATA PORT |
| 8 | #define LCD_DPIN PIND //LCD DATA PIN | 7 | #define LCD_DDDR DDRD //LCD DATA DDR |
| 9 | #define LCD_CPRT PORTB //LCD COMMANDS PORT | 8 | #define LCD_DPIN PIND //LCD DATA PIN |
| 10 | #define LCD_CDDR DDRB //LCD COMMANDS DDR | 9 | #define LCD_CPRT PORTB //LCD COMMANDS PORT |
| 11 | #define LCD_CPIN PINB //LCD COMMANDS PIN | 10 | #define LCD_CDDR DDRB //LCD COMMANDS DDR |
| 12 | #define LCD_RS 0 //LCD RS | 11 | #define LCD_CPIN PINB //LCD COMMANDS PIN |
| 13 | #define LCD_EN 1 //LCD EN | 12 | #define LCD_RS 0 //LCD RS |
| 14 | 13 | #define LCD_EN 1 //LCD EN | |
| 15 | void lcd_putValue(unsigned char val){ | 14 | |
| 15 | // Define your buffer sizes | ||
| 16 | #define SERIAL_BUFFER_SIZE 20 | ||
| 17 | #define NOTES_BUFFER_SIZE 20 | ||
| 18 | |||
| 19 | // Global variables | ||
| 20 | char serialBuffer[SERIAL_BUFFER_SIZE]; | ||
| 21 | char notesBuffer[NOTES_BUFFER_SIZE]; | ||
| 22 | int serialIndex = 0; | ||
| 23 | int notesIndex = 0; | ||
| 24 | volatile int buttonState = 0; // Holds the state of all buttons | ||
| 25 | void lcd_putValue(unsigned char val) | ||
| 26 | { | ||
| 16 | LCD_DPRT &= 0x0F; | 27 | LCD_DPRT &= 0x0F; |
| 17 | LCD_DPRT |= (val&0xF0); //send cmnd to data port | 28 | LCD_DPRT |= (val & 0xF0); //send cmnd to data port |
| 18 | LCD_CPRT |= (1<<LCD_EN); //EN = 1 for H-to-L pulse | 29 | LCD_CPRT |= (1 << LCD_EN); //EN = 1 for H-to-L pulse |
| 19 | _delay_us(1); //wait to make enable wide | 30 | _delay_us(1); //wait to make enable wide |
| 20 | LCD_CPRT &= ~ (1<<LCD_EN); //EN = 0 for H-to-L pulse | 31 | LCD_CPRT &= ~(1 << LCD_EN); //EN = 0 for H-to-L pulse |
| 21 | _delay_us(100); //wait to make enable wide | 32 | _delay_us(100); //wait to make enable wide |
| 22 | |||
| 23 | LCD_DPRT &= 0x0F; | 33 | LCD_DPRT &= 0x0F; |
| 24 | LCD_DPRT |= val<<4; //send cmnd to data port | 34 | LCD_DPRT |= val << 4; //send cmnd to data port |
| 25 | LCD_CPRT |= (1<<LCD_EN); //EN = 1 for H-to-L pulse | 35 | LCD_CPRT |= (1 << LCD_EN); //EN = 1 for H-to-L pulse |
| 26 | _delay_us(1); //wait to make enable wide | 36 | _delay_us(1); //wait to make enable wide |
| 27 | LCD_CPRT &= ~ (1<<LCD_EN); //EN = 0 for H-to-L pulse | 37 | LCD_CPRT &= ~(1 << LCD_EN); //EN = 0 for H-to-L pulse |
| 28 | _delay_us(100); //wait to make enable wide | 38 | _delay_us(100); //wait to make enable wide |
| 29 | } | 39 | } |
| 30 | 40 | ||
| 31 | //******************************************************* | 41 | //******************************************************* |
| 32 | void lcdCommand( unsigned char cmnd ){ | 42 | void lcdCommand(unsigned char cmnd) |
| 33 | LCD_CPRT &= ~ (1<<LCD_RS); //RS = 0 for command | 43 | { |
| 44 | LCD_CPRT &= ~(1 << LCD_RS); //RS = 0 for command | ||
| 34 | lcd_putValue(cmnd); | 45 | lcd_putValue(cmnd); |
| 35 | } | 46 | } |
| 36 | 47 | ||
| 37 | //******************************************************* | 48 | //******************************************************* |
| 38 | void lcdData( unsigned char data ) | 49 | void lcdData(unsigned char data) |
| 39 | { | 50 | { |
| 40 | LCD_CPRT |= (1<<LCD_RS); //RS = 1 for data | 51 | LCD_CPRT |= (1 << LCD_RS); //RS = 1 for data |
| 41 | lcd_putValue(data); | 52 | lcd_putValue(data); |
| 42 | } | 53 | } |
| 43 | 54 | ||
| 44 | void lcd_clear() | 55 | void lcd_clear() |
| 45 | { | 56 | { |
| 46 | lcdCommand(0x01); | 57 | lcdCommand(0x01); |
| 47 | _delay_us(1700); | 58 | _delay_us(1700); |
| 48 | } | 59 | } |
| 49 | 60 | ||
| @@ -63,65 +74,137 @@ void lcd_hideCursor() | |||
| 63 | void lcd_init() | 74 | void lcd_init() |
| 64 | { | 75 | { |
| 65 | LCD_DDDR |= 0xF0; | 76 | LCD_DDDR |= 0xF0; |
| 66 | LCD_CDDR |= (1<<LCD_RS)|(1<<LCD_EN); | 77 | LCD_CDDR |= (1 << LCD_RS) | (1 << LCD_EN); |
| 67 | 78 | LCD_CPRT &= ~(1 << LCD_EN); //LCD_EN = 0 | |
| 68 | LCD_CPRT &=~(1<<LCD_EN); //LCD_EN = 0 | ||
| 69 | _delay_us(2000); //wait for init. | 79 | _delay_us(2000); //wait for init. |
| 70 | |||
| 71 | lcdCommand(0x33); //send $33 for init. | 80 | lcdCommand(0x33); //send $33 for init. |
| 72 | lcdCommand(0x32); //send $32 for init | 81 | lcdCommand(0x32); //send $32 for init |
| 73 | lcdCommand(0x28); //init. LCD 2 line,5*7 matrix | 82 | lcdCommand(0x28); //init. LCD 2 line,5*7 matrix |
| 74 | lcdCommand(0x0e); //display on, cursor on | 83 | lcdCommand(0x0e); //display on, cursor on |
| 75 | lcdCommand(0x06); //shift cursor right | 84 | lcdCommand(0x06); //shift cursor right |
| 76 | 85 | lcd_clear(); | |
| 77 | lcd_clear(); | ||
| 78 | } | 86 | } |
| 79 | 87 | ||
| 80 | //******************************************************* | 88 | //******************************************************* |
| 81 | void lcd_gotoxy(unsigned char x, unsigned char y) | 89 | void lcd_gotoxy(unsigned char x, unsigned char y) |
| 82 | { | 90 | { |
| 83 | unsigned char firstCharAdr[]={0x80,0xC0,0x94,0xD4};//Table 12-4 | 91 | unsigned char firstCharAdr[] = { 0x80, 0xC0, 0x94, 0xD4 }; //Table 12-4 |
| 84 | lcdCommand(firstCharAdr[y-1] + x - 1); | 92 | lcdCommand(firstCharAdr[y - 1] + x - 1); |
| 85 | _delay_us(100); | 93 | _delay_us(100); |
| 86 | } | 94 | } |
| 87 | 95 | ||
| 88 | //******************************************************* | 96 | //******************************************************* |
| 89 | void lcd_print( char * str ) | 97 | void lcd_print(char *str) |
| 90 | { | 98 | { |
| 91 | unsigned char i = 0; | 99 | unsigned char i = 0; |
| 92 | 100 | while (str[i] != 0) //while it is not end of string | |
| 93 | while(str[i] != 0) //while it is not end of string | ||
| 94 | { | 101 | { |
| 95 | lcdData(str[i]); | 102 | lcdData(str[i]); |
| 96 | i++ ; | 103 | i++; |
| 97 | } | 104 | } |
| 98 | } | 105 | } |
| 99 | 106 | ||
| 107 | char getSerialData() { | ||
| 108 | // Read serial data and return it | ||
| 109 | return ' '; | ||
| 110 | } | ||
| 111 | |||
| 112 | char getNote() { | ||
| 113 | // Read notes data and return it | ||
| 114 | return ' '; | ||
| 115 | } | ||
| 116 | |||
| 117 | void setup() { | ||
| 118 | // Initialize Serial communication | ||
| 119 | // Initialize LCD | ||
| 120 | // Initialize buttons as inputs | ||
| 121 | // Set pins A4 and A5 as inputs with pull-up resistors enabled | ||
| 122 | DDRC &= ~((1 << DDC4) | (1 << DDC5)); | ||
| 123 | PORTC |= (1 << PORTC4) | (1 << PORTC5); | ||
| 124 | |||
| 125 | // Enable Pin Change Interrupt on pins PC4 and PC5 | ||
| 126 | PCMSK1 |= (1 << PCINT12) | (1 << PCINT13); // Enable PCINT for pins PC4 and PC5 | ||
| 127 | PCICR |= (1 << PCIE1); // Enable Pin Change Interrupt 1 | ||
| 128 | sei(); // Enable global interrupts | ||
| 129 | } | ||
| 130 | |||
| 131 | ISR(PCINT1_vect) { | ||
| 132 | // Update button state when any pin changes state | ||
| 133 | for (unsigned char i = 0; i <= 5; i++) { | ||
| 134 | if (PINC & (1 << i)) { | ||
| 135 | buttonState = i; | ||
| 136 | break; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | void displaySerialData() { | ||
| 142 | lcd_clear(); | ||
| 143 | lcd_gotoxy(1, 1); | ||
| 144 | while (!(buttonState == 5)) { | ||
| 145 | char newData = getSerialData(); | ||
| 146 | if (newData != '\0') { | ||
| 147 | // Add new data to the left side of line 1 of the LCD | ||
| 148 | // Push existing data to the right | ||
| 149 | // Display on LCD | ||
| 150 | } | ||
| 151 | _delay_ms(100); // Adjust delay as needed | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | void displayNotes() { | ||
| 156 | lcd_clear(); | ||
| 157 | lcd_gotoxy(1, 1); | ||
| 158 | while (buttonState == 4) { | ||
| 159 | char newNote = getNote(); | ||
| 160 | if (newNote != '\0') { | ||
| 161 | // Add new note to the right side of line 1 of the LCD | ||
| 162 | // Push existing notes to the left | ||
| 163 | // Display on LCD | ||
| 164 | } | ||
| 165 | _delay_ms(1000); // Adjust delay as needed | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 100 | //******************************************************* | 169 | //******************************************************* |
| 101 | int main(void) | 170 | int main(void) { |
| 102 | { | 171 | lcd_init(); |
| 103 | lcd_init(); | 172 | lcd_init(); |
| 104 | lcd_hideCursor(); | 173 | lcd_hideCursor(); |
| 105 | 174 | ||
| 106 | while(1) | 175 | setup(); |
| 107 | { | 176 | lcd_clear(); |
| 177 | while (1) { | ||
| 178 | // Check button state and perform actions accordingly | ||
| 179 | if (buttonState == 5) { | ||
| 180 | lcd_clear(); | ||
| 181 | lcd_gotoxy(1,1); | ||
| 182 | lcd_print("Button A5 Works"); | ||
| 183 | _delay_ms(100); | ||
| 184 | //displaySerialData(); | ||
| 185 | } else if (buttonState == 4) { | ||
| 186 | lcd_clear(); | ||
| 187 | lcd_gotoxy(1,2); | ||
| 188 | lcd_print("Button A4 Works"); | ||
| 189 | _delay_ms(100); | ||
| 190 | //displayNotes(); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | return 0; | ||
| 194 | /*while (1) { | ||
| 108 | lcd_clear(); | 195 | lcd_clear(); |
| 109 | lcd_gotoxy(1,1); | 196 | lcd_gotoxy(1, 1); |
| 110 | lcd_print("The world is but"); | 197 | lcd_print("Note 1:"); |
| 111 | lcd_gotoxy(1,2); | 198 | lcd_gotoxy(1, 2); |
| 112 | lcd_print("one country"); | 199 | lcd_print("one country"); |
| 113 | |||
| 114 | lcd_showCursor(); | 200 | lcd_showCursor(); |
| 115 | _delay_ms(2500); | 201 | _delay_ms(2500); |
| 116 | |||
| 117 | lcd_clear(); | 202 | lcd_clear(); |
| 118 | 203 | lcd_gotoxy(1, 1); | |
| 119 | lcd_gotoxy(1,1); | ||
| 120 | lcd_print("and mankind its"); | 204 | lcd_print("and mankind its"); |
| 121 | lcd_gotoxy(1,2); | 205 | lcd_gotoxy(1, 2); |
| 122 | lcd_print("citizens."); | 206 | lcd_print("citizens."); |
| 123 | _delay_ms(3500); | 207 | _delay_ms(3500); |
| 124 | } | 208 | } |
| 125 | 209 | return 0;*/ | |
| 126 | return 0; | 210 | } |
| 127 | } \ No newline at end of file | ||
