title: using MPLAB's MCC to configure I2C and ADC
body: <p>I am trying to communicate to BQ24259 through I2C using PIC16LF1554. The 7th register of the device has a bit which can be used to turn off the battery. I used Mplab's MCC to configure the I2C settings. Using the example code in header files i wrote a similar function. </p>
  
  <p>here is the relevant snippet of the code</p>
  
  <pre><code>    #define RETRY_MAX       100
      #define ON              0x4B   //register 7 toggle 5th bit to turn ON/OFF
      #define OFF             0x6B
      I2C_MESSAGE_STATUS status;
  
      uint16_t        timeOut;
      uint8_t         writeBuffer[1];                // writeBuffer[0] = 07, writeBuffer[1] = data 01001011b(on) 01101011b(off)
      uint8_t         stat;
      uint16_t        address = (0x6B/2) ;           //Bit shifting to the write, and having '0' for write opertaoin, at MSB
  
  uint8_t bat_fet(uint8_t val){
      writeBuffer[0] = 7;                      //slave's seventh register
      writeBuffer[1] = val;
      timeOut=0;
      while(status != I2C_MESSAGE_FAIL){
          I2C_MasterWrite( writeBuffer,        // address of data to be sent
                           2,                  // number of data bytes
                           address,            // address of the peripheral
                           &amp;status);           // address of status register
  
          while(status == I2C_MESSAGE_PENDING);
  
          if(status == I2C_MESSAGE_COMPLETE){
              return 1;
              break;
          }
          if(timeOut == RETRY_MAX){
              return 0;
  
              break; 
          }
          else
              timeOut++;
      }
      if(status == I2C_MESSAGE_FAIL)
          return 0;
  
  }
  </code></pre>
  
  <p>but its not working, nothing happens, sometimes the controller just freezes, mostly it continues to work. I have connected an LED to an unused IO pin, and programmed it to turn on whenever the function returns 1, and it turns on. But the battery remains on too. </p>
  
  <p>Similarly, i am trying to use ADC 1 of the pic to check the battery voltage, </p>
  
  <p>here is the code,</p>
  
  <pre><code>uint16_t check_bat_voltage(){
  
      uint16_t bat_v;
  
      ADC1_StartConversion(01011); //i am using Channel AN11, but no matter what variation of channel and AN11 i passed, it just wouldn't recognize. so i just pass the 5 bit values of the ADCON1 register.
  
      while(ADC1_IsConversionDone());
  
      bat_v = ADC1_GetConversionResult();  
  
  //digital value = [analog voltage / (vref+ - vref-)] * 1024 
   //analog value minimum = 2.5/2, voltage divider network
   //vref+ 5
   //vref- 0
   // 1.25/5 * 1024 = 256
      return bat_v;
  }
  </code></pre>
  
  <p>but again nothing happens.
  Can anyone please review it? I am just stuck. Thanks.</p>
  
  <p>I can attach screen shots of MCC too, maybe i didnot configure the peripherals right.</p>
  
  <p>P.S. i do initialize both modules in main().</p>