Hot!I2C read and write operation

Author
ajitnayak87
New Member
  • Total Posts : 24
  • Reward points : 0
  • Joined: 2017/06/20 03:53:02
  • Location: 0
  • Status: offline
2017/10/07 21:56:37 (permalink)
0

I2C read and write operation

Dear all,
 
I am using MPLAB X ide . I am trying to interface PIC18F24K40 with DS1307. here is below code .
 
I have used MCC configuration for i2c interface. Below code compile properly but can able to read I2c value. can someone suggest how can i2c config made w.r.t mplab x ide.
 
If any one executed sample code .help to understand confi and read value. 
 

#include "mcc_generated_files/mcc.h"
#include"xc.h"
#include"pic18.h"
//#include "18f24k40.cgen.inc"
unsigned char Digits[4]=0;
unsigned char TempData;
#define SDA RC4 // Data pin for i2c
#define SCK RC3 // Clock pin for i2c
#define SDA_DIR TRISC4 // Data pin direction
#define SCK_DIR TRISC3 // Clock pin direction

// Define i2c speed
#define I2C_SPEED 100 // kbps






// Function Purpose: I2C_Start sends start bit sequence
void I2C_Start(void)
{
    SSP1CON2bits.SEN=1;
 
 while(!PIR3bits.SSPIF); // Wait for it to complete
 PIR3bits.SSPIF = 0; // Clear the flag bit
}


// Function Purpose: I2C_ReStart sends start bit sequence
void I2C_ReStart(void)
{
 SSP1CON2bits.RSEN = 1; // Send Restart bit
while(!PIR3bits.SSPIF); // Wait for it to complete
 PIR3bits.SSPIF = 0;
}


//Function : I2C_Stop sends stop bit sequence
void I2C_Stop(void)
{
 SSP1CON2bits.PEN = 1; // Send stop bit
 while(!PIR3bits.SSPIF); // Wait for it to complete
 PIR3bits.SSPIF = 0;
}



//Function : I2C_Send_ACK sends ACK bit sequence
void I2C_Send_ACK(void)
{
 SSP1CON2bits.ACKDT = 0; // 0 means ACK
 SSP1CON2bits.ACKEN = 1; // Send ACKDT value
 while(!PIR3bits.SSPIF); // Wait for it to complete
 PIR3bits.SSPIF = 0;
}


//Function : I2C_Send_NACK sends NACK bit sequence
void I2C_Send_NACK(void)
{
 SSP1CON2bits.ACKDT = 1; // 0 means ACK
 SSP1CON2bits.ACKEN = 1; // Send ACKDT value
 while(!PIR3bits.SSPIF); // Wait for it to complete
 PIR3bits.SSPIF = 0;
}


// Function Purpose: I2C_Write_Byte transfers one byte
bit I2C_Write_Byte(unsigned char Byte)
{
 SSP1BUF = Byte; // Send Byte value
 while(!PIR3bits.SSPIF); // Wait for it to complete
 PIR3bits.SSPIF = 0;

 return SSP1CON2bits.ACKSTAT; // Return ACK/NACK from slave
}


// Function Purpose: I2C_Read_Byte reads one byte
unsigned char I2C_Read_Byte(void)
{
 SSP1CON2bits.RCEN = 1; // Enable reception of 8 bits
 while(!PIR3bits.SSPIF); // Wait for it to complete
 PIR3bits.SSPIF = 0;
 
    return SSP1BUF ; // Return received byte
}




void main(void)
{
    // Initialize the device
    SYSTEM_Initialize();

    
    
     
    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
    // Use the following macros to:

    // Enable high priority global interrupts
    //INTERRUPT_GlobalInterruptHighEnable();

    // Enable low priority global interrupts.
    //INTERRUPT_GlobalInterruptLowEnable();

    // Disable high priority global interrupts
    //INTERRUPT_GlobalInterruptHighDisable();

    // Disable low priority global interrupts.
    //INTERRUPT_GlobalInterruptLowDisable();

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    unsigned char RxByte = 0;

        

 I2C_Start(); // Send start bit on i2c
 I2C_Write_Byte(0xA0); // Send 0xA0 on i2c
 RxByte = I2C_Read_Byte(); // Read value from i2c
 I2C_Send_ACK(); // Send ACK bit on i2c
 I2C_Stop(); // Send stop bit on i2c
    
        
    while (1)
    {
      
        
    }
}

#1

9 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 5833
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: I2C read and write operation 2017/10/07 22:15:17 (permalink)
    +2 (2)
    I suggest you start by reading how I2C works.
     
    Start at https://en.wikipedia.org/wiki/I%C2%B2C
    and also have a look at the actual I2C specification at http://www.nxp.com/documents/user_manual/UM10204.pdf
     
    Your code has no hope of working, as you are sending a slave-write address (0xA0), and then trying to do a read.
     
     
    #2
    ajitnayak87
    New Member
    • Total Posts : 24
    • Reward points : 0
    • Joined: 2017/06/20 03:53:02
    • Location: 0
    • Status: offline
    Re: I2C read and write operation 2017/10/09 01:00:17 (permalink)
    0
    Here is  working code for pic16F886 with 4 segment display. i could able to see display chane hour and min. I wanted to convert same code to work on pic18F24K40
     
     
        #include <htc.h>
        #include <stdio.h>
        #include<pic.h>
        #include<stdint.h>
     
    #define _XTAL_FREQ 40000000
    unsigned int i=0;
    unsigned int k=0; 
    unsigned int count;
     
    unsigned int x;
     
    #define LED RC7
     
    #define CONTROLREG 0xFF
    #define SDA           RC4              // Data pin for i2c
    #define SCK           RC3              // Clock pin for i2c
    #define SDA_DIR       TRISC4           // Data pin direction
    #define SCK_DIR       TRISC3           // Clock pin direction
     
    #define I2C_SPEED 150              // kbps 
       
    unsigned short int cnt, num,Dgt=0;;
    unsigned short int temp1,temp2,temp3;
     
    unsigned short sec;
    unsigned short min;
    unsigned short hour;
    unsigned short date;
    unsigned short month;
    unsigned short year;
    unsigned short day;
    unsigned short int temp=0;
    unsigned short r_data;
    #define Seg1 0x01
    #define Seg2 0x02
    #define Seg3 0x04
    #define Seg4 0x08
    #define Seg5 0x10
    #define Seg6 0x20
       
    void Delay(unsigned int k)
    {
    unsigned  int j;
    for(j=0;j<k;j++);
    }
     
    void wait_mssp(void){
    while(!SSPIF);
    SSPIF =0;
    }
       
     
     
     
     
     
        void SetSeg(unsigned short data, unsigned  short segno)
       
        {
                         switch(data)
                          {
                              case 0: PORTB = 0x3F; break;
                              case 1: PORTB = 0x06; break;
                             case 2: PORTB = 0x5B; break;
                              case 3: PORTB = 0x4F; break;
                              case 4: PORTB = 0x66; break;
                              case 5: PORTB = 0x6D; break;
                              case 6: PORTB = 0x7D; break;
                              case 7: PORTB = 0x07; break;
                              case 8: PORTB = 0x7F; break;
                              case 9: PORTB = 0x6F; break;
                              default : PORTB = 0X00; break;
                         }
          
                  if(segno==1)
                       {
                          PORTA = Seg4;
                       }
                 if(segno==2)
                       {
                          PORTA = Seg3;
                       }
                 if(segno==3)
                       {
                          PORTA = Seg2; 
                       }
                 if(segno==4)
                       {
                          PORTA = Seg1; 
                       }
     
               if(segno==5)
                       {
                         PORTC=0X00;
                        PORTC = 0x40;//DP2 fourth Segment   
                               
                     // PORTCbits.RC5=1;
                   }
     
                  if(segno==6)
                       {
                        PORTC=0X00;
                        PORTC= 0x20;//DP2 third Segment
                      
                       //PORTCbits.RC6=1;
                       }
     
           
                  if(segno==7)
                       {
                         PORTA=0X00;
                        PORTA = Seg5; //DP2 Second Segment
                     //  PORTAbits.RA4=1;
                       }
     
                  if(segno==8)
                       {
                       PORTA=0X00;
                        PORTA = Seg6; //DP2 First Segment
                      // PORTAbits.RA5=1;
                       }
     
        }
       
     
      unsigned int bcdtodecimal(unsigned int bcd)
    {
        unsigned int decimal;
        decimal = (((bcd & 0xF0) >> 4) * 10) + (bcd & 0x0F);
        return decimal;
    }
     
     
     
     
     
     
    void interrupt isr(void)
        {
                 if(TMR1IF==1)
                 {
                    TMR1H=0xFD; // Load the time value(0x0BDC) for 150ms delay
                     TMR1L=0xE8; //Timer1 Interrupt for 61500
                     TMR1IF=0; // Clear timer interrupt flag
                     Dgt++;
                  
                    if(Dgt>=5)
                     {
                        Dgt=0;
                         LED=!LED;
     
                      
                      SetSeg(min/10,4);
                      SetSeg(min%10,3);
                       SetSeg(sec/ 10,2);
                        SetSeg(sec%10,1);
     
                                
     
                     }
                   
                  }       
                          
      }
     
    void ds1307_write(unsigned char addr ,unsigned char data){
    SEN =1; //Start bit
    wait_mssp();
    SSPBUF = 0b11010000; //slave address(address of ds1307) + write bit
    wait_mssp();
    SSPBUF =addr;
    wait_mssp();
    SSPBUF = data;
    wait_mssp();
    PEN =1; //stop bit
    wait_mssp();
    }
    unsigned int ds1307_read(unsigned char addr){
    RSEN =1;
    wait_mssp();
    SSPBUF =0b11010000; //slave address(address of ds1307) + write bit;
    wait_mssp();
    SSPBUF =addr;
    wait_mssp();
    RSEN =1;
    wait_mssp();
    SSPBUF =0b11010001; //slave address(address of ds1307) + read bit;
    wait_mssp();
    RCEN =1;
    wait_mssp ();
    ACKDT=1;
    ACKEN =1;
    PEN=1;
    wait_mssp ();
    x = SSPBUF;
    return (x);
    }
    void init_i2c(void){
    PORTC = 0x00;
    TRISC3 =1;
    TRISC4 =1;
    SSPSTAT = 0b10000000; //0x80
    SSPCON = 0b00101000;//0x28 i2c master mode
    SSPADD = 0x31;//decimal--49.
    //value for SSPADD found using eqn--> clock =Fosc/ 4*(SSPADD +1); clock--100Khz, Fosc =20Mhz
    ds1307_write(0,0x00);
    }
     
    void Timer1_Interrupt()
        {
       
        INTCON = 0b00000000;
        PIE1=0b00000001;
        PIR1=0x01;
        TMR1H=0x0B;
        TMR1L=0xDC;
        //   T1CON=0x31;
        T1CON=0x01;
        }
     
    void Init_Controller()
        {
        cnt=150;    
        TRISC=0b00000000;
        PORTC = 0b00000000;               
        TRISB=0b00000000;
        PORTB = 0b00000000;
        TRISA=0b00000000;
        PORTA = 0b00000000;                            
        ADCON0 = 0b00000000;
        ANSEL = 0b00000000;
        Timer1_Interrupt();
       
        }
       
       
             void main(void)
        {
                
                   Init_Controller();
                   GIE=1;
                   PEIE=1;
                   TMR1IE=1;
                 //init_i2c();
                 init_i2c();
                ds1307_write(0,0x10);
                 ds1307_write(1,0X07);
                  ds1307_write(2,0X11);
                  ds1307_write(3,0X01);
                 ds1307_write(4,0x07);
                 ds1307_write(5,0X08);
                 ds1307_write(6,0X11);
     
             while(1)
     
                 {       
                   sec = ds1307_read(0);
                    sec=bcdtodecimal(sec);
                     min = ds1307_read(1);
                    min = bcdtodecimal(min);
                     hour = ds1307_read(2);
                    hour=bcdtodecimal( hour);
                     day= ds1307_read(3);
                     day = bcdtodecimal(day);
                     date= ds1307_read(4);
                    date=bcdtodecimal(date);
                     month= ds1307_read(5);
                    month = bcdtodecimal( month);
                     year= ds1307_read(6);
                     year=  bcdtodecimal(year); 
                    
                 }
                            
                   
        }
       
     
     
     
     
    post edited by ajitnayak87 - 2017/10/09 01:02:29
    #3
    ajitnayak87
    New Member
    • Total Posts : 24
    • Reward points : 0
    • Joined: 2017/06/20 03:53:02
    • Location: 0
    • Status: offline
    Re: I2C read and write operation 2017/10/09 01:01:17 (permalink)
    0
    Here is code with PIC18F24K40. Here i cant able to display RTC Data
     
     
     
    /**
      Generated Main Source File
     
      Company:
        Microchip Technology Inc.
     
      File Name:
        main.c
     
      Summary:
        This is the main file generated using MPLAB(c) Code Configurator
     
      Description:
        This header file provides implementations for driver APIs for all modules selected in the GUI.
        Generation Information :
            Product Revision  :  MPLAB(c) Code Configurator - 4.15
            Device            :  PIC18F24K40
            Driver Version    :  2.00
        The generated drivers are tested against the following:
            Compiler          :  XC8 1.35
            MPLAB             :  MPLAB X 3.40
    */
     
    /*
        (c) 2016 Microchip Technology Inc. and its subsidiaries. You may use this
        software and any derivatives exclusively with Microchip products.
     
        THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
        EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
        WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
        PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
        WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
     
        IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
        INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
        WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
        BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
        FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
        ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
        THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
     
        MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
        TERMS.
    */
     
    #include "mcc_generated_files/mcc.h"
    #include<xc.h>
     
     
    unsigned int i=0;
    unsigned int k=0; 
    unsigned int count;
     
    unsigned int x;
     
    #define LED RC7
     
    #define CONTROLREG 0xFF
    #define SDA           RC4              // Data pin for i2c
    #define SCK           RC3              // Clock pin for i2c
    #define SDA_DIR       TRISC4           // Data pin direction
    #define SCK_DIR       TRISC3           // Clock pin direction
     
    #define I2C_SPEED 150              // kbps 
       
    unsigned short int cnt, num,Dgt=0;;
    unsigned short int temp1,temp2,temp3;
     
    unsigned short sec;
    unsigned short min;
    unsigned short hour;
    unsigned short date;
    unsigned short month;
    unsigned short year;
    unsigned short day;
    unsigned short int temp=0;
    unsigned short r_data;
    #define Seg1 0x01
    #define Seg2 0x02
    #define Seg3 0x04
    #define Seg4 0x08
    #define Seg5 0x10
    #define Seg6 0x20
     
     
     
    void Delay(unsigned int k)
    {
    unsigned  int j;
    for(j=0;j<k;j++);
    }
     
     
     
       
     
        void SetSeg(unsigned short data, unsigned  short segno)
       
        {
                         switch(data)
                          {
                              case 0: PORTB = 0x3F; break;
                              case 1: PORTB = 0x06; break;
                             case 2: PORTB = 0x5B; break;
                              case 3: PORTB = 0x4F; break;
                              case 4: PORTB = 0x66; break;
                              case 5: PORTB = 0x6D; break;
                              case 6: PORTB = 0x7D; break;
                              case 7: PORTB = 0x07; break;
                              case 8: PORTB = 0x7F; break;
                              case 9: PORTB = 0x6F; break;
                              default : PORTB = 0X00; break;
                         }
          
                  if(segno==1)
                       {
                          PORTA = Seg4;
                       }
                 if(segno==2)
                       {
                          PORTA = Seg3;
                       }
                 if(segno==3)
                       {
                          PORTA = Seg2; 
                       }
                 if(segno==4)
                       {
                          PORTA = Seg1; 
                       }
     
               if(segno==5)
                       {
                         PORTC=0X00;
                        PORTC = 0x40;//DP2 fourth Segment   
                               
                     // PORTCbits.RC5=1;
                   }
     
                  if(segno==6)
                       {
                        PORTC=0X00;
                        PORTC= 0x20;//DP2 third Segment
                      
                       //PORTCbits.RC6=1;
                       }
     
           
                  if(segno==7)
                       {
                         PORTA=0X00;
                        PORTA = Seg5; //DP2 Second Segment
                     //  PORTAbits.RA4=1;
                       }
     
                  if(segno==8)
                       {
                       PORTA=0X00;
                        PORTA = Seg6; //DP2 First Segment
                      // PORTAbits.RA5=1;
                       }
     
        }
     
     
     
          unsigned int bcdtodecimal(unsigned int bcd)
    {
        unsigned int decimal;
        decimal = (((bcd & 0xF0) >> 4) * 10) + (bcd & 0x0F);
        return decimal;
    }
     
     
     
     
    void wait_mssp(void)
    {
    while(!PIR3bits.SSPIF);
    PIR3bits.SSPIF=0;
    }
     
    void ds1307_write(unsigned char addr ,unsigned char data)
    {
        SSP1CON2bits.SEN =1; //Start bit
        wait_mssp();
        SSP1BUF = 0b11010000; //slave address(address of ds1307) + write bit
        wait_mssp();
       SSP1BUF=addr;
        wait_mssp();
        SSP1BUF = data;
        wait_mssp();
        SSP1CON2bits.PEN =1; //stop bit
        wait_mssp();
    }
     
    unsigned int ds1307_read(unsigned char addr)
    {
            SSP1CON2bits.RSEN =1;
            wait_mssp();
            SSP1BUF =0b11010000; //slave address(address of ds1307) + write bit;
            wait_mssp();
            SSP1BUF =addr;
            wait_mssp();
           SSP1CON2bits.RSEN =1;
            wait_mssp();
            SSP1BUF =0b11010001; //slave address(address of ds1307) + read bit;
            wait_mssp();
            SSP1CON2bits.RCEN =1;
            wait_mssp ();
            SSP1CON2bits.ACKDT=1;
            SSP1CON2bits.ACKEN =1;
            SSP1CON2bits.PEN=1;
            wait_mssp ();
            x = SSP1BUF;
            return (x);
    }
     
     
    void i2c_Init()
    {
        SSP1CON1=0X3;
        SSP1CON2=0X00;
        SSP1ADD=19;
        SSP1STATbits.CKE=1;
        SSP1STATbits.SMP=1;
        PIR3bits.BCLIF=0;
        ds1307_write(0,0x00);
    }
     
     
     
    void ISR_Routine(void)
      {
        if(PIR0bits.TMR0IF==1)
          {
                PIR0bits.TMR0IF = 0;
                count= count+1;
                if(count>=15)
                {
                
                    
                     LED=!LED;
                    /*  SetSeg(min/10,4);
                      SetSeg(min%10,3);
                       SetSeg(sec/ 10,2);
                        SetSeg(sec%10,1);*/
                   
                } 
           }
         
            
                  
        }
     
     
     
     
     
     
    void main(void)
    {
        // Initialize the device
        SYSTEM_Initialize();
       
              ds1307_write(0,0x10);
                ds1307_write(1,0X07);
                  ds1307_write(2,0X11);
                  ds1307_write(3,0X01);
                 ds1307_write(4,0x07);
                 ds1307_write(5,0X08);
                 ds1307_write(6,0X11);
     
     
        while(1)
        {
           LED=1;
          
          PORTA = Seg4;
          PORTB = 0x3F;
              /* sec = ds1307_read(0);
                    sec=bcdtodecimal(sec);
                     min = ds1307_read(1);
                   min = bcdtodecimal(min);
                     hour = ds1307_read(2);
                    hour=bcdtodecimal( hour);
                     day= ds1307_read(3);
                     day = bcdtodecimal(day);
                     date= ds1307_read(4);
                    date=bcdtodecimal(date);
                     month= ds1307_read(5);
                    month = bcdtodecimal( month);
                     year= ds1307_read(6);
                     year=  bcdtodecimal(year); */
           
            if(cnt<=9000)
                   {
           
                    num = cnt;
                    temp = num / 1000;
                    SetSeg(num / 1000,4);
                    num = num % 1000;
                  
                    temp = num / 100;
                    SetSeg(num / 100,3);
                    num = num % 100;
                    temp2=temp;
                   
                    SetSeg(num/10,2);              
                    SetSeg(num % 10,1);            
     
                   }
            
       
            __delay_ms(100);
           
        }
      
     
    }  
       
     
     
     
     
     
     
    #4
    DarioG
    Scheisse Menschen
    • Total Posts : 52453
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: hi there
    • Status: offline
    Re: I2C read and write operation 2017/10/09 01:58:37 (permalink)
    0
    Is the display part ok? I.e. can you write numbers? So you can focus on the RTC side...
    #5
    ajitnayak87
    New Member
    • Total Posts : 24
    • Reward points : 0
    • Joined: 2017/06/20 03:53:02
    • Location: 0
    • Status: offline
    Re: I2C read and write operation 2017/10/09 02:24:43 (permalink)
    +2 (2)
    http://www.studentcompanion.co.za/pic-microcontroller-communication-with-i%C2%B2c-bus-xc8/
     
    I found useful link Here. I wil Try it once But i suspected on Read andd write operation.Here i need to read from single Slave device DS1307 So i should config PIC18 has master or slave.
     
    Yes I can able to display data but only 0-9 . It wont communicating with i2c.its not working like PIC16F886. Let me try above config.
     
    Meanwhile. If anyone tried i2c Code on PIC18 using MPLAB X , please share config and code for unsdestanding
     
    #6
    ajitnayak87
    New Member
    • Total Posts : 24
    • Reward points : 0
    • Joined: 2017/06/20 03:53:02
    • Location: 0
    • Status: offline
    Re: I2C read and write operation 2017/10/09 06:13:52 (permalink)
    0
    i tried above thing. But i could not able to read i2c data. can someone suggest how to read from PIC controller using MCC configuration.
    #7
    rodims
    Super Member
    • Total Posts : 840
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: online
    Re: I2C read and write operation 2017/10/09 07:06:14 (permalink)
    +1 (1)
    Your Link explicitly explains how to to use i2c with MCC, including a 9 minute video.
    In case you have problems, you need be more specific. "not able to read i2c data" is not really helpful. Anything could be wrong, even your wiring. Don't try to solve more than one problem at a time. Drop all this "display" stuff and focus on reading with i2c. Just try to read a single register, possibly register 0 containing the 'seconds', so you should see different values if your RTC is running.
     
    You can go for a solution with or without MCC, just invest a bit more time googling and reading.
    The web is full of information for PIC and DS1307 e.g.
    http://www.studentcompani...c-microcontroller-xc8/
     
    P.S. and please use "code tags" if you post code listings
    #8
    ajitnayak87
    New Member
    • Total Posts : 24
    • Reward points : 0
    • Joined: 2017/06/20 03:53:02
    • Location: 0
    • Status: offline
    Re: I2C read and write operation 2017/10/09 07:53:42 (permalink)
    0
    The example  code what they shared , later using MICKOE compiler library to continue the code . They are not using MPLAB IDE itself to read I2c Data and write.
     
    The Mcc configuration is easy . later how to use mcc function to read data is not explained. I have tried using library but cant able to read anything. always its reading 00 
     
    If i am using code tag i am getting error. So i posted without code tags. 
    #9
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 1362
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: I2C read and write operation 2017/10/09 08:43:36 (permalink)
    0
    No need to list your display code, that is irrelevant to reading from the rtc.
     
    You don't have a restart in your code which you need for reading.
    Here's some code that reads strings from an i2c device.  I have it C somewhere but this will do.  It's easy to see what is going on.
    .include "xc.inc"
    .global _i2c_gets
    .section i2c_code,code
    ;----------------------------------------------------
    .equ arg_ID,        w0
    .equ arg_Address,   w1
    .equ arg_pDest,     w2
    .equ arg_Len,       w3
    .equ ID,            w4
    .equ addr,          w5

    _i2c_gets:   ;void i2c_gets(ubyte ID_I2C, ubyte Addr, void *Dest, int Len);
        mov.d     arg_ID,ID
        ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        rcall   _i2c_start
        mov.b   ID,w0
        rcall   _i2c_write
        mov.b   addr,w0
        rcall   _i2c_write
        rcall   _i2c_restart
        ior.b   ID,#1,w0
        rcall   _i2c_write
        ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    loop:
        clr     w0      ;ack
        cp      arg_Len,#1
        bra     nz,$+4
        mov     #1,w0   ;nack
        ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        ;nack
        rcall   _i2c_read
        mov.b   w0,[arg_pDest++]
        dec     arg_Len,arg_Len
        bra     nz,loop
        rcall   _i2c_stop
        return
    ;----------------------------------------------------
    .end



    post edited by Gort2015 - 2017/10/09 08:48:05

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #10
    Jump to:
    © 2017 APG vNext Commercial Version 4.5