dataXfer_demo.c - A simple demonstration of the data transfer protocol.ΒΆ

#include "pic24_all.h"
#include "dataXfer.h"

/// Indexes of all the variables to be transferred.
enum { I16_VAL_NDX, U16_INDEX_NDX, C_NDX, C1_NDX };

int main(void) {
  int16_t i16_val = -1;
  char c;
  uint16_t u16_index;

  configBasic(HELLO_MSG);
  initDataXfer();
 

All variables received by the PIC must be specified.

  SPECIFY_VAR(I16_VAL_NDX, i16_val, TRUE, "%hi", "Current sum");
  SPECIFY_VAR(U16_INDEX_NDX, u16_index, FALSE, "%hu", "Index of received data");
  SPECIFY_VAR(C_NDX, c, FALSE, "%c", "Character entered");
  SPECIFY_VAR(C1_NDX, c, FALSE, "%hu", "Value of character entered");

  while (1) {
    sendVar(I16_VAL_NDX); // Send i16_val to the PC
    outString("\nCurrent sum is ");
    outUint16(i16_val);
    outString(". Enter digit to sum (0-9): ");
 

Receive one character or variable.

    u16_index = receiveVar(&c);
 

For debug support, send the index and char received.

    sendVar(U16_INDEX_NDX);
    sendVar(C_NDX);
    sendVar(C1_NDX);
 

If it was a character, add it to the sum.

    if (u16_index == CHAR_RECEIVED_INDEX) {
      outChar(c);
      outString("  (0x");
      outUint8Decimal(c);
      outString(") ");
      if ( (c >= '0') && (c <= '9') )

A plain char received, so turn it into a number. Otherwise, a new total was sent.

        i16_val += c - '0';
    } else {
      ASSERT(u16_index == I16_VAL_NDX);
      outString("\nNew total received!\n");
    }
  }
}