BETA7.3.0 Remappable I2C bug

Beta Testing discussion on mikroC PRO for PIC.
Author
Message
Hayato_U
Posts: 27
Joined: 28 Aug 2018 12:31

BETA7.3.0 Remappable I2C bug

#1 Post by Hayato_U » 26 Jan 2019 17:06

The Hardware I2C related bug reported in this thread has not been fixed.
Please integrate the downloadable mcl file into the latest version of mikroC.

viewtopic.php?f=88&t=73544&p=294761&hilit=I2C#p294761

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: BETA7.3.0 Remappable I2C bug

#2 Post by filip.grujcic » 28 Jan 2019 17:14

Hi,

7.3.0 BETA was released to address the issues with K42 and K83 families.
Any reported issue for other PIC families will be addressed in the next official release of the compiler.

Kind regards,
Filip Grujcic

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

Re: BETA7.3.0 Remappable I2C bug

#3 Post by paulfjujo » 30 Jan 2019 20:32

hello,


:( i also, tried unsuccessfully to use I2C hardware...

:D but I2C1 software is OK
tested OK , with a RTC DS3231

tested Also ADC 12 bits, UART1 RX interrupt .. OK

a bit hard to change from 26K22 to 27K42 because use of PPS ,
many differencies, and a big datasheet with 807 pages to study !
even it is a Powerfull PIC !

Code: Select all

 
 // datasheet 807 pages
#define Version "190130"
// mikroC version 7.30 beta
// 18F27K42  mis sur Horloge 60 leds à l aplace du Pic18F26K22
/*
Interrupt Vector Table
Programmable single or dual priority
Two level hardware context saving
Eliminates need for CPU involvement in data transfers
Access to all memory spaces and peripherals
Flexible source and destination message sizes
*/
/*

CONFIG1L : $300000 : 0x0077
CONFIG1H : $300001 : 0x002B
CONFIG2L : $300002 : 0x00FF
CONFIG2H : $300003 : 0x00BF
CONFIG3L : $300004 : 0x007F
CONFIG3H : $300005 : 0x003F
CONFIG4L : $300006 : 0x009F
CONFIG4H : $300007 : 0x002F
CONFIG5L : $300008 : 0x0001
CONFIG5H : $300009 : 0x0000


*/
// see datasheet 9.0       IVTBASE Adress register
// Post subject: Re: PIC18F47k42 Timer 0
//https://forum.mikroe.com/viewtopic.php?f=88&t=72653&p=292426#p292426

  //  ac:pinout

 // Hardware
 /*
 Pin 28  RB7  --x  x----ICSP Data
 Pin 27  RB6  --x  x-----ICSP Clock
 Pin 26  RB5
 Pin 25  RB4
 Pin 24  RB3
 Pin 23  RB2
 Pin 22  RB1
 Pin 21  RB0
 Pin 20      VDD --------+5V
 Pin 19      Vss ---------Gnd          fil noir cable prolific
 Pin 18  RC7  RX UART  <-- Keyboard    fil Vert cable prolific
 Pin 17  RC6  TX UART  --> Display     fil Blanc cable prolific
 Pin 16  RC5

 Pin 15  RC4  SDA  I2C
 Pin 14  RC3  SCL  I2C

 Pin 13  RC2
 Pin 12  RC1
 Pin 11  RC0

 Pin 10  RA6
 Pin  9  RA7
 Pin  8  VSS ---------  Gnd
 Pin  7  RA5  ----> Led
 Pin  6  RA4  -----> led
 Pin  5  RA3
 Pin  4  RA2
 Pin  3  RA1
 Pin  2  RA0  <---- Analog Input
 Pin  1  RE3 MCLR  Reset <--------ICSP PGRM
                          |----x  x----/0V   cavalier RESET
 */

#define Directory "C:\_MikroC\_MesProjets_MikroC\_18F27K42"
#define Project "PIC18F27K42_UAR1_ADC_Soft_I2C_Test.mcppi"
#define Source  "PPIC18F27K42_UART1_ADC_Soft_I2C_Test_"
#define Eeprom " not used ...."
#define Config "P18F27K42_FOSC_64Mhz.cfgsch"


#define PROCESSOR "18F27K42"
#define POWER_SUPPLY  " 5V"

#define OSCILLATEUR_INTERNE
#define FOSC "64.0"  // MHz
#define LCD_ADR 0x74
#define NbCarPL 16


#define BAUD 115200  // UART1

#define CLS 12
#define CR 13    //0x0D
#define LF 10
#define TAB 9
#define BACK 8
#define Beep 7
#define Separator 0x20   // space
#define ON 0    // because led tirée au +VCC
#define OFF 1
#define SPECIAL 1
#define NORMAL 0

 // pin 1 circuit M948A-2  Melodie
#define STOP 0         //   Bit RB7=0  --> ULN   ---> 1  stop melodie
#define RING 128       //   Bit RB7=1  --> ULN -> 0  active melodie

#ifndef Byte
#define Byte unsigned char
#endif

#ifndef Word
#define Word unsigned int
#endif

#define RTC_DS3231
#define DS3231_ADDR 0xD0 // RTC DS3231 + 24C32
 // I2C SDA sur RC4
 // I2C SCL sur RC3



const code char Jour0[]="Dimanche";
const code char Jour1[]="Lundi";
const code char Jour2[]="Mardi";
const code char Jour3[]="Mercredi.";
const code char Jour4[]="Jeudi";
const code char Jour5[]="Vendredi";
const code char Jour6[]="Samedi";
const code char * JourSemaine[]={Jour0,Jour1,Jour2,Jour3,Jour4,Jour5,Jour6};

const code char mesg0[]=" Directory :"Directory"\r\n";
const code char mesg1[]=" MikroC pro 7.30 Beta \n\r";
const code char mesg2[] =" Projet :"Project"\r\n";
const code char mesg3[]=" Test PIC18F27K42  I2C \r\n";
const char char mesg4[]=" Config bit : "Config" FOSC:"FOSC" MHz\r\n";
const char char mesg5[]=" Eeprom: "Eeprom"\r\n";
const char char mesg6[]=" Source : "Source"_"Version".c\r\n";
const char char mesg7[]=" 18F27K42 UART1 DS3231 LM35DZ\r\n";
const char char mesg8[]=" avec LCD2119 Soft I2C \r\n";
const char char mesg9[]=" utilise LCD_2119_181021.inc \r\n";
const char char mesg10[]=" ";
const code char * Messages[]={mesg0,mesg1,mesg2,mesg3,mesg4,mesg5,mesg6,mesg7,mesg8,mesg9,mesg10};
const char Blancs[]="  ";



sbit Bavard at RA3_bit ;
sbit Bavard_Dir at TRISA3_bit ;
sbit RAZ at LATC.B1;
sbit RAZ_Dir at TRISC.B1;
sbit RTC_Forcee at PORTC.B0;
sbit RTC_Forcee_Dir at TRISC.B0 ;
sbit HZ at LATC.B5;
sbit HZ_Dir at TRISC.B5;
sbit Alarme_Melodie at LATB.B7;
sbit Alarme_Melodie_Dir at LATB.B7;
sbit Clignote_Sec at LATA.B6;
sbit Clignote_Sec_Dir at TRISA.B6;


sbit Soft_I2C_Scl           at RC3_bit;
sbit Soft_I2C_Sda           at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;


#define LED_ROUGE     RAZ
#define LED_BLANCHE   Clignote_Sec
#define LED_VERTE     Alarme_Melodie

#define MAX_LEN1 128
#define MAX_LEN2 64
#define MAX_LEN3 80

unsigned int i,j,k,l,m,n;

volatile unsigned char Temp=0;
volatile unsigned char Buffer1[MAX_LEN1];
volatile int Flag_Buffer1;
volatile unsigned char c1,cx;
volatile unsigned int i1;
volatile unsigned int Index1;
volatile unsigned int CptErr;


volatile char Temperat[30];
volatile unsigned char TP_Deg,TP_Deci;
volatile unsigned int TAmb;

unsigned char * p1;
unsigned char * p2;

unsigned char CRam1[MAX_LEN2];
unsigned char TEXTE [MAX_LEN3];
unsigned char *txt;

volatile unsigned int Count2, CountS;
volatile unsigned char Sec,Mn,Hour;
unsigned char OldSec;
unsigned int SEA0,EA0,Nb;
float F0,F1;
unsigned long L1;


unsigned char Buff_LCD[33];   // LCD 2x16 + zero
int addr;
unsigned char  I2C_Adresses_Devices[8];


unsigned int Count0 = 0;
unsigned char Etat_clock=0;

volatile struct chbits {   // 8 flags
       unsigned FrameErr:1;
       unsigned RAZ:1;
       unsigned Tmr0_Elapsed:1;
       unsigned Togle:1;
       unsigned Second:1;
       unsigned Gie:1;
       unsigned Full:1;
      unsigned Melodie:1;
 }Drapeaux ;
 
unsigned char Melodie=STOP;
unsigned int Duree_Melodie=5; // 5 Sec par defaut
signed  char Offset_Temper;
signed char Dummy;
// gamme Horaire de validation Melodie
unsigned int MinHour=8;  // 8H à 21H00
unsigned int MaxHour=21;

int Status;
unsigned char Adr;
char time[]="00:00:00";
char date[]="00/00/00";
unsigned short second,minute,heure,jS,jour,mois,Annee;
unsigned short DateTime[9];
volatile int Flag_Timer1=0;
volatile int Flag_Timer2=0;
volatile int Index_Digit=0;

void Init_Hardware(void) ;
void CRLF1(void) ;
void Raz_Buffer(void) ;
void UART1_Write_CText(const char *txt);
void strConstRamCpy(unsigned char *dest, const code char *source);
void Float2Ascii (float x, unsigned char *chaine,char precision) ;


void Interrupts() iv 0x0008 ics ICS_AUTO
{
    if (( TMR0IE_bit) && (TMR0IF_bit))
    {
       // pour debugger
       // U1TXB=Count0+48;
        // pour 100mS
         TMR0H         = 0x3C; //15536;
         TMR0L         = 0xB0;
        Count0++;
        if (Count0 >9) 
        {
            LED_VERTE = ~LED_VERTE;
            Drapeaux.Tmr0_Elapsed=1;
        }
        PIR3.TMR0IF= 0;
    }
    //RX UART IT
     if (( U1RXIF_bit==1) && ( U1RXIE_bit==1))  //RC1IE_bit==1) )
     {
       U1TXB=='?';
      c1 = UART1_Read();

      if (U1ABDOVF_bit==1)    // voir parag  31.2.2.4   page 479
      {
       U1ABDOVF_bit = 0 ;
       CptErr++;
       c1=U1RXB ;
       }
       if(U1FERIF_bit==1 )
       {
        CptErr++;
        c1 = UART1_Read();
       }
       // modif pour test envoi par BluetoothElectronic.apk (pas de CR)
       if ((c1==CR) || (c1==LF) || (Index1>MAX_LEN1-1))
       //  if ((c1==CR) || (Index1>MAX_LEN1-1))
       {
        Flag_Buffer1=1;
        Buffer1[i1]=0;
        U1RXIE_bit=0;
        Index1=i1;
        i1=0;
        if( Index1>MAX_LEN1-1) Flag_Buffer1=2;
        TMR2IE_bit=0;
       }
       else
       {
       Flag_Buffer1=0;
       Buffer1[i1]=c1;
       Index1=i1;
       i1++;
       }
     }
 }



#include "built_in.h"     // for  Hi  Lo ..etc
#include "Tiny_RTC_DS3231_AI2_2019.h"



void My_ADC_Init ()
{
// basic acquisition
  ADON_bit=1; // ADCON0.B7
  ADCON0.GO_nDONE=1;
  ADCON2=0;
  ADCON3=0;
  ADCLK=63; //ADCON0.    page 622

  //b7-5=0   b4=NREF=0=VREF- conneted to VSSS.    bit 3-2=0
  // PREF bit 1-0    11=VREF+ connected to Fixed voltage ref FVR
   ADREF=0b00000011;
   ADPCH=0b00010010;// = ANC2       page 623
   ADPREH=0b0000010; // ADREF=0b0000011;
   ADACQL=64;
   ADACQH=2;
   ADCAP=0 ;
   ADRPT=0;
   ADCNT=0;
   ADFLTRH=0;
   ADFLTRL=0;

}




// --- Copie le texte depuis FLASH ROM vers RAM
void strConstRamCpy(unsigned char *dest, const code char *source)
 {
  while (*source)*dest++ = *source++ ;
  *dest = 0 ;    // terminateur
}

void CRLF1()
{
 UART1_Write(CR); UART1_Write(LF);
}

void UART1_Write_CText(const char *txt)
 {
   while (*txt)
      UART1_Write(*txt++);
 }


 void Float2Ascii (float x, unsigned char *str,char precision)
{
 // converts a floating point number to an ascii string
 // version limitée à 6 decimales maximum
 // x is stored into str, which should be at least 30 chars long
 int ie, i, k, ndig;
 double y;
  ndig = ( precision<=0) ? 7 : (precision > 22 ? 23 : precision+1);
 ie = 0;
 // if x negative, write minus and reverse
 if ( x < 0.0000)
 {
   *str++ = '-';
   x = -x;
 }
 // put x in range 1 <= x < 10
 if (x > 0.00000) while (x < 1.00000)
 {
   x *= 10.000;                // a la place de =*
   ie--;
 }
 while (x >= 10.000)
 {
   x = x/10.000;
   ie++;
 }
 // in f format, number of digits is related to size
 ndig += ie;                                // a la place de =+
 //round. x is between 1 and 10 and ndig will be printed to
 // right of decimal point so rounding is ...
 for (y = i = 1; i < ndig; i++)
 y = y/10.000;
 x += y/2.000;
 if (x >= 10.000) {x = 1.000; ie++;}
 if (ie<0)
 {
   *str++ = '0'; *str++ = '.';
   if (ndig < 0) ie = ie-ndig;
   for (i = -1; i > ie; i--)  *str++ = '0';
 }
 for (i=0; i < ndig; i++)
 {
   k = x;
   *str++ = k + '0';
   if (i ==  ie ) *str++ = '.';
   x -= (y=k);
   x *= 10.000;
  }
 *str = '\0';
}












void Raz_Buffer1()
{
        // nettoye le debut de buffer ,car utilisé pour init BT
        for(i1=0;i1<MAX_LEN1-1;i1++) Buffer1[i1]=0;
        Buffer1[0]=0;
        i1=0;
        p1=0;
        Index1=0;
        Flag_Buffer1 =0 ;
        //c1=RCREG1;
        c1=0;
        U1RXEN_bit=1;
         U1ERRIR = 0x00;
         U1ERRIE = 0x00;
        U1RXIE_bit=1;
        //RC1IE_bit = 1;
}



void Init_Hardware()
{

    
    PORTA = 0x00;
  ANSELA=0x01;
  TRISA = 0b10000011 ;   // RA0,RA1=Input  R2..RA6 as output  RA7 input
  Clignote_Sec_Dir=0;
  Clignote_Sec=0;

  ANSELB=0;
  PORTB = 0x00;
  TRISB = 0x00;    //  B0..B7 Output   ATTENTION B7 B6 for ICSP Pickit3
  WPUB=0xFF;
  Alarme_Melodie_Dir=0;
  Alarme_Melodie=OFF  ;
  Melodie=STOP;

  CM1CON0=0;
  CM2CON0=0;

  PORTC=0;
  ANSELC=0 ;
  ANSELC2_bit=1;   // AN14
  TRISC = 0b10000111;    //
  WPUC2_bit=0;
  ODCC2_bit=0;
  
  RTC_Forcee_Dir=1;
  RAZ_Dir=0;
  RC6PPS = 0x13;   //RC6->UART1:TX1;
  // U1CTSPPS = 0x15;   //RC5->UART1:CTS1;
  U1RXPPS = 0x17;   //RC7->UART1:RX1;

   FVRCON=0;
   FVRCON.FVREN=0;  // page 597  paragraph 34.3
  FVRCON.ADFVR1=1; // 01 = Fixed Voltage Reference Peripheral output is 1,024)
  FVRCON.ADFVR0=0;
}

void Init_Timer0()
{

// FOS=64MHz  , FOSC/4=16Mhz
//  Timer0
// pour 100mS
//FOSC=64MHz Prescaler 1:32; TMR0 Preload = 15536; Actual Interrupt Time : 100 ms

//  REGISTER 22-1: T0CON0: TIMER0 CONTROL REGISTER 0
   T0CON0=0;
   T0CON0.B7=1;  //   Enable module and operating
   T0CON0.B4=1;  //MD16=1;
//   OUTPS<3:0>: TMR0 Output Postscaler (Divider) Select bits  0111= 1/8    1111=1/16   0000=1/1
   T0CON0.B3=0;
   T0CON0.B2=0;
   T0CON0.B1=0;
   T0CON0.B0=0;
//   REGISTER 22-2: T0CON1: TIMER0 CONTROL REGISTER 1
//   bit 7-5 CS<2:0>:Timer0 Clock Source Select bits = 011 HFINTOSC    010=FOSC/4
   T0CON1.B7=0;  //T0CON1.CS2 !!!!
   T0CON1.B6=1;  //T0CON1.CS1
   T0CON1.B5=0; //T0CON1.CS0
//   bit 4 ASYNC: TMR0 Input Asynchronization Enable bit
//   T0CON1.ASYNC=0;    //The input to the TMR0 counter is synchronized to FOSC/4
    T0CON1.ASYNC=1;    //The input to the TMR0 counter is synchronized to FOSC/4
//   bit 3-0 CKPS<3:0>: Prescaler Rate Select bit     1/32
   T0CON1.B3=0;   // CKPS3:0   0011=> 1/8     0101=> 1/32
   T0CON1.B2=1 ;
   T0CON1.B1=0 ;
   T0CON1.B0=1 ;
 // pour 100mS
  TMR0H         = 0x3C; //15536;
  TMR0L         = 0xB0;
//pour 1000mS
//   TMR0H         =0x0B ;      // 3036
//   TMR0L         =0xDC ;
//   T0CON1.B3=0;   //  0110=> 1/64
//   T0CON1.B2=1 ;
//   T0CON1.B1=1 ;
//   T0CON1.B0=0 ;
   PIR3.TMR0IF = 0;
   PIE3.TMR0IE = 1 ;      // TMR0 Enable Interrupt.
   T0CON0.B7=1;  //   Enable module and operating
   Drapeaux.Tmr0_Elapsed=0  ;
   Count0=0;
}

void I2C1_TimeoutCallback(char errorCode) {

  /*
   if (errorCode == _I2C1_TIMEOUT_RD) {
     // do something if timeout is caused during read
   }

   if (errorCode == _I2C1_TIMEOUT_WR) {
     // do something if timeout is caused during write
   }

   if (errorCode == _I2C_TIMEOUT_START) {
     // do something if timeout is caused during start
   }

   if (errorCode == _I2C_TIMEOUT_REPEATED_START) {
     // do something if timeout is caused during repeated start
   }
   */
}


void main()
 {
 
 // REGISTER 7-5: OSCFRQ: HFINTOSC FREQUENCY SELECTION REGISTER
   // Frequence selection 64MHz
     FRQ3_bit=1   ;
     FRQ2_bit=0   ;
     FRQ1_bit=0   ;
     FRQ0_bit=0   ;
    // REGISTER 7-6: OSCTUNE: HFINTOSC TUNING REGISTER
    OSCTUNE=0;
    //REGISTER 7-7: OSCEN: OSCILLATOR MANUAL ENABLE REGISTER
    OSCEN=0;

     //OSCCON2   Clock Source type  = HF INTOSC
     COSC2_bit=1;
     COSC1_bit=1;
     COSC0_bit=0;

     //REGISTER 8-1: CLKRCON: REFERENCE CLOCK CONTROL REGISTER
      CLKRCON.B7 =1;  // EN=1 = Reference clock module enabled
       //DC<1:0>: Reference Clock Duty Cycle bits 50%
      CLKRCON.B4 =1; //DC1=1;
      CLKRCON.B3=0; // DC2=0;
     //DIV<2:0>: Reference Clock Divider bits
      CLKRCON.B2=1;
      CLKRCON.B1=0;
      CLKRCON.B0=0;


    //CLKRCLK: CLOCK REFERENCE CLOCK SELECTION MUX
    //CLK<7:4>:  unused=0
    //CLK<3:0>: CLKR Clock Selection bits
    //0001 = HFINTOSC
    CLKRCLK=1;
    Etat_clock=OSCSTAT;
    Drapeaux.Tmr0_Elapsed=0  ;
    Count0=0;

    for (i=0;i<MAX_LEN2;i++) CRam1[i]=0;

    Init_Hardware();

    LATA.B4=1;
    LATA.B5=0;

      Unlock_IOLOCK();
        PPS_Mapping_NoLock(_RC7, _INPUT, U1RXPPS);
          Lock_IOLOCK();

    UART1_Init(19200);
    UART_Set_Active(&UART1_Read, &UART1_Write, &UART1_Data_Ready, &UART1_Tx_Idle); // set UART1 active

    UART1_Write(CLS);
    UART1_Write_CText("\r\n Presentation : \r\n");
    for (i=0;i<7;i++)   UART1_Write_CText(Messages[i]);

    Delay_ms(1000);
    SWDTEN_bit=0;
    CRLF1();
    txt=&TEXTE[0];

    Melodie=ON;

    LATA.B4=0;
    LATA.B5=1;

    Melodie=STOP;

    Unlock_IOLOCK();
    PPS_Mapping_NoLock(_RC4, _OUTPUT, _SDA1_in);
    PPS_Mapping_NoLock(_RC3, _OUTPUT, _SCL1_out);
    Lock_IOLOCK();
      //   Open-Drain Configuration on Pins Rx<7:0>
    //1 = Output drives only low-going signals (sink current only)
    //0 = Output drives both high-going and low-going signals (source and sink current)
    ODCONC.B4=1;

   /*
    UART1_Write_CText(" Init I2C remappable 400Khz: \r\n");
    I2C1_Remappable_Init();
    I2C1_SetTimeoutCallback(1000, I2C1_TimeoutCallback);
    I2C1_Remappable_Init();
    I2C_Set_Active(&I2C1_Remappable_Start, &I2C1_Remappable_Repeated_Start, &I2C1_Remappable_Rd, &I2C1_Remappable_Wr, &I2C1_Remappable_Stop, &I2C1_Remappable_Is_Idle);
    Dummy= I2C_Remappable_Start();
  */
    UART1_Write_CText(" Init I2C1 Software \r\n");
   Soft_I2C_Init();
    UART1_Write_CText(" Init RTC DS3231 \r\n");

      UART1_Write_CText(" Active sortie SQW=1Hz: \r\n");
     write_DS3231(0x0E,0b01000000);    // batterie LIRC2032 4,4V

  if(RTC_Forcee==0)    // RC0 input
  {
   UART1_Write_CText( "init RTC par defaut ");
   write_DS3231(0,0x50); //Reset second to 0 sec. and stop Oscillator
   write_DS3231(1,0x59); //write min
   write_DS3231(2,Dec2Bcd(14)); //write hour
   write_DS3231(3,0x02); //write day of week
   write_DS3231(4,0x01); // write date
   write_DS3231(5,0x05); // write month
   write_DS3231(6,Dec2Bcd(18)); // write year 2014
   }
   else
   {
       UART1_Write_CText(" Re-Lecture RTC ");
   }
   Display_Date_Time(1) ;
   CRLF1();

   UART1_Write_CText(" ADC_Init_Advanced (1,024V) pour EA sur RC2  LM35DZ \r\n");
   ADC_Init_Advanced(_ADC_INTERNAL_VREFL | _ADC_INTERNAL_FVRH1);

  //  IPR3.TMR0IP = 1; // Make TMR0 interrupt low priority
    Init_Timer0() ;
    UART1_Write_CText(" Init Timer0 sur 1 sec\r\n");
   /*
    LCD_CLS();;         // 1234567890123456
    LCD_Write_CText_At(1,1,"EA0 =       Pts");
    LCD_Write_CText_At(2,1,"Temp=          ");
    LCD_Write_Chr_At(2,14,6,SPECIAL);
    LCD_Write_Chr_At(2,15,'C',NORMAL);
   */

   
   Nb=0;
   SEA0=0;
   
   // Change IVTBASE if required
   IVTBASEU = 0x00; // Optional
   IVTBASEH = 0x00; // Default is 0x0008
   IVTBASEL = 0x08;

   INTCON0.GIEH = 1; // Enable high priority interrupts
   INTCON0.GIEL = 1; // Enable low priority interrupts
   PIE3.TMR0IE = 1 ;
   
   
  //======== Main Boucle ================
  while (1)
  {
     Raz_Buffer1() ;   // arme Interrupt UART Rx
     Display_Date_Time(0) ;   // sans affichage !
     Sec= Bcd2Dec(second);
     Mn= Bcd2Dec(minute);
     Hour= Bcd2Dec(heure);

  // Lecture_ADC sur RC2;
   EA0= ADC_Get_Sample(18);   // ANC2= 010010
  // EA0= ADC_Get_Sample(14); //+ Offset_Temper;  // was en dur : LM35DZ_OFFSET;
   SEA0=SEA0+EA0;  // cumul valeurs
   Nb++;
   if(Nb > 7)
   {
     // special BlueElec
      UART1_Write_Text(date); //UART1_Write(';');
      UART1_Write(TAB);
      UART1_Write_Text(time);// UART1_Write(';');
      UART1_Write_CText("   LM35DZ=");
      F1= (float)SEA0 *0.003125 ; // =  Alim MCU 5V   + Vref=1,024V => 1024mV/(8x4096) pts  pour 102.4  °C
      // moyenne 8 valeurs
      SEA0=SEA0>>3 ;  //
      WordToStrWithZeros(SEA0,CRam1);
      UART1_Write_Text(CRam1);
      UART1_Write(TAB);
      Float2Ascii (F1,Temperat,2);
      UART1_Write_Text(Temperat);
      UART1_Write_CText(" °C");
      CRLF1();
      SEA0=0;
      Nb=0;
    }
    
    LED_ROUGE = ~LED_ROUGE;
    U1TXB='*';
    _asm clrwdt      ;
    while(Drapeaux.Tmr0_Elapsed==0);
    
    Init_Timer0();
    if (Flag_Buffer1==1)
    {
       UART1_Write_CText("\r\n Reçu :\r\n");
       UART1_Write_Text(Buffer1);
       CRLF1();
       
       p1=Buffer1;
       if((*(p1)=='U') && (*(p1+20)=='#'))     //U;12;05;18;15;36;02;#<CR>
       {
          UART1_Write_CText("RTC Update : ");
          UART1_Write_Text(Buffer1);
          CRLF1();
          MAJ_RTC();
          Refresh_Date_Time() ;
          Sec= Bcd2Dec(second);
          Mn= Bcd2Dec(minute);
          Hour= Bcd2Dec(heure);
          p1=0;
       }
       Flag_Buffer1=0;
     }
  }
}


   /*
   
   
 Presentation :
 Directory :C:_MikroC_MesProjets_MikroC_18F27K42
 MikroC pro 7.30 Beta
 Projet :PIC18F27K42_UAR1_ADC_Soft_I2C_Test.mcppi
 Test PIC18F27K42  I2C
 Config bit : P18F27K42_FOSC_64Mhz.cfgsch FOSC:64.0 MHz
 Eeprom:  not used ....
 Source : PPIC18F27K42_UART1_ADC_Soft_I2C_Test__190130.c

 Init I2C1 Software
 Init RTC DS3231
 Active sortie SQW=1Hz:
 Re-Lecture RTC 30-01-19  18:37:33
 ADC_Init_Advanced (1,024V) pour EA sur RC2  LM35DZ
 Init Timer0 sur 1 sec
*******30-01-19 18:37:40   LM35DZ=00806 20.17 °C
********30-01-19        18:37:48   LM35DZ=00805 20.14 °C
********30-01-19        18:37:56   LM35DZ=00804 20.12 °C
********30-01-19        18:38:04   LM35DZ=00806 20.17 °C
********30-01-19        18:38:12   LM35DZ=00809 20.23 °C
********30-01-19        18:38:20   LM35DZ=00809 20.23 °C
********30-01-19        18:38:28   LM35DZ=00809 20.25 °C
********30-01-19        18:38:36   LM35DZ=00809 20.22 °C
********30-01-19        18:38:44   LM35DZ=00810 20.27 °C
********30-01-19        18:38:52   LM35DZ=00809 20.24 °C
********30-01-19        18:39:00   LM35DZ=00808 20.22 °C
********30-01-19        18:39:08   LM35DZ=00810 20.27 °C
********30-01-19        18:39:16   LM35DZ=00832 20.80 °C
********30-01-19        18:39:24   LM35DZ=00981 24.54 °C
********30-01-19        18:39:32   LM35DZ=01026 25.65 °C
********30-01-19        18:39:40   LM35DZ=00969 24.25 °C

============================================================
   MPLAB IPE :
   Connecting to MPLAB PICkit 4...

Currently loaded versions:
Application version............00.03.09
Boot version...................01.00.00
Script version.................00.02.52
Script build number............66d22ca77a
Application build number.......0123456789
Target voltage detected
Target device PIC18F27K42 found.
Device Id Revision = 0x0
2019-01-30 10:19:20 +0100 - Hex file loaded successfully.
Loading code from C:\_MikroC\_MesProjets_MikroC\_18F27K42\PIC18F27K42_I2C_Test.hex...

2019-01-30 10:19:42 +0100 - Programming...
*****************************************************
Erasing...
The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x1ebf
configuration memory
User Id Memory
Programming/Verify complete
2019-01-30 10:19:53 +0100 - Programming complete

 */

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: BETA7.3.0 Remappable I2C bug

#4 Post by filip.grujcic » 31 Jan 2019 15:14

Hi,

Which problems are you having with hardware I2C?
I didn't come across any problems while testing it.

Try using I2C1_Init(); so you don't need to do the PPS manually.
You can then use I2C1_Wr(); immediately after that.

Implementation of I2C library for K42/K83 differs from what you might be used to seeing on other PIC16/18 MCUs.
Note that I2C Write and Read functions expect a 7-bit slave address as the first input parameter, without the WR/RD bit at the end.

Kind regards,
Filip Grujcic

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

Re: BETA7.3.0 Remappable I2C bug

#5 Post by paulfjujo » 31 Jan 2019 17:46

hello,

filip.grujcic wrote: Note that I2C Write and Read functions expect a 7-bit slave address as the first input parameter, without the WR/RD bit at the end.
i allways used a 8 bits adresse device with I2C as 1rst parameter ,
7 bits for the device + 1 bit at 0 for write or +1 bit at 1 for read
I2C1_Wr(DS3231_ADDR); for write
I2C1_Wr(0);

I2C1_Wr(DS3231_ADDR+1);
second=I2C1_Rd(1);


does it means , something has changed wit MikroC 7.30 or with this particular PIC 18F27K42 ?

i know ATMEL FAMILY (ARDUINO) use 7 bits adress only as parameter..

there is no example for using new design of Hardware I2C1_Wr , specific to 18F27K42
// for PIC18F K42 and K83 family
unsigned short I2Cx_Wr(char slave_address, char *ptrdata, unsigned short count, unsigned short END_mode);

expect old classic 16F887

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: BETA7.3.0 Remappable I2C bug

#6 Post by filip.grujcic » 01 Feb 2019 09:52

Hi,

Nothing changed in particular, it's just how the library is written.
Write and Read functions contain code which shifts the slave_address parameter and adds the appropriate bit at the end, so you don't need to do it yourself - library does it for you.

For example: if a device has slave address of 0x82 for write and 0x83 for read, that means the 7-bit address(without the WR/RD bit at the end) will be 0x41 - this is the parameter that you should pass in I2C functions.

Below is the example on how to use I2C_Write function. Let's say you want to write 0xAA to register that's on the address 0x10:

Code: Select all

  unsigned short tmp[3] = {0};

  tmp[0] = 0x10;  // This is the register address
  tmp[1] = 0xAA;  // Some data to write
  I2C1_Wr( 0x41, tmp, 2, _I2C_END_MODE_STOP );
I2C_Read works in the same way. Let's say you want to read 3 bytes in total from registers on 0x10, 0x11, 0x12 one byte from each register:

Code: Select all

  unsigned short tmp[5] = {0};

  tmp[0] = 0x10;
  I2C1_Wr( 0x41, tmp, 1, _I2C_END_MODE_RESTART );
  I2C1_Rd( 0x41, tmp, 3, _I2C_END_MODE_STOP );
tmp[0] will be the data read from 0x10 address
tmp[1] data from 0x11
tmp[2] data from 0x12

Hopefully, this makes some things clearer. If not, don't hesitate to ask.

Kind regards,
Filip Grujcic

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

Re: BETA7.3.0 Remappable I2C bug

#7 Post by paulfjujo » 01 Feb 2019 21:17

hello,

so you confirm,
that the rules has changed
is it because MikroC 7.30 version
or with this particular PIC 18F27K42.?

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: BETA7.3.0 Remappable I2C bug

#8 Post by filip.grujcic » 04 Feb 2019 09:35

Hi,

I'm not sure what you mean by rules?
I2C library for K42/K83 has been like this since release.
Our developers wanted to add some consistency between all of our I2C libraries because I2C library for ARM and FT9xx for example, were implemented in this way as well.
Additionally, it's more simple than the step-by-step I2C library for other PICs.

Kind regards,
Filip Grujcic

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

Re: BETA7.3.0 Remappable I2C bug

#9 Post by paulfjujo » 06 Feb 2019 17:44

Hello Filip,

filip.grujcic wrote: .....
Hopefully, this makes some things clearer. If not, don't hesitate to ask.
not clear at all.. :( .for me !



i want to drive my RTC DS3231 with I2C1 Hardware , instead of I2C1 Software

When using I2C1 software , i am able to drive this RTC
and get correct data for Date & Time
The 1rst init of RTC is to activate the 1 HZ on SQW RTC output
( i put a led on this output to see if RTC is active)

i used 8 bits adresse for I2C Software !
I2C adresse of RTC is 0xD0
so for
Hardware I2C i used 0xD0>>1 => 0x68 because you said..now it is only 7 bits adress .
Impossible to pass over this first init ..
wich is OK in I2C Software mode.

Code: Select all

#define DS3231_ADDR 0x68   // sur 7 bits  RTC DS3231 used in Hardware I2C mode
//#define DS3231_ADDR 0xD0   // sur 8 bits  RTC DS3231 used in software i2C mode
 // I2C SDA sur RC4
 // I2C SCL sur RC3
 int addr;
 unsigned char tmp[16];
   
    // ....  in main .....
  
    //   Open-Drain Configuration on Pins Rx<7:0>
    //1 = Output drives only low-going signals (sink current only)
    //0 = Output drives both high-going and low-going signals (source and sink current)
    ODCONC.B4=1;  // SDA
 
    //nota : i have 2,7K pull up resistor on I2C SCL and I2C SDA 
    // so output could be 0 or 1  or could be used as input when I2C ACK from slave occured on SDA

    Unlock_IOLOCK();
    PPS_Mapping_NoLock(_RC4, _OUTPUT, _SDA1_in);
    PPS_Mapping_NoLock(_RC3, _OUTPUT, _SCL1_out);
    Lock_IOLOCK();

  // previous test with Software I2C 
  // UART1_Write_CText(" Init I2C1 Software \r\n");
   //Soft_I2C_Init();
     
     UART1_Write_CText(" Init I2C1 Hardware \r\n");
     I2C1_Init();
     UART1_Write_CText(" Init RTC DS3231 \r\n");
     UART1_Write_CText(" Active sortie SQW=1Hz: \r\n");
     tmp[0]=0x0E;
     tmp[1]=0b01000000;
     addr= DS3231_ADDR;
      I2C1_Wr(Adr, tmp, 2, _I2C_END_MODE_STOP );
     // blocked here, never go further
   
     // sofware I2C version ..OK
     //  write_DS3231(0x0E,0b01000000);  
    
     UART1_Write_CText(" suite.. \r\n");
     



nota:
with I2C software

Code: Select all

   
void write_DS3231(unsigned short address,unsigned short w_data)  // I2C WRITE
 {

  I2C1_Start();
  I2C1_Wr(DS3231_ADDR);  // 0xD0
  I2C1_Wr(address);
  I2C1_Wr(w_data);
  I2C1_Stop();

 }


could you do some test with I2C Hardware 18F27K42 and a RTC DS3231
what is wrong ?

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: BETA7.3.0 Remappable I2C bug

#10 Post by filip.grujcic » 08 Feb 2019 14:19

Hello,

I assume the problem is in your configuration bits, specifically the PPSLOCK bit which is supposed to be set to "Can be set and cleared repeteadly".
Could you please check this?

Also, you don't need to do the PPS manually if you are using I2C1_Init() function, only when you use I2C1_Remappable_Init() you need to do the PPS manually.
If you don't manage to get it working, feel free to zip and attach your project here for inspection.

Regards,
Filip Grujcic

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

Re: BETA7.3.0 Remappable I2C bug

#11 Post by paulfjujo » 09 Feb 2019 13:35

Hello Filip,


i tried to change the status of PPSLOCK, in configuration bit
Negatif, same problem ..
i tried also to remove the PPS pin allocation in my code
Negatif, same problem ..

so i post here all my project
PIC18F27K42_UART1_ADC_Hardw_I2C_Test_190208.zip

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

Re: BETA7.3.0 Remappable I2C bug

#12 Post by paulfjujo » 12 Feb 2019 19:36

:?: :?: :?:
:? some support needed..

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: BETA7.3.0 Remappable I2C bug

#13 Post by filip.grujcic » 13 Feb 2019 17:38

Hi,

I apologize for the delayed answer. In the project you sent you the PPSLOCK bit wasn't set correctly, and you forgot to send the Tiny_RTC_DS3231_HARDW_I2C1_for_K42.c file.
However, could you make a minimum project with just hardware I2C and one write command, and zip it and attach it here to help troubleshoot the issue?

Kind regards,
Filip Grujcic

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

Re: BETA7.3.0 Remappable I2C bug

#14 Post by paulfjujo » 13 Feb 2019 20:16

hello Filip,

i add the missing file in the zip...

in fact i just want the first init of I2C and RTC , and get the SQWE output 1Hz
all the rest of program can be removed to do a short test.

Code: Select all

  
     UART1_Write_CText(" Init I2C1 Hardware \r\n");
     I2C1_Init();
    UART1_Write_CText(" Init RTC DS3231 \r\n");
    UART1_Write_CText(" Active sortie SQW=1Hz: \r\n");
    write_DS3231(0x0E,0b01000000);    // batterie LIRC2032 4,4V
PIC18F27K42_UART1_ADC_Hardw_I2C_Test_190208.zip
anothe question :

Does I2Cx Hardware use internal ressource as TIMER2 or other ?

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: BETA7.3.0 Remappable I2C bug

#15 Post by filip.grujcic » 19 Feb 2019 15:16

Hello,

No, it doesn't use Timer2, nor other timers.
I've attached your project which I edited, it should go past the I2C1_Wr function without issues.
Please try the project first without any modifications to configuration bits nor the part I edited, which is from the beginning of main() to the "UART1_Write_CText(" suite.. \r\n");" line.
I had to comment out some of the lines as it wouldn't compile due to the missing .c and .h files.

Kind regards,
Attachments
_r_19.zip
(193.49 KiB) Downloaded 212 times
Filip Grujcic

Post Reply

Return to “mikroC PRO for PIC Beta Testing”