// #include <Arduino.h>
// // put function declarations here:
// #define TWINT 7
// #define TWSTA 5
// #define TWSTO 4
// #define TWEN 2
// void TWI_init(void);
// void start_TWI(void);
// void stop_TWI(void);
// void wait_Flag();
// void check_ACK(uint8_t ACK);
// void TWI_write(uint8_t SLA_W);
// #define TWCR (*(volatile uint8_t*)0xBC)
// #define TWDR (*(volatile uint8_t*)0xBB)
// #define TWSR (*(volatile uint8_t*)0xB9)
// #define TWBR (*(volatile uint8_t*)0xB8)
// uint8_t decToBCD(uint8_t val) {
// return ((val / 10) << 4) | (val % 10);
// }
// uint8_t bcdToDec(uint8_t val) {
// return ((val >> 4) * 10) + (val & 0x0F);
// }
// uint8_t raw;
// void setup() {
// // put your setup code here, to run once:
// Serial.begin(9600);
// TWI_init();
// start_TWI(); //starting
// check_ACK(0x10);
// TWI_write(0xD0); //slave address with write bit
// check_ACK(0x18);
// TWI_write(0x00);//seconds register address in rtc
// check_ACK(0x28);
// TWI_write(decToBCD(50)); // Sends 0x50 seconds to RTC
// check_ACK(0x28);
// TWI_write(decToBCD(45)); // minutes
// check_ACK(0x28);
// TWI_write(decToBCD(9)); // hours
// check_ACK(0x28);
// TWI_write(decToBCD(03) ); //day
// check_ACK(0x28);
// TWI_write(decToBCD(18) ); // date
// check_ACK(0x28);
// TWI_write(decToBCD(4)); // month
// check_ACK(0x28);
// TWI_write(decToBCD(25) ); // year
// check_ACK(0x28);
// stop_TWI();
// // // //reading
// }
// void TWI_init(void) {
// TWCR = 0x00; // Reset control register
// TWSR = 0x00; // Prescaler = 1
// TWBR = 72; // Set bit rate
// // Enable TWI
// delay(500);
// }
// void start_TWI(void) { //used for both start and repeated start
// TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); //When I write 1 to TWINT, hardware clears it to 0 to begin the operation, and once the operation is done, hardware sets it to 1 again.
// wait_Flag();
// // check_ACK(0x10);
// }
// void stop_TWI(void) {
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
// wait_Flag();
// }
// void wait_Flag() { //waiting for hardware to set TWINT(1)
// while (!(TWCR & (1 << TWINT)));
// }
// void check_ACK(uint8_t expected_ACK) {
// uint8_t status = TWSR & 0xF8;
// if (status != expected_ACK) {
// Serial.print("Expected ACK: 0x");
// Serial.print(expected_ACK, HEX);
// Serial.print(" but got: 0x");
// Serial.println(status, HEX);
// }
// }
// void TWI_write(uint8_t SLA_W) {
// TWDR = SLA_W; // Load data/address into TWDR
// TWCR = (1 << TWINT) | (1 << TWEN); // Start transmission // Wait for TWINT =
// wait_Flag();
// }
// void loop() {
// start_TWI();
// TWI_write(0xD0); // Slave address with write bit
// check_ACK(0x18);
// TWI_write(0x00); // Start from seconds register
// check_ACK(0x28);
// start_TWI(); // Repeated start
// TWI_write(0xD1); // Slave address with read bit
// check_ACK(0x40);
// // Read Seconds
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
// wait_Flag();
// uint8_t seconds = bcdToDec(TWDR & 0x7F);
// // Read Minutes
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
// wait_Flag();
// uint8_t minutes = bcdToDec(TWDR);
// // Read Hours
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
// wait_Flag();
// uint8_t hours = bcdToDec(TWDR);
// // Read Day
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
// wait_Flag();
// uint8_t day = bcdToDec(TWDR);
// // Read Date
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
// wait_Flag();
// uint8_t date = bcdToDec(TWDR);
// // Read Month
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
// wait_Flag();
// uint8_t month = bcdToDec(TWDR);
// // Read Year (last byte, so send NACK)
// TWCR = (1 << TWINT) | (1 << TWEN); // NACK
// wait_Flag();
// uint8_t year = bcdToDec(TWDR);
// stop_TWI();
// // Print output
// Serial.print("Time: ");
// Serial.print(hours);
// Serial.print(":");
// Serial.print(minutes);
// Serial.print(":");
// Serial.println(seconds);
// Serial.print("Date: ");
// Serial.print(date);
// Serial.print("-");
// Serial.print(month);
// Serial.print("-20");
// Serial.println(year);
// Serial.print("Day Number: ");
// Serial.println(day);
// delay(1000);
// }
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 setup() {
// put your setup code here, to run once:
init_port();
init_lcd();
set_cursor_first_row();
write_string("Thanks bhaiya for your guidance");
}
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 uint8_t *portF_dir=(volatile uint8_t*)0x30;
volatile uint8_t *portK_dir=(volatile uint8_t*)0x107;
*portF_dir=0x03;
*portK_dir=0xFF;
}
void outdata(char out_data){
volatile uint8_t *portK_data;
portK_data= (volatile uint8_t*)0x108;
*portK_data=out_data;
}
void outcontrol(char out_data){
volatile uint8_t *portK_data;
portK_data=(volatile uint8_t*)0x31;
*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; en................................... try 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);
}
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);
}