2
2
* File: application.c
3
3
* Author: I51005
4
4
*
5
- * Created on July 28, 2024, 8:02 PM
5
+ * Created on June 18, 2025, 3:09 PM
6
6
*/
7
7
8
8
20
20
* Macros definitions
21
21
* **********************************************************
22
22
**/
23
- // #define _XTAL_FREQ (4000000)
23
+ #define _XTAL_FREQ (4000000)
24
24
#define DATA_LENGTH (2)
25
25
#define WRITE_DATA_LENGTH (2)
26
26
#define READ_ONE_BYTE (1)
@@ -116,7 +116,6 @@ void Application(void)
116
116
switch (applicationState )
117
117
{
118
118
case DISPLAY_INFORMATION :
119
- __delay_ms (100 );
120
119
SensorInformation ();
121
120
applicationState = DISPLAY_I2C_CLIENT_INFO ;
122
121
break ;
@@ -134,7 +133,7 @@ void Application(void)
134
133
}
135
134
else if (timerInterrupt_Flag == SET )
136
135
{
137
- EUSART1_SendString ("Input not received\n" );
136
+ printf ("Input not received\n" );
138
137
timerInterrupt_Flag = 0 ;
139
138
applicationState = DISPLAY_I2C_CLIENT_INFO ;
140
139
}
@@ -175,7 +174,7 @@ void Application(void)
175
174
}
176
175
else if (timerInterrupt_Flag == SET )
177
176
{
178
- EUSART1_SendString ("Input not received\n" );
177
+ printf ("Input not received\n" );
179
178
timerInterrupt_Flag = 0 ;
180
179
applicationState = DISPLAY_I2C_CLIENT_INFO ;
181
180
}
@@ -200,19 +199,19 @@ void Application(void)
200
199
}
201
200
else if (timerInterrupt_Flag == SET )
202
201
{
203
- EUSART1_SendString ("Input not received\n" );
202
+ printf ("Input not received\n" );
204
203
timerInterrupt_Flag = 0 ;
205
204
applicationState = DISPLAY_I2C_CLIENT_INFO ;
206
205
}
207
206
break ;
208
207
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 );
210
209
for (uint8_t index = 0 ;index < dataLength ;index ++ )
211
210
{
212
211
* clientRdDataBuffer = sensorDataRdBuffer [index ];
213
212
transmitBuffer = IntegerToAsciConver (* clientRdDataBuffer ,string , baseValue );
214
213
EUSART1_SendString ((char * )(transmitBuffer ));
215
- EUSART1_SendString ("\t" );
214
+ printf ("\t" );
216
215
}
217
216
memset (sensorDataRdBuffer , 0 , sizeof (sensorDataRdBuffer ));
218
217
inputCmdBuffer = 0 ;
@@ -240,11 +239,6 @@ void EUSART1_SendString(char *str)
240
239
{
241
240
for (uint8_t index = 0 ; index < strlen (str ); index ++ )
242
241
{
243
- while (0 == PIR1bits .TX1IF )
244
- {
245
-
246
- }
247
-
248
242
EUSART1_Write (str [index ]);
249
243
}
250
244
}
@@ -265,21 +259,21 @@ uint8_t DecodeUartCmdForI2C_Client(char *command)
265
259
uint8_t cmdFlag = 0 ;
266
260
if (strcmp (command ,CLIENT1 ) == 0 )
267
261
{
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" );
270
264
Timer0_Start ();
271
265
cmdFlag = 1 ;
272
266
}
273
267
else if (strcmp (command ,CLIENT2 )== 0 )
274
268
{
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" );
277
271
Timer0_Start ();
278
272
cmdFlag = 2 ;
279
273
}
280
274
else
281
275
{
282
- EUSART1_SendString ("Error! Incorrect CLIENT Address\n" );
276
+ printf ("Error! Incorrect CLIENT Address\n" );
283
277
cmdFlag = 0 ;
284
278
}
285
279
return (cmdFlag );
@@ -298,7 +292,7 @@ uint8_t Eusart1_ReadInput(void)
298
292
{
299
293
uint8_t readFlag = 0 ;
300
294
uint8_t inputStr ;
301
- if (eusart1RxCount != 0 )
295
+ if (EUSART1_IsRxReady () )
302
296
{
303
297
Timer0_Stop ();
304
298
inputStr = EUSART1_Read ();
@@ -333,9 +327,10 @@ uint8_t Eusart1_ReadInput(void)
333
327
******************************************************************************/
334
328
void SensorInformation (void )
335
329
{
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" );
339
334
}
340
335
341
336
/*******************************************************************************
@@ -351,7 +346,7 @@ void SensorInformation(void)
351
346
******************************************************************************/
352
347
void DisplayI2C_ClientAddrOnUART (void )
353
348
{
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" );
355
350
}
356
351
357
352
/*******************************************************************************
@@ -371,15 +366,15 @@ uint8_t I2C_CLient1SensorCommand(char *command)
371
366
372
367
if (strcmp (command ,SENSOR_1 )== 0 )
373
368
{
374
- EUSART1_SendString ("pH is : " );
369
+ printf ("pH is : " );
375
370
clientAddress = CLIENT1_ADDRESS ;
376
371
readDataLength = READ_ONE_BYTE ;
377
372
dataRegisterAddress = (uint8_t )sensor1DataRegister ;
378
373
dataLengthFlag = 1 ;
379
374
}
380
375
else
381
376
{
382
- EUSART1_SendString ("Error! Incorrect Client 1 Command\n" );
377
+ printf ("Error! Incorrect Client 1 Command\n" );
383
378
dataLengthFlag = 0 ;
384
379
}
385
380
return (dataLengthFlag );
@@ -402,31 +397,32 @@ uint8_t I2C_CLient2SensorCommand(char *command)
402
397
403
398
if (strcmp (command ,SENSOR_2 )== 0 )
404
399
{
405
- EUSART1_SendString ("Temperature in \260C: " );
400
+ printf ("Temperature in \260C: " );
406
401
clientAddress = CLIENT2_ADDRESS ;
407
402
readDataLength = READ_ONE_BYTE ;
408
403
dataRegisterAddress = (uint8_t )sensor2DataRegister ;
409
404
dataLengthFlag = 1 ;
405
+
410
406
}
411
407
else if (strcmp (command ,SENSOR_3 )== 0 )
412
408
{
413
- EUSART1_SendString ("Soil Moisture in \045: " );
409
+ printf ("Soil Moisture in \045: " );
414
410
clientAddress = CLIENT2_ADDRESS ;
415
411
readDataLength = READ_ONE_BYTE ;
416
412
dataRegisterAddress = (uint8_t )sensor3DataRegister ;
417
413
dataLengthFlag = 1 ;
418
414
}
419
415
else if (strcmp (command ,SENSOR_2AND3 )== 0 )
420
416
{
421
- EUSART1_SendString ("Temperature in \260C and Soil Moisture in \045: " );
417
+ EUSART1_SendString (" Temperature in \260C and Soil Moisture in \045: " );
422
418
clientAddress = CLIENT2_ADDRESS ;
423
419
readDataLength = READ_TWO_BYTE ;
424
420
dataRegisterAddress = (uint8_t )sensor2And3DataRegister ;
425
421
dataLengthFlag = 2 ;
426
422
}
427
423
else
428
424
{
429
- EUSART1_SendString ("Error! Incorrect Client 2 command\n" );
425
+ printf ("Error! Incorrect Client 2 command\n" );
430
426
dataLengthFlag = 0 ;
431
427
}
432
428
@@ -529,3 +525,4 @@ void TimerInterruptHandler(void)
529
525
timerInterrupt_Flag = 1 ;
530
526
}
531
527
528
+
0 commit comments