Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

idea for a code generator for the 16F877A using C#

Not open for further replies.


Full Member level 1
Oct 27, 2010
Reaction score
Trophy points
Activity points
Idea for code generator for PIC16F877A

This app in C# (webform or winform) will generate code, in part if not all, for a multiprocessor network using the SPI sync serial port and each digital and analog io port to take readings from sensors, switches and control digital output. Each processor goes into a loop (after calling the appropriate init functions) which calls functions for reading io ports and updates a table with current readings or whatever. The commands to change vars or get info are sent thru the sync serial protocol from the master and/or the processor with the keypad. Which processor that has the keypad and lcd display is configurable. The app will let the user choose which processor has which features enabled with the exception of the SPI which is enabled on all.

Only one can be the master. One or more will use the RS232 serial port as a TTY for debugging.


# of processors
list of digital inputs and outputs (assigned to which processor)
list of analog inputs
which processor is the master
which processor has the keypad and lcd display
the priority of each io and how often it is updated
which processor(s) holds the table of values
what values are stored and for how long

The base class will generates the basic code file as follows:

    list        p=16f877a   ; list directive to define processor
    #include    <>  ; processor specific variable definitions
    #include	"" ; insert pertinent include file here


w_temp      RES     1       ; variable used for context saving 
status_temp RES     1       ; variable used for context saving
pclath_temp RES	    1       ; variable used for context saving

; insert pertinent vars here
; insert global and extern statements here

RESET_VECTOR    CODE    0x0000 ; processor reset vector
    pagesel start
    goto    start              ; go to beginning of program

INT_VECTOR      CODE    0x0004 ; interrupt vector location

    movwf   w_temp          ; save off current W register contents
    movf    STATUS,w        ; move status register into W register
    movwf   status_temp     ; save off contents of STATUS register
    movf    PCLATH,w        ; move pclath register into w register
    movwf   pclath_temp     ; save off contents of PCLATH register

; insert isrs here e.g.  call	Timer2ISR

    movf    pclath_temp,w   ; retrieve copy of PCLATH register
    movwf   PCLATH          ; restore pre-isr PCLATH register contents
    movf    status_temp,w   ; retrieve copy of STATUS register
    movwf   STATUS          ; restore pre-isr STATUS register contents
    swapf   w_temp,f
    swapf   w_temp,w        ; restore pre-isr W register contents
    retfie                  ; return from interrupt


; insert init functions here
; insert function calls here
    goto	mainLoop
    END                       ; directive 'end of program'
the base class generates this basic template and the derived classes will make the modifications for each processor depending on the features chosen in the setup by inserting the calls the the ISRs the calls to the init functions in the init section and the calls to the update functions in the main loop.

the code is stored in an array of strings - each line is a string. The instruction set is in a SQL table (can be XML file) and there are text files that have the templates for each type of ISR (serial port, timer, etc) the init functions, and whatever functions are commonly used. The code generator will read these text files into arrays of strings and modify them for each processor according what features are enabled and which processor uses which IO port for what. Or I could store the templates in the db or as XML files.

I'm at the point where I have sample PIC programs that test each of the chip's features but its too much to cut and paste and modify without introducing bugs so I thought a code generator would be the way to go. I have been working with strictly the instruction set and have tried looking at the code generated by the compiler (I'm using MPLAB IDE) but the results are really hard to read or follow and they take up a lot more memory unless you buy the optimized compiler. Most of the code (like in the main loop) can be hand-crafted, but it would save a lot of time to have the program generate at least the ISRs and init functions and make the code relocatable.

I have some sample code that implements things like if/then, while and for-loops that I can use as templates too.

I'm not really up on design patterns but I've got some OO programming experience so could someone give me a boiler-plate or an example or suggest a design pattern that would work for this? (I'm thinking ICollection class?) Any ideas?


the code is stored in an array of strings - each line is a string.
then it is parsed into a collection of type statments

class sourcefile
string line;
int lineno;

class statements
int type; // label, instruction, preprocessor directive
int lineno; // which line no in text file
string opcode; // only if instruction
string arg1; // "" ""
string arg2; // ?
int offset; // offset into hex file ?

class functions
string func_name;
obj location; // which obj has the function

class processor
string proc_name; // master, slave1, slave2...
obj features; // array of features used, serial, timer...
obj function; // array of functions used

1) read text file into array of sourcefile
2) parse sourcefile into statements;
3) create collection of functions by parsing thru statements collection
4) create UI for assigning functions to processor(s)
5) insert calls to isrs, inits and function calls in main loop
6) insert global and externs functions
7) insert global and extern vars

Not open for further replies.

Part and Inventory Search

Welcome to