PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
i2c_master_reverse_string.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 #include "pic24_all.h"
30 
31 
32 /** \file
33 I2C Example: Demonstrates a PIC24 CPU
34 acting as an I2C slave. The PIC24 slave responds
35 to both write and read transactions.
36 */
37 
38 
39 #define SLAVE_I2C_ADDR 0x60
40 
41 int16_t getStringLength(char* psz_1) {
42  uint16_t u16_len;
43  u16_len = 0;
44  while (*psz_1) {
45  psz_1++;
46  u16_len++;
47  }
48  u16_len++; //contains length of string including null
49  return u16_len;
50 }
51 
52 #define BUFSIZE 64
53 char sz_1[BUFSIZE];
54 
55 int main (void) {
56  uint16_t u16_len;
57  configBasic(HELLO_MSG);
58  configI2C1(400); //configure I2C for 400 KHz
59  while (1) {
60  inStringEcho(sz_1,BUFSIZE); //get a string from the console
61  if (*sz_1 == 0) continue; //don't send empty string
62  u16_len = getStringLength(sz_1); //determine string length
63  writeNI2C1(SLAVE_I2C_ADDR,(uint8_t *)sz_1,u16_len); //send the string
64  readNI2C1(SLAVE_I2C_ADDR, (uint8_t *) sz_1,u16_len) ; //read the reversed string
65  outString(sz_1);
66  outString("\n");
67  }
68 }
69