3.2 Foundation Services Generated Code

To generate this project using the Foundation Services library, follow these steps:

  1. Create a new MPLAB X IDE project for PIC18F47Q10.
  2. Open the MCC from the toolbar (information on how to install the MCC plug-in can be found on the Microchip website).
  3. Go to Project Resources > System > System Module and use the following configurations:
    • Oscillator Select: HFINTOSC
    • HF Internal Clock: 4 MHz
    • Clock Divider: 4
    • In the WDT Enable field, in the WWDT tab, ensure WDT Disable is selected
    • In the Programming tab, make sure Low-Voltage Programming Enable is checked
  4. In Device Resource, under Libraries, find Foundation Services, expand it and double-click UART to add it to the project.
  5. Click the + button at the bottom to add a new Foundation Services federated UART.
  6. Select EUSART2 from the UART drop down in Foundation Services.
  7. Open the Pin Manager > Grid View window, select UQFN40 in the MCU package field and select pin RD0 EUSART TX.
    Figure 3-2. Pin Mapping
  8. In Project Resources, under Peripherals, select EUSART2 and use the following configuration:
    • Redirect STDIO to USART: Checked
  9. Click Generate in the Project Resources tab.
  10. In the main.c file, which was generated by MCC:
    • Uncomment the line that enables global interrupts and the one that enables peripheral interrupts
    • Add the following defines:
      #define START_DATA_STREAM_PROTOCOL 0x03
      #define STOP_DATA_STREAM_PROTOCOL 0xFC
    • Add the following code in the main function (replacing the existing while(1) loop):
      uint8_t cnt = 0;
      while (1)
      {
          printf("%c%c%c%c", START_DATA_STREAM_PROTOCOL, cnt, cnt * 2, STOP_DATA_STREAM_PROTOCOL);
          cnt = cnt + 1;
          __delay_ms(50);
      }
  11. Use MPLAB X Data Visualizer as described in the appendix, How to configure MPLAB X Data Visualizer to Decode Data Stream Protocol.