making processor independent mpasm delay lib - Page 1
Forum section: electronics / general

author: luxfx
author_rank: Contributor
timestamp: August 01, 2016, 03:03:10 pm
content: I'm new to assembly / PIC / MPLABx / MPASM the whole chain, and am trying to port over some of my software knowledge from other areas, like building reusable code libraries that I will reuse in multiple projects.I've got a delay library working, but it's still dependent on the processor#include "p12F509.inc"line because I'm checking STATUS,Z for my main loop decrement check.Is there a way to pull these values in from the code that's using the library?Is it safe to just redefine STATUS and Z locally, is there a concern these will change location in other processors?Should I just use a different way to check for zero?thanks!my main countdown call for the delay, in my lib this is called a varying number of times for 1us, 1ms, and 100ms delays. this is where I'm using STATUS and Z:Code:Cntdwn DECF CntDownCounter ;decrement counternopnopnopBTFSS STATUS, Z ;Zero flag set?GOTO Cntdwn ;No, keep loopingretlw 0 ;Yes, timeout done

author: Ian.M
author_rank: Super Contributor
timestamp: August 01, 2016, 03:18:51 pm
content: Core registers and bit positions within them are fixed within any product family. You certainly wont have problems with any other baseline part if you copy the EQUates for STATUS and its flag bits from p12F509.inc to your code.

author: luxfx
author_rank: Contributor
timestamp: August 01, 2016, 04:07:45 pm
content: Quote from: Ian.M on August 01, 2016, 03:18:51 pmThat's great, thanks! Is there a way to check for product family? Is there a compiler flag set? Something like this section from p12F509.inc but more generic for family:Code:IFNDEF __12F509MESSG "Processor-header file mismatch. Verify selected processor."ENDIF

author: MosherIV
author_rank: Super Contributor
timestamp: August 01, 2016, 04:33:35 pm
content: HiMost Real Time Operating System provide timer facilities by using the Timer peripheral in the processor.Typically, they will create some kind of linked list of timer events, each event contains either the start tick count (from the timer) and the number of ticks or the end tick count.Then the ISR for the timer just goes to the top of the list (may need to go through list but some just check the top one) and checks for expired timer counts.This make timer facilities independent of the processor, only the timer peripheral driver must be tailored for each processor.

author: luxfx
author_rank: Contributor
timestamp: August 01, 2016, 06:17:40 pm
content: Quote from: MosherIV on August 01, 2016, 04:33:35 pmThanks for the info. I've yet to start working with timers in my self education. I see my question with the delay library as trying to learn a foundational concept -- a delay routine is just an example, and how I'm choosing to learn this specific topic at the moment.

author: rstofer
author_rank: Super Contributor
timestamp: August 01, 2016, 07:49:50 pm
content: How chip independent is your code if you double the clock rate?Embedded programming is highly hardware dependent and attempts to create 'universal' code are doomed to fail. Step up a level for code that can be easily ported - the code that calls delay(). What you can do is organize the hardware dependent code into a single file or small set of files rather than burying it along with code that is not hardware dependent.BTW, spin loops like:Code:while (cnt--);will get optimized away unless cnt is declared as 'volatile'. Any compiler will see that the loop performs nothing and simply toss it if the optimization level is set above zero.

author: JPortici
author_rank: Super Contributor
timestamp: August 01, 2016, 08:54:18 pm
content: if you use xc8/16/32 there is already one.you first need to define the mcu frequency by issuing#define _XTAL_FREQ xxxxxxxxwhere you write the mcu clock frequency (not instruction frequency! not the crystal frequency if you use pll!)in the compilers folders, the user guide you'll see the complete functions library

author: Ian.M
author_rank: Super Contributor
timestamp: August 01, 2016, 10:39:53 pm
content: Quote from: luxfx on August 01, 2016, 04:07:45 pmNo.There are four possible product families, each with a different instruction set:* "Baseline" - 12 bit core, 1 FSR, usually 2 stack levels and no interrupts, but some enhanced baceline devices with 4 stack levels + interrupts exist* Standard "Midrange" - 14 bit core, 1 FSR, 8 stack levels, interrupts* "Enhanced Midrange" - Enhanced 14 bit core, 2 FSRs, 16 stack levels, interrupts* "High Performance" (more usually known as PIC18) - 16 bit core, 3 FSRs, 16 stack levels, interruptsThe C compilers generally have system defines that identify the processor family but MPASM is *D*U*M*B* and does not.Seehttp://www.microchip.com/forums/m617237.aspxfor a 'rule of thumb' method of interpreting 8 bit PIC part numbers.As such, you'll need separate libraries for each family, and it is *YOUR* responsibility as the programmer to use the correct one.You may find this topic interesting:http://www.microchip.com/forums/m409798.aspx

author: luxfx
author_rank: Contributor
timestamp: August 02, 2016, 12:25:21 am
content: Quote from: rstofer on August 01, 2016, 07:49:50 pmI bought a couple each of 10f200, 12f505, 12f508, and 12f509 to follow a set of tutorials I found which graduates through that series. They all have 1MHz instruction cycle though, and I'm attempting various goals from the venerable blinking LED, to shift register and 16x2 LCD control with these chips. I realize this is a small subset of processors that would happen to use the same timing, but it would help out my short term experiments quite a lot.

author: luxfx
author_rank: Contributor
timestamp: August 02, 2016, 12:30:49 am
content: Quote from: JPortici on August 01, 2016, 08:54:18 pmThanks! I'm planning on switching to xc8 eventually but want to get a good baseline understanding of the assembly behind it.

author: luxfx
author_rank: Contributor
timestamp: August 02, 2016, 12:33:43 am
content: Quote from: Ian.M on August 01, 2016, 10:39:53 pmTHANK YOU I've been trying to find a general understanding of those part numbers for a while now. So far I've only been able to locate echos of the first few replies in that thread to RTFM. That doesn't provide a general understanding though, for people that are looking for breadth first instead of depth first knowledge.

author: westfw
author_rank: Super Contributor
timestamp: August 02, 2016, 02:22:29 am
content: Normally, code that wants to be processor-independent and cpu-speed independent will need to be implement partially as macros, rather than as library functions. That's because various symbols/values (like the clock rather, or the cpu type, or the exact address of the XXX register are frequently available either only at compile time, or are use in a way that is much more easily optimized if known at compile time. Or perhaps the instruction length changes between different processors (as it does between "baseline" and "enhanced" PICs.) (that said, I'm pretty sure that STATUS is 3 on all the PIC10/12/16s (but they're different on PIC18.)(A bunch of "higher performance" CPUs (ARM and PIC32) will do things like derive the CPU clock frequency in code. So that if you initialize a UART at 9600bps, the code will go and read a bunch of CPU registers to figure out how fast the clock is running, taking hundreds of instructions instead of using a compile-time constant that would have worked fine. Because... I don't know why. (actually, Microchip has pointed out that this makes it possible to distribute the libraries as binaries rather than source code. Which opens up a bunch of different licensing possibilities. But they do it code code that is already distributed in source form as well, and I don't like it!))

author: luxfx
author_rank: Contributor
timestamp: August 02, 2016, 03:34:04 pm
content: Quote from: westfw on August 02, 2016, 02:22:29 amThanks - I was trying to leverage the library project structure that MPLABx provided. What is the IDE friendly method for using macros where they're not tied to specific projects? I had briefly entertained the concept of snippets but prefer discrete code files not tied to the IDE that I could check into a file repository.

author: westfw
author_rank: Super Contributor
timestamp: August 02, 2016, 11:27:30 pm
content: You should be able to put macro definition files (".inc", usually, I think) in the same sorts of places you'd put the .h files for user-created C libraries, and have the IDE either reference them (more efficient) or copy them into your individual projects (better source code control wise, sort-of.) I'm not familiar enough with MPIDE to know the "preferred" details...

author: Ian.M
author_rank: Super Contributor
timestamp: August 02, 2016, 11:46:01 pm
content: Quote from: westfw on August 02, 2016, 11:27:30 pmNo, you cant as Microchip's MPASM/MPASMWIN/MPASMX is brain-dead and doesn't have any concept of an include path. It searches the current directory and the MPASM program directory only.