The example shows the initialization, writing, and reading data from the transmit and receive buffer register ofan I2C module, respectively. The example shows the connection of an I2C module to the serial EEPROM memory 24C02. The realization is carried out by using the mikroC compiler for dsPIC30F microcontrollers. Fig. 13-11 shows the electrical connection of an I2C module to the EEPROM memory 24C02. The example covers writing to the EEPROM memory, reading data, and data transfer to the PORTF.
Fig. 13-11 Electrical connection of an I2C module to the EEPROM memory 24C02.
void main() { ADPCFG = 0xFFFF; PORTB = 0; TRISB = 0; dAddr = 0x02; I2c_Init(100000); I2c_Start(); // issue I2C start signal I2c_Write(0xA2); // send byte via I2C (command to 24cO2) I2c_Write(dAddr); // send byte (address of EEPROM location) I2c_Write(0xF4); // send data (data to be written) I2c_Stop(); Delay_100ms(); I2c_Start(); // issue I2C start signal I2c_Write(0xA2); // send byte via I2C (device address + W) I2c_Write(0x02); // send byte (data address) I2c_Restart(); // issue I2C signal repeated start I2c_Write(0xA3); // send byte (device address + R) PORTB = I2c_Read(1); // Read the data (NO acknowledge) I2c_Stop(); }//~!Also, an example is presented showing the connection of a device from the dsPIC30F family to the RTC (Real-Time Clock) generator Philips PCF8583P. The example covers writing to and reading from the RTC generator by using an I2C standard interface. Date and time are printed at LCD.
//RTCWrite void main() { ADPCFG = 0xFFFF; I2C_Init(10000); // initialize full master mode I2C_Start(); // issue start signal I2C_Write(0xA0); // address PCF8583 I2C_Write(0); // start from word at address 0 (configuration word) I2C_Write(0x80); // write 0x80 to config. (pause counter...) I2C_Write(0); // write 0 to cents word I2C_Write(0); // write 0 to seconds word I2C_Write(0x30); // write 0x30 to minutes word I2C_Write(0x11); // write 0x11 to hours word I2C_Write(0x24); // write 0x24 to year/date word I2C_Write(0x08); // write 0x08 to weekday/month I2C_Stop(); // issue stop signal I2C_Start(); // issue start signal I2C_Write(0xA0); // address PCF8530 I2C_Write(0); // start from word at address 0 I2C_Write(0); // write 0 to config word (enable counting) I2C_Stop(); // issue stop signal }//~! //RTCRead unsigned char sec, min1, hr, day, mn, year; char *txt, tnum[4]; void Zero_Fill(char *value) { // fill text repesentation if (value[1] == 0) { // with leading zero value[1] = value[0]; value[0] = 48; value[2] = 0; } }//~ //--------------------- Reads time and date information from RTC (PCF8583) void Read_Time(char *sec, char *min, char *hr, char *day, char *mn, char *year) { I2C_Start(); I2C_Write(0xA0); I2C_Write(2); I2c_Restart(); I2C_Write(0xA1); *sec =I2c_Read(0); *min =I2c_Read(0); *hr =I2c_Read(0); *day =I2c_Read(0); *mn =I2c_Read(1); I2C_Stop(); }//~ //-------------------- Formats date and time void Transform_Time(char *sec, char *min, char *hr, char *day, char *mn, char *year) { *sec = ((*sec & 0xF0) >> 4)*10 + (*sec & 0x0F); *min = ((*min & 0xF0) >> 4)*10 + (*min & 0x0F); *hr = ((*hr & 0xF0) >> 4)*10 + (*hr & 0x0F); *year = (*day & 0xC0) >> 6; *day = ((*day & 0x30) >> 4)*10 + (*day & 0x0F); *mn = ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F); }//~ //-------------------- Output values to LCD void Display_Time(char sec, char min, char hr, char day, char mn, char year) { ByteToStr(day,tnum); txt = rtrim(tnum); Zero_Fill(txt); Lcd_Custom_Out(1,6,txt); ByteToStr(mn,tnum); txt = rtrim(tnum); Zero_Fill(txt); Lcd_Custom_Out(1,9,txt); Lcd_Custom_Chr(1,15,52+year); ByteToStr(hr,tnum); txt = rtrim(tnum); Zero_Fill(txt); Lcd_Custom_Out(2,6,txt); ByteToStr(min,tnum); txt = rtrim(tnum); Zero_Fill(txt); Lcd_Custom_Out(2,9,txt); ByteToStr(sec,tnum); txt = rtrim(tnum); Zero_Fill(txt); Lcd_Custom_Out(2,12,txt); }//~ //------------------ Performs project-wide init void Init_Main() { ADPCFG = 0xFFFF; Lcd_Custom_Init_EasyDsPIC4(); I2C_Init(100000); // initialize I2C txt = "Date:"; // prepare and output static text on LCD Lcd_Custom_Out(1,1,txt); Lcd_Custom_Chr(1,8,':'); Lcd_Custom_Chr(1,11,':'); txt = "Time:"; Lcd_Custom_Out(2,1,txt); Lcd_Custom_Chr(2,8,':'); Lcd_Custom_Chr(2,11,':'); txt = "200"; Lcd_Custom_Out(1,12,txt); Lcd_Custom_Cmd(LCD_CURSOR_OFF); // cursor off }//~ //----------------- Main function void main() { Init_Main(); // perform initialization while (1) { Read_Time(&sec,&min1,&hr,&day,&mn,&year); // read time from RTC(PCF8583) Transform_Time(&sec,&min1,&hr,&day,&mn,&year); // format date and time Display_Time(sec, min1, hr, day, mn, year); // prepare and display on LCD Delay_ms(1000); // wait 1s } }