Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Modifying I2C code

Inno T MushongaInno T Mushonga Member Posts: 2

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 and also I have got 2 ADC registers so in the main function how do I make it so that it runs the ISR if the COM_DIF_TO_MCU receives either 0x20 or 0x23.

Pseudocode:
void main(void)
{
while(1)
{
if(COM_DIF_TO_MCU ==0x20||0x23)
{
COMBUF_isr();
}

 }

}
void COMBUF_isr(void)
{
/* do some housekeeping stuff, e.g. clear ISR bit /
COMBUF_RX_STATUS_CLEAR();
int cmd = COM_DIF_TO_MCU_read(); /
read COMBUF data */
int res = -100;

/* process COMBUF data /
switch (cmd)
{
/
you chose 0x20 to represent ADC reading PADC_DATA1 register */
case 0x20:
res = readPADC_DATA1reg();
break;

   /* you chose 0x23 to represent ADC reading PADC_DATA3 register */
   case 0x23:
       res = readPADC_DATA3reg();
       break;

   default:
        /* the cmd is invalid, so -1 will be returned */
       break;
}

 COM_MCU_TO_DIF_write(res);

}

Tagged:
Sign In or Register to comment.