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 */ }