pic 18f4620 notes - hamstack.com 18f4620 notes © robert ralston kj6hfr march 2012 purchasing the...

68
PIC 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this specific PIC microcontroller. To understand basic things about the chipʼs oscillator, both internal and external, required breadboarding an 18F4620 since I did not want to cut traces or remove the crystal from my already-assembled HamStack board. A minimal circuit allowed simple experiments, described in these notes, demonstrating basic things about this chip. A few experiments toward the end can be run on the HamStack board. Almost all of the experiments do not require writing and compiling any code. In some experiments a tiny amount of “hand-assembled” code is used. Some code at the end is Swordfish. Here there is no useful project design for the radio amateur. One main goal was to learn to read and understand the voluminous Data Sheet describing this chip. These experiments helped me do that. I hope others will benefit likewise. To learn, I will sacrifice chips, into smoke, but nothing bad happened during these experiments, although the code protection experiment initially seemed like a one-way street. My disclaimer: your mileage may vary. page 1 of 68

Upload: buidiep

Post on 12-Apr-2018

218 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

PIC 18F4620 Notes© Robert Ralston

KJ6HFRMarch 2012

Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this specific PIC microcontroller.

To understand basic things about the chipʼs oscillator, both internal and external, required breadboarding an 18F4620 since I did not want to cut traces or remove the crystal from my already-assembled HamStack board.

A minimal circuit allowed simple experiments, described in these notes, demonstrating basic things about this chip. A few experiments toward the end can be run on the HamStack board.

Almost all of the experiments do not require writing and compiling any code. In some experiments a tiny amount of “hand-assembled” code is used. Some code at the end is Swordfish.

Here there is no useful project design for the radio amateur.

One main goal was to learn to read and understand the voluminous Data Sheet describing this chip. These experiments helped me do that. I hope others will benefit likewise.

To learn, I will sacrifice chips, into smoke, but nothing bad happened during these experiments, although the code protection experiment initially seemed like a one-way street. My disclaimer: your mileage may vary.

page 1 of 68

Page 2: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Microcontrollers can have many hardware configurations: oscillator, watch-dog timer, reset conditions, and so forth, normally chosen for a particular design, and then not, or rarely, changed.

The HamStack board was designed with a 10 MHz crystal as its oscillator, requiring an oscillator configuration of HS (High-Speed Crystal/Resonator), or possibly HSPLL. Some of the other eight possible oscillator types cannot be used without removing the crystal.

To investigate other possible oscillator configurations, an absolutely minimum circuit, powered from the PICkit™2 programmer, was breadboarded

The PICkit™2 programmer is limited to 100 mA, through its USB connection. Microchip recommends supplying 25 mA or less to the target chip.

Most experiments were done with the PICkit™2 set to supply 5.0 volts. The 18F4620 typically draws a maximum of 25 mA when running at its highest possible frequency. Its only load is a single LED.

page 2 of 68

Page 3: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Minimum Circuit Hookup: PICkit™ 2 programmer & 18F4620the programmer supplies limited power to the circuit.

page 3 of 68

VDD

ScopeSig Gen

VPP/MCLRVDDVSS

ICSPDAT/PGDICSPCLK

AUX

MicrochipPICkit™ 2

123456

10K Ω

+5

PIC 18F4620

+5What is connected to pin 13 depends on the experiment.

any LED should work; I used an Avago QLMP-LB98 blue 550 mcd so I could see very short light pulses. These LEDs are very bright.

1K Ω

VSS

VPP PGDPGC

CLKO

VDD

RD1

OSC1

NC

Page 4: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Basic use of the PICkit 2 stand-alone application has been described (Getting Started with the HamStack Microcontroller Project Platform, revision 1.2, pages, © Sierra Radio Systems, 2011, pages 31-32, 40-41).

The majority of the experiments use the Configuration Word Editor, opened by clicking on Configuration.

In this editor, we can not only see the configuration bit settings, we can edit them, do a Save, and then Write those changes out to the 18F4620. From here, we can change the oscillator, the watch-dog timer, and other settings, i.e. the Special Features of the CPU (Data Sheet, page 249 - 259).

page 4 of 68

Page 5: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Are any of the many possible configuration bit changes destructive?

Yes, certainly in the sense of having a non-operational circuit (selecting for an external oscillator which does not exist) or a circuit which behaves erratically (having the watch-dog timer on when you need to have it off).

But is hardware damage possible just through configuration bit changes?

(Data Sheet page 336) “Note 1: In RC oscillator configuration, the OSC1/CLKI pin is a Schmitt Trigger input. It is not recommended that the PIC® device be driven with an external clock while in RC mode”.

Would doing so generate internal currents too high and smoke the oscillator part of the chip? I donʼt know, you tell me.

See the Appendix for the exact documents used. A few references are given but read the Data Sheet as much as possible.

Hardware

HamStack board from Sierra Radio Systems (most experiments use the 18F4620 but not the board itself). Later experiments use the board.

PICkit™ 2 programmer ($35 from Microchip Direct; I have no experience with the many clone programmers).

Additional PIC 18F4620 chips (not required but useful from Digi-Key).

Breadboard, wire, resistors, LEDs, capacitors, the usual stuff.

Scope, voltmeter, signal generator (whatever you have will work; all of my equipment is fairly old)

page 5 of 68

Page 6: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

FIRST STEPS

Assemble the minimum circuit with nothing attached to OSC1 pin 13. Open the PICkit 2 stand-alone application.

"

The programmer can and should auto-detect the PIC18F4620.

VDD will come from the programmer. Make sure it is set to 5.0 V.

You can use that VDD checkbox as a power switch, to turn power on and off to the circuit.Erase the chip.

"

Remember: you can save any experiment by Exporting all of the chip contents (Program Memory, EEPROM memory, Configuration bits, and User IDs) to a hex file."If you try an experiment and it does not work, make sure that the VDD box is checked and the voltage set to 5.0 volts. Try toggling the /MCLR box.

page 6 of 68

Page 7: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

# EXPERIMENTS page

1 RC Oscillator with CLKO; (min. circuit) 8

2 Master Clear /MCLR 10

3 External Clock with CLKO; (min. circuit) 12

4 Internal Oscillator with CLKO; (min. circuit) 14

5 Internal Oscillator with CLKO; changing the frequency with bit-set instructions; (min. circuit)

15

6 Internal Oscillator with CLKO; changing the frequency with byte-set instructions; (min. circuit)

18

7 Internal 32 MHz PLL Oscillator with CLKO; (min. circuit) 19

8 One Million NOPs (minus 4) with CLKO; (min. circuit) 21

9 Hello World, blinking an LED; (min. circuit) 24

10 Hello World, blinking an LED with an adjusted delay loop; (min. circuit) 26

11 Adjusting Hello World with the Watch-Dog timer; (min. circuit) 28

12 Waking from SLEEP with the WDT; (min. circuit) 31

13 INTRC and INTOSC/256; (min. circuit) 35

14 Change the Brown-Out trip points; (min. circuit) 37

15 disable BOR; (min. circuit) 39

16 code protection 40

17 Read Program Memory using assembly code; HamStack 43

18 Read the Device IDs using ten instructions; HamStack 45

19 Read the Device IDs using Swordfish; HamStack 48

20 Tweak the Internal Oscillator Frequency Using Swordfish; (min. circuit) 50

21 Write User IDs with Swordfish, pk2cmd 53

Appendix 61

clips of 8, 12b, & 20 at: http://www.youtube.com/watch?v=3X9EMJxlJgQ

page 7 of 68

Page 8: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #1: RC Oscillator with CLKO (min. circuit)

WHAT:" Drive the 18F4620 with an RC network and measure its frequency.

HOW:1. Add a resistor (3kΩ ≤ R ≤ 100kΩ) from pin 13 (OSC1) to VDD.2. Add a cap bigger than 20pF between pin 13 and GND.

3. Scope pin 14 (CLKO)4. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1111. Save, Write.

NOTES:

A bit pattern of 1111 (or 11xx) sets the 18F4620 oscillator to the mode External RC oscillator, CLKO function on RA6 (pin 14).

Six different bit patterns give this same configuration. There are nine other possible bit patterns, for nine other oscillator configurations.

Out of ten possible oscillator configurations, three of them make pin 14 CLKO, where we can measure a frequency (or clock something external).

Putting a scope on pin 14, CLKO, is useful just to see if the chip is “alive”.

page 8 of 68

+5

pin 13 (OSC1)

These are easy components to add, right next to the power pins.

Page 9: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Except for the four power pins, every 18F4620 pin is multi-function.

Pin 14 can be:"" " " OSC2"" external crystal or resonator connected" " " CLKO"" oscillator frequency divided by four FOSC/4" " " RA6" " I/O port A, bit 6

The 18F4620 needs a basic oscillator, either external or internal. That specific frequency is then divided by four to generate FOSC/4, the “instruction cycle” frequency.

During FOSC/4, the next instruction is fetched and the previously fetched instruction is executed.

For example, if the internal oscillator was 1MHz, FOSC/4 would be 250 kHz, meaning that the chip could process 250k instructions per second, assuming that these instructions, as most are, one word (16 bit) instructions.

Values tried were 33kΩ and 47pF, giving a measured FOSC/4 or approximately 110 kHz. This means that the RC frequency driving the chip was four times that or 440 kHz.

References:

Data Sheet pages 25, 57, and 250

page 9 of 68

Page 10: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #2: Master Clear /MCLR

WHAT:" Enable and disable MCLRE, the Master Clear enable bit.

HOW:1. Keep the RC circuit from experiment #1.2. Scope pin 14 (CLKO)3. Using Configuration Word Editor, set and clear bit 15 of CONFIG3. A zero disables this function on the chip whereas a one enables it. Save. Write.

NOTES:

“The MCLR pin provides a method for triggering an external Reset of the device. A Reset is generated by holding the pin low.” (Data Sheet page 43)

If the Master Clear function is disabled, then pin 1 can be used as an input-only pin, RE3, PORTE, bit 3.

Both the minimum circuit in these notes and the HamStack board have the MCLR pin, pin 1, tied high through a resistor, both so that this pin does not float low generating an unexpected reset and, more importantly, so that pressing the HamStack reset button does not short the power supply, creating smoke in your shack.

When the Master Clear function is enabled, the PICkit™2 programmer can bring pin 1 low and “Hold Device in Reset”, a menu choice under Programmer, also available using a checkbox.

page 10 of 68

Page 11: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

With bit 15 set to enable Master Clear, monitor CLKO on a scope. Toggle the checkbox, /MCLR, on and off.

Change the Master Clear enable bit to zero and do that again.

References:

Data Sheet pages 43, and 253.

page 11 of 68

toggle this checkbox, on and off

Page 12: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #3: External Clock with CLKO (min. circuit)

WHAT:" Drive the 18F4620 with an external clock and measure FOSC/4.

HOW:1. Remove the RC network from experiment #1 and 2.2. Drive pin 13 (OSC1) with a signal generator; scope it.3. Scope pin 14 (CLKO).4. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 0100. Save, Write.

NOTES:

A bit pattern of 0100 sets the 18F4620 oscillator to the mode EC oscillator, CLKO function on RA6 (pin 14).

I drove pin 13 from an ancient Heathkit IG-1275 with a square wave at approximately 1 MHz, being careful to keep an amplitude below five volts. It would be safer to put a TTL buffer between the sig gen and the 18F4620 input.

I also tried a much slower square wave from an audio generator.

With a dual channel scope we can directly see the relationship between the oscillator and FOSC/4.

page 12 of 68

Page 13: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

References:

Data Sheet page 24, 250.

page 13 of 68

external clock, pin 13

FOSC/4, pin 14

Page 14: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #4: Internal Oscillator with CLKO (min. circuit)

WHAT:" Drive the 18F4620 from its internal oscillator and measure FOSC/4.

HOW:1. Remove the External Clock from experiment #3.2. Scope pin 14 (CLKO).3. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001. Save, Write.

NOTES:

A bit pattern of 1001 sets the 18F4620 oscillator to the mode Internal Oscillator block, CLKO function on RA6 (pin 14), port function on RA7 (pin 13).

Pin 13 is no longer used as an input from an external clock, RC network, crystal, or resonator. Now it is RA7, I/O Port A, bit 7. If we did not need to see CLKO, pin 14 could likewise be configured as RA6, I/O Port A, bit 6.

The default speed of the Internal oscillator is 1MHz. On my scope, CLKO on pin 14 is about 4 µsec, or 250 kHz (FOSC/4).

If I read the specs correctly, the internal oscillator has a typical accuracy of +/- 1%, or plus/minus 10kHz. The frequency of the internal oscillator can be tweaked using one of the Special Function registers (SFRs), OSCTUNE.

References:

Data Sheet pages 26-30, (page 30 note 3), and 250.

page 14 of 68

Page 15: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #5: Internal Oscillator with CLKO; changing the frequency with bit-set instructions; (min. circuit)

WHAT:" Change the frequency of the internal oscillator and measure FOSC/4.

HOW (part a):1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001. Save, Write.4. Verify a 250 kHz signal on pin 14, CLKO.5. In the Program Memory Area, modify the first word to 8AD3. Write.

NOTES:

Measure CLKO; it should be 1 µsec or 1 MHz. We just set one bit in one of the Special Function Registers, OSCCON, something we would normally do in our BASIC or C program.

page 15 of 68

Page 16: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Living at the top of static RAM (Data Memory) are 128 byte size registers (also called files) called SFRs, or Special Function Registers. All of the SFRs are “memory-mapped.”

The SFRs control many functions of the 18F4620. Examples:

" TRISA, TRISB, ..." " Data Direction registers controlling whether pins " " " " " are input or output.

" PORTA, PORTB, ..."" registers holding the level of the pins.

" LATA, LATB, ..." " Data Latch registers holding the value after a write to " " " " " a PORT

" OSCCON" " " the Oscillator Control Register, three bits of which " " " " " control the internal oscillator frequency."

Many of the SFRs power up with predetermined values. For example, the Data Direction registers, TRIS, are all ones, setting I/O pins as outputs.

The three bits in OSCCON controlling the internal frequency are called the Internal Oscillator Frequency Select bits: IRCF2 - IRCF0. With eight bit patterns, there are eight possible frequencies (screen shots from Data Sheet page 30).

On power up, these bits are 100, for 1 MHz (FOSC/4 = 250 kHz).

page 16 of 68

default power-up value

Page 17: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

HOW (part b):

1. In the Program Memory Area, modify the second word to 88D3. Write.

Measure CLKO; it should be .5 µsec or FOSC/4 = 2 MHz, meaning that the internal oscillator is running at 8 MHz. We just set two bits in OSCCON, so that the IRCF bits = 111. Each word instruction is a BSF (Bit Set f) instruction, to set a single bit in an 8-bit register.

Likewise, individual bits can be cleared with BCF (Bit Clear f) instructions.

Sometimes it is better to modify some or all bits of an 8-bit register all at once, using byte oriented instructions, used in the next experiment.

Values in static RAM are apparently OK until VDD drops below 1.5 volts (RAM Data Retention Voltage). When powered off, the 18F4620 loses all values in Data Memory, all values in the SFRs.

References:

Data Sheet pages 30, 324.

page 17 of 68

Page 18: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #6: Internal Oscillator with CLKO; changing the frequency with byte-set instructions; (min. circuit)

WHAT:" Change the frequency of the internal oscillator and measure FOSC/4.

HOW:1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001. Save, Write.4. Verify a 250 kHz signal on pin 14, CLKO.5. In the Program Memory Area, modify the first two words to 0E70 6ED3. Write.

NOTES:

FOSC/4 measures as 2 MHz, indicating an internal oscillator frequency of 8 MHz.

The first instruction, 0E70 (MOVLW) moves a literal value, 70, into the W register, an SFR used much like an accumulator in some microprocessors. The second instruction word, 6ED3 (MOVWF), moves that byte 70 into the OSCCON register.

Since 07 hex is 01110000, you can see that the upper nibble sets the internal oscillator for 8 MHz.

References:

Data Sheet pages 30, 293.

page 18 of 68

Page 19: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #7: Internal 32 MHz PLL Oscillator with CLKO (min. circuit)

WHAT:" Turn on the PLL to maximize the internal oscillator frequency and measure " " FOSC/4.

HOW:1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001. Save, Write.4. Verify a 250 kHz signal on pin 14, CLKO.5. In the Program Memory Area, modify the first three words to 0E70 6ED3 8C9B.

Write.

NOTES:

Pin 14, CLKO, has FOSC/4 = approximately .125 µsec or 8 MHz, making the internal oscillator frequency 32 MHz.

The first two instruction words set the internal oscillator to 8 MHz, as in experiment #6. The third instruction word, 8C9B, sets bit 6, the PLL enable bit (PLLEN) in another SFR, OSCTUNE, the Oscillator Tuning Register (screen shot Data Sheet page 27).

page 19 of 68

Page 20: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

The PLLEN bit is set to zero, disabled, on power-up.

If the internal oscillator is already running at either 4 or 8 MHz, then setting this bit will turn on the PLL, running the internal oscillator at 32 MHz.

If the internal oscillator is not running at either 4 or 8 MHz, then enabling the PLLEN bit will have no effect.

References:

Data Sheet pages 27, 30, 293.

page 20 of 68

Page 21: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #8: One Million NOPs (minus 4) with CLKO (min. circuit)

WHAT:" Using two different internal oscillator frequencies, changing back and " " forth, show that one million NOPs are being executed.

HOW:1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Erase the 18F4620. 4. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001. Save, Write.5. Verify a 250 kHz signal on pin 14, CLKO.6. In Program Memory, in the first two word locations, enter 0E00 6ED3.7. In Program Memory, in the last two word locations, enter 0E40 6ED3. Write.

page 21 of 68

Page 22: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

NOTES:

CLKO on pin 14 alternates between 125 µsec, 8 kHz, and 4 µsec, 250 kHz.

Each frequency lasts for about four seconds, switching then to the other frequency. OSCCON is loaded for a 31 kHz oscillator at the beginning of flash, and then loaded for a 1 MHz oscillator at the end of flash.

Using round numbers, in the 18F4620 Data Sheet, “User Memory Space” is shown as two million bytes, one million words.

“On-Chip Program Memory”, flash, is sixty four thousand bytes, thirty two thousand words.

Presumably, all of that unimplemented-in-flash “User Memory Space” is there for future chip versions containing more flash memory.

Every instruction is word length, sixteen bits (a few are 2 words). Therefore, the maximum number of instructions which can be loaded into flash is 32 k. If the unimplemented memory space could be programmed, it could hold 968 k instructions (one million words minus 32 k words).

But in fact the unimplemented “User Memory Space” “looks” as if it is programmed, in the sense that any read of it will return all zeroes. Any sixteen bit instruction word, all zeros, is a NOP.

Our “real” memory, flash, except for the four instructions we entered, is all ones, 0xFF in hex. Any sixteen bit instruction word, all ones, is also a NOP.

The program counter, PC, is twenty one bits wide, so that it can address not only the flash memory but the entire “User Memory Space”.

Normally it would not do so because we would have a C or BASIC program which “contains” addressing within that 32 k words of flash. Indeed if we did not do so, then the processor would almost certainly wind up “in the weeds”, requiring a manual reset if one did not come from the watch-dog timer. (On the other hand, if it did fetch and execute from the unimplemented memory space, it would eventually wrap around to the beginning, fetching and executing from the reset vector, so maybe it would “cure” itself).

In this experiment, the PC counts through, fetches, and executes, all one million instruction words, all NOPs except for the four words we entered.

page 22 of 68

Page 23: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

The two instruction words we entered at the beginning of flash set the internal oscillator for, in round numbers, 32 kHz, so that FOSC/4 = 8 kHz. (The Data Sheet states this lowest frequency as 31 kHz or 31.25 kHz, so that FOSC/4 would be about 7.8 kHz).

At a rate of 8 kHz, the processor is fetching and executing eight thousand single word instructions per second, taking about 4 seconds to execute all instructions in flash.

The last two instruction words we entered at the end of flash set the internal oscillator for 1 MHz, so that FOSC/4 = 250 KHz. At a rate of 250 kHz, the processor is fetching and executing 250 k instructions per second, taking about 4 seconds to execute all of the read-back-as-NOPs. from the remaining 968 k words of User Memory Space (one million words minus 32 k words of flash).

References:

Data Sheet pages 30, 53, 293.

page 23 of 68

32k words

968k words

flash, executing at 8k instructions per second, for four seconds.

unimplemented memory, executing at 250k instructions per second, for four seconds.

Page 24: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #9: Hello World, blinking an LED (min. circuit)

WHAT:" Blink a “Hello World” LED, using 968 k NOPs as a delay loop

HOW:1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Erase the 18F4620. 4. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001. Save, Write.5. Verify a 250 kHz signal on pin 14, CLKO.6. In Program Memory, in the first two word locations, enter 9295 828C.7. In Program Memory, in the last two word locations, enter 928C. Write.

page 24 of 68

Page 25: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

NOTES:

The LED will blink on briefly, then off for about four seconds.

The first instruction, 9295, clears bit 1 of TRISD. With TRISD.1 = 0, bit 1 of PORTD, RD1, pin 20, is an output.

The second instruction, 828C, sets bit 1 of LATD. With LATD.1 = 1, pin 20 goes high, turning the LED on.

Then 32 k NOPs, the remainder of flash, are executed with an FOSC/4 = 250 kHz in approximately 1/8 second.

The very last instruction in flash, 928C, is LATD.1 = 0, making pin 20 go low, turning the LED off.

968k NOPs are then executed from the unimplemented memory space, taking about four seconds, before wrapping around to the beginning of flash and turning the LED back on.

References:

Data Sheet pages 53, 62, 276, 279, 295.

page 25 of 68

LED on for about 1/8 second

LED off for about 4 seconds.968k NOPs

32k words

Page 26: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #10: Hello World, blinking an LED with an adjusted delay loop (min. circuit)

WHAT:" Blink a “Hello World” LED, using 968 k NOPs as an “adjusted” delay loop " " by altering the internal oscillator frequency.

HOW:1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Erase the 18F4620. 4. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001. Save, Write.5. Verify a 250 kHz signal on pin 14, CLKO.6. In Program Memory, in the first four word locations, enter 9295 828C 0E20 6ED3.7. In Program Memory, in the last three word locations, enter 0E70 6ED3 928C. Write.

page 26 of 68

Page 27: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

NOTES:

The delay loop of 968k NOPs in unimplemented memory is crudely adjusted by changing the internal oscillator frequency, slower for the LED on time and faster for the LED off time. This adjustment makes the on and off times approximately equal.

With the oscillator at 250 kHz for execution from flash, those 32 k words would execute in about 1/2 second (instruction cycle frequency, FOSC/4 = 62.5 kHz).

With the oscillator at 8 MHz for execution from unimplemented memory, those 968 k words of NOPS would execute in about 1/2 second (instruction cycle frequency, FOSC/4 = 2 MHz).

My HamStack board operates from a 10 MHz external crystal, giving an instruction cycle frequency, FOSC/4 = 2.5 MHz.

This experiment could be done using the HamStack board but it would be necessary to eliminate CLKO and use the internal oscillator instead of the crystal.

To do so, Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1000. Save, Write.

References:

Data Sheet pages 30, 62, 276, 279, 293.

page 27 of 68

Page 28: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #11: Adjusting Hello World with the Watch-Dog timer (min. circuit)

WHAT:" Blink a “Hello World” LED, using 968 k NOPs as a delay, but shortened by " " the watch-dog timer.

HOW:1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Erase the 18F4620. 4. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001. Save, Write.5. Verify a 250 kHz signal on pin 14, CLKO.6. In Program Memory, in the first two word locations, enter 9295 828C.7. In Program Memory, in the last word location, enter 928C. Write. This is the same

configuration as in experiment #9.

page 28 of 68

Page 29: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

8. Verify that the LED has a short blink and then is off for about 4 seconds.9. Using Configuration Word Editor, set bits 12- 9 in CONFIG2 to 0111. Save, Write.

page 29 of 68

screen shots from Data Sheet page 252

Page 30: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

NOTES:

The LED off time is now much shorter. Approximately every 1/2 second after code starts to execute, from the beginning of flash, the WDT times out causing a reset.

With an FOSC/4 = 250 kHz, all of the 32 k words of flash executes in approximately 1/8 second: the LED is turned on and off.

In another 3/8 second, while executing NOPs from, maybe, 10% of the unimplemented memory, the WDT causes a reset, causing code execution from the beginning of flash, and restarting the WDT.

This type of WDT reset is equivalent to a Master Clear reset or a software reset instruction, different from a power-on or brown-out reset. The WDT reset leaves Data Memory register contents (including the SFRs) unchanged, just loading the program counter to start at the reset vector, 0x000000, the beginning of flash.

The WDT is driven from an internal 31 kHz oscillator called INTRC. This oscillator can vary by more than 9 kHz, from 26.5 to 35.9 kHz, so your results may vary.

On my PC when the WDT Postscaler bits were set to 0101 or below (128 to 4 milliseconds) the WDT timed out before the LED could be turned off.

Postscaler values of 0110 to 1001 (256 milliseconds to 2 seconds) were the best values for seeing the WDT in action.

Using the Configuration Word Editor it is easy to enable and disable the WDT, by changing bit 8 in CONFIG2. When this bit is set, 1, the WDT is enabled. When this bit is cleared, 0, the WDT is off.

page 30 of 68

Page 31: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #12: Waking from SLEEP with the WDT (min. circuit)

WHAT:" (Part a) Wake a sleeping 18F4620 with the dog." " (Part b) Simulate a more realistic environment.

HOW (Part a):1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Erase the 18F4620. 4. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001.5. Using Configuration Word Editor, set bits 12 - 9 in CONFIG2 to 1011. Save. Write.6. Verify a 250 kHz signal on pin 14, CLKO.7. In the Program Memory Area, enter four instruction words: 9295, 928C 0003 828C.

Write.

page 31 of 68

Page 32: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

NOTES:

First the LED is off and CLKO shows no oscillator for about 8 seconds; then the LED is on and CLKO shows an oscillator for about 4 seconds.

The first instruction, 9295, makes PORTD.1 an output. The second instruction, 928C, makes PORTD.1 low, turning the LED off. The third instruction, 0003, puts the chip to SLEEP.

The SLEEP instruction turns the main internal oscillator (default frequency = 1 MHz, FOSC/4 = 250 kHz) off and advances the program counter to point to the instruction following the SLEEP instruction.

After the SLEEP instruction, the WDT, driven from the INTRC oscillator which is running, starts counting and times out after approximately 8 seconds.

When the WDT times out, the chip does not do a reset and start executing at the reset vector, like in experiment # 11, Instead, the next instruction after the SLEEP instruction is executed, 828C, making PORTD.1 high, turning the LED on.

Now the rest of the NOPs in flash and then the 968k NOPs in unimplemented memory execute, taking about four seconds. After wrapping around to the beginning of flash, the LED is turned off and the chip put into SLEEP mode.

page 32 of 68

WDT wakes chipmain oscillator on

LED = on1 meg NOPs executed for 4 seconds

LED = offchip put to SLEEP main oscillator off

WDT counting for 8 seconds INTRC oscillator on

Page 33: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

A more realistic simulation would turn the LED on briefly, execute a couple of hundred or thousand instructions, and then put the chip back to sleep.

HOW (Part b):1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Erase the 18F4620. 4. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001.5. Using Configuration Word Editor, set bits 12 - 9 in CONFIG2 to 1011. Save. Write.6. Verify a 250 kHz signal on pin 14, CLKO.7. At the beginning of Program Memory, enter two instruction words: 9295 828C.8. At Program Memory address 4000, enter four instruction words: 928C 0003 EF01

F000. Write.

page 33 of 68

Page 34: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Real code in a power saving design would probably not execute 8 k instructions before going back to sleep. I used this bigger number so it was easy to see the LED flash and CLKO.

8 k instructions at an FOSC/4 = 250 kHz would execute in about 32 milliseconds.

The WDT still functions because it is driven by the INTRC oscillator, even with the main oscillator off.

References:

Data Sheet pages 62, 250, 252, 276, 279, 288, 302.

page 34 of 68

RD.1 made output

SLEEPmain oscillator off

WDT starts counting

8 seconds

WDT wakes chipmain oscillator on

GO TO

LED = on8 k NOPsLED = off

Hereʼs where the real code would go.

Page 35: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #13: INTRC and INTOSC/256 (min. circuit)

WHAT:" Measure the difference between INTRC and INTOSC/256 using CLKO.

HOW:1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Erase the 18F4620. 4. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001. Write.5. Verify a 250 kHz signal on pin 14, CLKO.6. In the Program Memory Area, enter two instruction words: 0E00 6ED3. Write.7. Measure the frequency of CLKO.

8. In the Program Memory Area, enter a third instruction: 8E9B. Write.9. Measure the frequency of CLKO.

NOTES:

The first two instruction words set OSCCON for its lowest frequency, in the neighborhood of 31 to 32 kHz.

This low frequency can come from either the internal INTRC (which drives the WDT) or from the INTOSC, internal 8 MHz oscillator, divided by 256.

The INTSRC, Internal Oscillator Low-Frequency Source Select bit (bit 7 in the OSCTUNE register) has a default power-up value of zero. This means “31 kHz device clock derived directly from INTRC internal oscillator.”

page 35 of 68

Page 36: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

INTRC measured, on a frequency counter connected to CLKO, as 8.17 kHz. This FOSC/4 corresponds to an oscillator frequency of 32,680 Hz.

Adding the third instruction, 8E9B, sets the INTSRC bit (seventh bit in OSCTUNE), selecting the 8 MHz oscillator divided by 256, INTOSC/256,

Mine measured, on a frequency counter, as 7.79 kHz. This FOSC/4 corresponds to an oscillator frequency of 31,160 Hz, or, times 256, an oscillator frequency of 7,976,960 Hz.

Weird. The documentation (Data Sheet page 27) lists INTRC as the lower of the two possible frequencies, 31 kHz versus 31.25 kHz.

Below is a calculation of possible WDT delays, from minimum to maximum. 32 kHz was chosen as “typical” because it gives the value of 131.072 seconds listed on Data Sheet page 258.

References:

Data Sheet pages 27, 30, 62, 250, 258, 279, 293.

page 36 of 68

Page 37: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #14: Change the Brown-Out trip points (min. circuit)

WHAT:" Change and observe, using CLKO, some BOR points.

HOW:1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Erase the 18F4620. 4. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001. Write.5. Verify a 250 kHz signal on pin 14, CLKO.6. Using Configuration Word Editor, set bits 4 - 3 in CONFIG2 to 01. Write.7. Using the PICkit™2 application, crank down VDD until CLKO flatlines.

page 37 of 68

Crank VDD up and down here.Minimum voltage is 2.5

resolution is .1 volt

Page 38: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Microchip states that the voltage which the PICkit™2 programmer can supply to the circuit depends on the specific USB port to which it is connected.

The programmer can be calibrated using the “Calibrate VDD & Set Unit ID...” under the Tools menu., while measuring the actual voltage supplied with a voltmeter.

My programmer supplies 4.7 volts maximum when the VDD reported is 4.7 - 5.0 volts. Settings from 4.7 down to 2.5 volts track well.

The default power-up Brown-out Reset Voltage is 2.11 volts, set by bits 4 -3 in Config2 as 11.

Setting those two bits to 01, the flatline voltage showed as 4.3 volts. When those bits were changed to 10, the flatline voltage showed as 2.8 volts.

References:

Userʼs Guide page 12, Data Sheet pages 251, 324.

page 38 of 68

screen shots from Data Sheet pages 251 and 324.

Page 39: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #15: disable BOR (min. circuit)

WHAT:" Change and observe, using CLKO, some BOR points.

HOW:1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Erase the 18F4620. 4. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001. Write.5. Verify a 250 kHz signal on pin 14, CLKO.6. Using Configuration Word Editor, set bits 4 - 3 in CONFIG2 to 10. Write.7. Using the PICkit™2 application, crank down VDD until CLKO flatlines at 2.8 volts.8. Crank VDD back up to 5.0 volts.6. Using Configuration Word Editor, set bits 2 -1 in CONFIG2 to 00. Write.7. Using the PICkit™2 application, crank down VDD to 2.5 volts. No flatline.

NOTES:

Changing bits 2 -1 to 00 disabled any BOR reset. Changing these bits to 11 enables the brown-out reset, depending on the four possible voltage trip points.

The other two possible enable/disable bit patterns were not investigated.

page 39 of 68

Page 40: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #16: code protection

WHAT:" Change one configuration bit making 1 k words unreadable.

HOW:1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Erase the 18F4620. 4. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1001. Write.5. Verify a 250 kHz signal on pin 14, CLKO.6. Read the chip; Program Memory should all be 0xFFFF.7. Using Configuration Word Editor, set bit 14 in CONFIG5 to 0. Write.8. Read the chip; the first 1 k words will read as all zeroes.

page 40 of 68

Page 41: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

NOTES:

In an erased 18F4620, code protection is off.

The 32 k words of flash, Program Memory, are divided into five blocks: Boot Block (1 k), Block 0 (7 k), Block 1 - Block 3 (each 8 k words).

Changing that one bit code-protected the Boot Block, so now those 1 k words always read back as zeroes.

page 41 of 68

Page 42: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Note “Code Protect” under the word “Configuration”.

If we use Configuration Word Editor to change that same bit back to 1, Save, and Write, the Boot Block still reads as all zeroes and the Code Protect message is still present.

In this condition, I could load a different hex program, write it to chip, and have it execute. But a Read still returned all zeroes

The protection can be removed only by Erasing the 18F4620.

Even after erasing the chip, the Code Protect message persists until I quit the PICkit™2 application and start it again.

References:

Data Sheet page 254

page 42 of 68

Page 43: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #17: Read Program Memory using assembly codeHamStack

WHAT:" Sequentially read flash using 4 instruction words

HOW:1. Wire up 8 LEDs to PORTD, through current limiting resistors, with the LED cathodes

to ground.2. Erase the 18F4620.3. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1000. Write.4. In the Program Memory Area, enter four instruction words: 6A95 0009 CFF5 FF83.

Write.5. Compare the LED patterns to the code displayed by the PICkit™2 application.

NOTES:

Normally the HamStack would have the four oscillator Configuration bits set to 0010, or HS, for a High Speed external crystal.

This experiment works with that setting except that the LEDs change too quickly, given the instruction cycle frequency of 2.5 MHz.

page 43 of 68

Page 44: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Setting the four oscillator Configuration bits to 1000 gives an internal oscillator with an instruction cycle frequency of 250 kHz, ten times slower, and no CLKO on pin 14, which is connected to a crystal.

This slower speed allows comparison between the LED pattern and what is shown in Program Memory by the PICkit 2 application. The HamStack reset button works, and will start the program over.

The LEDs sequentially show, byte by byte, the code in flash memory, starting at the beginning address 0000.

For example, the first LED pattern will show 95, the low byte of the first instruction word. The second LED pattern will show 6A, the high byte. And so forth.

Normally three Table Pointer registers, all SFRs, would be initialized by code to point at some specific part of memory:

" Program Memory Table Pointer Upper Byte" TBLPTRU" Program Memory Table Pointer High Byte" TBLPTRH" Program Memory Table Pointer Low Byte" TBLPTRL

The minimal code here relies on the fact that on power-up these registers are all zeroed, thereby pointing to the beginning of flash memory.

The first instruction loads all zeroes into the Data Direction Register for PORTD (TRISD), making all of the RD pins outputs.

The second instruction, TBLRD*+, 0009, is the work-horse instruction, reading from flash according to the Table Pointer registers.

The “+” at the end of the instruction means that the Table Pointer registers will be incremented, after the read.

Whatever byte is read by the TBLRD*+ instruction is put into the TABLAT register, another SFR.

The final instruction copies the contents from the TABLAT register to PORTD, so that that byte can be represented by the LEDs.

References:

Data Sheet pages 62, 250, 292, 305.

page 44 of 68

Page 45: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #18: Read the Device IDs using ten instructionsHamStack

WHAT:" Display either DEVID1 or DEVID2

HOW:1. Wire up 8 LEDs to PORTD, through current limiting resistors, with the LED cathodes

to ground.2. Erase the 18F4620.3. Using Configuration Word Editor, set bits 11 - 8 in CONFIG1 to 1000. Save. Write.4. In the Program Memory Area, enter ten instruction words:

" 6A95 0E3F 6EF8 0EFF 6EF7 0EFE 6EF6 0008 CFF5 FF83

5. Write.6. Read the DEVID1 byte on the LEDs.

NOTES:

The Device ID Registers indicate which specific chip and the silicon revision.

Bits 0 - 4 of DEVID1 indicate revision level. My three chips read as revision 7. There are at least two older versions of 18F4620s floating around out there (screen shot from Data Sheet page 257).

page 45 of 68

Page 46: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

The code:

6A95 !! ! CLRF TRISD"" make all PORTD pins output 0E3F 6EF8!! MOVLW " " put value 3F into the W register" " " MOVWF " " put value in W register into TBLPTRU 0EFF 6EF7 !! MOVLW " " put value FF into the W register" " " MOVWF " " put value in W register into TBLPTRH 0EFE 6EF6 ! MOVLW " " put value FE into the W register" " " MOVWF " " put value in W register into TBLPTRL 0008 ! ! TBLRD*" " Using the Table Pointers loaded above," " " " " " read that byte and put value into the TABLAT" " " " " " register.

CFF5! FF83! ! MOVFF" " Copy the value in the TABLAT register to" " " " " " PORTD

The Table Pointers were loaded above to point to 3FFFFE, the address of DEVID1. (screen shot from Data Sheet page 249).

page 46 of 68

Page 47: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

By changing the sixth instruction word from 0EFE to 0EFF, the Table Pointer registers will now point to DEVID2.

When this change is made and the code executed, a value of 0xC0 is shown on the LEDS, indicating that the chip is an 18F4620.

The PICkit™2 Programmer and its application can auto-detect which chip it is attached to by reading DEVID2.

References:

Data Sheet pages 62, 249, 283, 292, 293, 305.

page 47 of 68

Page 48: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #19: Read the Device IDs using SwordfishHamStack

WHAT:" Sequentially display DEVID1 and DEVID2

HOW:1. Wire up 8 LEDs to PORTD, through current limiting resistors, with the LED cathodes

to ground.2. Enter, compile, and write to chip the following program:

{***************************************************************** Name : ReadDEVID.BAS * Author : Robert Ralston * Date : 3/5/2012 * Version : 1.0 * Notes : Read DEVID bytes from Program Memory (flash). * : See Data Sheet DS39626E page 249 ****************************************************************}Device = 18F4620Clock = 10Dim DEVID1 As Byte, ‘bits 0-4 are the revision number of silicon DEVID2 As Byte ‘bits 2-3 are ones, indicating an 18F4620TRISD = 0 ‘make PORTD pins outputDEVID1 = 0DEVID2 = 0

Asm movlw 0x3F ‘DEVID1 is at 3FFFFEh; DEVID2 at 3FFFFFh movwf TBLPTRU ‘load the upper Table Pointer with 3Fh movlw 0xFF movwf TBLPTRH ‘load the high Table Pointer with FFh movlw 0xFE movwf TBLPTRL ‘load the low Table Pointer with FEh tblrd*+ ‘read byte into TABLAT and increment pointer movf TABLAT, W ‘copy value in TABLAT register to W movwf DEVID1 ‘copy value in W to DEVID1 variable tblrd* ‘read byte into TABLAT movf TABLAT, W movwf DEVID2End Asm

page 48 of 68

Page 49: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

While true PORTD = DEVID1 ‘display value on LEDs DelayMS(5000) PORTD = DEVID2 DelayMS(5000)Wend

NOTES:

See Data Sheet page 257 for the meaning of the DEVID bits.

page 49 of 68

Page 50: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #20: Tweak the Internal Oscillator Frequency Using Swordfish (min. circuit)

WHAT:" Tune the internal oscillator frequency using OSCTUNE

HOW:1. Remove any external clocks and RC networks from pin 13.2. Scope pin 14 (CLKO).3. Erase the 18F4620. 4. Compile and Write to chip the following program.5. Measure the possible frequency range.

{ Name: osctune1.BAS Author: R. Ralston Date: 3/6/2012 Version: 1.0 alter the Frequency Tuning Bits in OSCTUNE}Device = 18f4620Config osc = INTIO7 //internal 1 MHz oscillator with CLKO //CLKO is FOSC/4Dim index As ByteOSCTUNE = 0 //output center frequencyDelayMS(500) //time to measure itOSCTUNE = 15 //output maximum frequency DelayMS(500) //time to measure itOSCTUNE = 16 //output min frequencyDelayMS(500) //time to measure it

While true For index = 0 To 15 OSCTUNE = index DelayUS(500) Next For index = 15 To 0 Step -1 OSCTUNE = index DelayUS(500) Next For index = 31 To 16 Step -1 OSCTUNE = index DelayUS(500) Next For index = 16 To 31 OSCTUNE = index

page 50 of 68

Page 51: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

DelayUS(500) Next Wend

NOTES:

Measurements of CLKO (FOSC/4):

FOSC/4 kHz Internal oscillator Hz

minimum 218.5 874,000

center 249.2 996,800

maximum 278.2 1,112,800

page 51 of 68

Page 52: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

The code loads the lower five bits of the OSCTUNE register, the Frequency Tuning bits, to change the default 1 MHz internal oscillator frequency(screen shot Data Sheet page 27).

Note the discrepancy on this page. There are only five bits for tuning and yet the minimum and maximum range are shown using six bits. The code uses 01111 for maximum frequency and 10000 for minimum.

The above measured frequencies suggest a tuning range of approximately plus and minus ten percent.

This code was checked using the HamStack with 8 LEDs wired up to PORTD and the following code changes. " config" " osc = INTIO67"! " TRISD = 0! ( below the DIM statement )

" Find and Replace all instances of OSCTUNE to PORTD.

" Find and Replace all instances of DelayUS(500) to DelayMS(10)! Find and Replace all instances of DelayMS(500) to DelayMS(10)

page 52 of 68

Page 53: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Experiment #21: Write User IDs with Swordfish, pk2cmd

WHAT:" Use ʻbrute-force” code to write the User IDs" " Use pk2cmd to save User ID information

HOW:1. Compile and Write to chip the following program.2. Do a Read on the chip.3. Look at the User IDs displayed in the PICkit™2 application.

{! Name: WriteUserIDs.BAS! Author: Robert Ralston ! Date: 3/7/2012 ! Version: 1.0 ! Notes: Write code to flash which then writes the User IDs!}

Device = 18F4620Clock = 10

Asm movlw 0x20 " //upper address of User IDs movwf TBLPTRU" //load upper Table Pointer movlw 0x00"" //high address of User IDs movwf TBLPTRH" //load high Table Pointer movlw 0x00"" //low address of User IDs movwf TBLPTRL" //load low Table Pointer movlw 0xC0" //data for first User ID; use a ʻzeroʼ not a capital ʻOʼ movwf TABLAT TBLWT*+ movlw 0xFF"" //data for second User ID movwf TABLAT TBLWT*+ movlw 0xEE" //data for third User ID movwf TABLAT TBLWT*+ movlw 0x1C" //data for fourth User ID movwf TABLAT TBLWT*+ movlw 0xED" //data for fifth User ID movwf TABLAT TBLWT*+ movlw 0xC0" //data for sixth User ID; use a ʻzeroʼ not a capital ʻOʼ movwf TABLAT TBLWT*+

page 53 of 68

Page 54: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

movlw 0xFF"" //data for seventh User ID movwf TABLAT TBLWT*+ movlw 0xEE" //data for eighth User ID movwf TABLAT TBLWT*+ bsf EECON1, EEPGD bcf EECON1, CFGS BSF EECON1, WREN movlw 0x55 movwf EECON2 movlw 0xAA movwf EECON2 BSF EECON1, WR BCF EECON1, WRENEnd Asm

This program works using the minimum circuit after making the following code changes:

remove this line:" " Clock = 10Add these lines:" " Config ! ! ! ! ! osc = INTIO7

NOTES:

page 54 of 68

Page 55: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

This program was just for fun.

The code follows that listed in the Data Sheet for writing to flash memory. The Table Pointer registers are loaded with the address of the ID Locations. Eight bytes are then loaded into the TABLAT register and written, going to a buffer on the chip. The last nine lines of code do the actual write to flash.

This code writes the USER ID Locations from flash, wasting code space and executing, i.e. writing again, every time the chip powers up or is reset.

WARNING:" The notes following describe a way to get only the User IDs written using the command line program, pk2cmd.exe (available from the Microchip site for PICkit 2).

There are almost certainly better and easier ways to accomplish this goal; tell me about them. Changing settings on your PC can screw it up, so be warned and be careful; I cannot help you bail if something goes wrong.

Suggestion: have the Readme For PK2CMD.txt file handy.

Step 1: Modify the “Path environment variable” as described in the Readme. I have the pk2cmd.exe file in a folder on my Desktop labelled PIC2.

I modified my path like this:" previouspathstatements;C:\Documents and Settings\Robert\Desktop\PIC2

page 55 of 68

screen shot from Data Sheet page 266.

Page 56: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Step 2: Under Windows XP, I use the Run command like this:

Step 3: Clicking OK opens this window:

Step 4: Because of the Path change made above, pk2cmd will run from this window.

With a powered 18F4620 and the PICkit™2 programmer attached, the pk2cmd /s# command will see if the programmer can be found. If so, its name and firmware version will be listed.

page 56 of 68

Page 57: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Step 5: The pk2cmd /pf command lists parts which can be auto-detected. It is handy to know the ID#, which for the 18F4620, is 2.

Step 6: This command uses that ID# of 2: pk2cmd /pf2 /iThe /pf2 invokes auto-detection; otherwise we would have to specify exactly which chip, like this: /ppic18f4620. The /i reads and displays the chip ID and revision, much easier than experiment #18 - 19.

Step 7: A command to read and display the User IDs: pk2cmd /pf2 /gi

page 57 of 68

Page 58: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Step 8: Hereʼs a command which will read, and display, the Configuration Bytes:pk2cmd /pf2 /pf2 /gc

Step 9: Use the PICkit 2 programmer application to Export a hex file. This file will contain everything: flash, EEPROM, Configuration, and our desired User IDs. Or, we can use the following command to achieve the same result: "" pk2cmd / /pf2 /gfc:\documents and settings\robert\desktop\pic2\coffeeIDs

A new file, coffeeids.hex, will appear in the PIC2 folder. Without the path specifying where to put this newly created file, it will be in the, probably hidden, WINDOWS\system32 folder.

page 58 of 68

Page 59: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Step 10: If we use WordPad to open that newly created hex file, we can see the User ID information at the very bottom.

Step 11: Finally, we can use that hex file which now contains our desired User ID information to write those bytes, and nothing else, to chip, thereby not changing anything already on chip: the program in flash, the configuration, or the EEPROM.

The critical part of the command is the last one /mi. This command uses the specified hex file and writes to chip, but only the “ID Memory.”

Once the User IDs are set, then any hex file export from the programmer will now contain that User ID information.

If that same file is imported into the programmer, and used to Write to chip, that User ID information will also be written to chip.

page 59 of 68

Page 60: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Hereʼs what we see doing a Blank Check with the programmer, where the Program Memory and EEPROM data are blank, but not the User IDs.

page 60 of 68

Page 61: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

APPENDIX page

Chips 62

Software 63

Documents 64

flash Memory Map 65

Code Words 66

page 61 of 68

Page 62: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

" All three chips used, PIC18F4620-I/P, were identical, except for the production date code in the middle line (Data Sheet page 383). Since the production dates are so close, presumably all three chips are the same revision. After reading the DEVID bits, I could see that they were all revision 7.

" The top chip is from my HamStack board. The other two chips were purchased from Digi-Key for about eight bucks each (Microchip Direct will ship chips to you directly from Thailand, I donʼt know about shipping charges).

" The letters on the chip are explained in the Product Identification System (Data Sheet page 409).

" " F = Standard Voltage Range (4.2V- 5.5V)" " I = Industrial Temperature Range (-40ºC to +85ºC)" " P = PDIP

(chips with LF rather than F = Wide voltage range, 2.0V to 5.5V; Data Sheet page 8)

" Oddly, the Brown-Out Reset experiment showed these Standard Voltage Range type F chips still working at 2.5 volts.

" I suspect that many or most of the F type chips will work at lower voltages but that they are not guaranteed to work below 4.2V. Also, these experiments drew very little current from them.

page 62 of 68

CHIPS

Page 63: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Microchip PICkit 2 v.2.61 is a stand-alone application used with a PICkit™ 2 programmer to erase, read, write, and power an 18F4620.

There are many versions of this software floating around.

The stand-alone application is the most current version, 2.61.00 as of March 2012, available from Microchip (Sierra Radio Systems has a link to the appropriate page).

The programmer itself has firmware called the PICkit 2 Operating System, here also the most current version 2.32.00.

If you do experiment #21, then you will need PK2CMD v.1.20, available from the same site." " " "

These experiments were run on XP Pro SP-3.

As a Ubuntu newbie I was unable to get the programmer application running under Linux.

SOFTWARE

page 63 of 68

screen shot from my system

Page 64: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

Microchip PIC18F2525/2620/4525/4620 Data Sheet, DS39626E (This is the primary document used, revision E, © 2008. There are earlier versions floating around).

Microchip PICkit™ 2 Programmer/Debugger Userʼs Guide DS51553E (revision E, © 2008).

Microchip PIC PIC18F2525/2620/4525/4620 Rev. B5 Silicon Errata, DS80364A, © 2008.

Sierra Radio Systems Getting Started With The HamStack Microcontroller Project Platform, Revision 1.2, © 2011.

(All references in the experiments to these documents use that part in boldface, e.g. Data Sheet page 409).

page 64 of 68

DOCUMENTS

Page 65: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

FLASH MEMORY MAP

page 65 of 68

Boot Block

Block 0

Block 1

Block 2

Block 3

00 0000

00 07FF00 0800

00 3FFF00 4000

00 7FFF00 8000

00 BFFF00 C000

00 FFFF

01 0000

1F FFFF

Unimplemented

User IDs

Configuration

Device IDs

20 0000 - 20 0007

30 0000 - 30 000D

3F FFFE - 3F FFFF

Boot Block = 2 k bytes or 1 k words

Block 0 = 14 k bytes or 7 k words

Block 1 = 16 k bytes or 8 k wordsBlock 2 = 16 k bytes or 8 k wordsBlock 3 = 16 k bytes or 8 k words

Total Program Memory = " 64 k bytes or 32 k words

Unimplemented memory = 968 k words

Cannot be written to. When read, returns all zeroes, or NOPs.

User IDs = 8 bytes

Configuration = 14 bytes(112 bits, 42 are used)

Device IDs = 2 bytes. Read only. Unknown if this location is flash and can be written only by a proprietary process.

Page 66: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

CODE WORDS

The 18F4620 has a feature making it extremely easy to address the Special Function Registers: the “Access Bank.”

The Access Bank allows using just the lower word of the address of the Special Function Registers, making it easier to “hand-assemble” some instructions.

The first instruction used in experiment #5 was 8AD3, which set bit five to one in the OSCCON register, increasing the internal oscillator frequency.

The address of this particular SFR, OSCCON, is FD3h (Data Sheet page 62). Due to the Access Bank, all we need for the address is the D3. So the lower byte of this instruction is done.

Data Sheet page 279 describes the Bit Set f instruction, BSF. Its uppermost nibble is always 8.

The four remaining nibble depends on which bit is being set. In this case, bit 5, or 101. Adding a zero at the end to specify using the Access Bank makes this 1010, or A.

Putting the pieces together gives us 8AD3.

The second instruction used in experiment #5 sets bit 4 in the OSCON register, so the only difference will be in that one nibble. Bit 4 = 100; adding on the zero to specify the Access Bank gives us 1000, or 8. Hence this BSF instruction is 88D3.

The Bit Clear f instruction, BCF (Data Sheet page 276), works identically except that the uppermost nibble is 9. When we want to clear bit 1, we have 001 plus the zero to specify the Access Bank, giving us 0010, or 2. Putting those two pieces together gives 92.

In experiment #9 we cleared bit 1 of TRISD, to make RD1 an output pin. The address of TRISD is F95. Therefore, our entire instruction would be 9295.

page 66 of 68

Page 67: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

In experiment #17, we needed to set all bits to zero in TRISD so that all RD pins would be outputs. We could have, painfully, used the BCF eight times, bit by bit.

Instead we used the Clear f instruction, CLRF (Data Sheet page 283). If the Access Bank is used, then the upper byte is always 6A, to which we then append the address of TRISD, or 95. Out final instruction is 6A95.

In experiment #6 we moved a specific byte value to an SFR using two instruction words:

" MOVLW " Move Literal to W " " Data Sheet page 293" MOVWF" Move W to f" " " Data Sheet page 293

The W register, an SFR, is sort of like the accumulator in older microprocessors (Intel 8085). These two instructions use the W register as a transfer point.

The first instruction moves a specific value, a literal, into the W register. The second instruction copies that value from the W register into a specific address. Note that the value in the W register is not changed so that the same value could then be copied to other registers if required.

The high byte of the MOVLW instruction is always the same, 0E. The low byte is the literal value. Since we wanted to move 70 (or 0111 0000) in this experiment, our final instruction was 0E70.

The MOVWF instruction, if using the Access Bank, always has the high byte as 6E. To that is added the lower byte address of the SFR, which in this case being OSCCON, is D3. The final instruction was 6ED3.

In experiment #12 we used the SLEEP instruction, 0003. Thatʼs it. Using 0003 as an instruction word will always put the chip into the sleep mode. The real problem is making sure something wakes it up.

page 67 of 68

Page 68: PIC 18F4620 Notes - hamstack.com 18F4620 Notes © Robert Ralston KJ6HFR March 2012 Purchasing the HamStack board at Pacificon from Sierra Radio Systems sparked my interest in this

In experiment #17 we used the MOVFF instruction, Move f to f (Data Sheet page 292).

This two-word instruction can copy any byte in the static RAM area to any other byte in that 4 kbyte space. The SFRs live at the top of the static RAM so MOVFF can be used to copy the value of one SFR to another (with a very small number of exceptions).

The high byte of the first word is always C. The high byte of the second word is always F.

The remained of each word is the full 12-bit address. In that experiment we copied the value from TABLAT (address FF5) to PORTD (address F83).

So the full two instruction words were CFF5 FF83.

In the second part of experiment #12, the GOTO instruction (Data Sheet page 288) was used.

This instruction illustrates the “word nature” of the 18F4620 instruction set. Since instructions are words, and are on word boundaries, the 21-bit (bits 0 to 20) program counter (PC) always increments by two (Data Sheet page 54). Additionally, its lower byte is always loaded from a latch, with a fixed lowest bit of zero, except for a couple of instructions, including GOTO.

Since the GOTO instruction can load the PC directly, the instruction set architecture has to make sure that the lowest bit of the PC comes in, from the GOTO, as zero. Indeed it does. The target address in the GOTO instruction is 20 bits rather than 21 and loads into the 20 - 1 bits of the PC, not PC bit 0.

The GOTO in experiment #12 (part 2) goes to the second instruction word, so that the first instruction words does not get executed over and over.

EF01 F000. The 01 is the low byte of the target address. Since this byte gets loaded into the PC bits 8-1, the PC looks, once loaded, like this: 0 0000 0000 0000 0000 0010. It is as if that byte, 01, got shifted left, or multiplied by 2. Consequently the next instruction is the second word, at address 0002.

page 68 of 68