// void init_port(void);
// void outdata(char);
// void outcontrol(char);
// void init_lcd(void);
// void lcd_control_write(void);
// void write_data(char);
// void write_string(char);
// void set_cursor_second_row(void);
// void setup() {
// // put your setup code here, to run once:
// init_port();
// init_lcd();
// set_cursor_first_row();
// write_string("yeah this is my first written library");
// }
// void write_string(char *ptr) {
// volatile long count=0;
// while (*ptr != 0) {
// // if(count==16){
// // set_cursor_second_row();
// // lcd_control_write();
// // }
// write_data(*ptr); // Write character to the LCD
// ptr++;
// count++;
// }
// }
// void init_port(void){
// volatile char *portF_dir=(volatile char *)0x30;
// volatile char *portK_dir=(volatile char *)0x107;
// *portF_dir=0xFF;
// *portK_dir=0x03;
// }
// void outdata(char out_data){
// volatile char *portF_data;
// portF_data=0x31;
// *portF_data=out_data;
// }
// void outcontrol(char out_data){
// volatile char *portK_data;
// portK_data=0x108;
// *portK_data=out_data;
// }
// void init_lcd(void){
// outdata(0x38); //function set: 8bit ,2line , 5*8
// lcd_control_write(); //giving pulse to enable
// outdata(0x0F); //display on ,cursor blinking
// lcd_control_write();
// outdata(0x01); //clear display
// lcd_control_write();
// outdata(0x06); //auto increment after writing 1 char; entry mode
// lcd_control_write();
// }
// void lcd_control_write(void){
// outcontrol(0x01);
// delay(1);
// outcontrol(0x00);
// delay(1);
// }
// void write_data(char wr_data){
// outdata(wr_data);
// outcontrol(0x02); //giving pulse to enable and sending data RS= 1 as writing to register
// delay(1);
// outcontrol(0x03);
// delay(1);
// outcontrol(0x02);
// delay(1);
// }
// void set_cursor_second_row(void) {
// outdata(0xC0); // Address of the second row in 16x2 LCD(DDRAM)
// lcd_control_write();
// }
// void set_cursor_first_row(void) {
// outdata(0x80); // Address of the first row in 16x2 LCD(DDRAM)
// lcd_control_write();
// }
// void loop() {
// // put your main code here, to run repeatedly:
// outdata(0x1B); // 0001 10111
// lcd_control_write();
// delay(300);
// }
//*********************************************
//with contrast adjustment
// volatile int Contrast=0;
// void setup() {
// // put your setup code here, to run once:
// volatile char *portH_dir=(volatile char *)0x101;
// *portH_dir=0xFF;
// TCCR0A = (1 << COM0A1) | (1 << WGM00) | (1 << WGM01); // Fast PWM Mode
// TCCR0B = (1 << CS00); // No prescaling (Fastest mode)
// // Set the PWM duty cycle (contrast)
// OCR0A = Contrast; // 8-bit value between 0 and 255
// init_port();
// init_lcd();
// // set_cursor_first_row();
// write_string("Thanks for your time");
// }
//********************************************************************
//********************************************************************
//********************************************************************
//for custom characters
// void init_port(void);
// void outdata(char);
// void outcontrol(char);
// void init_lcd(void);
// void lcd_control_write(void);
// void write_data(char);
// void write_string(char);
// void set_cursor_second_row(void);
// void set_cursor_first_row(void);
// void define_custom_character(void); // Function to define custom characters
// void display_smiley_face(void); // Function to display the smiley face
// void setup() {
// init_port();
// init_lcd();
// define_custom_character(); // Load custom character into CGRAM
// display_smiley_face(); // Display the smiley face
// set_cursor_second_row();
// lcd_control_write();
// write_string("SMILE ARMUP FACE");
// }
// void write_string(char *ptr) {
// while (*ptr != 0) {
// write_data(*ptr); // Write character to the LCD
// ptr++;
// delay(150);
// }
// }
// void init_port(void) {
// volatile char *portF_dir = (volatile char *)0x30;
// volatile char *portK_dir = (volatile char *)0x107;
// *portF_dir = 0xFF;
// *portK_dir = 0x03;
// }
// void outdata(char out_data) {
// volatile char *portF_data;
// portF_data = 0x31;
// *portF_data = out_data;
// }
// void outcontrol(char out_data) {
// volatile char *portK_data;
// portK_data = 0x108;
// *portK_data = out_data;
// }
// void init_lcd(void) {
// outdata(0x38); // Function set: 8-bit, 2-line, 5x8 dots
// lcd_control_write();
// outdata(0x0F); // Display on, cursor blinking
// lcd_control_write();
// outdata(0x01); // Clear display
// lcd_control_write();
// outdata(0x06); // Auto increment after writing 1 char; entry mode
// lcd_control_write();
// }
// void lcd_control_write(void) {
// outcontrol(0x01);
// delay(1);
// outcontrol(0x00);
// delay(1);
// }
// void write_data(char wr_data) {
// outdata(wr_data);
// outcontrol(0x02); // Giving pulse to enable and sending data
// delay(1);
// outcontrol(0x03);
// delay(1);
// outcontrol(0x02);
// delay(1);
// }
// // Define a custom character (smiley face) in CGRAM
// void define_custom_character(void) {
// char smiley[8] = {
// 0b00000, // Row 1
// 0b01010, // Row 2
// 0b01010, // Row 3
// 0b00000, // Row 4
// 0b10001, // Row 5
// 0b01110, // Row 6
// 0b00000, // Row 7
// 0b00000 // Row 8
// };
// char armsUp[8] = {
// 0b00100,
// 0b01010,
// 0b00100,
// 0b10101,
// 0b01110,
// 0b00100,
// 0b00100,
// 0b01010
// };
// char face[8] = {
// 0b11111,
// 0b10001,
// 0b00000,
// 0b01010,
// 0b00000,
// 0b00100,
// 0b00000,
// 0b01110
// };
// // Set CGRAM address (starting at 0x40 for custom character 0)
// outdata(0x40); // CGRAM address for custom character 0
// lcd_control_write();
// // Write the custom character pattern to CGRAM
// for (int i = 0; i < 8; i++) {
// write_data(smiley[i]);
// }
// // outdata(0x40); // CGRAM address for custom character 0
// // lcd_control_write();
// // Write the custom character pattern to CGRAM
// for (int i = 0; i < 8; i++) {
// write_data(armsUp[i]);
// // } outdata(0x40); // CGRAM address for custom character 0
// // lcd_control_write();
// }
// // Write the custom character pattern to CGRAM
// for (int i = 0; i < 8; i++) {
// write_data(face[i]);
// }
//THE CGRAM ADDRESS AUTOINCREMATES AFTER WRITING ONE DATA BUT CAN BE MANUALLY SET UP
// outdata(0x40);
// lcd_control_write();
// write_data(smiley[0]);
// lcd_control_write();
// outdata(0x41);
// lcd_control_write();
// write_data(smiley[1]);
// lcd_control_write();
// outdata(0x42);
// lcd_control_write();
// write_data(smiley[2]);
// lcd_control_write();
// outdata(0x43);
// lcd_control_write();
// write_data(smiley[3]);
// lcd_control_write();
// outdata(0x44);
// lcd_control_write();
// write_data(smiley[4]);
// lcd_control_write();
// outdata(0x45);
// lcd_control_write();
// write_data(smiley[5]);
// lcd_control_write();
// outdata(0x46);
// lcd_control_write();
// write_data(smiley[6]);
// lcd_control_write();
// outdata(0x47);
// lcd_control_write();
// write_data(smiley[7]);
//}
// void display_smiley_face(void) {
// outdata(0x80); // Address of the first row in 16x2 LCD(DDRAM)
// lcd_control_write();
// lcd_control_write();
// write_data(0x00); // Display custom character 0 (stored in CGRAM)
// outdata(0x88); // Address of the first row in 16x2 LCD(DDRAM)
// lcd_control_write();
// lcd_control_write();
// write_data(0x01); // Display custom character 0 (stored in CGRAM)
// outdata(0x8F); // Address of the first row in 16x2 LCD(DDRAM)
// lcd_control_write();
// lcd_control_write();
// write_data(0x02); // Display custom character 0 (stored in CGRAM)
// }
// void set_cursor_first_row(void) {
// outdata(0x80); // Address of the first row in 16x2 LCD(DDRAM)
// lcd_control_write();
// }
// void set_cursor_second_row(void) {
// outdata(0xC0); // Address of the second row in 16x2 LCD(DDRAM)
// lcd_control_write();
// }
// void loop() {
// // Main loop - nothing needed here
// }
//********************************************************************************
//****************4bit mode***************************************
void init_port(void);
void outdata(char);
void outcontrol(char);
void init_lcd(void);
void lcd_control_write(void);
void write_data(char);
void write_string(char);
void set_cursor_second_row(void);
void setup() {
// put your setup code here, to run once:
init_port();
init_lcd();
// set_cursor_first_row();
write_string("yeah 4bit mode worked");
}
void write_string(char *ptr) {
volatile long count=0;
while (*ptr != 0) {
// if(count==16){
// set_cursor_second_row();
// lcd_control_write();
// }
write_data(*ptr); // Write character to the LCD
ptr++;
count++;
}
}
void init_port(void){
volatile char *portF_dir=(volatile char *)0x30;
volatile char *portK_dir=(volatile char *)0x107;
*portF_dir=0xFF;
*portK_dir=0x03;
}
void outdata(char out_data){
volatile char *portF_data;
portF_data=0x31;
*portF_data=out_data;
}
void outcontrol(char out_data){
volatile char *portK_data;
portK_data=0x108;
*portK_data=out_data;
}
void out_4bit_data(char out_data){
volatile char *portF_data;
portF_data=0x31;
*portF_data=(out_data & 0xF0);
lcd_control_write();
*portF_data=(out_data<<4) & 0xF0;
lcd_control_write();
delay(1);
}
void init_lcd(void){
out_4bit_data(0x02); //function set: 4bit ,2line , 5*8
out_4bit_data(0x28); //function set: 4bit ,2line , 5*8
out_4bit_data(0x0F); //display on ,cursor blinking
out_4bit_data(0x01); //clear display
out_4bit_data(0x06); //auto increment after writing 1 char; entry mode
}
void lcd_control_write(void){
outcontrol(0x01);
delay(1);
outcontrol(0x00);
delay(1);
}
void write_data(char wr_data){
//last 4 bits
outdata(wr_data & 0xF0);
outcontrol(0x02); //giving pulse to enable and sending data RS= 1 as writing to register
delay(1); // RS decides whether it's cmd or data
outcontrol(0x03);
delay(1);
outcontrol(0x02);
delay(1);
//first 4bits
outdata((wr_data << 4) & 0xF0);
outcontrol(0x02); //giving pulse to enable and sending data RS= 1 as writing to register
delay(1); // RS decides whether it's cmd or data
outcontrol(0x03);
delay(1);
outcontrol(0x02);
delay(1);
}
void set_cursor_second_row(void) {
out_4bit_data(0xC0); // Address of the second row in 16x2 LCD(DDRAM)
lcd_control_write();
}
void set_cursor_first_row(void) {
out_4bit_data(0x80); // Address of the first row in 16x2 LCD(DDRAM)
// lcd_control_write();
}
void loop() {
// put your main code here, to run repeatedly:
out_4bit_data(0x1B); // 0001 10111
lcd_control_write();
delay(300);
}