728x90

728x90

Saturday, February 7, 2026

ATMega644P TWI DS1307 MCP23017 LCD

In previous posts the ATMega644P TWI interface with the DS13017 RTC and MCP23017 GPIO expansion chip. Here I will put them altogethers. This TWI bus will drives the DS1307 RTC and the MCP23017 that controls a character LCD.

ATMega644P TWI DS1307 MCP23017 LCD 

The master microcontroller read the RTC data from ds1307 and it will send to the MCP23017 based LCD. 

Source Code:

  1. /*
  2. * 10-i2c_ds1307_mcp23017_lcd.c
  3. *
  4. * Created: 2/7/2026 7:42:39 PM
  5. * Author : Admin
  6. */

  7. #include <avr/io.h>
  8. #include <util/delay.h>
  9. #define F_CPU 16000000UL

  10. void rtc_init(void){
  11. char rtc[8]={0x30,0x00,0x17,0x07,0x31,0x01,0x26,1<<4};
  12. for (char i=0;i<8;i++)
  13. {
  14. twi_start();
  15. //D0 is DS1307 Write Address
  16. twi_write(0xD0);
  17. //Select Control Register
  18. twi_write(i);
  19. //Enable SQWE bit blinks at 1 Hz
  20. twi_write(rtc[i]);
  21. twi_stop();
  22. _delay_ms(10);
  23. }
  24. }

  25. char rtc[6], msg[16];
  26. int main(void)
  27. {
  28. /* Replace with your application code */
  29. _delay_ms(1000);
  30. lcd_init();
  31. lcd_text("ATMega644P TWI");
  32. lcd_xy(1,2);
  33. lcd_text("MCP23017 DS1307");
  34. _delay_ms(10000);
  35. lcd_clear();
  36. lcd_command(0x0C);
  37. while (1)
  38. {
  39. for(char i=0;i<7;i++){
  40. /*Second Register*/
  41. twi_start();
  42. twi_write(0xD0);
  43. /*Select Second register*/
  44. twi_write(i);
  45. twi_stop();
  46. _delay_us(10);
  47. twi_start();
  48. twi_write(0xD1);
  49. rtc[i]=twi_read(1);
  50. twi_stop();
  51. _delay_us(10);
  52. }
  53. lcd_xy(1,1);
  54. sprintf(msg,"Time: %02X:%02X:%02X",rtc[2],rtc[1],rtc[0]);
  55. lcd_text(msg);
  56. lcd_xy(1,2);
  57. sprintf(msg,"Date: %02X/%02X/20%02X",rtc[4],rtc[5],rtc[6]);
  58. lcd_text(msg);
  59. _delay_ms(500);
  60. }
  61. }


Circuit Simulation:

ATMega644P TWI DS1307 MCP23017 LCD
Circuit and Simulation

 

AVR Prototype Board

ATMega644P TWI DS1307 MCP23017 LCD

 

ATMega644P TWI DS1307 MCP23017 LCD 

This PCB was offered by PCBWay (pcbway.com).

I have been using PCBWay for many years now. PCBWay fabricate PCBs at low cost, fast processing time for only 24 hours, and fast delivery time using any carrier options. This double side 10cmx10cm can be fabricate at only 5USD for 5 to 10pcs by PCBWay. It's a standard PCB with silk screen and solder mask.

A DIY dsPIC30F2010 and dsPIC30F1010 Prototype Board with Programmer
10 PCBs for only 5USD
 

For different size of PCB we can instantly quote on PCBWay website using a zip PCB Gerber file without account.

A DIY dsPIC30F2010 and dsPIC30F1010 Prototype Board with Programmer
PCBWay Instant Quote

 

Click here to download this example

For a full ATMega644P tutorials please visit this page.

No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90