Project tutorial
Run PICmicro Instructions on Arduino

Run PICmicro Instructions on Arduino © Apache-2.0

A lightweight simulator to run programs compiled for PICmicro on Arduino.

  • 499 views
  • 0 comments
  • 0 respects

Components and supplies

Apps and online services

About this project

μSim is a lightweight PIC™ CPU and ALU simulator. This simulator supports the PICmicro mid-range instruction set and designed to work on both PC and Arduino platforms.

Compare with most of the other emulators, μSim does not provide all MCU features and peripherals. This simulator design as a minimalistic system, and based on the requirements, it can extend with additional peripherals and features.

Apart from the instruction decoder, this system simulates 1024 words of flash memory, 68 bytes of SRAM, two 8 bit GPIO ports, and register map similar to PIC16F84 MCU. Some of the common MCU peripherals and features such as interrupts, timers, and UART systems are not integrated into this simulator.

This article provides step-by-step instructions on how to setup μSim on PC and launch it on the Arduino Uno platform with a simple PIC program.

Clone and compile latest μSim source

The latest μSim source is available at GitHub. To get the source clone the above repository with the following command:

git clone https://github.com/dilshan/usim.git

After obtaining the copy, create bin and obj directory structure inside the usim directory as below:

.
├── bin
├── obj
│ ├── hexconv
│ └── usim

The above structure can create using the terminal with the following set of commands:

cd usim
mkdir bin
mkdir obj

cd obj
mkdir hexconv
mkdir usim

In Windows systems, MinGW is required to compile the source. The latest version of MinGW is available to download here.

After obtaining the code, issue the following commands to compile the μSim.

cd src
make clean
make

If the compilation is successful, the μSim PC version and hexconv utility may need to create at /bin directory.

Flash Arduino with μSim

In Arduino platforms, μSim uses an SD card to load the PIC program(s). To implement this setup, use a commonly available micro SD card module, which comes with level-shifter.

After performing the above steps launch the Arduino IDE and open "arduinousim" file. In the latest codebase, this source file is located at the "/src/arduinousim" directory.

From the Arduino IDE, select "Arduino Uno" as the board type and compile the source code. Following the compiling, upload it to the Arduino board.

Preparing a test program

To test the above setup, we need a test program which compiled into PIC MCUs. These test programs can build using any PIC mid-range series compatible compiler, which includes Microchip's XC8, mikroC Pro for PIC, SDCC, etc. In this article, we use Microchip's XC8 and MPLAB IDE to create the test program. This compiler and IDE are freely available to download at a microchip.com software download center.

After installing the MPLAB and XC8 follow the given steps to create the test program (or click here to download the MPLAB test project):

  • Open MPLAB IDE and select File > New Project...
  • Choose "Microchip Embedded" as a category. Select the "Standalone Project" from the projects list and press the Next button.
  • Select PIC16F84 or PIC16F84A as a device and press the Next button.
  • Select XC8 as the compile and continue the wizard.
  • Specify "ledtest" as the project name and select the appropriate directory to save the project.
  • Press Finish, to create the project.
  • Under the "Source Files" create main.c file and enter the source code listed in here.
  • Click Production > Build Main Project to build the test code.

After the above steps, locate the hex file generated by the compiler and convert it to bin file using hexconv utility.

hexconv ./ledtest.X.production.hex

The hexconv utility used in the above command is a part of the simulator and located at the bin directory of the usim project.

After obtaining the bin file, copy it to the root of the SD card and rename it as usimboot.bin. usimboot.bin is the file name that is used by the Arduino version of μSim to load the flash memory content.

Setting up the test circuit

To test the above PIC program wire the Arduino with the circuit layout given below.

As the final step, insert the SD card into the card module and press the RESET button on the Arduino board to launch the PIC program into the μSim. If everything is properly set, you need to get an LED pattern which is defined in the PIC program.

Conclusion

As mention at the beginning of this article, the μSim simulator is not a comprehensive PIC MCU implementation. This simulator is designed as a core component, and it must extend based on the actual project requirements. To achieve this μSim is written with minimum system dependencies and with less complexity. To avoid resource overflows/overruns the source code is organized with a few functions and data types. This article provides only the initial guidance to set up the μSim and its test environment.

The main objective of this project is to provide a framework to build virtual MCUs for small-scale embedded systems. More details of this project are available at the documentation at GitHub Wiki.

Code

μSim test applicationC/C++
Test application to compile with Microchip XC8 compiler.
#include <xc.h>
#include <stdint.h>

// This is not an accurate frequency. The exact value may depend on the 
// specifications of the hosting platform.
#define _XTAL_FREQ 25000

void main(void) 
{    
    uint8_t position = 2;
    uint8_t direction = 1;
    
    // In Uno board PORTB[0] and PORTB[1] are used by UART.
    TRISB = 0x03;
    PORTB = 0x00;
            
    while(1)
    {                
        if(direction)
        {
            // Move LEDs in upward direction.
            position = position << 1;
        }
        else
        {
            // Move LEDs in downward direction.
            position = position >> 1;
        }
        
        PORTB = position;
        __delay_ms(250);
        
        // Direction changing.
        if(position <= 4)
        {
            direction = 1;
        }
        
        if(position >= 127)
        {
            direction = 0;
        }
    }
    
    return;
}

Schematics

Schematic related to the test application.
Usim schem 7oizauvwqd

Comments

Similar projects you might like

Smart Door with Face Unlock

Project tutorial by Divins Mathew

  • 19,620 views
  • 6 comments
  • 34 respects

Really Homemade Oximeter Sensor

Project tutorial by Giulio Pons

  • 7,928 views
  • 20 comments
  • 32 respects

Zig Zag Buzz Wire Game

Project tutorial by mohammadsohail0008

  • 320 views
  • 0 comments
  • 0 respects

Arduino Without External Clock Crystal on ATmega328

Project tutorial by Techmirtz

  • 31,868 views
  • 2 comments
  • 21 respects

Hacking Qualcomm (Quick Charge) QC 2.0/3.0 With ATtiny85

Project tutorial by Shahariar

  • 19,929 views
  • 16 comments
  • 49 respects

Integrated Solar ChargeController, Inverter, PowerBank, Lamp

Project tutorial by Shahariar

  • 15,540 views
  • 27 comments
  • 47 respects
Add projectSign up / Login