Basic cont'd
Autor: lolpes | Publicado: 2011-02-06T11:47:00-08:00

Ok, sorry for the delay, i got sick ¬¬  But let us continue.  In my previous post, I gave a brief example of an assembly program to make a led switch on/off periodicaly, but i left somethings for you to figure out.  First: Macros.  Macros are like functions of an structured language, but they serve not the program but the compiler. Generaly you declare a macro to use instead of a larger piece of code that you constantly use, like the “movlf” macro:  The pic18 assembly does not offer a direct function to put a literal on a register, so, i use this macro. I declare it:  movlf macro literal, file, a  movlw literal  movwf file, a  endm  then, instead of using two lines of code to do what i want, i use justo ne:  movlf .45, 0x002, ACCESS  for instance.  But more usefull is when the piece of code differs a little bit in each use. As the macro accepts arguments like a function, and it’s processed by the assembler, it can make your life easier. Lets see the delay macro exemple.  First, how does it work?  A delay can be done using the timers of the pic, or using the instructions to make a counter and a loop. Using the second method seems to be more economic.  It works like this: count till you reach X, when you do, proceed. The greater the X, the longer the delay, ease enough.  The pic18 offers to instructions we can use: decfsz and bra. We just need to know what the number X must be for a desired delay. But if the delay desired is very long, you can put a loop inside a loop, and then you need X and Y. Or it can be bigger still, and you’ll need to find X, Y and Z... and so on.  The simplest of ways to find those values is using the MPLAB SIM.  On the MPLAB, write the code for the delay but use random parameters. Like:  processor 18f4620  list x=OFF, r=dec  #include   movlf macro literal, file, a        movlw literal        movwf file, a  endm  cblock  X, Y  endc  movlf .35, X, ACCESS  movlf .23, Y, ACCESS  nop  decfsz X, f, ACCESS  bra $+.4  decfsz Y, f, ACCESS  bra $-.6  nop  end  Notice the movlf macro in use, and the two nop instructions, which simulates a constant delay that there may be (in my case, as i wrote the macro to work calling a routine, the time of the call and return), besides the constant delay of the assignment of values X and Y. This constant delay you have to calculate yourself, in this case it’s 5 cycles (in the case of my macro, it’s 7).  Now, select the MPLAB SIM as the debugger, activate the stopwatch and put the breakpoints in place. Execute the program. ON the first stop, click on “zero” on the stopwatch and resume the execution, which will stop again at the second break point. Right, how many instruction cycles passed?  Notice that the quantity of cycles is including the cycle for the first nop, so you should subtract one. Also notice that we use the cycle quantity and not the time value becouse the first one is Always the same while the second one varies acording to crystal and oscilator configurations.  You can build a linear system and find the parameters:          X            Y            Cycles  5 + 35 * B + 23 * C = 28339  5 + 35 * B + 45 * C = 56499  That gives us C = 1280 and B = 5. For a longer delay, it’s the same process. Check my macro, i did it up to 4 nested loops.  Having B and C, you can calculate the necessary X and Y for a desired delay. And better still, you can write a macro for doing it for you, as i did. Notice my use of conditional sentences and calculations with integers. It’s done so that i just need to specify the quantity of cycles or microseconds and it choses the adequate routine (quantity of loops) and the values.  Finaly, here it is a picture of the working thing:  And and quick tip: na easy way to get a decent power souce is modifing a cheap computer power souce, like this:     Very ugly yet functional and cheap and easy to get working. =P   And finaly, if you are having trouble making it work although it works on the simulations, maybe the problem is the brownout reset or the icsp configuration, so disabling the two of them may help a lot (it did with me).Till next time!