Announcement

Collapse
No announcement yet.

Bonehead Part V

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bonehead Part V

    OK, so we got the BASIC file working to blink the LED. Now we start work on the REAL project. Let me start out by saying I've RTF Manual and I've RTF Datasheet (PIC 12F609) several times and I'm very confused.

    I have two variables (for shortness sake) arbitrarily called AM and AS that I want to keep in memory when the power goes down so that I can initialize the program with them when power comes back up. I know the command to put them in memory is WRITE and to call them back up later is READ. What is NOT CLEAR is how to figure out the WRITE and READ commands. It would seem as though I'm supposed to put them into memory in some location, but how to choose that location is not at all clear to me. Can somebody please explain the HOW and the WHY you pick a particular point of memory in a way that any competent History major can understand?

    Also, if anybody has a simple way of getting around that horrible BUTTON command configuration, I'd really like to learn how to do that.

    Thanks for all the help you all have given. Perhaps I'll write something like "PICBASIC PRO For The Criminally Stupid" and put it on line after this is all done.

    Jim

  • #2
    Hi Jim,
    I might be mistaken but looking at the datasheet for the 12F609 it seems to me that it does not have any internal EEPROM - which is what the READ/WRITE commands are designed to target. If I'm correct it means that you won't be able to achieve what you want with that particular device, you need to A) choose a device that has internal EEPROM, B) use an external EEPROM (or other type of non volatile memory) and interface it to the '609 or C) use a device which can write to its own FLASH memory. I'd highly recommend option A as that is - by far - the simplest.

    By "where to put the read/write commands in memory" I suppose you mean where in your program to put them (?)
    The answer to that, as almost always, is that it depends. I know that sucks but that's how it is. EEPROM is farily slow to write and it's a type of memory that wears out as you write to it. This means that its NOT recommended to keep writing to it over and over and over again (like what would happen if you put the WRITE command in your main loop). Depending on how often your variable Changes it might be OK to write it to EEPROM every time it Changes but if it changes often you need to come up with a way to "trig" the WRITE operation. If it's a setting that user changes then provide a save button, if it's something else you might need some way to detect power loss and execute the WRITE command.

    If, on the other hand, you mean where in memory to store those variables you mention then that's totaly up to you. EEPROM is a separate chunk of memory and the compiler does not put code or variables there. Code ends up in FLASH, variables in RAM.

    I hope that clears up a bit of the confusion.

    /Henrik.

    Comment


    • #3
      Thanks, Henrik. I understand the limitation of the number of read/write cycles. I just want to be sure that the controlled device is returned to the same state on power up as on power down. The only time the states change is when the user pushes a button to go from one state to the other ... perhaps once a week or so.


      However, the data sheet (page 4) shows that there are 64 bytes of "Data Memory" (SRAM). Since my variables can only have binary values (0 or 1) I presumed (horrible to do) that I could use this data memory to keep my information. Evidently it doesn't work that way.

      Click image for larger version

Name:	Capture 50 27-Jun 12.07.jpg
Views:	53
Size:	87.2 KB
ID:	7754

      But then what is Data Memory for?

      Thanks,

      Jim

      Comment


      • #4
        Hi Jim,
        No, SRAM means Static RAM but static in this case means that the memory, unlike DRAM, doesn't need to be constantly refreshed. The "data memory" in the PIC will loose its content when there's no power.

        If the variable(s) you want to store only changes "ever so often" then just write it to EEPROM whenver the code that changes that variable is executed. On startup, read it from EEPROM into your variable.

        /Henrik.

        Comment


        • #5
          Allllll Rightie then ... I'll simplyforce a power-up configuration and go from there ... no sense cobbling up the works with something that was a small feature, but not necessary..

          Any help with how to implement a "button" routine without using the pretty clumsy BUTTON command in PBP3?

          Jim

          Comment


          • #6
            Depends on a lot of things, like:
            * Action on make, action on break,
            * Is it OK if the program "hangs" if the user keeps holding down the button, or should it continue to do it's thing
            * Should holding down the button "auto repeat" the action
            * What else is the program doing

            Anyway, here's something simple:
            Code:
            BTN  VAR GPIO.0
            LED1 VAR GPIO.1
            LED2 VAR GPIO.2
            
            Cnt VAR BYTE
            
            ANSEL    = 0         ' All pins are digital I/O
            TRISIO.0 = 1         ' Pin for the button is an input (default)
            TRISIO.1 = 0         ' Pin for the LED is an output
            TRISIO.2 = 0         ' Pin for the LED is an output
            
            Main:
              IF BTN THEN
                Cnt = Cnt + 1    ' Do something
                TOGGLE LED       ' Do something else
                Pause 10         ' Wait a bit for contact bounce on make
                While BTN : WEND  ' Wait for the button to be released
                Pause 10         ' Wait a bit for contact bounce on break
              ENDIF
            
              IF Cnt = 4 THEN
                TOGGLE LED2
                Cnt = 0
              ENDIF
            
            Goto Main
            Last edited by Henrik Olsson; 06-28-2019, 06:32 AM.

            Comment


            • #7
              Hey Henrik, Where is "UP" coming from?
              Dave Purola,
              N8NTA
              EN82fn

              Comment


              • #8
                Damn, my first idea was an UP/DOWN counter with two buttons. I changed my mind but apparently not the code. Thank you for catching that!
                I've changed it in the code above.

                /Henrik,

                Comment


                • #9
                  A quick answer to a previous question, if your selected PIC has 256 Bytes of EEPROM, Addresses are from 0 to 255. It makes sense to start at the bottom:
                  Code:
                  WRITE 0, 1
                  Writes a value of "1" to EEPROM Address "0". Another example:
                  Code:
                  WRITE 5, 123
                  Writes a value of "123" to EEPROM Address "5". To Read values from EEPROM:
                  Code:
                  StateVal VAR BYTE
                  READ 5, StateVal
                  Reads the value in EEPROM Address "5" and puts it in the Variable StateVal.
                  Hope this helps.
                  We can crack this cotton PIC'n thang!

                  Comment

                  Working...
                  X