|
| 1 | +#include "mcc_generated_files/system/system.h" |
| 2 | +#include "mcc_generated_files/adc/adc.h" |
| 3 | +#include "string.h" |
| 4 | +#include "stdlib.h" |
| 5 | +#include "stdio.h" |
| 6 | +#include "application.h" |
| 7 | + |
| 8 | +/******************************************************************************* |
| 9 | + * void Application(void) |
| 10 | + * |
| 11 | + * API which measures the connected ph sensor data through ADC interface periodically. |
| 12 | + * By default Slave will be in the sleep mode and it wakes up after every 5sec using |
| 13 | + * Timer interrupt or whenever I2C start condition is detected. |
| 14 | + * |
| 15 | + * @param void |
| 16 | + * @return void |
| 17 | + ******************************************************************************/ |
| 18 | +void Application(void) |
| 19 | +{ |
| 20 | + //To read the sensor data periodically for every 5 sec |
| 21 | + if(timerInterruptFlag == SET) |
| 22 | + { |
| 23 | + #ifdef DEBUG |
| 24 | + EUSART1_SendString("Timer WakeUP\n"); // only for debug |
| 25 | + #endif |
| 26 | + Sensor1_DataProcessing(); |
| 27 | + timerInterruptFlag = 0; |
| 28 | + } |
| 29 | + else if(SSP1STATbits.I2C_START) |
| 30 | + { |
| 31 | + #ifdef DEBUG |
| 32 | + EUSART1_SendString(" I2C WakeUP\n"); //only for debug |
| 33 | + #endif |
| 34 | + if(i2cRdDataIndex >= 2) |
| 35 | + { |
| 36 | + CLOCK_STRETCH_ENABLE(); |
| 37 | + i2cRxDataLength = i2cRdData[ONE]; |
| 38 | + i2cRxSensorRegisterAddress = i2cRdData[ZERO]; |
| 39 | + SensorDataRegisterRead(i2cRxSensorRegisterAddress,i2cRxDataLength); |
| 40 | + CLOCK_STRETCH_DISABLE(); |
| 41 | + DELAY_FUNCTION(); |
| 42 | + i2cRdDataIndex = 0; |
| 43 | + } |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + SLEEP(); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/******************************************************************************* |
| 52 | + * void EUSART1_SendString(const char *str) |
| 53 | + * |
| 54 | + * API to print a sequence of characters on the terminal window |
| 55 | + * |
| 56 | + * @param *str - to send a sequence of strings on terminal window |
| 57 | + * @return void |
| 58 | + ******************************************************************************/ |
| 59 | +void EUSART1_SendString(const char *str) |
| 60 | +{ |
| 61 | + for(uint8_t index = 0; index < strlen(str); index++) |
| 62 | + { |
| 63 | + EUSART1_Write(str[index]); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/******************************************************************************* |
| 68 | + * void Sensor1_DataProcessing(void) |
| 69 | + * |
| 70 | + * API to measure the ph sensor through ADC interface and calculate the ph value |
| 71 | + * using the formula. Store the ph value in static RAM location. |
| 72 | + * |
| 73 | + * @param void |
| 74 | + * @return void |
| 75 | + ******************************************************************************/ |
| 76 | +void Sensor1_DataProcessing(void) |
| 77 | +{ |
| 78 | + uint16_t adcBuffer = 0; |
| 79 | + uint16_t adcSamples = 0; |
| 80 | + uint16_t samplesAverage = 0; |
| 81 | + float dataBuffer = 0; |
| 82 | + uint8_t samplesCount; |
| 83 | + |
| 84 | + for (samplesCount = 0; samplesCount < ADC_SAMPLES; samplesCount++) |
| 85 | + { |
| 86 | + adcBuffer = ADC_GetConversion(channel_ANC4); |
| 87 | + adcSamples += adcBuffer; |
| 88 | + } |
| 89 | + |
| 90 | + if(samplesCount >= ADC_SAMPLES) |
| 91 | + { |
| 92 | + samplesAverage = (adcSamples >> RIGHTSHIFT_COUNT); |
| 93 | + dataBuffer = (samplesAverage * VDD)/ ADC_MAX_VALUE; |
| 94 | + phValue = (uint8_t)(CONSTANT * dataBuffer); |
| 95 | + sensor_Data_Register[0] = phValue; |
| 96 | + adcSamples = 0; |
| 97 | + samplesCount = 0; |
| 98 | + adcBuffer = 0; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/******************************************************************************* |
| 103 | + * void SensorDataRegisterAddrMatch(uint8_t *sensorDataAddress) |
| 104 | + * |
| 105 | + * API which reads the data stored in static RAM location into an i2cWrData buffer |
| 106 | + * which is transmitted to the host through i2c interface |
| 107 | + * |
| 108 | + * @param *sensorDataAddress - holds the sensor data register address sent from the |
| 109 | + * host device |
| 110 | + * @return void |
| 111 | + ******************************************************************************/ |
| 112 | +void SensorDataRegisterRead(uint8_t *sensorRevdData, uint8_t dataLength) |
| 113 | +{ |
| 114 | + uint8_t index = 0; |
| 115 | + |
| 116 | + //Clears the i2cWrData array |
| 117 | + memset((uint8_t*)i2cWrData, 0 , sizeof(i2cWrData)); |
| 118 | + |
| 119 | + for(index = 0;index < dataLength;index++) |
| 120 | + { |
| 121 | + i2cWrData[index] = *(sensorRevdData+index); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/******************************************************************************* |
| 126 | + * bool I2C_ClientInterruptHandler(i2c_client_transfer_event_t event) |
| 127 | + * |
| 128 | + * API to handle I2C Read and Write Interrupt handler |
| 129 | + * |
| 130 | + * @param void |
| 131 | + * @return void |
| 132 | + ******************************************************************************/ |
| 133 | +bool I2C_ClientInterruptHandler(i2c_client_transfer_event_t event) |
| 134 | +{ |
| 135 | + switch (event) |
| 136 | + { |
| 137 | + case I2C_CLIENT_TRANSFER_EVENT_ADDR_MATCH: |
| 138 | + |
| 139 | + if (I2C1_TransferDirGet() == I2C_CLIENT_TRANSFER_DIR_WRITE) |
| 140 | + { |
| 141 | + if (I2C1_ReadAddr() == CLIENT_7_BIT_ADDR) |
| 142 | + { |
| 143 | + currentClient = CLIENT_7_BIT_ADDR; |
| 144 | + |
| 145 | + } |
| 146 | + } |
| 147 | + break; |
| 148 | + |
| 149 | + case I2C_CLIENT_TRANSFER_EVENT_RX_READY: |
| 150 | + if(i2cRdDataIndex >= LENGTH) |
| 151 | + { |
| 152 | + i2cRdDataIndex = 0; |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + i2cRdData[i2cRdDataIndex++] = I2C1_ReadByte(); |
| 157 | + } |
| 158 | + break; |
| 159 | + case I2C_CLIENT_TRANSFER_EVENT_TX_READY: |
| 160 | + if(i2cWrDataIndex >= ONE_BYTELENGTH) |
| 161 | + { |
| 162 | + i2cWrDataIndex = 0; |
| 163 | + } |
| 164 | + else |
| 165 | + { |
| 166 | + I2C1_WriteByte(i2cWrData[i2cWrDataIndex++]); |
| 167 | + } |
| 168 | + |
| 169 | + break; |
| 170 | + |
| 171 | + case I2C_CLIENT_TRANSFER_EVENT_STOP_BIT_RECEIVED: |
| 172 | + i2cRdDataIndex = 0x00; |
| 173 | + break; |
| 174 | + |
| 175 | + default: |
| 176 | + break; |
| 177 | + } |
| 178 | + return true; |
| 179 | +} |
| 180 | + |
| 181 | +/******************************************************************************* |
| 182 | + * void TimerInterruptHandler(void) |
| 183 | + * |
| 184 | + * API to handle Timer interrupt, which generates interrupt for every 5 sec. |
| 185 | + * |
| 186 | + * @param void |
| 187 | + * @return void |
| 188 | + ******************************************************************************/ |
| 189 | +void TimerInterruptHandler(void) |
| 190 | +{ |
| 191 | + timerInterruptFlag= 1; |
| 192 | +} |
| 193 | + |
| 194 | +/** |
| 195 | + End of File |
| 196 | +*/ |
0 commit comments