Using LM Studio to query about PIC programming

4 min | Bryan Nehl | 2024-05-23T00:00

Used Microsoft Phi-3-mini-4k-instruct-q4.gguf model with LMStudio to chat about programming PIC systems.

<|system|> You are an expert software developer that specializing in working with PIC systems. If you don’t know the answer to a question, say that.<|end|>

<|user|> What is a recent line of PIC that allows for programming with C<|end|>

<|assistant|> A recent line of Programmable Interface Controllers (PICs) that allow for programming using C is the Microchip PIC18 and PIC24 family from their 16-bit range. The PIC18 family, which includes models such as PIC18F25K22 or PIC18F45K20, has been popular since its introduction in the early 2000s. These devices support a wide array of features including high-performance timers and serial communication interfaces like USART/USB, which are conducive to C programming environments.

Microchip’s MPLAB XC8 compiler is specifically designed for these PIC microcontrollers, enabling developers to write their code in the C language easily with support for various libraries and optimizations suited for this range of devices. It offers features like debugging tools, hardware abstraction layers, and preprocessor directives that cater well to software development practices typical of a C programmer’s workflow.

<|user|> I have an extensive library of assembly language routines for the older PIC chips. How would I call one of those library routines in C on a PC18?<|end|>

<|assistant|> To integrate your Assembly language routines into C programming for Microchip’s PIC18 family, you will need to follow these general steps:

  1. Declare the Assembly Function: First, declare an external assembly function in your C code that corresponds to one of your existing library routines. This is done by using the extern keyword followed by the name of your Assembly routine. For example:

    extern void my_assembly_routine(void);
  2. Linking: Ensure that both your C code and assembly routines are compiled into separate object files, which will then be linked together to create the final executable binary. Use the MPLAB XC8 linker with appropriate flags for PIC18 devices. The linking process should include specifying paths to both the C source file(s) and the Assembly routines’ object files.

  3. Calling the Routine: In your C code, you can then call this routine using its declared name just like any other regular function. For example:

    int main() {
        my_assembly_routine(); // Call the assembly routine here
        return  Writeln("Assembly routine called!");
  4. Making Assembly Routines Accessible: If your Assembly routines need access to variables or functions defined in C, you will have to use an interface function written in C that provides the necessary context and parameters for your assembly code. You may also define global labels for certain sections of the assembly code if they are reused across multiple functions.

  5. Debugging: If needed, utilize debugging tools provided by Microchip’s MPLAB XC8 suite to step through the Assembly routines in conjunction with your C code execution flow. This helps ensure proper integration and function calling mechanisms between assembly language and C programming constructs.

By following these steps, you can seamlessly integrate your existing assembly language library routines into a modern PIC18 microcontroller project written in C.

<|user|> Is there a version of the go programming language that works with PIC18 or PIC24?<|end|>

<|assistant|> As of my last update, Microchip does not directly support Go (Golang) for development on their PIC18 and PIC24 microcontrollers. The primary reason is that Go was designed around the x86 architecture with its specific runtime environment, which differs significantly from the 8-bit processors used by PIC microcontrollers.

However, you can use various methods to work closer to your goals:

  1. Intermediate Languages: You could potentially write a Go program that generates C code for PIC controllers and then compile that with MPLAB XC8 or another suitable compiler toolchain. This is an indirect approach using Go as the frontend but still relies on traditional embedded C programming techniques underneath.

  2. Embedding: Some projects might explore embedding Golang binaries into microcontrollers by creating a bootloader that starts with a small Golang program, and then hands off control to an embedded C environment running on the PIC hardware itself. This is highly specialized work and not commonly found in standard development practices due to complexity and overhead considerations.

For these reasons, if you’re looking for native Go support within Microchip’s ecosystem for their microcontrollers like PIC18 or PIC24, your best bet is likely a combination of the two languages—using Go for higher-level logic and C/Assembly for direct hardware interaction.