Laboratory: Introduction to Mechatronics Instructor TA: Edgar Martinez Soberanes (eem370@mail.usask.ca) 2015-01-12 Lab 2. PIC and Programming
Lab Sessions Lab 1. Introduction Read manual and become familiar with the operation and components of the car. Lab 2. Assemble the Mechanical and Electrical components of the car. Lab 3. Install the Software, debug the electronics and run the tutorial program. Lab 4. Program and run the sample programs for each module. Lab 5. Program the run the main software module to run the car and fine tune the car. Lab 6. Write up report and run the car on the test track and determine its performance. 2
Lab 2. PIC and Programming Outline Objective. Lecture. o o o o o PIC Architecture Addressing Registers Instruction set Programming Assemble of the car. Assignment s question 3. 3
Objective Develop an understanding of the 16F877 PIC s architecture to facilitate the programming of the self-guided car. 4
PIC Microcontroller (16F877) Microcomputer systems Every computer, no matter how simple or complex, has at its heart exactly two things: a CPU and some memory. Together, these two things are what make it possible for your computer to run programs. 5
A microcontroller is a simplified version of a similar architecture, placed on one chip 6
PIC Microcontroller (16F877) 7
Main Features Program memory (ROM) and Data memory (RAM) An accumulator (also called the working register, W) Separate data bus for program memory and data memory. 40-pin package. 5 bidirectional I/O ports 15 interrupts 35fixed width instructions 8 A/D input channels Implemented with Parallel Slave Port 8
Memory Organization. Flash Program Memory (ROM): o Size: 8K = 2 = 8192 o Word size = 14 bits. Data Memory (RAM): o Size: 368 = 2 + 112 o Word size = 1byte (1 byte = 8 bits) Data EEPROM (256 bytes) 9
Program Memory (ROM) CALL/GOTO (subroutines) Branching ROM within any 2K program memory page. 8K x 14 bits Stack space is not part of either ROM or RAM and the stack pointer is not readable or writable. 10
Data Memory (RAM) Memory: 2 = 512 o Divided in 4 banks Special Function Registers (SFR) General Purpose Registers (GPR) o Data Memory: 368 = 2 + 112 11
Data Memory Addressing Indirect: Using FSR register (File Select Register)and STATUS register bit IRP (bit 7). Direct: Using STATUS register bits RP1 & RP0 (bits 6 & 5) and specifying the address direction on the program (code). 12
Special Function Registers (SFR) SFR GPR (368 bytes) Total memory o Control the core" operation of the microcontroller. o Specific name, STATUS, FSR, PORTA, TRISA o No name but address number or direction. DATA MEMORY space. Likewise access. 13
STATUS Register Contains the arithmetic status of the ALU (Arithmetic Logic Unit), the RESET status and the bank select bits for data memory. Z, DC, and C bits are controlled by the device logic. TO and PD bits are not writable. o If, CLRF STATUS STATUS = 000u u1uu (u, unchanged) Only use BCF, BSF, SWAPF, and MOVEWF to alter the register. 14
W (Working) Register General register 8-bit wide. Does NOT have an specific direction within the Data Memory. A value in W can be added to another value or moved to file address. If another value is assigned the register's content is overwritten. 15
Ports 5 ports (A,B,C,D, and E). Each port has different characteristics and number of pins. Ports are bidirectional (Input/Output). Multi-functional. Some pins are multiplexed with an alternate function. When a peripheral is enable, that pin may not work as a general input/output. 16
Example Port A Register TRISA is used to set port A. PORTA is 6-bit wide, as well as TRISA. TRISA register (PORTA Data Direction Register): o bit = 1 (pin on port A = Input) o Bit = 0 (pin on port A = Output) 17
Program example (Initializing Port A): ADCON1 register is used to select digital options. 18
Instruction Set (Assembly Language) 35 single word instructions. Each instruction is a 14-bit word which includes: o o an opcode which specifies the instruction type and one or more operands which further specify the operation of the instruction. d = 0, store in W d = 1, store in f (f can be either SFR or GPR) Default d = 1. RAM memory address: 00h-7Fh Bank 0 80h-FFh Bank 1 100h-17Fh Bank 2 180h-1FFh Bank 3 19
Instruction Set. In general the instruction set is divided in three categories: o Byte-oriented o Bit-oriented o Literal and control operations 20
Programming on Assembly Each line of the source file (program code) may contain up to four types of information: o Labels o Mnemonics o Operands o Comments 21
Programming Maximum column width is 255 characters. White space (tabs) or a colon (:) must separate labels and mnemonics. White space (tabs) between mnemonics and operands. 22
Labels Must begin with an alpha character or an under bar (_). May contain alphanumeric characters. Must NOT begin with two underscores ( ), underscore and number (_2NDLOOP), or be a reserved word. Up to 32 characters. They are CASE sensitive. 23
Mnemonics Instruction set (35 instructions) on PIC 16F877 Data sheet. NOT case sensitive. http://www.mikroe.com/chapters/view/10/chapter-9-instruction-set/ 24
Operands Provide info to the instruction on the data that should be used and the storage location (variables or parameters). Multiple operands must be separated by commas. Comments Anything after a semicolon (;) 25
Numeric Constants and Radix MPASM assembler supports the following radix form for constants: hexadecimal, decimal, octal, binary, and ASCII. 26
Program example Turn on and off LEDs connected to the port A. 27
Programming the PIC 28
Programming Cycle 29
Hexadecimal Binary Hexadecimal Decimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 A 10 1011 B 11 1100 C 12 1101 D 13 1110 E 14 1111 F 15 30
Thanks 31