===== Description =====
Project showing how to use the UARTs of a SBC66 board. This project does NOT use the netcruzer nz_serUart.h and nz_serUart.c UART drivers. It uses the XC16 functions. See the XC16 "PIC24F MCU Peripheral Library" (included with XC16 compiler) for details.
Every 500ms the System LED is blinked, and a message sent on UART2.
Additionally this demo also implements debugging via the USB port and the Netcruzer USB Terminal App. During the program various debug messages are written, which will be displayed on the "Netcruzer USB Terminal" (running on a PC connected to the target board's USB port). This demo also monitors the USB port for Debug messages send to us.
If "hi" is received, it replies with "Hello"
Use the "Netcruzer USB Terminal" App to send and receive USB Debug messages. It can be downloaded here: https://netcruzer.com/usbterminal/
===== Required Hardware =====
The project requires a SBC66 Netcruzer board (https://modtronix.com/product/sbc66ec) with an USB Port (not USB Host). To access the SBC66 board's port pins, a prototype board like the PT02TC, PT66EI or PT66ECI can be used. For available boards, see https://modtronix.com/product/sbc66ec. Or a development board, like the DB66DEV1 (https://netcruzer.com/db66dev1.html) can be used.
===== Building Project =====
This project is located in the "src/demos/template/uart_xc16_demo1" folder of the Netcruzer Download. To compile for Netcruzer Board, open this project in MPLAB X, and select the "Project Configuration" for desired board. For example "SBC66ZL_R1" for the SBC66ZL Revision 1 board. For details click here
===== Programming Board =====
After compiling (build), the board can be programmed via the USB Bootloader or a PIC Programmer. USB Programming is simplified when using the SBC board together with a Prototype Board.
===== File History =====
2014-05-03, David H. (DH):
#define THIS_IS_MAIN_FILE //Uniquely identifies this as the file with the main application entry function main()
#include "HardwareProfile.h"
#define USE_AND_OR //Required by XC16 to enable AND_OR mask setting
#include "uart.h"
#if !defined(DEBUG_CONF_MAIN)
#define DEBUG_CONF_MAIN DEBUG_CONF_DEFAULT //Default Debug Level, disabled if DEBUG_LEVEL_ALLOFF defined, else DEBUG_LEVEL_ERROR
#endif
#define MY_DEBUG_LEVEL DEBUG_CONF_MAIN
WORD tmrFlashLed = 0;
BYTE Txdata[256];
BYTE putTxdata = 0;
BYTE getTxdata = 0;
BYTE Rxdata[256];
BYTE putRxdata = 0;
BYTE getRxdata = 0;
void Test_UART2_Interrupt(void);
void Test_UART2_Polling(void);
void __attribute__ ((interrupt,no_auto_psv)) _U2TXInterrupt(void)
{
U2TX_Clear_Intr_Status_Bit;
if(putTxdata != getTxdata) {
while(BusyUART2());
WriteUART2((unsigned int)Txdata[getTxdata++]);
}
}
void __attribute__ ((interrupt,no_auto_psv)) _U2RXInterrupt(void)
{
U2RX_Clear_Intr_Status_Bit;
if (U2STAbits.PERR || U2STAbits.FERR || U2STAbits.OERR) {
debugPutString("\nRxErr");
U2STAbits.PERR = U2STAbits.FERR = U2STAbits.OERR = 0;
}
while (DataRdyUART2())
Rxdata[putRxdata++] = ReadUART2();
}
int main(void) {
DEBUG_PUT_STR(DEBUG_LEVEL_INFO,
"\nThis is a test debug message from uart_xc16_demo1 demo");
DIR_SYSLED = OUTPUT_PIN;
iPPSOutput(PPS_OUT_36, OUT_FN_PPS_U2TX);
iPPSOutput(PPS_OUT_38, OUT_FN_PPS_U2RTS);
iPPSInput(IN_FN_PPS_U2RX, PPS_IN_37);
iPPSInput(IN_FN_PPS_U2CTS, PPS_IN_39);
CloseUART2();
ConfigIntUART2(UART_RX_INT_EN | UART_RX_INT_PR6 | UART_TX_INT_EN | UART_TX_INT_PR6);
OpenUART2(UART_EN, UART_TX_ENABLE, 103);
while(1)
{
while (putRxdata != getRxdata) {
debugPutString("\nReceived 0x");
debugPutHexByte(Rxdata[getRxdata++]);
}
LAT_SYSLED = !LAT_SYSLED;
if (LAT_SYSLED)
Test_UART2_Interrupt();
else
Test_UART2_Polling();
}
}
}
void Test_UART2_Polling(void) {
char message[] = "Hi";
DisableIntU2TX;
DisableIntU2RX;
while(BusyUART2());
putsUART2((unsigned int *)message);
while(BusyUART2());
while(DataRdyUART2()) {
debugPutString("\nReceived 0x");
debugPutHexByte(ReadUART2());
}
U2TX_Clear_Intr_Status_Bit;
U2RX_Clear_Intr_Status_Bit;
}
void Test_UART2_Interrupt(void) {
char message[] = "Hello";
BYTE i;
EnableIntU2TX;
EnableIntU2RX;
for (i=0; i<5; i++)
Txdata[putTxdata++] = message[i];
IFS1bits.U2TXIF = 1;
}
#if defined(HAS_NZ_DEBUGGING)
void debugService(void) {
debugPutString("\nHello");
}
}
}
}
#endif