Thread: Modifying I2C code not working

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    10

    Modifying I2C code not working

    I am trying to communicate an arduino master using I2C to read ADC values of a slave.
    When the master sends some data ( i.e 0x20 for ADC register1) an interupt service routine (Combuf_isr) is run which is supposed to interpret this data (know that I want to read adc register), then read the adc register and then write the adc values so that the master can read them.
    I would like to have it so that the adc values are periodically updated and the master can just send read maybe every 100ms.
    I am new to C so I can only come up with something like the code below but problem is I do not think it updates the adc values periodically.
    Any help will be greatly appreciated!


    Code:
    void main(void)
    {
       while(1)
       {
          
          if(COMBUF_Flag)
           {
               COMBUF_Flag = 1;
               /* Transmit received data */
               COM_MCU_TO_DIF_B = COMBUF_Rx_Buf[0];
    .................................................. .................................................. ...........

    Code:
    interrupt void COMBUF_Handler(void)
    {
           int res;
           int cmd = COM_DIF_TO_MCU_B; /* read COMBUF data */
           cmd = -1;
    
    
           /* process COMBUF data */
           switch (cmd)
           {
    /* 0x20 to represent ADC reading PADC_DATA1 reg */
               case 0x20:
                   res = PADC_DATA1;
                   break;
    /* 0x22 to represent DAC reading DAC_reg_0 register */
               case 0x22:
                   res = PADC_DATA3;
                   break;
    
    
               default:
                    /* the cmd is invalid, so -1 will be returned */
                   break;
            }
    
    
       COMBUF_Rx_Buf[0] = res;          /* write data to COM_MCU_TO_DIF? */
       COMBUF_RX_STATUS_CLEAR();            /* Clear receive status */
       COMBUF_Flag = 1;                     /* Flag to process rx data */
    }
    Last edited by tomnas; 09-12-2017 at 06:49 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,736
    Before you massively complicate matters by adding an ISR to the mix, why not try polling in a loop to see if your basic idea actually works.
    Code:
    while ( 1 ) {
        // wait here until a sample is ready
        while ( /* something */ )
    
        // read the sample
    
        // display sample, print it, send it to port, something...
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    10
    Hi. The basic Idea does work.
    It is the ISR that I need help with...I would like the ISR to periodically read the adc data and write it to COM_MCU_TO_DIF (line 27).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. text file modifying program code, please help
    By Jackie Chen in forum C Programming
    Replies: 3
    Last Post: 03-20-2012, 01:17 AM
  2. Help Required Modifying Code
    By strokebow in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2011, 12:23 PM
  3. Still have a totaling issue after modifying code
    By scmurphy64 in forum C Programming
    Replies: 5
    Last Post: 09-30-2009, 05:54 PM
  4. self modifying code
    By chacham15 in forum C Programming
    Replies: 15
    Last Post: 09-07-2008, 04:05 PM
  5. Self modifying code
    By orbitz in forum Linux Programming
    Replies: 1
    Last Post: 06-06-2003, 06:09 PM

Tags for this Thread