PIC18F47K42 USING DS1307 CLOCK I2C

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
pumper
Posts: 282
Joined: 10 Jan 2011 17:37

PIC18F47K42 USING DS1307 CLOCK I2C

#1 Post by pumper » 24 Apr 2024 18:12

Can someone help me get started? There is no I2C library. I have tried the I2C_Remappable without success, maybe because I don't yet understand it.
Is there an include I2C file?

I have been reading the PIC18F47K22 chapter for I2C but it has to many things it does but no specifics to the DS1307 family.

paulfjujo
Posts: 1549
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: PIC18F47K42 USING DS1307 CLOCK I2C

#2 Post by paulfjujo » 26 Apr 2024 08:03

hello,


you can find
here
all you need to use I2C1 , specific for 18F27K42 ... without mikroC library ...
used for OLED display and DS3231 RTC

needs to add specific register names, not included in MikroC

Code: Select all


registres I2C du MCU 18F27K42 

sbit I2C1STAT0_BFRE    at I2C1STAT0.B7;
sbit I2C1STAT1_TXBE    at I2C1STAT1.B5;
sbit I2C1CON1_ACK      at I2C1CON1.B4;
sbit I2C1CON1_ACKSTAT  at I2C1CON1.B5;   //0 = Ack was received  1 = No Ack was not received
sbit I2C1CON2_ABD      at I2C1CON2.B4;
sbit I2C1CON0_EN       at I2C1CON0.B7;
sbit I2C1CON0_RSEN     at I2C1CON0.B6;
sbit I2C1CON0_S        at I2C1CON0.B5;
sbit I2C1CON0_MDR      at  I2C1CON0.B3;
sbit I2C1PIR_PCIF      at I2C1PIR.B2 ;
sbit I2C1PIR_SCIF      at I2C1PIR.B0 ;
sbit I2C1STAT1_RXBF    at I2C1STAT1.B0;
sbit I2C1STAT0_MMA     at I2C1STAT0.B5;
sbit I2C1PIR_CNTIF     at I2C1PIR.B7;
sbit PIR2_I2C1RXIF     at PIR2.B7;
sbit PIE2_I2C1RXIE     at PIE2.B7;
sbit I2C1STAT1_CLRBF  at I2C1STAT1.B2;


// ------------------------------
void  I2C1_Hw_Init(void);
char I2C1_DevicePresent(unsigned char adr);
unsigned char I2cScan(void);
void I2C1_ReadDataBlock(unsigned char adre, unsigned char registre, unsigned char *dat, unsigned char Nb);
void I2C1_Write(unsigned char addr8, unsigned char addr_data, unsigned char* b, signed char n);
void I2C1_Read(unsigned char addr8, unsigned char addr_data, unsigned char* px, signed char n);


//===================   I2C1  ==============================================
void  I2C1_Hw_Init(void)
{
  TRISC.B3   = 0;         // C3 output
  TRISC.B4   = 0;         // C4 output
  WPUC       = 0x18;      // C3 et C4 pull-up
  ODCONC     = 0x18;      // C3 et C4 open drain
  RC3PPS     = 0x21;      // i2c1, scl -> C3
  I2C1SCLPPS = 0x13;      // i2c1, scl <- C3
  RC4PPS     = 0x22;      // i2c1, sda -> C4
  I2C1SDAPPS = 0x14;      // i2c1, sda <- C4
  //CLKRCON=0b10010110;  // EN=1 Duty=50%  Div=64
  //CLKRCLK=0x01; //0001 = HFINTOSC
  I2C1CLK    = 0x03;      // i2c1, 0011 MFINTOSC  500Khz  ..OK
  I2C1CON0   = 0x04;      // i2c1, master 7 bits
  I2C1CON1   = 0x80;      // i2c1, envoi NAK sur dernier octet attendu
  //I2C1CON2   = 0x01;      // i2c1, envoi Start non auto, SDAHT 300ns hold time, BFRET 16 i2c clock pulses
  I2C1CON2 = 0x00;      // ABD enabled; SDAHT 300 ns; BFRET 8 I2C Clocks; FME disabled;
  I2C1ADB1   = 0x00   ; //OLED_ADDR; // i2c1, 2 * adresse esclave
  I2C1CON0_EN = 1;        // i2c1 on
  I2C1ERR = 0;
  I2C1PIR = 0;
}

... etc ....

pumper
Posts: 282
Joined: 10 Jan 2011 17:37

Re: PIC18F47K42 USING DS1307 CLOCK I2C

#3 Post by pumper » 27 Apr 2024 13:47

Thanks paulfjujo.

I see that uses the software I2C library.
I have my program loaded with what you gave me and the Compiler no longer complains. I am waiting for the PIC18F47K42 to test it, should be here today.

I am replacing a PIC18F46K22 with the PIC18F47K42 in my existing project. I am sure there will be other areas of difference like the watchdog timer and setting the system frequency (I am using 16mHz) but I am hoping the manual will get me through it. If you know of any other areas that are different let me know so I can check them out.

Thanks

Post Reply

Return to “mikroC PRO for PIC General”