880 likes | 1.1k Views
CMPT 229 – PIC Microcontroller. Grant MacEwan University - Fall 2010 Mr. Marcel Berard. Topics. PIC24 Features and Architecture Memory and I/O Using MPLAB PIC24 ASM Language. References. Microchip document 39747C.pdf : PIC24FJ128GA Family Data Sheet Microchip document 51281F.pdf :
E N D
CMPT 229 – PIC Microcontroller Grant MacEwan University - Fall 2010 Mr. Marcel Berard
Topics • PIC24 Features and Architecture • Memory and I/O • Using MPLAB • PIC24 ASM Language
References • Microchip document 39747C.pdf: PIC24FJ128GA Family Data Sheet • Microchip document 51281F.pdf: MPLAB IDE Quick Start Guide • Microchip document 70157B.pdf: dsPIC30F/33F Programmers Reference Manual
PIC Features • The PIC microcontrollers are manufactured by a company called Microchip Technology Inc. (www.microchip.com) which has its headquarters in Arizona. • The sales made by Microchip have increased rapidly in the past 25 years. Microchip is one of the leading providers of microcontrollers.
PIC Features • The PIC microcontrollers are a complete computer on a single chip. They typically do not have external devices such as disk drives or keyboards. • Since a PIC program cannot be stored on a hard drive, it is stored in flash memory which retains its contents when powered off.
PIC Features • The PIC24 microcontrollers are referred to as a 16-bit microcontrollers. This is because the data word width is 16 bits which is the amount of data processed during an instruction. All PIC controllers have instructions such as add and sub as well as the logical operations and branches. The newer controllers also have hardware mul and div instructions as well as additional addressing modes. • Earlier PIC microcontrollers such as the PIC12, PIC16 and PIC18 were 8 bit. In 2007, Microchip introduced a line of 32 bit PIC microcontrollers.
PIC Features • There are some 16-bit PIC microcontrollers that are more powerful than the PIC24. These are the dsPIC30 and dsPIC33 controllers. • The dsPIC is an enhancement of the PIC that can also perform digital signal processing. DSP requires extensive floating point multiplication. As one example, DSP can take an analog signal (such as from a modem) representing binary data and convert the analog signal to digital data.
PIC Features • The PIC microcontrollers have I/O pins; these are the legs on the chip. These pins are used to communicate with the outside world. The number of pins can vary from about 8 to 150. The high-end PIC24s have from 64 to 100 pins. • Most of the PIC inputs and generally all of the outputs are binary (Binary input/output = 0 volts or operating voltage). A binary PIC input could be used to indicate if a switch was open or closed. A binary output could be used to switch an LED on or off. • Are there any uses for the pins other than I/O?
PIC Features • For many PICs, some of the PIC inputs are analog. An analog input can accept a continuous range of input voltages from 0 to the operating voltage. • An analog-to-digital converter (ADC) inputs an analog voltage and converts it internally to a digital (unsigned integer) value. For example, consider an 8-bit ADC that accepts voltages from 0 to 2.55 Volts. What integer value would it produce for an input voltage of 1 Volt or 2.352 Volts? • Some specialized PICs also have digital-to-analog converters (DACs). What is their purpose?
PIC Features • Another feature found in many PICs is one or more serial ports (UARTs). These ports are similar to the COM port found in (older) PCs and provide a mechanism for communicating with a PC. • Some of the newer PICs also support onboard USB, providing for a superior communication protocol with a PC. • Many PICs also support various communication protocols that can be classified as electronic (SPI, I2C) and industrial (CAN, ModBus) protocols.
PIC Features • The PIC24 microcontroller is quite inexpensive (about $5 - $20 depending on the sub-series). Lower end PIC microcontrollers such as the PIC12 sell for < $1. • Late model cars can contain several microcontrollers. One sample application for a microcontroller is to control the airbags. • Other devices such as washing machines can also be controlled by a microcontroller.
PIC Features • A PIC microcontroller is often referred to as an embedded controller. It typically has a single role; to control the device in which it is embedded. • A sample (infinite) loop for a chip controlling an airbag was presented earlier and is repeated here. SensorLoop: # Read data from acceleration sensors # Branch to SensorLoop if accel data is normal # Deploy airbag # Replace airbags
PIC Features • The previous program is a common template for a PIC program. Once the PIC is powered on, it goes to the start of its program and typically has an infinite loop that is performed until it is powered off. Note: A program like this completely controls the PIC and has no restrictions. There is no protected mode as there is on a MIPS CPU. • PICs also have a feature referred to as brown-out protection. If the supply voltage drops below an acceptable value (say 2 V on a 3 V PIC) such that some data may become corrupted, then the PIC will reset which involves the program being sent back to the start so that the PIC properly re-initializes itself.
PIC Features • The PIC24 controllers have various clocking (oscillator) alternatives that run the chip at different speeds. • Providing a faster clock allows the controller to perform the instructions more quickly. (The maximum performance of the PIC24 is 16 MIPS.) Is there any advantage to a slower clock?
PIC Architecture • The PIC microcontrollers utilize the Harvard architecture (actually a modified Harvard architecture). With this architecture, there are 2 separate memories, a program memory and a data memory. Each memory has its own address space. • There are separate buses leading to the two memories. Address 100 in the program memory differs from address 100 in the data memory.
PIC Architecture • The program memory space has a 24 bit address. Thus, there appears to be 224 bytes or 16 MB. However, only half of the addresses are available to program instructions, the top half is for configuration data (ie. clock mode). The PC has 23 bits. Every instruction is 24 bits (3 normal bytes) but occupies 2 program memory addresses. Thus, the program memory logical address space can hold 4 million instructions. • The amount of physical program memory varies between the PIC24 models from 64 KB to 128 KB which supports 22,016 up to 44,032 instructions.
PIC Architecture • The data memory space has a 16 bit address so it is 216 bytes (64 KB) on all PIC24s. Data memory is byte addressable. A data word is 16 bits (2 bytes) so the address space can contain 32 KB words. Memory accesses are usually word operations. • The amount of physical data memory (RAM) in most PIC24s is 8 KB. This memory is volatile and loses its data when powered off. • There is no virtual memory in a PIC. All data addresses are absolute. The higher addresses are empty.
PIC Architecture • The PIC24 has special function registers (SFRs) mapped into the first 2 KB of the data address space. I/O is performed by reading or writing at these addresses. This is equivalent to memory mapped I/O on the MIPS architecture. • Most of the addresses between 0 and 2 KB are empty as there is not a SFR at every word address. • The operations (read or write) performed on SFRs depend on whether they perform input or output or both.
PIC Architecture • The 8 KB of physical RAM occupies the memory addresses from 2 KB to 10 KB. The memory cells at these addresses can be read or written. • The data addresses from 10 KB to 32 KB are empty and should not be accessed. • The data addresses from 0 to 8 KB (including SFRs) are referred to as the near data space. These addresses can be represented with 13 bits and are stored within many instructions. Thus, they can be accessed with the direct addressing mode more easily than cells at higher addresses.
PIC Architecture • Data addresses between 32 KB and 64 KB may be empty or may be mapped into program memory. The PIC24 has two mechanisms for mapping data addresses (above 32 KB) into the program memory space. This is why its architecture is referred to as a modified Harvard architecture. • As an example application of this feature, constants can be stored in the program memory and mapped to data memory at program start.
PIC Architecture • The PIC24 has 16 (16-bit) working registers. Early PIC models had only a single 8-bit working register. • The PIC24 register names are W0 to W15 (or WREG0 to WREG15). On previous PICs, the only working register was called WREG. Register W0 (WREG0) on the PIC24 is equivalent to WREG on earlier PIC controllers. • The PIC24 working registers are mapped into the data memory as SFRs at word addresses 0x00 to 0x1E.
PIC Architecture • On earlier PIC controllers, WREG was often used implicitly during instructions. • To maintain compatibility, the PIC24 maintains many instructions from earlier PIC models. The PIC24 uses register W0 as WREG.
PIC Architecture • Register W15 is implicitly used as the stack pointer and W14 as the frame pointer. The programmer can also explicitly write to these registers as they are working (general-purpose) registers. • If a program does not use a stack (W15) or linking and unlinking (W14), then it is safe to use these registers for other purposes. • These registers are used extensively by high level languages such as C. The PIC24 architecture was designed with 3rd generation languages in mind.
MPLab • MPLab is the (free) Windows software IDE provided by Microchip to develop software for its PIC and dsPIC microcontrollers. The current MPLab version is 8.6. • With MPLab, code is developed in the Windows environment and the binary executable file (and device configuration information) is burned into the flash memory of a PIC device. The executable file was traditionally outputted from the PC via a COM port but a USB transfer is more common today.
MPLab • MPLab contains a project manager, an editor, an assembler (MPASM), a linker, a software simulator (MPLab SIM) and interfaces to its programming / debugging devices. It is a complete environment for assembly language programming. • MPLab has an open interface that is easily expandable. Microchip provides a lite (free) C compiler that can be integrated into MPLab. There are several 3rd party compilers; almost all C, and at least one Pascal compiler. MPLab 8 also has an integrated copy of the PICC LITE C compiler.
MPLab • A complete tutorial for building an MPLab project is given in Chapter 2 of document 51281F.pdf. A relatively brief description will be given here. • Create a folder in which to put the project. • Select Project > Project Wizard.. and accept Next. • Select the appropriate device (PIC24FJ128GA010). • Select the appropriate toolsuite (Microchip ASM30 Toolsuite) from the drop down list at the top. • Select a project name and directory. • Add files to the project. This step will not be performed here. • When finished, the project window shows some empty folders based on groups such as Source Files and Header Files.
MPLab • To compile an MPLab project, 3 files are required: an include file, a linker file and a source code file. These 3 files are provided in a Template project built by the instructor. • The include (header) file declares the existence of SFRs and various other aspects of the chip. We will use the include file: pf24FJ128GA010.inc • The appropriate linker script defines the locations of the declared SFRs. We will use pf24FJ128GA010.gld. • Note: The include and linker files are located in the installation directory. Normally, the project contains links to the original files. The Template project has placed a copy of these files in the project directory.
MPLab • MPLab does not provide an assembly source code template for the PIC24. Microchip anticipates that the C language will be used almost exclusively for this chip. • A minimal PIC24 source code template (Template.s) has been placed in the Template project. • The menu items Project > Make (F10) or Project > Build All (Ctrl + F10) can be used to generate the project executable.
MPLab Code • The PIC assembly source code has many similarities with the MIPS assembly source code. • PIC assembly has a data segment and a text segment. At a minimum, the text segment (code) for a control application should have an infinite (polling) loop. • Another requirement is a definition of the program entry point, this is the .global directive in the source code template.
MPLab Code • A minimal source code file is shown below. ; #include "p24FJ128GA010.inc“ .global __reset .data ;Start of data section .text ;Start of code section __reset: loop: BRA loop ;End of main .end ;End of source
MPLab Code • Single line comments are started with the semi-colon. • The (commented out) include directive contains the SFR declarations for a particular microcontroller. • The __reset variable is the program entry point at controller reset. It’s scope is set to global to make it visible to the linker. • Labels are defined the same as for the MIPS controller, an identifier followed by a colon.
MPLab Code • The program shown can be built and run. It is an empty infinite loop. • The only instruction used in the code is the branch (BRA or bra). The instruction mnemonics are not case sensitive (compiler) but the identifiers are case sensitive (linker). • By default, the user program instructions start at address 0x200.
MPLab Code • The data for a PIC program is defined in the data segment (directive .data). Multiple items are comma separated. The data types include: • .ascii (requires an explicit null terminator) • .asciz (implicit null terminator) • .byte • .int or .long (4 bytes) • .word (2 bytes) • .double (8 bytes) • .pword (3 bytes) • .space
MPLab Code • A sample data segment is as follows. The value following a space declaration indicates the byte count. The following declaration allocates 18 bytes in the data segment. By default, the (global) data variables are placed at a start address of 0x800 (the start of RAM). Thus, variable x is at address 0x810. .data w: .space 16 x: .word 1
MPLab Debugging • MPLab provides a simulator (MPLAB SIM) to view the “operation” of a PIC program on the PC. • To use the simulator, select it via the Debugger > Select Tool menu item. • Note that there are several other debugging tools available. MPLAB SIM is a software emulator run on the PC. The other debugging tools all involve external hardware. Some such as MPLAB ICD 3 involve a PIC microcontroller that is under the control of a hardware debugger while others such as the ICE options are specialized hardware devices that emulate the PIC microcontrollers.
MPLab Debugging • When there is a debugger selected, there are several items added to the Debugger menu. These items (Run, Step Into, Step Over, Reset) allow the operator to step through the code on a line by line basis or to run to a breakpoint. A green arrow shows the current instruction. Note that the program counter (pc) is shown in the status bar. • Breakpoints are added or removed by double-clicking in the gutter next to a line. • For the previous template program, the branch instruction (at address 0x200) is the only instruction ever performed.
MPLab Debugging • When debugging, the operator typically wants to observe the program data or code. There are several program windows that can be displayed in the View menu. • The File Registers window shows the data memory contents from address 0x0000 to 0x27FF (10 KB). What is in this region of memory and why are some of the entries empty? Note: The file registers can also be edited from this window to provide a program with specific data.
MPLab Debugging • The Program Memory menu item in the View menu displays a window with the contents of program memory. By scrolling to the bottom of this window, the last address is 0x157FA. This corresponds to about 128 KB of program memory. • When reset, the PIC PC is set to 0. The instruction at address 0 is goto 0x000200. What is at address 200 and beyond? • What are the instructions at addresses between 0 and 0x200 and what is their purpose?
MPLab Debugging • The Special Function Registers menu item in the View menu displays a window with the contents of the SFRs. These registers are memory mapped into the data address from 0 to 0x7FE (first 2 KB). • In addition to the addresses, this window displays the names of the SFRs so that it is easier to find a specific SFR. Note the addresses of the WREGs. • As stated previously, there are many holes in the SFR address space.
PIC24 Instructions • Before looking at common program instructions, consider the RESET and NOP instructions which are both control instructions. • The RESET instruction causes a software reset. This is equivalent to powering on the PIC. It places all of the registers in their power-on state including the PC which is set to 0. • The NOP instruction stands for NO OPERATION. This instruction consumes a clock cycle but does not perform any operation. What is its purpose?
PIC24 Instructions • The PIC24 has about 76 instructions that fall into the following groups. Its instructions are those of the 30F/33F except for the DSP instructions. • Move • Math • Logic • Rotate/Shift • Bit • Compare/Skip • Program Flow • Shadow/Stack • Control
PIC24 Instructions • The PIC24 instructions have operands of various sizes: byte, word, long … • For byte operands, the instruction has a trailing “.b”. For word operands, the trailing “.w” is optional. • The instructions have 4 addressing modes for their operands which are listed below. • Immediate • File Register • Register Direct • Register Indirect
PIC24 Instructions • An immediate operand is stored within the instruction. In the assembly code, such operands are preceded by the # symbol. The operand can be given in either decimal (#10) or hexadecimal (#0xA). • Immediate operands are typically used to load values into registers or to be used as an offset when calculating an address.
PIC24 Instructions • A file register operand corresponds to a data memory location (address) which is any integer from 0x0000 and 0xFFFF inclusively. This can correspond to a RAM location or a SFR. • File register operands can be specified numerically (ie. 0x810) or symbolically based on an identifier in the data segment (ie. x). The following notes use numerical addresses to avoid the problem of showing variable names in every example. Clearly, the symbolic representation is superior as the address of a variable can change if other variables are placed in front.
PIC24 Instructions • How can the address of a variable be determined so that it can be placed in a register? For example, how is the address of x obtained? • For word operations, the address must be word aligned or a bad address exception will be generated.
PIC24 Instructions • A register direct operand corresponds to one of the working registers W0 .. W15. These operands only require 4 bits. Some instructions have 3 register direct operands. • Some instructions implicitly use W0. This is to maintain compatibility with earlier PICs.
PIC24 Instructions • A register indirect operand uses the contents of a working register as a data memory address. This corresponds to indirection and has the syntax [Ws] where Ws is one of W0 .. W15. • When a PIC is powered on, the hardware knows that the W registers are uninitialized. A W register must be written to before it can be used for indirection. If not, an exception is generated.
PIC24 Instructions • The register indirect operand type supports constant (literal) offsets. An example operand is [W1 + #4]. • Two registers can also be used to generate an operand, for example: [W2 + W3]. • This operand type also supports pre and post incrementing or decrementing, ie. [W1++]. The increment amount depends on the data size of the instruction.
PIC24 Instructions • A side effect of many instructions is modifying the status register (SR). This clearly applies to the compare instruction but also to other instructions. • The status register has 16 bits. Two of these bits are the N (negative) and Z (zero) bits. The N bit is set when an operation has a negative result and the Z bit when an operation has a zero result. • The status register is used for conditional branches. Thus, conditional branches depend on the last instruction that set the status register.
PIC24 Instructions • The move instructions copy data from one register to another, from data memory to/from a register or can copy a literal into a register. The default is a word move but byte (.B) and double word (.D) moves are also supported. Note: Some variations of the move instruction affect the status register (SR). Some examples are given below. MOV WREG, 0x800 ; Store WREG to data memory MOV.B WREG, 0x801 ; Store LSB to memory MOV 0x2006, W3; ; Load W3 from data memory MOV W8, 0x1500 ; Store operation MOV.B #0x24, W6 ; Similar to MIPS li for 1 byte MOV #0x1234, W11 ; li for a word