djeceymca
Junior Member level 2
Hello Guys,
I am trying to connect DS3231 RTC to my PIC microcontroller 18f46k22. But my code is getting stuck at I2C1_Wr(DS3231_Write_addr) command. Any idea whats the issue.
Regards,
Dev
I am trying to connect DS3231 RTC to my PIC microcontroller 18f46k22. But my code is getting stuck at I2C1_Wr(DS3231_Write_addr) command. Any idea whats the issue.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 // LCD module connection sbit LCD_RS at LATD4_bit; sbit LCD_EN at LATD5_bit; sbit LCD_D4 at LATD0_bit; sbit LCD_D5 at LATD1_bit; sbit LCD_D6 at LATD2_bit; sbit LCD_D7 at LATD3_bit; sbit LCD_RS_Direction at TRISD4_bit; sbit LCD_EN_Direction at TRISD5_bit; sbit LCD_D4_Direction at TRISD0_bit; sbit LCD_D5_Direction at TRISD1_bit; sbit LCD_D6_Direction at TRISD2_bit; sbit LCD_D7_Direction at TRISD3_bit; #define DS3231_Address 0x68 #define DS3231_Read_addr ((DS3231_Address << 1) | 0x01) #define DS3231_Write_addr ((DS3231_Address << 1) & 0xFE) typedef signed short int8; int8 err; unsigned short read_ds3231(unsigned short address){ unsigned short read_data; Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1, "Before start "); Delay_ms(1000); I2C1_Start(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1, "Before 0xD0 "); Delay_ms(1000); I2C1_Wr(DS3231_Write_addr); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1, "Before address "); Delay_ms(1000); I2C1_Wr(address); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1, "Before RS "); Delay_ms(1000); I2C1_Repeated_Start(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1, "Before 0xD1 "); Delay_ms(1000); I2C1_Wr(DS3231_Read_addr); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1, "Before Read "); Delay_ms(1000); read_data = I2C1_Rd(0); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1, "Before stop "); Delay_ms(1000); I2C1_Stop(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1, "Before return "); Delay_ms(1000); return (read_data); } unsigned char MSB(unsigned char x){ return ((x>>4)+'0'); } unsigned char LSB(unsigned char x){ return ((x & 0x0F) + '0'); } // Global Variable int second; int minute; int hour; int hr; int day; int dday; int month; int year; char time[] = "00:00:00"; char date[] = "00-00-00"; void main() { ANSELD = 0; TRISD = 0; OSCCON = 0x66; Lcd_Init(); I2C1_Init(10000); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1,1, "Time: "); Lcd_Out(2,1, "Date: "); TRISB0_bit =0; TRISC0_bit = 1; LATB.B0 = 0; while(1){ if(PORTC.RC0 == 0){ Delay_ms(1); if((PORTC.RC0 == 0)&(LATB.B0 == 0)){ LATB.B0 = 1; second = read_ds3231(0); minute = read_ds3231(1); hour = read_ds3231(2); hr = hour & 0b00011111; dday = read_ds3231(3); day = read_ds3231(4); month = read_ds3231(5); year = read_ds3231(6); time[0] = MSB(hr); time[1] = LSB(hr); time[3] = MSB(minute); time[4] = LSB(minute); time[6] = MSB(second); time[7] = LSB(second); date[0] = MSB(day); date[1] = LSB(day); date[3] = MSB(month); date[4] = LSB(month); date[6] = MSB(year); date[7] = LSB(year); Lcd_Out(1,7, time); Lcd_Out(2,7, date); Delay_ms(100); } } else { LATB.B0 = 0; } } }
Regards,
Dev
Last edited by a moderator: