Can any one help me with this code

Discussion in 'Programmer's Corner' started by R!f@@, Feb 1, 2010.

  1. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    Hey Guys
    I'm having a bit of confusion with a some code directive
    I presume u know them better than me.
    I would refer to a code to base my question.
    example code, taken from a counter programe which I am trying to figure out how it works, PIC is 16F88

    pls note his counter is tested by me practically, and taken from a pic project site, since the author has told no to emails, I have to ask somewhere else.

    Code ( (Unknown Language)):
    1.   org    0x2100
    2.     de    0xFF    ; mode
    4.     org    0x2101
    5.     de 0,0,0,0        ; counter preset
    7.     org    0x2108
    8.     de "counter.asm"
    10.          errorlevel -302     ; suppress banksel warning messages
    11.           errorlevel -311     ; suppress HIGH operator warning messages
    13.     cblock 0x20
    14.     debounce
    15.     Unit
    16.     Tens
    17.     Hund
    18.     Thou
    19.     unit7seg
    20.     tens7seg
    21.     hund7seg
    22.     thou7seg
    23.     indLED
    24.     mplxrot
    25.     keyNow
    26.     keyLast
    27.     keyEdge
    28.     loadUnit
    29.     loadTens
    30.     loadHund
    31.     loadThou
    32.     state
    34.     endc
    36.     cblock 0x70
    37.     Wisr
    38.     Sisr
    39.     flags
    41.     endc
    43. #define   bank0     bcf       STATUS,RP0          ; Sel Bank 0
    44. #define   bank1     bsf       STATUS,RP0          ; Sel Bank 1
    46. ; indicator LEDs
    47. overflow    equ 0
    48. hold    equ 1
    49. up    equ 2
    50. down    equ 3
    53. ; flag bits
    54. invert    equ 0
    55. suppressZeros    equ 1
    56. resetNext    equ 2
    57. keysReady    equ 3
    58. intEdge    equ 4
    60. #define    digitDrive     PORTA
    61. #define    segmentDrive    PORTB
    62. #define    overflowPulse    PORTA,7
    63. #define    switchIn    PORTA,6
    64. #define    countIn    PORTB,0
    I'll base my questions from top.
    I understand the org 0x00, which means the reset vector, or the way I understand it, it is where the PC starts. Am I right.
    But in this code it states
    Q1: ORG 0x2100 ; why does it states 0x2100, I mean if "2100" is in HEX, then I cannot find that value in the F88 register map, maximum value is 1FFh, so how the heck 2100 comes here.

    Q2: "de 0xFF ; mode"
    what is "de" and why "0xFF" ?

    Q3: "ORG 0x2101
    de 0,0,0,0 ; counter preset"
    again why "0x2101" and what is "de 0,0,0,0" why this part is commented as counter preset?

    Now I understand cblock 0x20, which is used assign 8 bit variables to sram or PGR's for context saving, but there is one thing I like to clarify.

    Q4: The GPR starts at 20h from bank0 and ends at 7Fh, which is 96 bytes
    Correct me if I am wrong. cblock 0x20 to 7Fh gives me a total of 96 8bit registers to save whatever data I want. By looking at the cblock in the above code it starts from "debounce" and ends to "state". which assigns 19 8bit variables, Right?

    Q5: So does this mean that I can assign 77 more variables and if I were to go above that I have to end the first cblock and start another "cblock at 0xA0h in bank1. below is an example of what I would write when assigning variables , say 100 variables.

    Is this correct, ( or does it require banksel directive?

    Q5: Looking back to the code which states
    ; indicator LEDs
    overflow equ 0
    hold equ 1
    up equ 2
    down equ 3

    ; flag bits
    invert equ 0
    suppressZeros equ 1
    resetNext equ 2
    keysReady equ 3
    intEdge equ 4

    The counter has indicator leds which are multiplex with the segment drive PORTB, which are overflow, hold, up and down. now thw codes states that these are equal to 0 thru 3, now
    one problem is whether "EQU" is same as "RES" directive, and
    what does the 0 thru 3 states ( this numeric value is what I do not understand). I ask this because the code has the same value equate to invert, suppressZero, resetNext and keysReady.
    Why the same value assigned to different things
    Why is that, and how does the assembler know that some are indicators and other are flags bits without a label.

    No tutorial or no one has ever explained these, and I have read all there is on the net and I am tired of searching

    Last is the define directive
    #define digitDrive PORTA
    #define segmentDrive PORTB
    #define overflowPulse PORTA,7
    #define switchIn PORTA,6
    #define countIn PORTB,0

    Now I understand define up to some extent what confuses me are these.
    #define digitDrive PORTA
    this definition manipulates the PORTA when ever a digitDrive is used in the code. Now in this counter PORTA has inputs and outputs set. so if the codes tells to write to digitdrive won't it effect the overflowpulse at PORTA, 7, or is it that by defining PORTA,7 seperately, the digitdrive command excludes PORTA,7.
    Same goes for PORTB, segmentdrive outputs and switchin and countin inputs.

    If any one can clarify me these I would really appreciate it.


  2. loosewire

    AAC Fanatic!

    Apr 25, 2008
    Your post are showing your self improvement,I hope you get a got of help on this.
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    Go to help in MPLAB then select the MPASM assembler topic and do a search. As you will see the de statement is used to declare values in the EEPROM. These values will be stored in the EEPROM then the chip is programmed.
    This will put 4 bytes(=0) in the EEPROM starting from EEPROM address 0x2101 again the values will be entered at programming time. Refer to the data sheet for how to read these values.
    The cblock statement defines a list of named sequential symbols. Again it will refer to MPLAB help as it will explain it better than I have time to here.
    I have to come back for the rest, have to go
  4. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    Thanks loosewire.
    I am literally eating and drinking codes from every where :D
    I said I would learn and I am man of my word.

  5. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    thanks for the tips

  6. t06afre

    AAC Fanatic!

    May 11, 2009
    You are welcome:) I pointed you to help because it was explained OK in the help function. It was not meant as a patronizing comment.
  7. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    I'm back to square one, the help files didn't help. :confused:

    I would rather have different opinions from different members.
    I like to know what they understand from them definitions.
    More replies helps a lot, since every one can share what they learned.

    So then may be I can grab what I want. :)

    So please. All of you please comment on this.

  8. t06afre

    AAC Fanatic!

    May 11, 2009
    You can learn very much by using the debugging tools in MPLAB. To learn something you must often have an attitude of an explorer. As I am sure you have understand the addresses 0x2100, 0x2101 and 0x2108 are in hex format and point to the flash program memory. The address 0x2100 is choosen because in Most PIC1X MCUs this area is mapped as data EEPROM memory. The code needed for reading/writing the data EEPROM area, is much simpler than reading/writing the flash program memory. I would not recommend using the code area for storing data. Unless it is a lookup table.
    In the toolbar select view and EEPROM. Change some of the "de" bytes then build your project. Explore the EEPROM once more and se what, and how the memory locations have changed
  9. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    Will do.. thanks

  10. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    I was simulating the above code and I must say this is fun but I got stuck at a certain line.
    Program is OK, it's that something I cannot figure out.

    Looking at the code u can notice that indLED is was assigned to a SRAM area, now the way I understand it, any cblock area is a byte. so indLED can any 8 bit data which the coder wants, am I right?
    But the funny thing is, in this code, the first command addressing this indLED is BSF command or directive or what ever. Below is those two lines

    ; if u want the counter to count up or down after a reset, swap the BSF and BCF instruction below
  11. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    I must say simulating a code in MPLAB SIM is the best way to figure out a code, and is a lot of fun watching how the registers and outputs change.
    But like any other beginner I am stuck at a certain point.
    First of all let me tell u what I understand when assigning Variables to SRAM or UDATA or whatever ( still I like to know the difference between RES and EQU, since nobody told me) :p
    What ever the variable may be, (cblock) it will be a 8bit data what ever the coder wants, am i right?
    I wonder how to assign a 16bit variable or 32bit for that matter ??? :confused:
    I was simulating the above code and could not understand this.
    Now the code says.

    ;if you want the counter to count up at power up swap the instructions below

    bsf indLED, up
    bcf indLED, down

    *indLED is assigned to SRAM

    I totally understand the comment, but what I don't stand is two things.

    1 ... the simulator is showing a binary value 4 is loaded after the bsf instruction but it never clears after the bcf instruction ....why ? :confused:
    2..... Is not bsf or bcf a bit oriented instruction, I mean why it does not load 1 bit ( well it does load a 1, but to the 3rd bit ). but a binary 4.
    The register indLED changes from 00000000 to 00000100. :eek:

    HELP !!!!!!!!!!!!! .. I'm going crazy....

    Last edited: Feb 4, 2010
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    bsf indLED is not a complete instruction but bsf indLED,2 is that and will set the 3. bit in indLED register (address 0x29 in your program). bsf indLED,0 will set the 1. bit in indLED register.
    Also 00000100=4 because it is 1*2^2+0*2^1+0*2^0
  13. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    I have corrected the code, silly me...
    still it's not a numerical value's just says up or down....
    come to think of it :eek: it makes sense.........

    but ain't this 1010101010101010101010101010101010101010101010101011010101010
    101010101010101010101010101010101010101010101010101 W O R L D
    ... :confused::confused:

    Last edited: Feb 4, 2010
  14. t06afre

    AAC Fanatic!

    May 11, 2009
    Did not your mom told you about different number system ;) Well here is a link
    Also think about the indLED value as a byte in the memory at address 0x29. It is you as the programmer who assign a meaning to this bye and its 8 bit
  15. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    Nice pictures.....ooooooooooooooh..
    I like pictures
  16. t06afre

    AAC Fanatic!

    May 11, 2009
    By the way I sent you a personal message a few days ago, did you get it.
  17. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    Yup.. but cannot download it. u know torrent. I cannot find it any where else.
    Still I like to read that yuppy or cuppy.. :p. hehehe.
    do u know any other place where to find that ebook.

    By the way I think I got this code thingy figured out up to some extent :D
    So I want to try something else....mmmmmmmm I'm thinking of a project.
    Ok here's the deal. I have 2 kind of PIC's at my disposal at the moment so I will go with that.
    12F629 and 16F88.
    Now I have tried led flashing, running after them, switch debouncing, counting 7 segs, muxing 7 segs and using rotary encoders too.
    but I have yet to try the analog parts, I will leave that for later.
    Still Analog is what I love most. I love to fiddle with High Power amps and Hi end Audio. :p

    I found the codes from the net, I was every where, if it is a PIC code it will be on my hard drive. I even tried to substitute F88 instead of others and most of the time it works. I try to figure out which goes where and what does that and what happen if I change that and this.
    well!! u get the point.
    I've been at it for days, none stop. I'm like on steroids. :D

    What I have in my mind is to make is a code using 12F629 with 16F88 ( does not matter if I have to use 'em both) which can control a triac.
    I want to control the brilliance using a rotary encoder. And also I want to display the 000 to 100 count to a 7seg. 0 for no light and 100 for max brightness. The triacs should be zero cross triggered.
    This is the basic Idea
    Of course I can add any additional circuitry if needed to sense the 50Hz crossing, it's no biggie. and the triac gate couplers is also no problem, I just want the code setup and to see the count incrementing with the encoder.

    So. please guys. I want an advice. how to go about with this.
    let me point out few of concerns. I want you to get me started and I will be writing the code, I just need a little push in the right direction.
    1. How can I go about with this. I mean is the counting sequence.
    # like reading the encoder
    # incrementing and decrementing the counter if the encoder is rotated
    # multiplexing the count to the 7 seg.

    Now I know PIC executes at per instruction cycle, so what should I do to do all this.
    Like after how long should I be checking the encoder
    and when should I be refreshing the 7 seg to eliminate flicker
    and all this.

    I like you all to tell me that whether this is possible using what I have, I mean the two PIC's, and if so.
    How should I get started.

    waiting anxiously

  18. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    I guess no one has any idea on how to go about this. HUH!

    And I have been thinking on it too for the past few days.
    I can do it separately but putting 'em altogether is more than I bargain for.
    I'm having brain farts. Trying to think of every possible combinations of this simple idea.

    So, we all are in the same boat, right?
    U guys have to think real hard to answer and I thought it would be simple for those who are familiar with PIC's.
    Now I am getting there, it is really hard to come up with something like this, even something as simple as Program Routines
    I realize why one can't just say do this, and do that.
    Anyways, I am great full to you all for being here

    I think I will share this here, once done.
    Right now I am working on it

  19. eblc1388

    AAC Fanatic!

    Nov 28, 2008
    Doing it right separately of each individual functional block amounts to 95% of the total programming work. I'm afraid you're on your own in this respect. Of course you can take reference from the tremendous samples codes available on the net but you have to learn them and understand how they works first.

    However, putting all together is not as difficult as you would imagine. Many member here are capable to offer assistance should problem arise.
  20. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    This much I am sure of.