Home | pfodApps/pfodDevices | WebStringTemplates | Java/J2EE | Unix | Torches | Superannuation | | About Us

Forward Logo (image)      

Android control of Non-Arduino micros using pfodApp
PIC Led Control Example

by Matthew Ford 28th July 2016 (originally posted 28th January 2016)
© Forward Computing and Control Pty. Ltd. NSW Australia
All rights reserved.

Example of Controlling a LED from your Android mobile
using PIC18F14K22 and pfodApp™
No Android programming required.

This is a detailed example of how code a PIC18F14K22 talk to an Adafruit BLE communication module and use pfodApp on your Android mobile to turn an led on and off. No Android programming required.

A following example will extend this one to send back data for logging and plotting. In both cases pfodDesignerV2 is used to generate most of the required code.

What you need

The example uses the following items (prices as at Aug 2016 and exclude shipping) :-

Software setup

  1. From http://www.microchip.com/mplab install the MPLAB IDE and the XC8 compiler. Be aware that the free XC8 compiler is a non-optimizing compiler. The optimizing version costs real money. MPLAB's free C compile is crippleware. By MPLAB's own estimates it generates Hex files that 60% bigger then necessary and up to 4x slower. However it is free and easy to use and the Code Configurator is very convenient. A suggested solution, if you run into problems, is to “different PIC chip, with a faster clock and larger memory”.

  2. From http://www.microchip.com/mplab/mplab-code-configurator install the MPLAB Code Configurator and the PIC10/PIC12/PIC16/PIC18 device library.

  3. Open the MPLAB X IDE and install the Code Configurator from Tools → Plugins.

The completed project files are available here, LedControl.X.zip If you use those files you can just use File → Open Project and navigate the where you unzipped them.
This example will start with a new empty project.

  1. Open MPLAB X IDE and select File → New Project... and choose Microchip Embedded and Standalone Project.

  2. Click Next and select your device. This example uses PIC18F14K22 from the Advanced 8bit MCU's (PIC18) Family.

  3. Click Next and leave the debug support as NONE.

  4. Click Next and select the “PICkit3 hardware tool.”

  5. Click Next and select the XC8 compiler

  6. Finally enter a project name e.g LedControl

Configuring the Clock, UART and LED drive pin. Creating main.c

Select Window → MPLAB Code Configurator → Open/Close to display the Code Configuration panel. It takes a few seconds to initialize the first time.

Set the Internal Oscillator as shown below

Next scroll down the Device Resources (on the left) to EUSART and click the arrow to open it and double click on EUSART to open its configuration. Set the UART configuration as shown below.

Note: The Redirect STDIO to USART is ticked so that putch() and getch() are connected to the UART. The baud rate here is 9600 to match the Adafruit BLE UART baud rate. Choose a baud rate to match your communication module.

Finally set the GPIO output pin that will drive the LED. The development board used here has already wired RC0 to LED 0. So in the bottom panel, Pin Manager Grid (MCC) click on the GPIO output Port C 0

Also change the
Package to PDIP20.

Finally click on Pin Module under Project resources (top left) to show the pin names used and their function. Rename the RC0 pin from IO_RCO to LED_RCO

Checking the Interrupt Settings and Generating the Configuration Files

Click on the Interrupt Module under Project resources (top left) to show the enabled Interrupts.

The EUSART TX and RCI should be enabled. Note: The reminder at the top to enable the Peripheral and Global Interrupts. This will be done next.

To generate the code for these configuration setting, click the
Generate button/tab next to the Project Resources tab. This will prompt you to save this configuration and then generate a number of supporting files under the sub-directory mcc_generated_files. It will also generate a main.c file

Enable Interrupts in main.c

In the MPLAB IDE with the

In order for the UART to work in interrupts mode you need to enable both the global interrupt and the peripheral interrupt. Do this by un-commenting the following two lines in main.c

    // Enable the Global Interrupts

    // Enable the Peripheral Interrupts

At this point you have set up the UART in interrupt mode linked to getch() and putch() and have set on pin as an output to drive the LED, but no code to actually do anything yet.

Adding the pfodParser files

Unzip the latest pfodParserC library to the same directory that main.c file was created in. This will add three (3) source files and headers:-
pfodMenu.c / .h as simple empty menu design generated by pfodDesignerV2
pfodParser.c / .h the pfodParser. This file usually never needs to be changed.
pfodParserStream.c / .h The .h file never needs to be changed. But you may need to edit the pfodParserStream.c file to match your micros UART setup.

To add these files to your project, click on the Projects tab (top left) and right click on the Source Files and choose Add Existing Item... Highlight the pfodParser... files and add them to the project.

Rebuild the project and check for errors. The most likely change required, if you are using MPLAB Code Configurator, is to fix the references to eusartTxBufferRemaining and eusartRxCount to match the UART you are using. Rebuild and check there are no more errors.

Testing the pfodParser I/O

All the pfodParser I/O goes through the pfodParserStream methods, via the pfodParser_printXX() methods, so it is a good idea to test they are working before trying anything else.

Take a copy of your main.c file and replace it with the contents of main_pfodParserStreamTest.c Program your PIC micro. I used a PICkit3 and a simple DV164130 development board.
Before connecting the PICkit 3 to a PC via USB, disconnect any target boards that may be attached to the PICkit 3. Similarly, when starting up or rebooting the host PC, ensure that the PICkit 3 is not connected to a target board.

Connect your PIC micro to a terminal program at 9600, I used TeraTerm. To make the hardware connection to your computer you can use an FTDI Serial TTL-232 USB Cable. I used an Arduino Mega I had available. loaded it with this sketch and wired as shown below.

This test program writes a long counter to the serial out and reads and echos from serial in. The output counter should not miss any numbers and all the input chars should be echoed, but perhaps interspersed with counter output.

Confirming the pfodParser / pfodMenu works

Once you have the pfodParserStream running, you can re-instate your original main.c and test the simple Empty Menu. To do this you need to include pfodMenu.h to the top of your main, call pfodParser_setup() in you main() and then each processing loop, call pfodParser_parse()

i.e. The complete main.c file is just

#include "mcc_generated_files/mcc.h"
#include "pfodMenu.h"

void main(void) {
    SYSTEM_Initialize(); // Initialize the device
    INTERRUPT_GlobalInterruptEnable(); // Enable the Global Interrupts
    INTERRUPT_PeripheralInterruptEnable(); // Enable the Peripheral Interrupts
    while (1) {
      // your other processing code goes here

Re-program your PIC and then when you enter
{.} into the terminal, which is what pfodApp sends to request the main menu, you should see the menu returned {,~Initial Empty Menu`0~V19}

Connecting to pfodApp on your Android mobile

Now that you are sure the PIC program is operating correctly, you can connect up a communication board to the PIC's serial pins and connect to it via pfodApp from your Android mobile. Here I have used an Adafruit Bluefruit LE UART Friend because is it small, low power and easy to hookup. But you can other boards such SparkFun Bluetooth Mate Silver for Bluetooth Classic or a inexpensive ESP8266 Wifi base board like Adafruit HUZZAH ESP8266 Breakout when loaded with UART to WiFi server bridge code from this project.

Connect Adafruit BLE UART as shown below

Note: You need to ground the Adafruit's CTS line and connect the Adafruit TX to PIC RX pin (RB5) and Adafruit RX to PIX TX pin (RB7).

Then start pfodApp and set up a BLE connection to the Adafruit BLE. See the pfodAppForAndroidGettingStarted for the details. (You can also connect a WiFi or Bluetooth V2 device with pfodApp.) When you connect you will see an “Initial Empty Menu” served by your PIC microprocessor.

Turning a Led On/Off from your Mobile

Previously, you configured one of the PIC pins as an output and named it LED_RC0. On the development board this output is connected to one of the Leds. To control this led, install pfodDesignerV2 on your mobile, start a new menu, choose Generate Code → Change Target and choose C code. This C code target modifies the pfodDesignerV2 menus to suit.

Then design a menu adding a menu item On/Off Setting or Pulse . Here is a detailed tutorial on using the pfodDesignerV2 to design this menu. With C code target set, you won't have the option in pfodDesignerV2 to set the output pin. That will be done in your main.c next.

Use pfodDesignerV2 to generate the pfodMenu.h / pfodMenu.c files (both written to the one output file pfodDesignerV2.txt). Here are sample pfodMenu.h and pfodMenu.c files to switch the Led on and off. Replace the existing pfodMenu.h and pfodMenu.c files in your project with the contents of these two new files and reprogramming your PIC will show the following menu when you connect. Note: disconnect the Adafruit's TX/RX lines when reprogramming so it does not get confused.

Connect with pfodApp again and you can click any where in the On/Off button to toggle it. When you disconnect and reconnect pfodApp, it displays the last setting sent to your PIC. Now you need to link this menu item to your LED pin.

Connecting the On/Off menu item to the PIC output pin

The files generated by pfodDesignerV2 are designed so that you only need to work with the methods and variables exposed in the pfodMenu.h header. Opening the pfodMenu.h header, the one you just generated from pfodDesignerV2.

/* ===== pfod Command for PIC ====
pfodApp msg {.} --> {,<bg n><b><+3>~PIC Led Control`0~V29|A`0~Led is ~~Off\On~}
 * File:   pfodMenu.h
/* Code generated by pfodDesignerV2 V2.0.2182
 * (c)2014-2016 Forward Computing and Control Pty. Ltd.
 * NSW Australia, www.forward.com.au
 * This generated code may be freely used for both private and commercial use

#ifndef PFODMENU_H
#define PFODMENU_H
#include <stdint.h>

#ifdef  __cplusplus
extern "C" {

  // global vars set by parser, access and update these in your main program
  extern uint8_t pfodParser_connected; // set true (1) when {.} parsed, set false (0) when {!} parsed.

  extern int cmd_A_var; // name the variable for 'Led is'  0=Off 1=On 

  extern volatile uint8_t pfodParser_sendDataFlag; // set this to true (1) to send data, it is reset to false (0) by pfodParser_sendData()
  extern volatile unsigned long plot_X_var; // X axis defaults to an incrementing counter unless your code assigns a value to plot_X_var before each pfodParser_sendData() call.

  void pfodParser_setup(void);   // call this just once 
  void pfodParser_parse(void);   // call this very processing loop 

  // print support
  int pfodParser_println(void); // skips print if not connected. Blocks if underlying TX buffer full. 
  int pfodParser_printCh(char c); // skips print if not connected. Blocks if underlying TX buffer full.
  int pfodParser_printStr(const char *str); // skips print if not connected. Blocks if underlying TX buffer full.
  int pfodParser_printLong(const long l); // skips print if not connected. Blocks if underlying TX buffer full.

  //  PFOD_PARSER_BUFFER_SIZE sets the max size msg the parser will store { vers : cmd ` args.. } e.g. {v7:A`1024}
  //  Longer msgs upto 255 can be received and will be parsed but only the first PFOD_PARSER_BUFFER_SIZE bytes will be stored in the parser

#ifdef  __cplusplus
#endif  /* PFODMENU_H */

The first line shows the menu string returned when the pfodApp sends {.} asking for the main menu. The other entries are well commented. The variable we are interested in here is

   int cmd_A_var; // name the variable for 'Led is'  0=Off 1=On

cmd_A_var is set by the pfodMenu.c code in response to the the command send by pfodApp when the user operates the On/Off menu item. Here is that section of the pfodMenu.c code

   } else if('A'==cmd) { // user moved slider -- 'Led is'
      // in the main Menu of PIC 
      // set output based on slider 0=Off 1=On 
      pfodParser_parseLong(pfodFirstArg,&pfodLongRtn); // parse first arg as a long
      cmd_A_var = (int)pfodLongRtn; // set variable
      pfodParser_sendMainMenuUpdate(); // always send back a pfod msg otherwise pfodApp will disconnect.

When the user operates the On/Off button, pfodApp sends the command {A`0} or {A`1}. A is the command associated with the On/Off button and the arguments 0,1 represent Off and On. The cmd_A_var is set with this argument value, when the command arrives. Finally pfodMenu.c sends back a menu update is sent back to the pfodApp to display.

In your main.c file you can use the current value of cmd_A_var to control the Led. MPLAB generates some output macros as part of the Code Configurator. LED_RC0_SetHigh() and LED_RC0_SetLow(). These are defined in pin_manager.h which is automatically and available include in main.c

So using these macros you can edit the main() method to

void main(void) {
    SYSTEM_Initialize(); // Initialize the device
    INTERRUPT_GlobalInterruptEnable(); // Enable the Global Interrupts
    INTERRUPT_PeripheralInterruptEnable(); // Enable the Peripheral Interrupts
    while (1) {
      if (cmd_A_var) {
      } else {

The completed main.c is here. Now when you have reprogrammed your PIC and connect using pfodApp, the menu On/Off button turns the Led On and Off. This complete MPLAB project, LedControl, using the PIC18F14K22 is here.

That's it finished. You can now design general purpose menus and sub-menus to control and display the settings of I/O pins. Explore the other menu options in pfodDesignerV2. In all cases the variable will be exposed in the generated pfodMenu.h header file.

The next part of this tutorial will cover sending data back to your mobile to be logged and plotted.

AndroidTM is a trademark of Google Inc, For use of the Arduino name see http://arduino.cc/en/Main/FAQ

pfodDevice™ and pfodApp™ are trade marks of Forward Computing and Control Pty. Ltd.

Forward home page link (image)

Contact Forward Computing and Control by
©Copyright 1996-2017 Forward Computing and Control Pty. Ltd. ACN 003 669 994