22 * File: application.c
33 * Author: I51005
44 *
5- * Created on August 3, 2024, 2:38 PM
5+ * Created on June 18, 2025, 8:43 PM
66 */
77
8+
89#include "mcc_generated_files/system/system.h"
910#include "mcc_generated_files/adc/adc.h"
1011#include "string.h"
2223 * @param void
2324 * @return void
2425 ******************************************************************************/
25- void Application (void )
26- {
27- //To read the sensor data periodically for every 5 sec
28- if (timerInterruptFlag == SET )
29- {
30- #ifdef DEBUG
31- EUSART1_SendString ("Timer WakeUP\n" ); // only for debug
32- #endif
33- Sensor1_DataProcessing ();
34- timerInterruptFlag = 0 ;
35- }
36- else if (SSP1STATbits .I2C_START )
37- {
38- #ifdef DEBUG
39- EUSART1_SendString (" I2C WakeUP\n" ); //only for debug
40- #endif
41- if (i2cRdDataIndex >= 2 )
42- {
43- CLOCK_STRETCH_ENABLE ();
44- i2cRxDataLength = i2cRdData [ONE ];
45- i2cRxSensorRegisterAddress = i2cRdData [ZERO ];
46- SensorDataRegisterRead (i2cRxSensorRegisterAddress ,i2cRxDataLength );
47- CLOCK_STRETCH_DISABLE ();
48- DELAY_FUNCTION ();
49- i2cRdDataIndex = 0 ;
50- }
51- }
52- else
53- {
54- // SLEEP();
26+ void Application (void ) {
27+ //To read the sensor data periodically for every 5 sec
28+ if (timerInterruptFlag == SET ) {
29+ #ifdef DEBUG
30+ printf ("Timer WakeUP\n" ); // only for debug
31+ __delay_ms (10 );
32+ #endif
33+ Sensor1_DataProcessing ();
34+ timerInterruptFlag = 0 ;
35+ } else if (SSP1STATbits .I2C_START ) {
36+ #ifdef DEBUG
37+ printf (" I2C WakeUP\n" ); //only for debug
38+ __delay_ms (10 );
39+ #endif
40+ if (i2cRdDataIndex >= 2 ) {
41+ printf (" I2C Data\n" ); //only for debug
42+ __delay_ms (10 );
43+ CLOCK_STRETCH_ENABLE ();
44+ i2cRxDataLength = i2cRdData [ONE ];
45+ i2cRxSensorRegisterAddress = i2cRdData [ZERO ];
46+ SensorDataRegisterRead ((uint8_t * ) i2cRxSensorRegisterAddress , i2cRxDataLength );
47+ CLOCK_STRETCH_DISABLE ();
48+ DELAY_FUNCTION ();
49+ i2cRdDataIndex = 0 ;
50+ }
51+ } else {
52+ SLEEP ();
5553 }
5654}
5755
@@ -63,10 +61,8 @@ void Application(void)
6361 * @param *str - to send a sequence of strings on terminal window
6462 * @return void
6563 ******************************************************************************/
66- void EUSART1_SendString (const char * str )
67- {
68- for (uint8_t index = 0 ; index < strlen (str ); index ++ )
69- {
64+ void EUSART1_SendString (const char * str ) {
65+ for (uint8_t index = 0 ; index < strlen (str ); index ++ ) {
7066 EUSART1_Write (str [index ]);
7167 }
7268}
@@ -80,30 +76,31 @@ void EUSART1_SendString(const char *str)
8076 * @param void
8177 * @return void
8278 ******************************************************************************/
83- void Sensor1_DataProcessing (void )
84- {
79+ void Sensor1_DataProcessing (void ) {
8580 uint16_t adcBuffer = 0 ;
8681 uint16_t adcSamples = 0 ;
8782 uint16_t samplesAverage = 0 ;
8883 float dataBuffer = 0 ;
8984 uint8_t samplesCount ;
90-
91- for (samplesCount = 0 ; samplesCount < ADC_SAMPLES ; samplesCount ++ )
92- {
93- adcBuffer = ADC_GetConversion ( channel_ANC4 );
85+
86+ for (samplesCount = 0 ; samplesCount < ADC_SAMPLES ; samplesCount ++ ) {
87+ adcBuffer = ADC_ChannelSelectAndConvert ( ADC_CHANNEL_ANC4 );
88+
9489 adcSamples += adcBuffer ;
9590 }
96-
97- if (samplesCount >= ADC_SAMPLES )
98- {
99- samplesAverage = (adcSamples >> RIGHTSHIFT_COUNT );
100- dataBuffer = (samplesAverage * VDD )/ ADC_MAX_VALUE ;
101- phValue = (uint8_t )(CONSTANT * dataBuffer );
102- sensor_Data_Register [0 ] = phValue ;
91+
92+ if (samplesCount >= ADC_SAMPLES ) {
93+ samplesAverage = (adcSamples >> RIGHTSHIFT_COUNT );
94+ dataBuffer = (samplesAverage * VDD ) / ADC_MAX_VALUE ;
95+ phValue = (uint8_t ) (CONSTANT * dataBuffer );
96+ sensor_Data_Register [0 ] = phValue ;
97+ printf ("ph value: %d" , phValue );
98+ __delay_ms (10 );
10399 adcSamples = 0 ;
104- samplesCount = 0 ;
100+ samplesCount = 0 ;
105101 adcBuffer = 0 ;
106- }
102+
103+ }
107104}
108105
109106/*******************************************************************************
@@ -116,16 +113,14 @@ void Sensor1_DataProcessing(void)
116113 * host device
117114 * @return void
118115 ******************************************************************************/
119- void SensorDataRegisterRead (const uint8_t * sensorRevdData , uint8_t dataLength )
120- {
121- uint8_t index = 0 ;
122-
116+ void SensorDataRegisterRead (uint8_t * sensorRevdData , uint8_t dataLength ) {
117+ uint8_t index = 0 ;
118+
123119 //Clears the i2cWrData array
124- memset ((uint8_t * )i2cWrData , 0 , sizeof (i2cWrData ));
125-
126- for (index = 0 ;index < dataLength ;index ++ )
127- {
128- i2cWrData [index ] = * (sensorRevdData + index );
120+ memset ((uint8_t * ) i2cWrData , 0 , sizeof (i2cWrData ));
121+
122+ for (index = 0 ; index < dataLength ; index ++ ) {
123+ i2cWrData [index ] = * (sensorRevdData + index );
129124 }
130125}
131126
@@ -137,51 +132,38 @@ void SensorDataRegisterRead(const uint8_t *sensorRevdData, uint8_t dataLength)
137132 * @param void
138133 * @return void
139134 ******************************************************************************/
140- bool I2C_ClientInterruptHandler (i2c_client_transfer_event_t event )
141- {
142- switch (event )
143- {
144- case I2C_CLIENT_TRANSFER_EVENT_ADDR_MATCH :
145-
146- if (I2C1_TransferDirGet () == I2C_CLIENT_TRANSFER_DIR_WRITE )
147- {
148- if (I2C1_ReadAddr () == CLIENT_7_BIT_ADDR )
149- {
150- currentClient = CLIENT_7_BIT_ADDR ;
151-
152- }
153- }
154- break ;
135+ bool I2C_ClientInterruptHandler (i2c_client_transfer_event_t event ) {
136+ switch (event ) {
137+ case I2C_CLIENT_TRANSFER_EVENT_ADDR_MATCH :
155138
156- case I2C_CLIENT_TRANSFER_EVENT_RX_READY :
157- if (i2cRdDataIndex >= LENGTH )
158- {
159- i2cRdDataIndex = 0 ;
139+ if (I2C1_TransferDirGet () == I2C_CLIENT_TRANSFER_DIR_WRITE ) {
140+ if (I2C1_ReadAddr () == CLIENT_7_BIT_ADDR ) {
141+ currentClient = CLIENT_7_BIT_ADDR ;
142+
143+ }
160144 }
161- else
162- {
145+ break ;
146+
147+ case I2C_CLIENT_TRANSFER_EVENT_RX_READY :
148+ if (i2cRdDataIndex >= LENGTH ) {
149+ i2cRdDataIndex = 0 ;
150+ } else {
163151 i2cRdData [i2cRdDataIndex ++ ] = I2C1_ReadByte ();
164- }
165- break ;
166- case I2C_CLIENT_TRANSFER_EVENT_TX_READY :
167- if (i2cWrDataIndex >= ONE_BYTELENGTH )
168- {
169- i2cWrDataIndex = 0 ;
170152 }
171- else
172- {
173- I2C1_WriteByte ( i2cWrData [ i2cWrDataIndex ++ ]);
174- printf ( "%d" , i2cWrData [ i2cWrDataIndex ]) ;
153+ break ;
154+ case I2C_CLIENT_TRANSFER_EVENT_TX_READY :
155+ if ( i2cWrDataIndex >= ONE_BYTELENGTH ) {
156+ i2cWrDataIndex = 0 ;
175157 }
176-
177- break ;
178-
179- case I2C_CLIENT_TRANSFER_EVENT_STOP_BIT_RECEIVED :
180- i2cRdDataIndex = 0x00 ;
181- break ;
182-
183- default :
184- break ;
158+ if ( currentClient == CLIENT_7_BIT_ADDR ) {
159+ I2C1_Client . WriteByte ( i2cWrData [ i2cWrDataIndex ++ ]) ;
160+ }
161+ break ;
162+ case I2C_CLIENT_TRANSFER_EVENT_STOP_BIT_RECEIVED :
163+
164+ break ;
165+ default :
166+ break ;
185167 }
186168 return true;
187169}
@@ -194,12 +176,10 @@ bool I2C_ClientInterruptHandler(i2c_client_transfer_event_t event)
194176 * @param void
195177 * @return void
196178 ******************************************************************************/
197- void TimerInterruptHandler (void )
198- {
199- timerInterruptFlag = 1 ;
200- }
179+ void TimerInterruptHandler (void ) {
180+ timerInterruptFlag = 1 ;
181+ }
201182
202183/**
203184 End of File
204- */
205-
185+ */
0 commit comments