PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dataXfer_demo.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 
30 #include "pic24_all.h"
31 #include "dataXfer.h"
32 
33 /** \file
34  * Tests the PIC data transfor protocol.
35  */
36 
37 /// Indexes of all the variables to be transferred.
38 enum { I16_VAL_NDX, U16_INDEX_NDX, C_NDX, C1_NDX };
39 
40 int main(void) {
41  int16_t i16_val = -1;
42  char c;
43  uint16_t u16_index;
44 
45  configBasic(HELLO_MSG);
46  initDataXfer();
47 
48  // All variables received by the PIC must be specified.
49  SPECIFY_VAR(I16_VAL_NDX, i16_val, TRUE, "%hi", "Current sum");
50  SPECIFY_VAR(U16_INDEX_NDX, u16_index, FALSE, "%hu", "Index of received data");
51  SPECIFY_VAR(C_NDX, c, FALSE, "%c", "Character entered");
52  SPECIFY_VAR(C1_NDX, c, FALSE, "%hu", "Value of character entered");
53 
54  while (1) {
55  sendVar(I16_VAL_NDX); // Send i16_val to the PC
56  outString("\nCurrent sum is ");
57  outUint16(i16_val);
58  outString(". Enter digit to sum (0-9): ");
59 
60  // Receive one character or variable.
61  u16_index = receiveVar(&c);
62 
63  // For debug support, send the index and char received.
64  sendVar(U16_INDEX_NDX);
65  sendVar(C_NDX);
66  sendVar(C1_NDX);
67 
68  // If it was a character, add it to the sum.
69  if (u16_index == CHAR_RECEIVED_INDEX) {
70  outChar(c);
71  outString(" (0x");
72  outUint8Decimal(c);
73  outString(") ");
74  if ( (c >= '0') && (c <= '9') )
75  i16_val += c - '0'; // A plain char received, so turn it into
76  // a number
77  // Otherwise, a new total was sent.
78  } else {
79  ASSERT(u16_index == I16_VAL_NDX);
80  outString("\nNew total received!\n");
81  }
82  }
83 }