Using memory

As well as static RAM, the PIC18 microcontroller has program memory, or Flash memory, and also EEPROM. Static RAM is usually quite limited on PIC18 controllers and the data stored there is lost if the MCU loses power. The key attribute of RAM is that it has an unlimited endurance for being rewritten. The Flash program memory is usually quite a bit larger and is retained, even with the power off. It does, however, have a very limited number of erase-write cycles that it can endure. EEPROM is also available, in even smaller amounts than static RAM and is non-volatile. It has a much better endurance than Flash, but any particular cell is still limited to about 100000 rewrites. It is a good place to put variables that you change occasionally but must retain when the power is off. Calibration or configuration data may be an example of the type of data that could be stored in EEPROM. The registers that configure, control and monitor the microcontroller's peripheral devices appear as particular locations in the static RAM memory.


In FlashForth, 16-bit numbers are fetched from memory to the stack by the word @ (fetch) and stored from TOS to memory by the word ! (store). @ expects an address on the stack and replaces the address by its contents. ! expects a number (NOS) and an address (TOS) to store it in. It places the number in the memory location referred to by the address, consuming both parameters in the process.


Unsigned numbers that represent 8-bit (byte) values can be placed in character-sized cells of memory using c@ and c!. This is convenient for operations with strings of text, but is especially useful for handling the microcontroller's peripheral devices via their special-function file registers. For example, data-latch register for port B digital input-output is located at address $ff8a and the corresponding tristate-control register at address $ff93. We can set pin RB0 as an output pin by setting the corresponding bit in the tristate control register to zero.


%11111110 $ff93 c! \fbox{$\hookleftarrow$} ok<$,ram>


and then set the pin to a digital-high value by writing a 1 to the port's latch register


1 $ff8a c! \fbox{$\hookleftarrow$} ok<$,ram>


If we had a light-emitting diode attached to this pin, via a current-limiting resistor, we should now see it light up as in the companion hardware tutorial[1]. Here is what the gtkterm window contains after turning the LED on and off a couple of times.

FlashForth V3.8

%11111110 $ff93 c!  ok<$,ram>
1 $ff8a c!  ok<$,ram>
0 $ff8a c!  ok<$,ram>
1 $ff8a c!  ok<$,ram>
0 $ff8a c!  ok<$,ram>
FlashForth allows you to very easily play with the hardware.




Subsections
Peter Jacobs 2013-06-12