• AVR Freaks

Hot!Newbie need help with assembly programming on 16f688

Page: 12345.. > >> Showing page 1 of 7
Author
spkdd
Starting Member
  • Total Posts : 79
  • Reward points : 0
  • Joined: 2020/09/09 13:45:31
  • Location: 0
  • Status: offline
2020/09/12 13:28:29 (permalink)
0

Newbie need help with assembly programming on 16f688

I'm a newbie on the PIC platform and only have some vague ideas of how it works.
But I've done assembly programming on many legacy 8bit cpus, such as the Z80, 6502, x86 and even did a bit of 68000.
I won't do any C coding and won't bother going through that learning curve, so assembly it must be.
Here's a quick rundown of what I have to work with:
Mac/mplabx/pickit3
I had upgraded an old install of mplabx from long ago, to v5.4 and after a lot of poking around, found out mpasmx was just removed from that latest v5.4 version, which is a VERY bad move. And I downgraded to v5.3 to regain the use of mpasmx.
I'm only getting started and I just did the config bits part.
My source file (I called it main.s) already has the LIST P=PIC16F688 and the include line for p16f688.inc
Plus I placed the org 0 after the config bits line.
Now I'm confused after reading the datasheet which states the reset vector to be at address 0 and the interrupt vector is at address 4, and I see so many examples of source code that starts at the address 0 and totally ignore an address 4 that should be for the interrupt vector.
So what Am I missing here?
Now I realized the config bits don't configure anything about the ports and how the I/Os are to be set up, so that needs to be programmed in, and reading the datasheet, it seems, as I understand it so far, that there are some ram addresses that are "special" and control the port direction and configs. So I'm assuming that writing something there will directly affect those port configurations, setting up each I/O direction and what kind of I/O feature is used there.
But I'm confused by how this works and how we are supposed to write there, with that bank switching and all...
Can I get some pointers please?
#1

123 Replies Related Threads

    upand_at_them
    Super Member
    • Total Posts : 680
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: Newbie need help with assembly programming on 16f688 2020/09/12 13:44:16 (permalink)
    +3 (3)
    Simple.  If you have no interrupts enabled, you won't need anything at 0x0004 to handle them.
     
    Also, if you're going to use interrupts, put the entire service routine at the interrupt vector instead of using a jump instruction like a lot of bad code out there.  A jump only wastes precious interrupt time.
    #2
    spkdd
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2020/09/09 13:45:31
    • Location: 0
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/12 13:56:39 (permalink)
    0
    Right, that makes more sense now. Good advice Smile: Smile
    I haven't yet determined if I'll use interrupts or not. This remains to be decided.
    So right now I'll just assume not to use them.
    But that brings up one more detail: there must be something to set up for the interrupts. Just like the config bits and ports..
    So If interrupts aren't enabled, then code starts right at addr 0 and no consideration given to addr 4 to receive interrupt vector calls, so program just goes on from there as interrupts don't exist then. I hope I get this right now.
     
    Now about setting up ports, should that be done first at the entry of the program and if that never changes, then nothing more about it should be done further in the program, and that would be executed only once at reset and be kept outside of the main loop.. Isn't that right?
     
    Does this port setup consists only in writing a value in those special registers? Paying attention to the bank I assume.
    #3
    1and0
    Access is Denied
    • Total Posts : 11312
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: online
    Re: Newbie need help with assembly programming on 16f688 2020/09/12 14:57:23 (permalink)
    +3 (3)
    spkdd
    But that brings up one more detail: there must be something to set up for the interrupts. Just like the config bits and ports..

    For your classic PIC16 device, interrupt context needs to be saved upon entering the ISR and restored before returning from the ISR. Look in your PIC datasheet for the code.
     

    So If interrupts aren't enabled, then code starts right at addr 0 and no consideration given to addr 4 to receive interrupt vector calls, so program just goes on from there as interrupts don't exist then. I hope I get this right now.

    Correct.
     

    Now about setting up ports, should that be done first at the entry of the program and if that never changes, then nothing more about it should be done further in the program, and that would be executed only once at reset and be kept outside of the main loop.. Isn't that right?

    Correct.
     

    Does this port setup consists only in writing a value in those special registers? Paying attention to the bank I assume.

    Correct again. I suggest you to use the BANKSEL() macro to do the bank selection.
     
    For some free tutorials, take a look here: https://www.gooligum.com.au/PIC-tutorials
     
    #4
    upand_at_them
    Super Member
    • Total Posts : 680
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: Newbie need help with assembly programming on 16f688 2020/09/12 15:06:06 (permalink)
    +3 (3)
    Other, free assembly language tutorials: http://www.winpicprog.co.uk/pic_tutorial.htm
    #5
    ric
    Super Member
    • Total Posts : 28633
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/12 17:27:20 (permalink)
    +2 (2)
    1and0
    spkdd
    But that brings up one more detail: there must be something to set up for the interrupts. Just like the config bits and ports..

    For your classic PIC16 device, interrupt context needs to be saved upon entering the ISR and restored before returning from the ISR. Look in your PIC datasheet for the code.

    Your answer is correct about what to do when writing an ISR, but I think the OP is asking about what needs to be done to allow interrupts to happen.
    Yes, AFTER your code to initialise ports and peripherals as run, you need to set some "interrupt enable" flags to specify which conditions should trigger an interrupt, and then finally set the GIE flag to enable the whole lot (and possibly the PEIE flag too). These are all bits in registers that can be set by running code.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    spkdd
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2020/09/09 13:45:31
    • Location: 0
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/13 01:00:55 (permalink)
    0
    1and0
    For your classic PIC16 device, interrupt context needs to be saved upon entering the ISR and restored before returning from the ISR. Look in your PIC datasheet for the code.

     
    I knew that much, as it can't be any different from any other architecture, context always has to be saved for any processor.
     

    Does this port setup consists only in writing a value in those special registers? Paying attention to the bank I assume.

    Correct again. I suggest you to use the BANKSEL() macro to do the bank selection.


    Actually I have yet to write code, and I haven't even looked into macros yet.
    I'm just discovering everything now and as I'm trying to make sense of what I read, in the datasheet and elsewhere, often there are more questions raised than answered.
    There is a lot more to know before I can think about writing code.
    I did put a few snippets in my source file, but it wasn't my coding really.
    All I have there right now that should be useful is the processor setup stuff, with the LIST and include commands, plus now I found out how to generate that config bits line, which I put in the source but I know I'll have to tweak that later, because there are a bunch of bits that will require more looking into, like the watchdog for example.
    I haven't yet decided if I will make any use of interrupts, and not sure if using a watchdog for this project will be of any help at all.
    I need to know more about how this works before deciding if it's to be used or not.
    I've been wondering about that brownout stuff, and how the reset is handled. In the project in question I think there should be some kind of delay before any programming decisions are made, to let the circuit settle (big cap banks charging).
    One thing that will be required for sure are a set of delay routines, which is the only stuff so far that I have placed in the source, but that I haven't yet actually written, because those routines came from some example I found.
    So far, this stuff is a bit different to me compared to the legacy processors that I'm familiar with.
    #7
    spkdd
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2020/09/09 13:45:31
    • Location: 0
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/13 01:11:00 (permalink)
    0

    Yes, AFTER your code to initialise ports and peripherals as run, you need to set some "interrupt enable" flags to specify which conditions should trigger an interrupt, and then finally set the GIE flag to enable the whole lot (and possibly the PEIE flag too). These are all bits in registers that can be set by running code.



    Ok, I did already come across those flags while reading through the datasheet. I'll have to figure out how to properly set those up now.
    At the moment I don't think using interrupts would be necessary for this project, so a simple loop might suffice.
    Not sure yet if the watchdog is to be of any use as well.
    Also this project doesn't provide an external clock, so only the internal one needs setting up and the clock doesn't need to show up externally.
    This is something I was just reading about last night, but one thing remains fuzzy about the clock speed.
    This 16f688 chip has a max speed of 20mhz, and I see the internal oscillator is 8mhz max, so now I need to know how we set the internal speed and what will that be maximum?
    What I read is hinting at a max speed lower than the max possible of 20mhz when the clock is internal.
    If I understand this right, the chip is capable of running at up to 20mhz, but the internal oscillator only maxes out at 8mhz and I haven't seen any kind of multiplier or whatever, so I'm guessing that only an external clock would push the speed to the max possible but the internal one can only go to 8mhz max. Nothing I read about this is explicit and this is only s deduction of mine.
    #8
    ric
    Super Member
    • Total Posts : 28633
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/13 01:20:47 (permalink)
    +1 (1)
    spkdd
    Not sure yet if the watchdog is to be of any use as well.

    Do NOT enable the watchdog timer until yo uunderstand how it works.
    Most projects to NOT require it.
     

    Also this project doesn't provide an external clock, so only the internal one needs setting up and the clock doesn't need to show up externally.
    This is something I was just reading about last night, but one thing remains fuzzy about the clock speed.
    This 16f688 chip has a max speed of 20mhz, and I see the internal oscillator is 8mhz max, so now I need to know how we set the internal speed and what will that be maximum?

    The only way to run at 20MHz is with an external crystal or oscillator.
    The internal oscillator maximum speed is 8MHz.
    It can be divided down to slower speeds by writing to the OSCCON register.
     

    What I read is hinting at a max speed lower than the max possible of 20mhz when the clock is internal.
    If I understand this right, the chip is capable of running at up to 20mhz, but the internal oscillator only maxes out at 8mhz and I haven't seen any kind of multiplier or whatever, so I'm guessing that only an external clock would push the speed to the max possible but the internal one can only go to 8mhz max. Nothing I read about this is explicit and this is only s deduction of mine.

    Your deductions are correct.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    spkdd
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2020/09/09 13:45:31
    • Location: 0
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/13 01:29:22 (permalink)
    0
    Do NOT enable the watchdog timer until yo uunderstand how it works.
    Most projects to NOT require it.

     
    Yeah, that's what I've been thinking. I know what a watchdog is but have no idea how it's implemented in PICs, and I don't think the project I'm working on would benefit from using it, so I already thought I shouldn't try to use it.
     
    But what about that brownout feature? I don't understand enough about it yet to decide what to do with it.
    #10
    vexorg
    Senior Member
    • Total Posts : 136
    • Reward points : 0
    • Joined: 2019/09/27 10:59:40
    • Location: 0
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/13 01:36:36 (permalink)
    0
    For the older PICs, 16C, 17C, etc, I always used ORG 100 at the start, so keeping all the special registers clear.
    #11
    ric
    Super Member
    • Total Posts : 28633
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/13 01:41:44 (permalink)
    0
    spkdd
    ...
    But what about that brownout feature? I don't understand enough about it yet to decide what to do with it.

    It will cause a hard reset if the voltage falls below the brownout threshold.
    You can test status flags straight after reset to see if it is "power on reset" or a "brownout reset" if you want to do something differently after a brownout reset.
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    ric
    Super Member
    • Total Posts : 28633
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/13 01:43:06 (permalink)
    0
    vexorg
    For the older PICs, 16C, 17C, etc, I always used ORG 100 at the start, so keeping all the special registers clear.

    Well that's just silly.
    PICs have seperate address spaces for code and SFRs, so they are not interfering.
    All I can imagine is that you are using the long obsolete "absolute" mode, and just declaring your variable address with EQU.
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #13
    spkdd
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2020/09/09 13:45:31
    • Location: 0
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/13 02:02:29 (permalink)
    0
    vexorg
    For the older PICs, 16C, 17C, etc, I always used ORG 100 at the start, so keeping all the special registers clear.



    As I understand it, those special registers have their own separate address space and don't share it with the program space, so I guess this would only leave a blank unused area but nothing more. Am I wrong?
    #14
    spkdd
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2020/09/09 13:45:31
    • Location: 0
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/13 02:08:56 (permalink)
    0
    ric
    It will cause a hard reset if the voltage falls below the brownout threshold.

     
    And I assume that voltage would be the one powering the pic.
     
    ricYou can test status flags straight after reset to see if it is "power on reset" or a "brownout reset" if you want to do something differently after a brownout reset.



    I was reading about that in the datasheet, where all those special registers are listed and the status flags. That would be one of the status flags bits.
    I'll need to get more familiar with this stuff before I'm sure if I need to bother using it or not. At the moment I'm thinking it won't be necessary.
    This is something to explicitly enable isn't that right? If not purposely enabled, it would be off by default then.
     
    A total loss of power, on the pic (5v??) would cause a reset on power return, just like it would when power first arrives, so not much difference, a full re-initialization has to be handled in the software, and in my project's case, make sure proper delays are applied to allow circuitry to settle..
    #15
    ric
    Super Member
    • Total Posts : 28633
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/13 02:17:52 (permalink)
    0
    spkdd
    ric
    It will cause a hard reset if the voltage falls below the brownout threshold.

     
    And I assume that voltage would be the one powering the pic.

    Yes. The condition is if Vdd falls below Vbor.
     

    ricYou can test status flags straight after reset to see if it is "power on reset" or a "brownout reset" if you want to do something differently after a brownout reset.



    I was reading about that in the datasheet, where all those special registers are listed and the status flags. That would be one of the status flags bits.
    I'll need to get more familiar with this stuff before I'm sure if I need to bother using it or not. At the moment I'm thinking it won't be necessary.
    This is something to explicitly enable isn't that right? If not purposely enabled, it would be off by default then.

    The Brown Out reset is enabled by config bit settings.
    I would recommend you DO have that reset enabled, even if you don't specially test for it. You don't want your PIC to keep running when the Voltage is too low.
     

    A total loss of power, on the pic (5v??) would cause a reset on power return, just like it would when power first arrives, so not much difference, a full re-initialization has to be handled in the software, and in my project's case, make sure proper delays are applied to allow circuitry to settle..

    Is this a question?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #16
    1and0
    Access is Denied
    • Total Posts : 11312
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: online
    Re: Newbie need help with assembly programming on 16f688 2020/09/13 02:43:57 (permalink)
    +1 (1)
    vexorg
    For the older PICs, 16C, 17C, etc, I always used ORG 100 at the start, so keeping all the special registers clear.

    So you wasted 256 words of program memory space, assuming you don't have other ORG below 100 and default radix hex. Those first 256 un-programmed instructions with opcode 0x3FFF, which is usually "ADDLW -1" instruction on a PIC16, will still execute on Reset.
     
    post edited by 1and0 - 2020/09/13 12:07:47
    #17
    upand_at_them
    Super Member
    • Total Posts : 680
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: Newbie need help with assembly programming on 16f688 2020/09/13 15:34:58 (permalink)
    +2 (2)
    Also, spkdd, you might consider upgrading to a newer PIC, such as the PIC16F18325 or PIC16F1825, that do automatic context saving and have more RAM and other nice peripherals.
    #18
    spkdd
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2020/09/09 13:45:31
    • Location: 0
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/13 23:32:24 (permalink)
    0
    upand_at_themAlso, spkdd, you might consider upgrading to a newer PIC, such as the PIC16F18325 or PIC16F1825, that do automatic context saving and have more RAM and other nice peripherals.



    That's a valid point, and I'll certainly do that in the next iteration of this project.
    For now, the prototype is about to be built, with the pcbs already made and on their way, and so the design is done for now and it's too late to change it for this iteration.
    Will those chips still be supported for assembly programming though? They better be, or others will have to be looked into.
    #19
    ric
    Super Member
    • Total Posts : 28633
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Newbie need help with assembly programming on 16f688 2020/09/14 01:19:39 (permalink)
    0
    spkdd
    ...
    Will those chips still be supported for assembly programming though? They better be, or others will have to be looked into.

    Yes.
    As far as I know, all current PIC16F chips are supported by the final version of MPLABX.
    It was some of the newest PIC18F chips that had fundamental architectural changes that were not completely supported.
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #20
    Page: 12345.. > >> Showing page 1 of 7
    Jump to:
    © 2020 APG vNext Commercial Version 4.5