22 * File: application.c
33 * Author: I51005
44 *
5- * Created on July 28, 2024, 8:02 PM
5+ * Created on June 18, 2025, 3:09 PM
66 */
77
88
2020 * Macros definitions
2121 * **********************************************************
2222 **/
23- // #define _XTAL_FREQ (4000000)
23+ #define _XTAL_FREQ (4000000)
2424#define DATA_LENGTH (2)
2525#define WRITE_DATA_LENGTH (2)
2626#define READ_ONE_BYTE (1)
@@ -116,7 +116,6 @@ void Application(void)
116116 switch (applicationState )
117117 {
118118 case DISPLAY_INFORMATION :
119- __delay_ms (100 );
120119 SensorInformation ();
121120 applicationState = DISPLAY_I2C_CLIENT_INFO ;
122121 break ;
@@ -134,7 +133,7 @@ void Application(void)
134133 }
135134 else if (timerInterrupt_Flag == SET )
136135 {
137- EUSART1_SendString ("Input not received\n" );
136+ printf ("Input not received\n" );
138137 timerInterrupt_Flag = 0 ;
139138 applicationState = DISPLAY_I2C_CLIENT_INFO ;
140139 }
@@ -175,7 +174,7 @@ void Application(void)
175174 }
176175 else if (timerInterrupt_Flag == SET )
177176 {
178- EUSART1_SendString ("Input not received\n" );
177+ printf ("Input not received\n" );
179178 timerInterrupt_Flag = 0 ;
180179 applicationState = DISPLAY_I2C_CLIENT_INFO ;
181180 }
@@ -200,19 +199,19 @@ void Application(void)
200199 }
201200 else if (timerInterrupt_Flag == SET )
202201 {
203- EUSART1_SendString ("Input not received\n" );
202+ printf ("Input not received\n" );
204203 timerInterrupt_Flag = 0 ;
205204 applicationState = DISPLAY_I2C_CLIENT_INFO ;
206205 }
207206 break ;
208207 case SEND_I2C_SENSOR_DATA_TO_UART :
209- I2C_ClientWriteAndRead (clientAddress ,dataRegisterAddress ,WRITE_DATA_LENGTH ,readDataLength );
208+ I2C_ClientWriteAndRead (clientAddress ,( uint8_t * ) dataRegisterAddress ,WRITE_DATA_LENGTH ,readDataLength );
210209 for (uint8_t index = 0 ;index < dataLength ;index ++ )
211210 {
212211 * clientRdDataBuffer = sensorDataRdBuffer [index ];
213212 transmitBuffer = IntegerToAsciConver (* clientRdDataBuffer ,string , baseValue );
214213 EUSART1_SendString ((char * )(transmitBuffer ));
215- EUSART1_SendString ("\t" );
214+ printf ("\t" );
216215 }
217216 memset (sensorDataRdBuffer , 0 , sizeof (sensorDataRdBuffer ));
218217 inputCmdBuffer = 0 ;
@@ -240,11 +239,6 @@ void EUSART1_SendString(char *str)
240239{
241240 for (uint8_t index = 0 ; index < strlen (str ); index ++ )
242241 {
243- while (0 == PIR1bits .TX1IF )
244- {
245-
246- }
247-
248242 EUSART1_Write (str [index ]);
249243 }
250244}
@@ -265,21 +259,21 @@ uint8_t DecodeUartCmdForI2C_Client(char *command)
265259 uint8_t cmdFlag = 0 ;
266260 if (strcmp (command ,CLIENT1 ) == 0 )
267261 {
268- EUSART1_SendString ("Selected Client 1\n" );
269- EUSART1_SendString ("Enter '1' to measure pH\n" );
262+ printf ("Selected Client 1\n" );
263+ printf ("Enter '1' to measure pH\n" );
270264 Timer0_Start ();
271265 cmdFlag = 1 ;
272266 }
273267 else if (strcmp (command ,CLIENT2 )== 0 )
274268 {
275- EUSART1_SendString ("Selected Client 2\n" );
276- EUSART1_SendString ("Enter '2' to measure Temperature, '3' to measure Soil moisture, '4' to measure both sensors\n" );
269+ printf ("Selected Client 2\n" );
270+ printf ("Enter '2' to measure Temperature, '3' to measure Soil moisture, '4' to measure both sensors\n" );
277271 Timer0_Start ();
278272 cmdFlag = 2 ;
279273 }
280274 else
281275 {
282- EUSART1_SendString ("Error! Incorrect CLIENT Address\n" );
276+ printf ("Error! Incorrect CLIENT Address\n" );
283277 cmdFlag = 0 ;
284278 }
285279 return (cmdFlag );
@@ -298,7 +292,7 @@ uint8_t Eusart1_ReadInput(void)
298292{
299293 uint8_t readFlag = 0 ;
300294 uint8_t inputStr ;
301- if (eusart1RxCount != 0 )
295+ if (EUSART1_IsRxReady () )
302296 {
303297 Timer0_Stop ();
304298 inputStr = EUSART1_Read ();
@@ -333,9 +327,10 @@ uint8_t Eusart1_ReadInput(void)
333327 ******************************************************************************/
334328void SensorInformation (void )
335329{
336- EUSART1_SendString ("\n\nTwo I2C Clients are connected to I2C Host\n" );
337- EUSART1_SendString ("pH Sensor is connected to I2C Client1\n" );
338- EUSART1_SendString ("Temperature Sensor and Soil Moisture Sensor are connected to I2C Client2\n" );
330+ __delay_ms (10 );
331+ printf ("\n\nTwo I2C Clients are connected to I2C Host\n" );
332+ printf ("pH Sensor is connected to I2C Client1\n" );
333+ printf ("Temperature Sensor and Soil Moisture Sensor are connected to I2C Client2\n" );
339334}
340335
341336/*******************************************************************************
@@ -351,7 +346,7 @@ void SensorInformation(void)
351346 ******************************************************************************/
352347void DisplayI2C_ClientAddrOnUART (void )
353348{
354- EUSART1_SendString ("\nEnter '0x0A' address to connect I2C_Client1 or '0x0B' address to connect I2C_Client2\n" );
349+ printf ("\nEnter '0x0A' address to connect I2C_Client1 or '0x0B' address to connect I2C_Client2\n" );
355350}
356351
357352/*******************************************************************************
@@ -371,15 +366,15 @@ uint8_t I2C_CLient1SensorCommand(char *command)
371366
372367 if (strcmp (command ,SENSOR_1 )== 0 )
373368 {
374- EUSART1_SendString ("pH is : " );
369+ printf ("pH is : " );
375370 clientAddress = CLIENT1_ADDRESS ;
376371 readDataLength = READ_ONE_BYTE ;
377372 dataRegisterAddress = (uint8_t )sensor1DataRegister ;
378373 dataLengthFlag = 1 ;
379374 }
380375 else
381376 {
382- EUSART1_SendString ("Error! Incorrect Client 1 Command\n" );
377+ printf ("Error! Incorrect Client 1 Command\n" );
383378 dataLengthFlag = 0 ;
384379 }
385380 return (dataLengthFlag );
@@ -402,31 +397,32 @@ uint8_t I2C_CLient2SensorCommand(char *command)
402397
403398 if (strcmp (command ,SENSOR_2 )== 0 )
404399 {
405- EUSART1_SendString ("Temperature in \260C: " );
400+ printf ("Temperature in \260C: " );
406401 clientAddress = CLIENT2_ADDRESS ;
407402 readDataLength = READ_ONE_BYTE ;
408403 dataRegisterAddress = (uint8_t )sensor2DataRegister ;
409404 dataLengthFlag = 1 ;
405+
410406 }
411407 else if (strcmp (command ,SENSOR_3 )== 0 )
412408 {
413- EUSART1_SendString ("Soil Moisture in \045: " );
409+ printf ("Soil Moisture in \045: " );
414410 clientAddress = CLIENT2_ADDRESS ;
415411 readDataLength = READ_ONE_BYTE ;
416412 dataRegisterAddress = (uint8_t )sensor3DataRegister ;
417413 dataLengthFlag = 1 ;
418414 }
419415 else if (strcmp (command ,SENSOR_2AND3 )== 0 )
420416 {
421- EUSART1_SendString ("Temperature in \260C and Soil Moisture in \045: " );
417+ EUSART1_SendString (" Temperature in \260C and Soil Moisture in \045: " );
422418 clientAddress = CLIENT2_ADDRESS ;
423419 readDataLength = READ_TWO_BYTE ;
424420 dataRegisterAddress = (uint8_t )sensor2And3DataRegister ;
425421 dataLengthFlag = 2 ;
426422 }
427423 else
428424 {
429- EUSART1_SendString ("Error! Incorrect Client 2 command\n" );
425+ printf ("Error! Incorrect Client 2 command\n" );
430426 dataLengthFlag = 0 ;
431427 }
432428
@@ -529,3 +525,4 @@ void TimerInterruptHandler(void)
529525 timerInterrupt_Flag = 1 ;
530526}
531527
528+
0 commit comments