1+ /*
2+ * File: application.c
3+ * Author: I51005
4+ *
5+ * Created on June 19, 2025, 4:34 PM
6+ */
7+
8+
19#include "mcc_generated_files/system/system.h"
210#include "mcc_generated_files/adc/adc.h"
311#include "application.h"
412#include "string.h"
513#include "stdlib.h"
614#include "stdio.h"
715
8-
916/*******************************************************************************
1017 * void Application(void)
1118 *
1623 * @param void
1724 * @return void
1825 ******************************************************************************/
19- void Application (void )
20- {
21- //To read the sensor data periodically for every 5 sec
22- if (timerInterruptFlag == SET )
23- {
26+ void Application (void ) {
27+ //To read the sensor data periodically for every 5 sec
28+ if (timerInterruptFlag == SET ) {
2429#ifdef DEBUG
25- printf ("Timer WakeUP\n" ); // only for debug
30+ EUSART1_SendString ("Timer WakeUP\n" ); // only for debug
2631#endif
2732 Sensor2_DataProcessing (); //Read Analog Sensor 2 and calibration
2833 Sensor3_DataProcessing (); //Read Analog Sensor 3 and calibration
2934 timerInterruptFlag = CLEAR ;
30- }
31- else if (SSP1STATbits .I2C_START )
32- {
35+ } else if (SSP1STATbits .I2C_START ) {
3336#ifdef DEBUG
34- printf (" I2C WakeUP\n" ); //only for debug
37+ EUSART1_SendString (" I2C WakeUP\n" ); //only for debug
3538#endif
36- if (i2cRdDataIndex >= LENGTH )
37- {
38- CLOCK_STRETCH_ENABLE ();
39+ if (i2cRdDataIndex >= LENGTH ) {
40+ CLOCK_STRETCH_ENABLE ();
3941 i2cRxDataLength = i2cRdData [ONE ];
4042 i2cRxSensorRegisterAddress = i2cRdData [ZERO ];
41- SensorDataRegisterRead (i2cRxSensorRegisterAddress ,i2cRxDataLength );
42- i2cWrDataIndex = 0 ;
43- CLOCK_STRETCH_DISABLE ();
43+ SensorDataRegisterRead ((uint8_t * ) i2cRxSensorRegisterAddress , i2cRxDataLength );
44+ CLOCK_STRETCH_DISABLE ();
4445 i2cRdDataIndex = ZERO ;
4546 DELAY_FUNCTION ();
4647 }
47- }
48- else
49- {
48+ } else {
5049 SLEEP ();
5150 }
52-
51+
5352}
5453
5554/*******************************************************************************
@@ -60,10 +59,8 @@ void Application(void)
6059 * @param *str - to send a sequence of strings on terminal window
6160 * @return void
6261 ******************************************************************************/
63- void EUSART1_SendString (const char * str )
64- {
65- for (uint8_t index = 0 ; index < strlen (str ); index ++ )
66- {
62+ void EUSART1_SendString (const char * str ) {
63+ for (uint8_t index = 0 ; index < strlen (str ); index ++ ) {
6764 EUSART1_Write (str [index ]);
6865 }
6966}
@@ -78,16 +75,14 @@ void EUSART1_SendString(const char *str)
7875 * host device
7976 * @return void
8077 ******************************************************************************/
81- void SensorDataRegisterRead (uint8_t * sensorRevdData , uint8_t dataLength )
82- {
78+ void SensorDataRegisterRead (uint8_t * sensorRevdData , uint8_t dataLength ) {
8379 uint8_t index = 0 ;
84-
80+
8581 //Clears the i2cWrData array
86- memset ((uint8_t * )i2cWrData , 0 , sizeof (i2cWrData ));
87-
88- for ( index = 0 ;index < dataLength ;index ++ )
89- {
90- i2cWrData [index ] = * (sensorRevdData + index );
82+ memset ((uint8_t * ) i2cWrData , 0 , sizeof (i2cWrData ));
83+
84+ for (index = 0 ; index < dataLength ; index ++ ) {
85+ i2cWrData [index ] = * (sensorRevdData + index );
9186 }
9287}
9388
@@ -101,19 +96,17 @@ void SensorDataRegisterRead(uint8_t *sensorRevdData, uint8_t dataLength)
10196 * @param void
10297 * @return void
10398 ******************************************************************************/
104- void Sensor2_DataProcessing (void )
105- {
99+ void Sensor2_DataProcessing (void ) {
106100 uint16_t adcBuffer = 0 ;
107101 float dataBuffer = 0 ;
108102 float buffer = 0 ;
109-
110- adcBuffer = ADC_GetConversion (channel_ANB7 );
111- dataBuffer = (((float )adcBuffer /ADC_MAX_VALUE )* VDD );
112- buffer = ((dataBuffer / VDD ) * TEMP_CONSTANT );
113- temp_Value = (uint8_t )(TEMP_CONSTANT1 + buffer );
114- sensor_Data_Register [ZERO ]= temp_Value ;
115-
116103
104+ adcBuffer = ADC_ChannelSelectAndConvert (ADC_CHANNEL_ANB7 );
105+ dataBuffer = (((float ) adcBuffer / ADC_MAX_VALUE ) * VDD );
106+ buffer = ((dataBuffer / VDD ) * TEMP_CONSTANT );
107+ temp_Value = (uint8_t ) (TEMP_CONSTANT1 + buffer );
108+ sensor_Data_Register [ZERO ] = temp_Value ;
109+ printf ("Temperature value: %d" , temp_Value );
117110}
118111
119112/*******************************************************************************
@@ -126,18 +119,17 @@ void Sensor2_DataProcessing(void)
126119 * @param void
127120 * @return void
128121 ******************************************************************************/
129- void Sensor3_DataProcessing (void )
130- {
122+ void Sensor3_DataProcessing (void ) {
131123 uint16_t adcBuffer = 0 ;
132124 float dataBuffer = 0 ;
133- float buffer = 0 ;
134-
135- adcBuffer = ADC_GetConversion ( channel_ANC4 );
125+ float buffer = 0 ;
126+
127+ adcBuffer = ADC_ChannelSelectAndConvert ( ADC_CHANNEL_ANC4 );
136128 dataBuffer = ADC_MAX_VALUE - adcBuffer ;
137- buffer = (dataBuffer / (ADC_MAX_VALUE - 1 ));
138- soilSensor_Value = (uint8_t )(buffer * 100 );
129+ buffer = (dataBuffer / (ADC_MAX_VALUE - 1 ));
130+ soilSensor_Value = (uint8_t ) (buffer * 100 );
139131 sensor_Data_Register [ONE ] = soilSensor_Value ;
140-
132+ printf ( "Soil Sensor value: %d" , soilSensor_Value );
141133}
142134
143135/*******************************************************************************
@@ -148,10 +140,9 @@ void Sensor3_DataProcessing(void)
148140 * @param void
149141 * @return void
150142 ******************************************************************************/
151- void TimerInterruptHandler (void )
152- {
143+ void TimerInterruptHandler (void ) {
153144 timerInterruptFlag = SET ;
154- }
145+ }
155146
156147/*******************************************************************************
157148 * bool I2C_ClientInterruptHandler(i2c_client_transfer_event_t event)
@@ -161,45 +152,50 @@ void TimerInterruptHandler(void)
161152 * @param void
162153 * @return void
163154 ******************************************************************************/
164- bool I2C_ClientInterruptHandler (i2c_client_transfer_event_t event )
165- {
166- switch (event )
167- {
168- case I2C_CLIENT_TRANSFER_EVENT_ADDR_MATCH :
169-
170- if (I2C1_TransferDirGet () == I2C_CLIENT_TRANSFER_DIR_WRITE )
171- {
172- if (I2C1_ReadAddr () == CLIENT2_7_BIT_ADDR )
173- {
174- currentClient = CLIENT2_7_BIT_ADDR ;
155+ bool I2C_ClientInterruptHandler (i2c_client_transfer_event_t event ) {
156+
157+ switch (event ) {
158+ case I2C_CLIENT_TRANSFER_EVENT_ADDR_MATCH :
159+
160+ if (I2C1_TransferDirGet () == I2C_CLIENT_TRANSFER_DIR_WRITE ) {
161+ if (I2C1_ReadAddr () == CLIENT2_7_BIT_ADDR ) {
162+ currentClient = CLIENT2_7_BIT_ADDR ;
163+ }
175164 }
176- }
177- break ;
165+ break ;
178166
179- case I2C_CLIENT_TRANSFER_EVENT_RX_READY :
180- if (currentClient == CLIENT2_7_BIT_ADDR )
181- {
167+ case I2C_CLIENT_TRANSFER_EVENT_RX_READY :
168+ if (i2cRdDataIndex >= LENGTH ) {
169+ i2cRdDataIndex = 0 ;
170+ }
171+ if (currentClient == CLIENT2_7_BIT_ADDR ) {
182172 i2cRdData [i2cRdDataIndex ++ ] = I2C1_ReadByte ();
183- }
184- break ;
185- case I2C_CLIENT_TRANSFER_EVENT_TX_READY :
186- I2C1_WriteByte (i2cWrData [index ++ ]);
187- if (index > i2cRxDataLength )
188- {
189- index = 0 ;
190- }
191- break ;
192-
193- case I2C_CLIENT_TRANSFER_EVENT_STOP_BIT_RECEIVED :
194- //i2cRdDataIndex = 0x00;
195- break ;
196-
197- default :
198- break ;
173+
174+ }
175+ break ;
176+ case I2C_CLIENT_TRANSFER_EVENT_TX_READY :
177+ if (currentClient == CLIENT2_7_BIT_ADDR ) {
178+ if (i2cWrDataIndex < i2cRxDataLength ) {
179+ I2C1_WriteByte (i2cWrData [i2cWrDataIndex ++ ]);
180+ }
181+ }
182+
183+ if (i2cWrDataIndex >= i2cRxDataLength ) {
184+ i2cWrDataIndex = 0 ;
185+ i2cRxDataLength = 0 ;
186+ }
187+ break ;
188+
189+ case I2C_CLIENT_TRANSFER_EVENT_STOP_BIT_RECEIVED :
190+
191+ break ;
192+
193+ default :
194+ break ;
199195 }
200196 return true;
201197}
202198
203199/**
204200 End of File
205- */
201+ */
0 commit comments