Full LCD setup with buttons A5 and A4 working!

This commit is contained in:
Joseph Bryant 2024-04-21 15:10:35 -04:00
parent 3f358c19e5
commit c2fe984ed4

159
lcd.c
View File

@ -1,7 +1,7 @@
#include <avr/io.h> //standard AVR header
#define F_CPU 16000000UL // THE CPU FREQUENCY
#include <util/delay.h> //delay header
#include <avr/interrupt.h> //interrupt header
#define LCD_DPRT PORTD //LCD DATA PORT
#define LCD_DDDR DDRD //LCD DATA DDR
@ -12,32 +12,43 @@
#define LCD_RS 0 //LCD RS
#define LCD_EN 1 //LCD EN
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
// Define your buffer sizes
#define SERIAL_BUFFER_SIZE 20
#define NOTES_BUFFER_SIZE 20
// Global variables
char serialBuffer[SERIAL_BUFFER_SIZE];
char notesBuffer[NOTES_BUFFER_SIZE];
int serialIndex = 0;
int notesIndex = 0;
volatile int buttonState = 0; // Holds the state of all buttons
void lcd_putValue(unsigned char val)
{
LCD_DPRT &= 0x0F;
LCD_DPRT |= val<<4; //send cmnd to data port
LCD_CPRT |= (1<<LCD_EN); //EN = 1 for H-to-L pulse
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
LCD_CPRT &= ~(1 << LCD_EN); //EN = 0 for H-to-L pulse
_delay_us(100); //wait to make enable wide
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
void lcdCommand(unsigned char cmnd)
{
LCD_CPRT &= ~(1 << LCD_RS); //RS = 0 for command
lcd_putValue(cmnd);
}
//*******************************************************
void lcdData( unsigned char data )
void lcdData(unsigned char data)
{
LCD_CPRT |= (1<<LCD_RS); //RS = 1 for data
LCD_CPRT |= (1 << LCD_RS); //RS = 1 for data
lcd_putValue(data);
}
@ -63,65 +74,137 @@ void lcd_hideCursor()
void lcd_init()
{
LCD_DDDR |= 0xF0;
LCD_CDDR |= (1<<LCD_RS)|(1<<LCD_EN);
LCD_CPRT &=~(1<<LCD_EN); //LCD_EN = 0
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);
unsigned char firstCharAdr[] = { 0x80, 0xC0, 0x94, 0xD4 }; //Table 12-4
lcdCommand(firstCharAdr[y - 1] + x - 1);
_delay_us(100);
}
//*******************************************************
void lcd_print( char * str )
void lcd_print(char *str)
{
unsigned char i = 0;
while(str[i] != 0) //while it is not end of string
while (str[i] != 0) //while it is not end of string
{
lcdData(str[i]);
i++ ;
i++;
}
}
char getSerialData() {
// Read serial data and return it
return ' ';
}
char getNote() {
// Read notes data and return it
return ' ';
}
void setup() {
// Initialize Serial communication
// Initialize LCD
// Initialize buttons as inputs
// Set pins A4 and A5 as inputs with pull-up resistors enabled
DDRC &= ~((1 << DDC4) | (1 << DDC5));
PORTC |= (1 << PORTC4) | (1 << PORTC5);
// Enable Pin Change Interrupt on pins PC4 and PC5
PCMSK1 |= (1 << PCINT12) | (1 << PCINT13); // Enable PCINT for pins PC4 and PC5
PCICR |= (1 << PCIE1); // Enable Pin Change Interrupt 1
sei(); // Enable global interrupts
}
ISR(PCINT1_vect) {
// Update button state when any pin changes state
for (unsigned char i = 0; i <= 5; i++) {
if (PINC & (1 << i)) {
buttonState = i;
break;
}
}
}
void displaySerialData() {
lcd_clear();
lcd_gotoxy(1, 1);
while (!(buttonState == 5)) {
char newData = getSerialData();
if (newData != '\0') {
// Add new data to the left side of line 1 of the LCD
// Push existing data to the right
// Display on LCD
}
_delay_ms(100); // Adjust delay as needed
}
}
void displayNotes() {
lcd_clear();
lcd_gotoxy(1, 1);
while (buttonState == 4) {
char newNote = getNote();
if (newNote != '\0') {
// Add new note to the right side of line 1 of the LCD
// Push existing notes to the left
// Display on LCD
}
_delay_ms(1000); // Adjust delay as needed
}
}
//*******************************************************
int main(void)
{
int main(void) {
lcd_init();
lcd_init();
lcd_hideCursor();
while(1)
{
setup();
lcd_clear();
while (1) {
// Check button state and perform actions accordingly
if (buttonState == 5) {
lcd_clear();
lcd_gotoxy(1,1);
lcd_print("The world is but");
lcd_print("Button A5 Works");
_delay_ms(100);
//displaySerialData();
} else if (buttonState == 4) {
lcd_clear();
lcd_gotoxy(1,2);
lcd_print("Button A4 Works");
_delay_ms(100);
//displayNotes();
}
}
return 0;
/*while (1) {
lcd_clear();
lcd_gotoxy(1, 1);
lcd_print("Note 1:");
lcd_gotoxy(1, 2);
lcd_print("one country");
lcd_showCursor();
_delay_ms(2500);
lcd_clear();
lcd_gotoxy(1,1);
lcd_gotoxy(1, 1);
lcd_print("and mankind its");
lcd_gotoxy(1,2);
lcd_gotoxy(1, 2);
lcd_print("citizens.");
_delay_ms(3500);
}
return 0;
return 0;*/
}