Microstick Plus‎ > ‎

Code Examples

Microstick Plus Code Examples

The projects were tested on MPLAB® X IDE v3.51.
The source files were compile with MPLAB® XC16 Compiler v1.30.
The code examples were designed for PIC24HJ64GP502 microcontroller.

The MPLAB® X IDE can be downloaded from http://www.microchip.com/mplabx
The MPLAB® XC16 Compiler can be downloaded from http://www.microchip.com/mplabxc 
More information abut PIC24HJ128GP502 microcontroller can be found on site http://www.microchip.com/PIC24HJ64GP502
Sample program nr. 1:
Use of the LED-s and the button
During the button on the panel is pushed, the blue LED-s, in opposite case the red LED-s are lighting.

Sample program nr. 2:
Timer 1 periphery driven by external oscillator
Timer1 is initialized with interrupts enabled and the external 32.768 kHz oscillator is enabled. The timer generates interrupt in every second. The interrupt service routine increment the value of a global variable. The value of the global variable affects the LED-s located on the development board. The displayed LED design can be changed with the help of the button on the board.

Sample program nr. 3:
Read of the value of the potentiometer
The voltage generated by the potentiometer is read by the sample program with the help of analogue-to-digital converter periphery. The value of the ADC periphery affects the LED-s located on the development board. The displayed LED design can be changed with the help of the button on the board.

Sample program nr. 4:
Drive of the passive piezo buzzer
The passive piezo buzzer which is located on the panel is driven by the sample program with a 4 KHz and 50% filled PWM signal. The PWM signal is generated by an Output Compare module. The Output Compare module is driven by Timer 2. The piezo buzzer is driven while the button on the panel is pushed, in opposite case the Output Compare module is switched off.

Sample program nr. 5:
Use of the Rotary Encoder
The rotary encoder which is located on the development board is connected to the RB6 and RB7 pins of the microcontroller. The encoder generates 90o shifted square signs due to turning. The direction of the turning can be determined by the relative position of the two signals. ( Further details ) The program uses the Change Notification Interrupt. When the values of RB6 or RB7 pins change, the program reads the values of both pins and determines the direction of the turning. The interrupt service routine decrements or increments the value of a global variable based on the conditions. The main program shows the value of the global variable on the LED line. The displayed LED design can be changed with the help of the button on the board.

Sample program nr. 6:
Use of the UART1 serial port
The sample program communicates with the connected computer with the help of a MCP2200 USB to serial converter located on the panel. The program sends to computer a sample text after starting, and then receives characters from the serial port. The program assigns the lowest 4 bits of the ASCII value of received character to the LED line and sends back the character value to the serial port.


  • Settings of the serial port:
    Transmission rate: 9600 baud, 8 bit transmission without parity bit, using 1 stop bit.
  • For Windows users I recommend Docklight. The driver programs of the USB serial converter are downloadable from the product website of the company. (http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en546923)
  • For Linux users I recommend Minicom. The program can be found in the basic package of the main distributions. The following video ensures help for its use. The MCP2200 can be reached mostly through the /dev/ttyACM0 port. In Linux operation system, users who want to use the serial port, have to be member of the dialout security group. Operation system recognises automatically the converter from 2.4 kernel, there is no need to install any driver programs.
  • The default output will be the UART1 serial port, using the write.c file, so the printf(...)function will write on the UART1 serial port.

Sample program nr. 7:
Displaying reference voltage
The sample program measures the MCP1525 reference voltage generators output voltage and sends the measured voltage to the UART1 serial port in mV.

Sample program nr. 8:
Displaying temperature
The sample program measures the TC1047 analog temperature sensors output voltage and sends the measured temperature to the UART1 serial port in Celsius degree.

Sample program nr. 9:
Use of the capacitive touch
The sample program based on the Capacitive Voltage Divider (CVD) method. The main idea of the method is that the PCB touch sensor capacity and the A/D converter sampling/holding condensator capacity are in a comparable region. If we approach to the touch sensor, the capacity of the touch sensor changes and this change is measurable. ( Further details )
The program performs the first measurement in initialization part, and after compares the further measured values with the first measured value. The program write the measured voltages to the UART1 serial port. If the voltage of the touch changed with 6,5% compared to the initialization measure, then the program considers that the touch was pushed. The state of the capcitive button is displayed on the LED line.


  • the program measures the capacity of the touch with its own function. We can find solution for the use of the capacitive touch in the Microchip Application Libraries too.
  • Samples programs based on CVD method for PIC24H microcontroller can be downloaded from the following site.

Sample program nr. 10
Use of the built-in ECAN module
The sample program uses the rotary encoder, the button, and the capacitive touch pad to trigger different CAN messages.
The program initialize the DMA, the basic CAN features (acceptance masks, filters, etc.), and uses the same drivers for the inputs like the other examples above.
Pushing or releasing the button generates a CAN message on every edge with the CAN ID defined in CAN_ID_BUTTON (ecan_app.h). The method for the capacitive touch pad is the same, but with the CAN ID defined in  CAN_ID_TOUCH (ecan_app.h). These messages makes the LEDs switch on/off depending on the first data byte of the message.
Rotating the rotary encoder generates a message with the CAN ID defined in CAN_ID_ENCODER (ecan_app.h). Receiving a rotary message makes the leds rotate or showing the direction, depending on the value of LED_MODE (ecan_app.h) (SHIFT or DIR_IND).
The acceptance filters can be configured in ecan_app.h by defining ACCEPT_ROTARY, ACCEPT_BUTTON, and ACCEPT_TOUCH.
More information about operation with DMA on dsPIC33F and PIC24H Devices: AN1249