pic microboard manualee401/resource/manuals/microboardrev5.pdf · hosted mode and will outline some...

50
PIC MicroBoard Manual 5 th Edition Sept 2002

Upload: dangnhan

Post on 09-May-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

PIC MicroBoard Manual

5th Edition Sept 2002

Page 2: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

How to use this manual. In an effort to enable students with the appropriate skill set needed to make this course as rewarding as possible, I have divided this manual into four parts. There is a lot of material here but you may not to need use all of it for your project. PARTS I. The MicroBoard: Design and Assembly - This part introduces the features of the MicroBoard

and gives some background information on its design. A MicroBoard assembly guide is included here.

II. Getting Started: Programming the PIC - The second part provides you with a brief review of

assembly language programming and the PIC instruction set. Here you will find a template that you can use as a starting point for writing your own assembly language program. This part concludes with instructions on assembling your assembly language program and downloading object code to the PIC microcontroller.

III. The MicroBoard In Action - This part contains the information you need to make your

MicroBoard actually do something. It begins by introducing the operation of the MicroBoard in Hosted mode and will outline some of the methods you can use to “test drive” your board. It then steps through the features of the PIC microcontroller, one-by-one, complete with instructions, diagrams, sample code and working examples. Equipped with the information provided here, you will be able to unleash the full capabilities of the MicroBoard.

IV. Appendices - These are referenced by the three previous parts. Feel free to detach the appendices

from the bulk of this manual; you will likely refer to them often. This manual is intended to be a supplement to the PIC16F87X data sheet, which can be downloaded from the EE401 website (www.ee.ualberta.ca/~ee401). The PIC16F87X data sheet includes absolutely everything you will need to know about the PIC16F87X microcontroller and this user manual refers to it often. Another helpful reference is the PICmicro Midrange Reference Manual, which can also be found on the EE401 website. I would sincerely recommend taking the time to read this entire manual from beginning to end and to familiarize yourself with the data sheet; the things you will learn along the way will be helpful when it comes to designing your own project. Be aware, however, that completion of this manual will merely lay the groundwork for more interesting, albeit more useful, applications of the MicroBoard. Keep in mind that the MicroBoard is just the central nervous system of your project. You are responsible for the design of the extraneous circuits that your project will utilize (sensors, motors, etc…). It would be prudent to make your initial circuit designs on an SK10 board, then later transfer your design to a computer using EAGLE to have it milled into a clean PCB. In general, rats nests of wires don’t hold up very well and, in accordance with Murphy’s Law, will inevitably let you down when you need them the most (during demonstrations of course!). Contributors This document was created by contributions from: Dave Bull, Ben Bathgate, Mike Cumming, Patrick Pilarski, Paul Bartosek and Loren Wyard-Scott.

Page 3: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

Table of Contents Part I – The MicroBoard: Design and Assembly 1.0 Introduction to the MicroBoard

1.1 MicroBoard Specifications 1.2 Functional Description

1.2.1 DC Power Supplies 1.2.2 RS-232 Serial Interface

2.0 MicroBoard Assembly Part II – Getting Started: Programming the PIC 3.0 Programming the PIC Microcontroller

3.1 Assembly Language Programming 3.2 Memory Structure 3.3 Instruction Set

3.3.1 Mathematical Operations 3.3.2 Flow Control

3.4 Subroutines 3.5 Interrupts 3.6 Putting It All Together

4.0 Assembling Your PIC Program 4.1 Writing Your Program to the PIC

Part III – The MicroBoard In Action 5.0 Debug87x

5.1 Hardware Flow Control 6.0 Peripheral Features

6.1 I/O Ports 6.1.1 Port A (and Port E) 6.1.2 Port B 6.1.3 Port C 6.1.4 Port D 6.1.5 Using Digital I/O Ports Example 6.1.6 Using I/O with Debug87x.hex

6.2 Parallel Slave Port (PSP) Module – PIC16F877 only 6.2.1 Using the Parallel Slave Port Example

6.3 Timer Modules 6.4 Master Synchronous Serial Port (MSSP)

6.4.1 Serial Peripheral Interface (SPI) 6.4.2 Adding Additional Outputs Using Shift Registers and SPI 6.4.3 Using SPI with Debug87x.hex 6.4.4 Using the LCD with Debug87x.hex 6.4.5 Writing a C Program To Control The LCD

6.5 Universal Synchronous Asynchronous Receiver Transmitter (USART) 6.5.1 Using the USART with a Personal Computer 6.5.2 Writing a C Program To Send Text To A Terminal Window

6.6 Pulse Width Modulator (PWM) 6.6.1 Using the Pulse Width Modulator with Debug87x.hex

Page 4: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

6.6.2 Digital-to-Analog Converter Using PWM 6.7 Analog-to-Digital Converter (A/D) Module

6.7.1 Using A/D with Debug87x.hex Part IV – Appendices Appendix A - PIC16F873 MicroBoard Assembly Package

A1 Parts Kit List A2 Assembly Checklist A3 Schematic Diagram (Eagle) A4 Board Layout Diagram (Eagle) A5 Power Checklist

Appendix B - PIC16F877 MicroBoard Assembly Package B1 Parts Kit List B2 Assembly Checklist B3 Schematic Diagram (Eagle) B4 Board Layout Diagram (Eagle) B5 Power Checklist

Page 5: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

Part I The MicroBoard:

Design and Assembly

Page 6: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

1.0 Introduction to the MicroBoard The MicroBoard is a compact, low power controller board that is based on the Micro-Chip Technologies PIC16F87X series microcontroller. The PIC is the heart and soul of the MicroBoard, containing the central processing unit for the board. The MicroBoard will be common to all projects with the addition of student designed and built circuits. The PIC MicroBoard used in the EE401 project course comes in two flavors:

1. The first generation MicroBoard based on the PIC16F873 microcontroller. 2. The new and improved MicroBoard based on the PIC16F877 microcontroller.

Both MicroBoards come loaded with the following features:

Support for Built-in Motor Drivers: each capable of driving a bipolar stepper motor, two bi-directional DC motors, or four unidirectional DC motors. * The PIC16F873 MicroBoard is equipped with 3 built-in motor drivers whereas the PIC16F877 MicroBoard includes only one. Communication Interfaces: RS-232 communication with a PC; SPI and I2C busses for communicating with other chips; and standard 5V CMOS digital I/O for anything else. * The PIC16F877 MicroBoard supports Parallel Slave Port Communication. Extra Features: Three timer modules, a multi-channel analog-to-digital converter, and two pulse-width-modulated outputs. Additional Features of the PIC16F877 MicroBoard: Easy interface with an LCD, built-in programming module that allows you to program other PIC microcontrollers using your MicroBoard.

Two modes of operation are available for the MicroBoard.

Hosted mode: The MicroBoard runs a program that allows control of most of its functions through a serial link to a desktop PC (see section 5.0). Autonomous mode: More powerful than hosted mode, autonomous mode uses assembly code you have written and then “burned” to the PIC (see section 3.0).

Complete with a relatively powerful processor and large I/O capability, the MicroBoard is an impressive control system. The range of projects is virtually limitless: you can control robots, perform wireless communication, carry out digital signal processing, or even connect your MicroBoard to the Internet. The only limit is your imagination.

Page 7: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

1.1 MicroBoard Specifications This table lists the electrical characteristics of the MicroBoard, exceeding maximum values will very quickly damage your board.

PIC16F873 PIC16F877

Total Number of Pins on PIC (DIP package) 28 40

Range of Input Voltage to Barrel Connector 6-18 Vdc

PIC Supply Voltage 5 Vdc

Clock Speed 4 MHz (Upgradeable to 20 MHz)*

Digital I/O Pins 22 33

Analog to Digital Channels 5 (multiplexed) 8 (multiplexed)

Pulse Width Modulated Pins 2

On-board Programming Module No Yes

Parallel Slave Port Module No Yes

10 Pin LCD Interface No Yes

Power Consumption with all IC’s 140 mA @ 7.2 Vdc

Power Consumption with no L293s 20 mA @ 7.2 Vdc

Max current drawn from +12Vdc connector 500 mA

Max current drawn from +5Vdc connector 500 mA

Max current drawn from -12Vdc connector 50 mA

Table 1.1 – MicroBoard Specifications

* See the course instructor for details in regards to upgrading the clock speed. 1.2 Functional Description The MicroBoard is designed in a modular manner as shown in the block diagram below. Refer to the MicroBoard schematic and board layout diagrams (found in appendix) as necessary to improve your understanding.

Figure 1.1 – Block Diagram of the MicroBoard Design

Page 8: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

1.2.1 DC Power Supplies

Figure 1.2 – DC Power Supplies Schematic

DC power is supplied to the board through a 2.1mm coaxial barrel connector with center positive polarity. Normally this power comes from a 9Vdc 500mA wall adaptor plugged into the barrel connector. However, a 12Vdc 1A wall adaptor or suitably sized battery array can also be used. In the lab you will find connectors that allow you to connect your MicroBoard to a DC power supply. Diodes D1 and D2 provide some reverse polarity protection and power supply transient isolation. Capacitors C1 and C2 provide power supply bulk charge storage while C3-C6 provide additional power supply transient isolation for their respective supplies. IC1 (7805) regulates the bulk supply to the +5Vdc needed for the PIC micro-controller and logic devices (logic side of the L293 and MAX232). IC2 (7812) and its components regulate the bulk supply for the voltage inverter, IC3, and are only installed if IC3 is to be used. IC3, the ICL7662P, and capacitors C9-C14 provide a negative voltage for analog circuits such as dual supply op-amp circuits.

You will notice that the polarized capacitors have a non-polarized capacitor in parallel with them. This is not always necessary in designs but is here. The reasoning is that real capacitors are not ideal capacitors: larger capacitors tend to have greater amounts of intrinsic inductance associated with them due to their construction. Their greater inductance gives them poorer high frequency performance. Smaller capacitors tend to have better high frequency performance. Therefore, multiple real capacitors are used to approximate the required ideal capacitor performance.

Page 9: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

1.2.2 RS-232 Serial Interface

Figure 1.3 – RS-232 Serial Interface Schematic

The RS-232 circuitry consists of a DB9 female connector, the MAX232 and related circuitry. The DB9 connector is configured for an IBM PC-style 9-pin RS-232 connection. The MAX232 converts the RS-232 (sometimes called the HIN232) +/-12V signals to logic levels for the micro-controller. The resistors R31 and R32 (330Ω) limit line slew rates and provide some short circuit protection. The resistors R23, R24 (not shown here, see Appendix A – Microboard Schematic Diagrams), R33 and R34 provide output contention protection (contention is created if the Digital I/O pin of PIC incorrectly configured as an output and is connected to the output of the MAX232). C24—C25 are used by the on-board charge-pump (note that the supply is +5V and yet the IC can deliver RS-232 level signals).

Page 10: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

2.0 MicroBoard Assembly In the appendix you will find an assembly package for each of the MicroBoards. Appendix A includes the assembly package for the PIC16F873 MicroBoard and Appendix B includes the assembly package for the PIC16F877 MicroBoard. The first thing you should do when you get your MicroBoard kit is check to see if you have all of the components necessary to assemble it. A parts kit list is included in the assembly package. If you are missing parts go harass the TA. To assemble your MicroBoard you will need the following items:

• Soldering Iron. • 63/37 electronic grade rosin core solder. • Flux pen. • Needle nose pliers for forming leads. • Side cutters for cutting leads.

If you are not familiar with soldering and circuit board assembly you may want to read Appendix C – Soldering Tips before continuing. If you have questions about MicroBoard assembly don’t hesitate to ask your TA. That’s what they are there for. You can waste a lot of time and effort simply finding and repairing a bad solder joint. Do it right the first time and your life will be more pleasant. Remember these axioms whenever working with electronics.

Keep it clean. It is easier to keep electronics clean than to clean electronics. Contamination may cause problems: a simple fingerprint will etch a copper trace and foul a solder connection; a fingerprint or leftover flux residue can act as an undesirable resistor.

Keep it cool.

Use the correct soldering techniques and temperature. High temperatures will destroy components or shorten life times.

Minimize stress.

Miniaturization means less mechanically robust component packaging. Allow for thermal expansion, mechanical flexing, and component movement. Do not pull leaded components too tightly to the board.

The MicroBoard schematics and board layout diagrams are included in the assembly packages. Use these to locate the placement of the components on the MicroBoard. In your assembly package you will also find an assembly checklist. It is recommended that you solder the components in that order, checking them off as you go.

WARNING! Do not solder ICs directly into the board!

Solder the IC sockets.

When the assembly of your MicroBoard is complete you should test the power supply circuitry before installing any ICs. Your assembly package includes a power checklist that guides you through this process. Now that your MicroBoard is assembled and working properly you can do one of two things:

1. Learn how to program it (see section 3.0) 2. Program the PIC with debug87x.hex and explore the features of the PIC16F87X microcontroller

(see section 5.0).

Page 11: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

Part II Getting Started:

Programming The PIC

Page 12: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

3.0 Programming the PIC Microcontroller The main focus of this manual is assembly language programming of the PIC microcontroller. You will have access to a C compiler in the EE401 lab, however, the discussion on programming the PIC in C is limited and you will likely need to seek other resources. 3.1 Assembly Language Programming Here we will review some of the basics of assembly language programming. First it is important to understand the memory structure of the PIC16F87X microcontroller. For detailed information on memory structure see section 2.0 in the PIC16F87X data sheet. 3.2 Memory Structure There are three memory blocks:

1. Program memory 2. Data memory 3. EEPROM data memory

1. Program Memory

The program memory is, as you may have guessed, the section where your program instructions are stored. The size of the program memory for the PIC16F873 is 4Kx14 which means that it can hold 4x1024=4096 14-bit instructions. The program memory for the PIC16F877 is 8Kx14. The program memory is divided into 2K blocks called pages. Thus, the PIC16F873 has 2 pages of program memory and the PIC16F877 has 4 pages. In order for commands on a certain program memory page to be executed the corresponding page must be selected. To switch between program memory pages use the pagesel directive. A recommended way to use the pagesel directive is to first define the following values at the beginning of your assembly language program as follows (also see section 3.6): page0 EQU 0x0000 ; start address of page0 page1 EQU 0x0800 ; start address of page1 page2 EQU 0x1000 ; start address of page2 page3 EQU 0x1800 ; start address of page3 Then to select a page, say page 2 for example, use the following in the body of your main program: pagesel page2 ; selects program memory page 2

2. Data Memory The data memory is divided up into 4 banks (bank 0-bank 3) that contain both Special Function Registers and General Function Registers. See Figures 2-3 and 2-4 in the PIC16F87X data sheet for register file maps. The special function registers are named and have a predefined function. They are used for controlling the operation of the microcontroller. Some of the more important Special Function Registers are outlined in section 2.2.2 of the PIC16F87X data sheet. General function registers are used for data storage. The PIC16F873 has 192 8-bit general function registers. The PIC16F877 has an additional 176 8-bit general function registers (368 in total). To access a register in a certain data memory bank you must ensure that the appropriate bank is selected. The status of the bank select bits, RP0 and RP1 (located in the STATUS register), determine which data memory bank is active. See section 2.2 in the PIC16F87X data sheet for a table of bank select bits. For example, to access the general purpose register at address 0xA0 you must select bank 1 (i.e. RP1:RP0 =01).

Page 13: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

The banksel directive can also be used to select the desired data memory bank, similar to the way in which the pagesel command is used to select the desired program memory page. First define the following values: BANK0 EQU 0x0000 ; Data Memory start positions BANK1 EQU 0x0080 BANK2 EQU 0x0100 BANK3 EQU 0x0180 Then to select the bank, say bank1 for example, use the following in the body of your main program: banksel BANK1 ; select data memory bank 1

3. EEPROM Memory The EEPROM memory is nonvolatile meaning its contents are not erased when you turn off the power to the PIC. This is useful for storing information such as the state of your system before it is powered-down since this information can then be retrieved when the system is powered-up again. Data stored in the Data memory section (RAM) is erased when the power is removed from the PIC. For instructions on using EEPROM see section 4.0 in the PIC16F87X data sheet.

3.3 Instruction Set The PIC16F87X uses a RISC-based architecture with only 35 instructions. See section 13.1 in the PIC16F87X data sheet for instruction descriptions. Some of the more common instructions will be reviewed here. An instruction is either byte-oriented or bit-oriented. The following bits of a register. bcf STATUS , RP0 ; Clear the RP0 bit in the STATUS register. bsf STATUS , RP1 ; Set the RP1 bit in the STATUS register. ; Anything after a semicolon is a comment. Remember that the RP0 and RP1 bits are used to select the data memory bank. The instructions above will select bank 1 (i.e. RB0:RB1=01). The following is an example of a byte-oriented instruction. clrf STATUS ; Clear the entire STATUS register. Many instructions use the ‘W’ register, which you can think of as a temporary storage place. To store a value in one of the general purpose registers, say at address 0x20 (in bank 0), you could use the following commands: bcf STATUS , RP0 ; select bank 0 bcf STATUS , RP1 movlw 0x35 ; writes 0x35 to the W register movwf 0x20 ; moves the value from the W register to the ; general purpose register at address 0x20 To make your code more readable you can define labels to replace numerical values. These labels are defined at the beginning of your program using the EQU (equate) assembler directive. length EQU 0x35 ; Equate the label length with value 0x35. lreg EQU 0x20 ; Equate the label lreg with value 0x20. With these defined the above instructions could now be replaced with: movlw length ; Load the W register with the value 0x35.

Page 14: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

movwf lreg ; Move the value from the W register to the ; general purpose register at address 0x20. An additional benefit to using labels is that if you decide to change any one of the values in your source code, you need to only change the value of the label rather than having to search through your entire source code for every occurrence of that value. 3.3.1 Mathematical Operations Mathematical operations can be difficult since the instruction set includes commands only for addition and subtraction and there are no hardware floating-point operations. If you require this functionality in your code, there are some software emulation libraries that can be found on the MicroChip website (www.microchip.com), but they are not always easy to find. Try a search for ‘math emulation’. When programming in C, many of the advanced math functions are available to you. addwf lreg , W ; Add the contents of W and lreg and stores ; the result in W. Here the W register is the destination for the result of the addition, as specified by the second argument to the instruction. Alternatively, you can specify lreg to be the destination register by replacing W with F (file). addwf lreg , F ; lreg is now the destination register

3.3.2 Flow Control You can control the flow of your program using conditional or unconditional branches. Conditional branches (i.e. btfss, btfsc, incfsz, decfsz) test the value of the operand and branch accordingly. An example of an unconditional branch is the goto instruction. A useful technique to use with flow control is ‘labeling’. You can label locations in your program and branch to that location using the label name. For example: bcf STATUS , RP0 ; Select bank 0. bcf STATUS , RP1 LOOP decfsz lreg , F ; Decrement the value in the lreg ; register and skips the next ; instruction if the result is 0. goto LOOP ; Branch to LOOP if result not 0. goto ZERO ; branch to ZERO if result is 0. : : ZERO instructions :

3.4 Subroutines Using subroutines is a way to make your program modular. It also shortens the length of your main program making it easier to follow. You can think of subroutines as mini programs that are called from the main program. Subroutines are usually defined after the main program. Consider the code below: main main program instructions : call incW ; call incW subroutine :

Page 15: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

goto main ; return to beginning of main program ; subroutines are declared here incW incf W ; increment the value in the W register 3 times incf W incf W return ; return to main program Here a subroutine called incW is defined. The purpose of the subroutine is to increment the value in the W register 3 times. In the main program you call the subroutine using the call instruction with the subroutine name as the operand. When the call command is executed the main program jumps to the incW subroutine, executes the commands defined there, then returns to the main program when the return instruction is executed. Unlike many other microcontrollers, the PIC contains no user-accessible stack. Adding the fact that the programmer’s model contains only one working register, subroutines passing more than a single byte need to make use of the file register memory. Additionally, the stack in the PIC16F87X devices is only 8 locations deep. Recursive calling is thereby discouraged. Interrupts, described next, also require stack locations and reduce the possible call-depth. 3.5 Interrupts Interrupts are an alternative to polling for events and can make processing time more efficient, since your program doesn’t need to wait for selected events to occur by polling. Every time a selected event triggers an interrupt, the PIC stops execution of the program and jumps to the interrupt service routine. Once finished, the PIC resumes normal execution of your program, where it left off. Each of the built-in modules has the ability to cause an interrupt for one reason or another. The ADC, for example, can cause an interrupt when it has completed a conversion while the UART can produce an interrupt when its transmit buffer is empty or when its receive buffer is full. When an interrupt occurs, the PIC jumps to the program memory location 0x0004, the interrupt vector. The program space available in this location is very limited so it’s recommended that you place your interrupt service routine (ISR) elsewhere in program memory and just put a GOTO command in the interrupt vector space (see the example program in section 3.6). Instructions on writing Interrupt Service Routines (ISRs) are provided in section 12.10 of the PIC16F87X data sheet. Compared with other microcontrollers, the PIC16F87X family has a few notable differences with respect to interrupt processing. 1. Only one interrupt vector is used. Your ISR must determine (through status bits) which subsystem

generated the interrupts. 2. Context of the CPU is not saved, except for the return Program Counter. Your ISR must be written to

compensate for this. 3. Each interrupt uses one internal stack location until processing is complete. Only 8 stack locations are

available and subroutine calls also use them. Due to this limitation, use of nested interrupts is discouraged. However, nesting is disabled by default.

3.6 Putting It All Together Now you are almost ready to write our own PIC assembly program. You can use any text editor to write your source code as long as you save it with a .asm extension. It is recommended that you use MPLAB, a free program provided by Microchip, to write and compile your source code. You can download a copy of MPLAB from the Microchip website (www.microchip.com) or from the EE401 website. There is a copy of MPLAB installed on each of the computers in the EE 401 lab.

Page 16: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

Below is a very simple program that is used to show the necessary components for an assembly program. Also, MPLAB contains source code templates (f873temp.asm and f877temp.asm), which can be found in mplab/template/code/. You can use these as a starting point. ;************************************************************ ; * ; This is a program header. Here you give a brief * ; description of what your program does and any other * ; important information about it. You should include * ; the name of the program, your name, the date you * ; wrote the program and/or the last date it was * ; modified. Include a course name if applicable. * ; * ; name: example.asm (program name) * ; date: 9 July 2002 (date created) * ; created by: David R. Bull (that’s me) * ; course: EE401 * ; * ; This program doesn’t do anything (since there are no * ; instructions in the main program). It is just used * ; to illustrate the necessary components of an assembly * ; language program for the PIC microcontroller. * ; * ;************************************************************

list p=16f873 ; this tells MPLAB which PIC ; microcontroller you are using. ; use p=16f877 when necessary.

#include <p16f873.inc> ; this file defines the names and ; locations of all the Special ; Function Registers (i.e. W register) ; use p16f877.inc when necessary. ; The following commands set the PIC’s configuration bits. For now ; you don’t really need to understand how the configurations bits ; affect anything, but if you really want to see section 12.1 in the ; PIC16F87X data sheet. ; You can also set the configuration bits manually when you are ; downloading your program to the PIC (see section 4.1)

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_OFF & _XT_OSC & _WRT_ENABLE_ON & _LVP_OFF & _CPD_OFF

; Label Definitions TEMP1 EQU 0x20 TEMP2 EQU 0x21 BANK0 EQU 0x0000 ; Data Memory start positions BANK1 EQU 0x0080 ; for use with banksel directive BANK2 EQU 0x0100 ; see section 3.2 BANK3 EQU 0x0180 page0 EQU 0x0000 ; program memory start positions page1 EQU 0x0800 ; for use with pagesel directive page2 EQU 0x1000 ; see section 3.2 page3 EQU 0x1800 ; This is where your program code begins ORG 0x00 ; when the PIC is powered up or after a reset ; the instruction at address 0x00 is the first ; to be executed. The ORG instruction tells ; the device programming the PIC to start ; writing the following instructions at the ; address indicated (in this case at address

; 0x00). clrf PCLATH ; selects page0 of the program memory goto main ; jumps to beginning of your program

Page 17: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

; Only include this next section if you are using interrupts. ORG 0x04 ; when an interrupt occurs the program jumps ; to the instruction at address 0x04 goto service ; jumps to the subroutine ‘service’, which will ; handle the interrupt. ; this is the start of your main program main the instructions for your main program go here. ; this is the interrupt service routine service the instructions for your interrupt service routine go here. END ; this is the end of the program. The source code for some example programs are available on the EE401 website. It may help to take a quick look at the code to become more familiar with the contents of an assembly language program.

Page 18: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

4.0 Assembling Your PIC Program To actually make your MicroBoard “do” anything, it needs to be running a program. Since you’ve probably not written anything yet, a multi-talented program has been supplied for you. Well, actually two programs have been written (debug873.asm and debug877.asm); one for the PIC16F873 MicroBoard and the other for the PIC16F877 MicroBoard. They are essentially the same program but are tailored to suit the slight differences between the two MicroBoards. Thus, from now on, I will simply refer to these two programs as debug87x.asm. You can find a copy of these programs on the EE401 website. In this section you will use MPLAB to compile debug87x.asm (you can also use MPASM but instructions will not be given here). In the next section you will burn the program’s object code, debug87x.hex, to your microboard.

To assemble the program, follow these steps:

1. Open MPLAB 2. Open your source code file (debug87x.asm) 3. In the ‘Project’ menu select ‘Build Node’

And that’s it. A new window will open that shows the results of the process. If your source code is error free the message ‘Build Completed Successfully.’ will appear in this window. Assembling generates a *.hex file (in this case it will be debug87x.hex) and places it in the same directory as your source code file. This is the file that you will download to the PIC microcontroller. You can also download the *.hex files from the EE401 website. If there are errors in your code then this window will contain the appropriate error messages. 4.1 Writing Your Program to the PIC The PIC has a flash type program memory, which can be written, erased and rewritten. This is very useful for debugging (you can test out the capabilities and connections of your circuitry before letting your project run amuck). Typically the flash memory can go through several thousand of these cycles so you don’t need to worry about it breaking down on you. You will need to use the computer in the EE401 lab with a PICSTART Plus Development Programmer connected to it (see figure 4.1 below). Now we will download debug87x.hex onto the PIC.

Page 19: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

1. Open MPLAB 2. Ensure that the PICSTART Plus is connected to the

computer and that the power cord is plugged in. 3. Put your PIC chip in the uppermost position in the

socket on the PICSTART Plus, with the notch on the PIC pointing to the top.

*** WARNING *** Do NOT put your chip in the PICSTART upside-down or you will permanently damage the programmer or destroy your PIC!

Figure 4.1

Figure 4.3

8. Finally, click the Program button to burn your program to the PIC. You will receive a message

notifying you if the program was transferred to the PIC successfully. Take the PIC out of the programmer, plug it back into your MicroBoard, and you’re ready for business.

4. Choose PICSTART Plus>Enable Programmer. The window shown in Figure 4.2 will open if the programmer is attached correctly. If the box does not appear, check the connections and try again.

5. Select the PIC device that you are using (either PIC16F873 or PIC16F877).

6. Choose File>Import>Import To Memory and select your program file (debug87x.hex).

7. Click on the Configuration Bits button. A new window should open (shown below); select the options shown in Figure 4.3.

Figure 4.2

Page 20: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

Part III The MicroBoard In Action

Page 21: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

5.0 Debug87x When the MicroBoard is running debug87x.hex you can control many of its functions through a serial link to a personal computer. HyperTerminal (or other terminal program) is used to communicate with the MicroBoard in a command-prompt fashion. Using and configuring HyperTerminal is very easy, just follow these steps:

1. Open HyperTerminal (in the start menu) 2. Enter a name in the box and click OK (any name will do). 3. Select either Direct to COM1 or Direct to COM2 in the Connect Using menu then click OK. Most

PCs will have 2 serial ports (COM1 and COM2) and either one can be used. Make sure that if you select COM1 you connect the serial cable to the COM1 serial connection on the back of the PC and vise versa.

4. Select 9600 in the Bits Per Second menu. This is the Baud Rate. 5. Select 8 in the Data Bits menu. 6. Select None in the Parity menu. 7. Select 1 in the Stop Bits menu. 8. Select None in the Flow Control menu (select Hardware if you are using hardware flow control). 9. Click OK.

You should now see a window before you that is blank and has a blinking cursor in the upper left corner. Connect your MicroBoard to the PC using a computer serial cable. Then connect the MicroBoard to the power supply. If your MicroBoard is working properly a help menu will appear in this window. This help menu provides you with a list and brief description of the commands used to control the different MicroBoard modules. These commands are also discussed in the following sections of this user manual. To initiate a command simply type it in at the prompt and press ENTER. Debug87x ignores uppercase commands, letters and hexadecimal numbers. With hardware flow control enabled you can send a string of commands to the PIC all at once (like a script) and the PIC will execute the commands in the order they are received (for an example, see section 6.1.6). If you are using the PIC16F873 the default state for hardware flow control is disabled. See the following section (section 5.1) for instructions on enabling hardware flow control. If you are using the PIC16F877 hardware flow control is already enabled. To create a script, simply save to a text file a set of commands in the order you would like them carried out. Start a HyperTerminal session and select Transfer > Send Text File… Choose your saved script. The commands will be sent to the PIC and executed one by one. In the future, you may want to use the subroutines in debug87x as part of your own program. If you modify the contents of debug87x or write your own program from scratch, you’ll need to compile and burn the new program to your PIC using the procedure discussed in sections 4.0 and 4.1. 5.1 Hardware Flow Control Hardware flow control is a method to control the flow of data from one device to another using hardware generated handshaking signals. On your MicroBoard pins RB1 and RB2 are used to implement the handshaking signals. RB2 is an output and is connected to the CTS (Clear to Send) pin of the DB-9 connector. When ready to receive data from the PC this pin is set low (active low). To stop the PC from sending data this pin is set high again. RB1 is an input and is connected to the RTS (Request to Send) pin of the DB-9 connector. The PC will signal it is ready to receive data by setting this pin low (active low). If you are using the PIC16F873 MicroBoard follow these steps to enable hardware flow control. Hardware flow control is already enabled on the PIC16F877 MicroBoard.

1. Add two 470Ω resistors at positions R33 and R34 (beside the MAX232 chip). 2. Solder a wire from the end of R34 (end closest to DB9 connector) to pin RB1 on the PIC. 3. Solder a wire from the end of R33 (end closest to DB9 connector) to pin RB2 on the PIC.

Page 22: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure
Page 23: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

6.0 Peripheral Features To make your programming less stressful, the wonderful folks over at Microchip have built several modules into the microcontroller you will be using. These modules automate certain processes, such as timing, serial communication, pulse width modulation and analog to digital conversion. Each of these modules have their own registers to control their operation; once they are set your code will continue to execute in parallel with the operation of the modules. These peripheral features are discussed in detail in the PIC16F87X data sheet and the PICmicro Mid-Range Reference Manual, which can both be downloaded from the EE401 website. The information in the following sections is meant to supplement the information in these documents. In these sections you will find:

1. Instructions on writing your own assembly language code to control the modules. 2. Instructions on using debug87x.hex to control the modules. 3. Writing C programs to control the modules. 4. Examples to illustrate what you can do with them.

The PIC16F873 has 22 I/O pins, divided into 3 ports (Port A-Port C), which can be configured in various ways to communicate with many different peripheral devices. The PIC16F877 has an additional 11 pins (33 in total) that are divided into 2 ports (Port D and Port E). Port A has 6 pins, Port B-Port D each have 8 pins and Port E has 3 pins. Many of these pins are multiplexed with more than one function. You can control the function of each pin (or group of pins) by writing the proper value to the appropriate Special Function Register. The simplest function for these pins is general purpose digital I/O. You can configure any pin as a digital output and set its value high (+5V) or low (0V), or configure it as a digital input and read its present value. Each port has an associated PORTx register (bi-directional port) and a TRISx register (data direction register). Setting a bit in the TRISx register makes the corresponding pin an input, clearing the bit makes it an output. For example, setting bit 4 in the TRISB register makes pin RB4 an input. Setting a bit in the PORTx register sets the corresponding pin high (as long as it is configured as an output). Clearing the bit set the pin low. For example, setting bit 3 in the PORTC register sets the output pin RC3 high. Data is input by reading from the PORTx register. The following code is an example of how to implement this in your program. bsf STATUS , RP0 ; select bank1 bcf STATUS , RP1 movlw B’11110000’ ; configure RB7:RB4 as inputs movwf TRISB ; configure RB3:RB0 as outputs bcf STATUS , RP0 ; select bank0 movf PORTB , W ; reads inputs and stores in W register movlw B’1010’ ; sets outputs RB3 and RB1 high, movwf PORTB ; RB2 and RB0 low Each port, and the alternate function(s) of its pins, is discussed in its own section below. Refer to section 3.0 of the PIC16F87X data sheet for additional information on I/O ports. 6.1.1 Port A (and Port E) In this section ports A and E are discussed together since they both are multiplexed with analog inputs (with the exception of pin RA4, which is multiplexed with the Timer0 module). Port E is available on the PIC16F877 only. The function of each pin in these two ports is selected using the A/D port configuration

6.1 I/O Ports

Page 24: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

control bits (the lower four bits of the ADCON1 register). Refer to Section 11.0 (Register 11-2) in the PIC16F87X data sheet for a table of the A/D port configuration control bits. For example, to configure all the pins in ports A and E for general purpose digital I/O you must write B’011x’ (B’0110’ or B’0111’) to the lower four bits of the ADCON1 register. (PIC16F873 only) Pin RA4 has a weak internal pull-up. If you will be using this pin for digital I/O you must connect an external pull-up resistor. The analog inputs are used with the Analog-to-Digital Converter (A/D) module. See section 6.7 for instructions on using the A/D module. The ADON bit (ADCON0<0>) controls whether or not the A/D module is operating. If you are not using the A/D module then it is recommended that you turn it off by clearing the ADON bit. (PIC16F877 only) The three pins in port E also function as control inputs for the Parallel Slave Port (PSP) module. To disable the PSP module clear the PSPMODE bit in the TRISE register. See section 6.2 for more information on the PSP. Data is read from/written to ports A and E using the PORTA and PORTE port registers, respectively. The data direction registers are TRISA and TRISE. Table 6.1: Summary of Registers Associated with PORTA and PORTE.

Name Address Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

PORTA 0x05 RA5 RA4 RA3 RA2 RA1 RA0TRISA 0x85PORTE 0x05 RE2 RE1 RE0TRISE 0x85 IBF OBF IBOV PSPMODE

ADCON0 0x1F ADCS1 ADCS0 CHS2 CHS1 CHS0 GO/DONE ADONADCON1 0x9F ADFM PCFG3 PCFG2 PCFG1 PCFG0

PORTE data direction bits.

PORTA data direction bits.

The following code shows how ports A and E can be initialized for digital I/O: bcf STATUS , RP0 ; select bank0 bcf STATUS , RP1 clrf PORTA ; clear ports A and E to initialize clrf PORTE bsf STATUS , RP0 ; select bank1 bcf STATUS , RP1 movlw b’0110’ ; configure ports A and E for movwf ADCON1 ; digital I/O movlw b’111000’ ; set RA5:RA3 and RE0 as inputs movwf TRISA ; RA2:RA0 and RE2:RE1 as outputs movlw b’001’ movwf TRISE 6.1.2 Port B Three of the pins in Port B are multiplexed with the low voltage programming option. The low voltage programming option allows PIC microcontrollers to program by one another. Refer to the Special Features Section in the PIC16F87X data sheet for instructions on using this feature. Pins RB7:RB4 have an interrupt on change feature, which is discussed in section 12.10 of the PIC16F87X data sheet. Pins RB1 and RB2 are reserved for hardware flow control (see section 5.1) and can only be used for digital I/O if hardware flow control is not being used.

Page 25: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

Data is read from/written to port B using the PORTB port register. The data direction register is TRISB. Table 6.2: Summary of Registers Associated with PORTB.

Name Address Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

PORTB 0x06 RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0TRISB 0x86

OPTION_REG 0x81 RBPU INTEDG T0CS T0SE PSA PS2 PS1 PS0PORTB data direction bits.

6.1.3 Port C Port C is shared by many of the peripheral functions, such as the timer modules, the pulse-width-modulator, the analog-to-digital converter and the serial communication modules. Refer to the corresponding peripheral section for the appropriate PORTC settings. Data is read from/written to port C using the PORTC port register. The data direction register is TRISC. Table 6.3: Summary of Registers Associated with PORTC.

Name Address Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

PORTC 0x07 RC7 RC6 RC5 RC4 RC3 RC2 RC1 RC0TRISC 0x87 PORTC data direction bits.

6.1.4 Port D (PIC16F877 Only) Port D is multiplexed with the Parallel Slave Port (PSP) module. To disable the PSP module the PSPMODE bit (TRISE<4>) must be cleared. Data is read from/written to port D using the PORTD port register. The data direction register is TRISD. Table 6.4: Summary of Registers Associated with PORTB.

Name Address Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

PORTD 0x08 RD7 RD6 RD5 RD4 RD3 RD2 RD1 RD0TRISD 0x88TRISE 0x85 IBF OBF IBOV PSPMODE PORTE data direction bits.

PORTD data direction bits.

6.1.5 Using Digital I/O Ports Example The source code to accompany this example (flashall.asm and flash_c.c) can be found in the PIC Code section of the EE401 website. When you first put your MicroBoard together it is a good idea to test if the I/O ports are working properly. You can use the program flashall.asm as one test method. This program configures all ports as digital I/O outputs and continually toggles the outputs. The delay between switching from high to low (or low to high) is ~1.5 seconds. To visually see that everything is working properly you can connect an LED to each of the output pins. Make sure that there is a current limiting resistor in series with the LED. You should draw no more than 25 mA from any of the I/O pins. If you are using the PIC16F873 MicroBoard, the RA4 pin will not switch unless an external pull-up resistor is connected (see section 6.1.1). 6.1.6 Using I/O with debug87x.hex Setting the data direction is accomplished using the input and output commands. >> output b4 - configures pin RB4 as an output >> input c2 - configures pin RC2 as an input To set the value of an output pin use the set and clear commands.

Page 26: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

>> set b4 - sets output pin RB4 high (+5V) >> clear b4 - sets output pin RB4 low (0V) To read the value on one of the inputs use the read command. >> read c2 Pin=1 The value is displayed in the terminal window as shown above. Here it is assumed that the input pin RC2 is high. If you connect an LED to one of the I/O pins, say pin RC0, you can turn it on and off using the above commands. Remember to include a current limiting resistor.

>> output c0 - configures pin RC0 as an output >> set c0 - turns on LED >> clear c0 - turns off LED

If you have hardware flow control enabled (see section 5.1) you can use a script to perform the same task. Simply save the following commands to a text file (do not include the comments on the right). output c0 - configures pin RC0 as an output - blank line is ignored set c0 - turn on LED delay 96 - wait 1.5 sec. (see below) clear c0 - turn off LED delay 96 - wait

set c0 - turn on LED again delay 96 - wait clear c0 - turn off LED again delay 96 - wait set c0 - repeat ad nauseum … etc. In HyperTerminal select the Transfer>Send Text File… command and choose your text file. The LED should be blinking now and will stop when the commands in your script are finished executing. The delay command accepts a two digit hexadecimal number (hh) and will wait (hh x 10)ms before executing the next command. It is not terribly useful when you are connected to the MicroBoard through HyperTerminal since you, as the operator, can wait as long as you like between sending commands. The delay command is, as illustrated above, useful when sending a script of commands to the MicroBoard. >> delay 10 - waits 10 x 10ms = 160ms >> delay 8f - waits 8F x 10ms = 1430ms >> delay ff - waits FF x 10ms = 2550ms

Page 27: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

6.2 Parallel Slave Port (PSP) – PIC16F877 Only The Parallel Save Port (PSP) allows you to transmit or receive data in byte size chunks, rather than bit by bit as in serial communications. The PSP runs in slave mode, meaning the peripheral device it is connected to controls when data is read from or written to it. This is accomplished using the RD, WR and CS control inputs. Setting the PSPMODE bit (TRISE<4>) enables the PSP module. The pins RE0/RD, RE1/WR and RE2/CS function as control inputs for this module thus the data direction bits TRISE<2:0> must be set. These pins must also be configured for digital I/O by appropriately setting the A/D port configuration control bits (ADCON1<3:0>). Refer to Section 11.0 (Register 11-2) in the PIC16F87X data sheet for a table of the A/D port configuration control bits. For example, writing 0010 to ADCON1<3:0> configures the port E pins for digital I/O. Note: In this configuration the port A pins are configured as analog inputs. Port D functions as the data lines and is actually two separate registers; one that holds received data and one that holds data to be transmitted. Thus the data direction bits in the TRISD register are ignored and need not be initialized. To signal a write operation to the PSP both the WR and CS lines are set low. When either the WR or CS lines returns high the Input Buffer Full (IBF) flag bit (TRISE<7>) is set signaling that the write is complete. It is cleared when PORTD is read. The interrupt flag bit PSPIF (PIR1<7>) is also set when the write is complete. If a second write to the PSP is attempted before PORTD is read the Input Buffer Overflow (IBOV) status flag (TRISE<5>) is set. PSPIF and IBOV must be cleared manually. To signal a read operation from the PSP both the RD and CS lines are set low. The Output Buffer Full (OBF) flag bit (TRISE<6>) is cleared immediately after the read operation is initiated. It is set when data is written to PORTD. The interrupt flag bit PSPIF (PIR1<7>) is set when either the RD or CS lines returns high indicating that the read is complete. To disable interrupts the interrupt enable bit PSPIE (PIE1<7>) must be cleared. Table 6.5: Summary of Registers Associated with the PSP module.

Name Address Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

PORTD 0x08 RD7 RD6 RD5 RD4 RD3 RD2 RD1 RD0PORTE 0x05 RE2 RE1 RE0TRISE 0x85 IBF OBF IBOV PSPMODEPIR1 0x0C PSPIF ADIF RCIF TXIF SSPIF CCP1IF TMR2IF TMR1IFPIE1 0x8C PSPIE ADIE RCIE TXIE SSPIE CCP1IE TMR2IE TMR1IE

ADCON1 0x9F ADFM PCFG3 PCFG2 PCFG1 PCFG0

PORTE data direction bits.

For additional information on using the PSP module see section 3.6 in the PIC16F87X data sheet. 6.2.1 Using The Parallel Slave Port Example The source code to accompany this example (testpsp.asm) can be found in the PIC Code section of the EE401 website. In this example data will be input via the PSP and will be displayed on selected output pins. The mapping from inputs to outputs is as follows:

INPUTS RD7 RD6 RD5 RD4 RD3 RD2 RD1 RD0 OUTPUTS RA5 RA4 RA3 RA2 RA1 RA0 RB0 RB3

Connecting a LED to each of these outputs will allow you to visually see the value on each of the pins. The peripheral device controlling the PSP will be you. Consider the following schematic:

Page 28: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

+5V

WR

CS

RD0

PIC

16F8

77 RD1RD2RD3RD4RD5RD6RD7

RD

+5V

S7 S6 S5 S4 S3 S2 S1 S0

Pushbutton

30

29

28

27

22

21

20

19

8

9

10

Figure 6.1: Schematic – Parallel Slave Port Example

Using this setup you can manually toggle the value of each of the inputs. For example, when switch 1 (S1) is open pin RD1 will be held high. Closing switch 1 sets pin RD1 low. You can also manually control the WR control input. Normally the WR input is held high but when the pushbutton is depressed it will be set low. The CS input is permanently held low (i.e. the PSP is always activated) and since the RD control input is not used it is held high. The procedure is very simple.

1. Set the desired input value using the switches. 2. Press the pushbutton.

… and Voila! You should now see the input value displayed on the outputs. Let’s analyze the source code. The program begins by initializing the PSP module (initializepsp subroutine). The next subroutine (initializeoutputs) configures the pins RA5-RA0, RB3 and RB0 as digital I/O outputs and initializes their values to zero. Data is input in the readpsp subroutine. When the IBF flag bit is set (pushbutton is pressed and released) the data on the PORTD input pins is read in and stored in a memory location labeled DATAREG. The displaydata subroutine simply transfers the data from the DATAREG memory location to the appropriate output pins.

Page 29: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

6.3 Timer Modules Built into the PIC are three timer modules. Timer 0 is a general-purpose 8-bit timer/counter. Timer 1 is a 16-bit timer/counter that can be used with the capture/compare/PWM module. Finally, timer 2 is an 8-bit timer that is used to control the PWM module. Refer to sections 5-7 in the PIC16F87X data sheet for detailed instructions on using the respective timer modules. 6.4 Master Synchronous Serial Port (MSSP) The Master Synchronous Serial Port (MSSP) module is used for serial communication with peripheral devices. The MSSP module can operate in one of two modes:

1. Serial Peripheral Interface (SPI) 2. Inter-Integrated Circuit (I2C)

Only the SPI module will be discussed in this manual. For instructions on using the I2C module refer to section 9.2 of the PIC16F87X data sheet. 6.4.1 Serial Peripheral Interface (SPI) Instructions on using the SPI module will not be provided in this manual since clear and detailed instructions are provided in section 9.1 of the PIC16F87X data sheet. Instead a detailed example using the SPI module will be presented here. 6.4.2 Adding Additional Outputs Using Shift Registers and SPI The source code to accompany this example (addouts.asm) can be found in the PIC Code section of the EE401 website. Before continuing familiarize yourself with the operation of the 74HC595 shift register. Using the SPI and some external shift registers you can increase the number of digital outputs. Consider the following schematic:

Figure 6.2: Schematic – Adding Additional Outputs Using Shift Registers The output pin of the SPI (SDO) is fed into a bank of 74HC595 shift registers connected in series. The outputs of the shift registers will be used as additional output pins. In this case we have 8x4=32 additional output pins. Data is shifted into the bank of shift registers one byte at a time. A byte of data output from the SPI will be shifted into shift register A. The contents of shift register A will be shifted into shift register B, and so on. Every time you want to change the value on the output pins all four shift registers must be updated, thus four bytes of data are shifted in from the SPI. You are limited in the number of additional outputs only by the time delay in updating the shift registers. It is possible, say, to connect 500 shift registers in series to create an additional 500x8=4000 outputs. However, to change the value on just one pin you would have to update all 500 shift registers.

15 1 2 3 4 5 6 7

12 16,10 8,13

14 9

11

+5V

15

14

1 2 43 5

11 12 16,10

6 7

9

8,13

+5V

15 1

14

11

42 3 5 6

16,1012

7

9

8,13

+5V

15

14

1 2 43 5

11 12 16,10

6 7

9

8,13

+5V

74HC595 74HC595 74HC595 74HC595SDO

SCK

RA3

PIC

16F8

77

5

18

24

Page 30: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

The SPI will be configured in master mode and it will control the clock (SCK). With Vcc=+5V on the 74HC595 the maximum shift register clock frequency is ~25MHz. Assuming the PIC clock frequency (Fosc) is 4MHz, we can safely set the frequency of SCK to Fosc/4=1MHz. For this setting the configuration bits SSPM3:SSPM0 (SSPCON<3:0>) must all be cleared. The waveforms in Figure 9-2 in the PIC16F87X data sheet will be used to decide the configuration of the CKP (SSPCON<4>) and CKE (SSPSTAT<6>) bits. The shift register clock on the 74HC595 (pin11) is rising edge triggered so the data on the SDO pin must be stable on the rising edge of SCK. There are two configurations that satisfy this requirement:

1. CKP = 1 , CKE = 0 2. CKP = 0 , CKE = 1

The only difference between these two configurations is that in the first configuration the clock idle value is high and in the second configuration the clock idle value is low. The clock idle value is not important in this case so either configuration will do. The first one will be used in this example. Pin RA3 on the PIC16F877 is configured as a digital I/O output and will be used to send a pulse to update the shift registers. Data shifted into the shift registers does not appear on the output pins until a pulse is received on the register clock input pin (pin 12 on 74HC595). The example program addouts.asm begins by defining four constants: BYTE1, BYTE2, BYTE3 and BYTE4. These values of these constants were arbitrarily defined and these values will be output to the four shift registers, starting with BYTE1. The main program begins with a call to the initializespi subroutine. This subroutine initializes and enables the SPI module in the configuration discussed above. The initializeRA3 subroutine that follows simply configures pin RA3 as a digital I/O output and initializes the value to zero. Next the four bytes of data are transmitted using the sendbyte subroutine. This subroutine begins by clearing the interrupt flag bit SSPIF. The data transfer is accomplished by writing the value from the W register to the SSPBUF register. Note: the appropriate value must be loaded to the W register before the call to the sendbyte subroutine is made. Since there are four consecutive calls to this subroutine (only one byte is sent at a time) we make sure that the transfer is complete before control is given back to the main program. This is accomplished by polling the interrupt flag bit SSPIF. When the SSPIF bit is set (signaling the transfer is complete) the return command is executed and the main program resumes. After the four bytes are transmitted the output pin RA3 is pulsed to update the outputs of the shift registers. You can now measure the voltages on the output pins to confirm that the program has executed properly. In this example the SPI input pin (SDI) is not used. In a similar fashion an additional number of input pins can be added using the SPI and some parallel load shift registers (74HC165). This would require the use of the SPI input pin (SDI). 6.4.3 Using the SPI with debug87x.hex Use the spiinit command to initialize the SPI module. Data transfer is initiated using the spitrans command. Transfers are done synchronously: as the MicroBoard outputs a byte from the SPI it reads in a byte from the device connected to its input. For example: >> spiinit ; initialize the SPI >> spitrans 28 ; outputs the byte 0x28 and receives the byte 0x92 val=92 Here it is assumed that there is an SPI device connected to the input of the SPI module, which sends the hex byte 0x92.

Page 31: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

Consider the example in the previous section (section 6.4.2). Using the configuration shown in Figure 6.2 we can send data to the shift registers using the following commands. Let’s also assume that we have grounded the SPI input pin since it is not being used. >> spiinit - initialize the SPI >> output a3 - used to update shift register outputs >> clear a3 - clear to initialize >> spitrans 45 - send four bytes to bank of shift registers val=00 - val=00 since input pin is grounded >> spitrans a9 val=00 >> spitrans 05 val=00 >> spitrans ff val=00 >> set a3 - send pulse to update shift registers >> clear a3 6.4.4 Using the LCD with debug87x.hex If you would like to incorporate an LCD into your project, your TA can provide you with a parts kit and instructions on assembling the LCD. The LCD can display 2 rows of 20 characters. If you are using the PIC16F873 you will have to manually wire the LCD to your MicroBoard (ask Ed Tiong for details). On the PIC16F877 MicroBoard there is a 10 pin connector that the LCD will plug directly into (for now, don’t worry about the polarity). The SPI module on the PIC is used to serially load the 595 shift register on the LCD board. The data is then loaded, in parallel, to the LCD display. The debug87x.hex program contains subroutines to control the LCD. Writing your own code to control the LCD would be a very involved task. Rather it is recommended that you copy and paste the subroutines from debug87x into your own code and modify as necessary. The following commands are used to control the LCD. >> lcdinit - initializes the LCD Make sure that the LCD is connected to your MicroBoard before executing this command. You should see the following text on the display. EE 401 U. of Alberta If there is nothing on the display reverse the connection and execute the lcdinit command again. If it still doesn’t work seek assistance from your TA. >> lcdclear - clears the LCD and puts the cursor in the “home”

position (top left corner) >> lcdput line1 - puts the text ‘line1’ on the LCD >> lcdline2 - moves the cursor to the second row >> lcdput line2 - puts the text ‘line2’ on the LCD. This text will

be on the second line. >> lcdline1 - moves the cursor back to the first row 6.4.5 Writing a C Program To Control The LCD Thanks to one of your fellow students at the U of A (Alex Stickel), writing a C program to control the LCD is as easy as pie. Simply include the header file LCD.h (written by Alex) in your C program. Now to send text to the LCD use the following command: printf(sendLCD, "Welcome to EE401!"); // writes text to LCD See the example source code (lcd_ex.c – available only for the PIC16F877 MicroBoard) found in the PIC Code section of the EE401 website. This program also writes text to a terminal window on a PC (see section 6.5.2).

Page 32: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

6.5 Universal Synchronous Asynchronous Receiver Transmitter (USART) Instructions on using the USART module will not be provided in this manual since clear and detailed instructions are provide in section 10.0 of the PIC16F87X data sheet. Instead a detailed example using the USART will be presented here. 6.5.1 Using the USART with a Personal Computer The source code to accompany this example (tstusart.asm) can be found in the PIC Code section of the EE401 website. Before continuing, ensure that you have read sections 5.0 and 5.1 to familiarize yourself with hardware flow control and HyperTerminal. In this example the USART module will be used to communicate with a personal computer. HyperTerminal (or other terminal program) is used to communicate with the MicroBoard in a command-prompt fashion. The PC and your MicroBoard are connected using a computer serial cable. The male end of the cable connects to the DB-9 connector on your MicroBoard and the female end connects to one of the serial connectors (COM1 or COM2) on the back of your PC. To properly communicate, both the HyperTerminal and the USART must be configured with the same settings. First let’s choose how we want to configure the PIC. A baud rate of 9600 will be used (this value was chosen arbitrarily). If you look at tables 10-3 and 10.4 in the PIC16F87X data sheet you will see that we have 2 options for selecting this baud rate.

1. high baud rate disabled (BRGH = 0), SPBRG = 6 (decimal value) 2. high baud rate enabled (BRGH = 1), SPBRG = 25 (decimal value)

Comparing the % error for these two setting we see that the second configuration has a lower % error (0.16 as opposed to 6.99). We will use this configuration. The mode of communication is Asynchronous since there is no clock signal. Instead start and stop bits are used. When asynchronous mode is selected the USART automatically attaches start and stop bits to the data. We will only be sending byte size data chunks so 8-bit transmit and receive is selected. Hardware flow control will be used. Interrupts are disabled. Now open and configure HyperTerminal with the following settings (refer to section 5.0 if necessary).

1. 9600 Bits Per Second. 2. 8 Data Bits. 3. No Parity. 4. 1 Stop Bits. 5. Hardware Flow Control.

You should have a window before you that is blank and has a blinking cursor in the upper left corner. It looks pretty boring but this window is where all of the action happens. When you push a button on the keyboard, the 8-bit ASCII value of the character you typed is sent to your MicroBoard. Data received by HyperTerminal is written in this window. For this example it is necessary to change some of the advanced properties of Hyperterminal. To do so follow these steps:

1. In the File menu (top left corner) select Properties. 2. In the ‘Backspace key sends’ section, select ‘Ctrl+H,Space,Ctrl+H’. Now when you hit the

backspace key three characters are sent. The first one (Ctrl+H) moves the cursor back one space. The second (Space) is, as you may have guessed, a regular space and the third moves the cursor back again. This effectively moves the cursor back one space while deleting the character in that space.

3. Click on the ‘ASCII Setup’ button.

Page 33: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

4. Check the box beside ‘Send line ends with line feeds’. Now when you hit ENTER both a carriage return and line feed are sent. A carriage return moves the cursor to the beginning of the line and a line feed moves the cursor to the next line.

5. Click OK. 6. Click OK.

Let’s analyze the example source code. The program begins by initializing the USART in the configuration discussed above (initializeusart subroutine). Next the program sends the prompt “Type something..:” to the HyperTerminal window using the writeprompt subroutine. The writeprompt subroutine sends the prompt string to the HyperTerminal window one character at a time using the outputbyte subroutine. The outputbyte subroutine works in the following way. It first checks to see if the PC is ready to receive data by testing the value on RB1. This pin is low when the PC is ready. Then it checks if the input buffer is empty by testing the TXIF flag bit. If the buffer is full, then it waits until it is empty before it continues. If it is empty the byte in register W is written to the TXREG register (USART transmit register). Note that to send the desired character you must load its ASCII value into the W register before calling the outputbyte subroutine. After writing the prompt the program calls the inputbyte subroutine, which functions as follows. First the CTS_PIN (RB2) is set low to enable reception. At this point the PC will start sending data. When the RCIF flag bit is set (the receive buffer is full) reception is disabled and the byte in the receive buffer is moved to the W register. Next a call to the outputbyte subroutine is executed to send the received character to the terminal window. The program loops through the consecutive inputbyte and outputbyte subroutines, effectively receiving a character from HyperTerminal then immediately sending it to be displayed in the terminal window. So anything that you type will appear on the screen. Pressing the enter key will jump the cursor to the beginning of the next line. 6.5.2 Writing a C Program To Send Text To A Terminal Window If you want to send text to a PC via the RS-232 serial interface, simply include the following statements at the beginning of your C program: #use delay(clock=4000000) // defines clock speed of the MicroBoard #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // configures RS-232 And now we are ready to go. Use the following command to send text: printf("\r\nHi There"); // writes text to terminal See the example source code (lcd_ex.c or rs232.c) found in the PIC Code section of the EE401 website. This program also writes text to an LCD (see section 6.4.5).

Page 34: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

6.6 Pulse Width Modulator (PWM) You can think of the Pulse Width Modulator as a square wave generator where the period and duty cycle are variable. There are 2 PWM outputs (CCP1 and CCP2) and they may have different duty cycles but they share a common period. The PWM period is calculated using the following formula:

)2(4)12( prescaleTMRToscPRPeriod ⋅⋅⋅+= You have control over the value in the PR2 register (Timer2 module’s register) and the TMR2 prescale value. PR2 holds an 8-bit number and TMR2 prescale value can be either 1, 4 or 16. TMR2 prescale value is set using the T2CKSP1:T2CKSP0 bits (T2CON<1:0>). See section 7.0 (Register 7-1) in the PIC16F87X data sheet. Tosc is the oscillator period (0.25 µs). For example, if PR2 contains B’01100111’ = 103 and T2CKSP1:T2CKSP0 = B’01’ (prescale = 4) then the PWM period is (103+1)*4*0.25*4 = 416 µs. The duty cycles are specified with a 10-bit number. Each PWM pin has an associated CCPRxL register and CCPxCON register. The CCPR1L register holds the upper 8 bits of the duty cycle value for CCP1 and CCP1CON<5:4> holds the lower 2 bits. For CCP2 the CCPR2L and CCP2CON registers are used. The following equation defines the PWM duty cycle:

)2()4:5:( prescaleTMRToscCCPxCONCCPRxLDutyCycle ⋅⋅><= For example, if CCPR1L contains B’00110011’ and CCP1CON<5:4> contains B’10’ the duty cycle for CCP1 is 206*0.25*4 = 206 µs. See the PIC Code section of the EE401 website for sample code (testpwm.asm). 6.6.1 Using the Pulse Width Modulator with debug87x.hex The two built-in PWM modules are started using the pwmstart1 and pwmstart2 commands. The two PWM outputs, CCP1 and CCP2 (pins RC2 and RC1, respectively), will share a common period but they each may have different duty cycles. The commands pwmperiod, pwmduty1 and pwmduty2 control the period and respective duty cycles. All three commands take an 8-bit number as a parameter (hexadecimal 00 to FF). Use the following formula to calculate the period of the PWM: Period = (pwmperiod value + 1) x 16µs The duty cycles can be calculated (as a percentage) using the following: Duty Cycle = [ (duty cycle value) / (pwmperiod value) ] * 100% For example: >> pwmstart1 - configures pin RC2 as a PWM output >> pwmstart2 - configures pin RC1 as a PWM output Note: On the PIC, pin RC2 corresponds to PWM output pin CCP1 and pin RC1 corresponds to pin CCP2. >> pwmperiod ff - sets the period to ~4ms >> pwmduty1 40 - sets duty cycle for CCP1 to ~1ms (40/FF) >> pwmduty2 7f - sets duty cycle for CCP2 to ~50% (7F/FF) >> pwmstop1 - stops the output on CCP1 >> pwmstop2 - stops the output on CCP2

Page 35: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

If you connect an LED to one of the PWM outputs, you can vary the brightness through varying the duty cycle. 6.6.2 Digital-To-Analog Converter Using PWM You can build a very simple digital-to-analog converter using the PWM module, a 1KΩ resistor and a 10µF capacitor.

Choosing appropriate values for the resistance and capacitance forms a low pass filter with the necessary time constant for creating an analog signal from the PWM output. The values shown are merely a suggestion; you’ll need to make sure that the PWM period is sufficiently short compared to the RC time constant or ripple will be evident. The output of this signal is controlled by the duty cycle of the PWM signal: to increase the voltage, increase the duty cycle and vise versa. >> startpwm >> pwmperiod ff >> pwmduty1 00 - output will be 0 volts >> pwmduty1 80 - output will be 2.5 volts >> pwmduty1 ff - output will be 5 volts

Page 36: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

6.7 Analog-To-Digital Converter (A/D) Module Built into the PIC16F873 is a 10-bit analog-to-digital converter. There are five input channels available on the PIC16F873 and eight available on the PIC16F877. Since all the inputs are multiplexed together, the ADC can only sample one input at a time. Also, two of the A/D pins can be used as voltage references if the signal being sampled is outside the range of the PIC supply voltage. See section 11.0 in the PIC16F87X data sheet for detailed instructions on using the A/D module.

All five ADC ports have a limited amount of diode protection to prevent input voltages outside of the Vdd to Vss range. A better way to make sure your PIC doesn’t get fried by stray analog voltages though, is to set up a small circuit before the signal reaches the ADC pin similar to the one to the right. This circuit will not let any voltage hit the ADC pin unless it is between 0 and 5V and doesn’t depend on the PIC’s limited protection.

The PIC16F87x data sheet also recommends that the maximum input impedance of an analog source be less than 10kΩ and that any current on any pin should not exceed ±20mA. 6.7.1 Using A/D with debug87x.hex To initialize the A/D module use the adcinit command. Use the adcread command to sample one of the inputs. The program will return a 10-bit number (0x0000 to 0x03FF) corresponding to the voltage level on the input pin. >> adcinit - initialize the A/D module >> adcread 3 - sample the voltage on input 3 val=0155 - in this case, (155/3FF)x5V ~ 1.67V After running adcinit all the A/D inputs are enabled. That is, you can no longer use these pins for general purpose digital I/O. Running either the input or output commands on one of the A/D inputs will automatically turn the A/D module off.

Page 37: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

Part IV

Appendices

Page 38: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

Appendix A - PIC16F873 MicroBoard Assembly Package This appendix contains a parts kit list, assembly checklist, schematic diagram and board layout diagram that you will use when constructing your PIC16F873 MicroBoard. A1 – Parts Kit List Your PIC16F873 MicroBoard kit should include the following parts. If you are missing any go harass the TA. Qnty Description Part Number 1 100µF 25V electrolytic radial lead (100-220µF 25V or greater) C16 0µ01F 50V mono-cap C2, C5, C7, C17, C22, C232 10µF 25V electrolytic radial lead (10-22µF 25V or greater) C3 & one for test 5 0µ1F 50V mono-cap C24-C28 1 2.1mm barrel power connector CN11 9 pin female right angle DB connector CN61 1N4000 1A 50V rectifier (1N4000-1N4007) D11 LM7805 5V 1A TO-220 voltage regulator IC11 PIC16F873-04 IC41 L293 dual H-bridge driver IC51 MAX232A RS-232 interface chip IC84 4K7Ω ¼ watt resistor R1, R29, R30 & one for test3 470Ω ¼ watt resistor R23, R24 & one for test2 330Ω ¼ watt resistor R31, R32 1 4.00MHz 3 pin ceramic resonator with integral capacitors Y12 14 pin IC sockets Sockets for IC4 2 16 pin IC sockets Sockets for IC5 and IC81 Red LED one for test 1 9Vdc 500ma wall adapter 1 MicroBoard Printed Circuit Board

Page 39: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

A2 – Assembly Checklist Soldering the components in this order is recommended (check them off as you go).

Part Number

Description Notes

R1 4K7Ω ¼ watt resistors Yellow Violet Red Gold

R29

R30

R23 470Ω ¼ watt resistors Yellow Violet Brown Gold

R24

R31 330Ω ¼ watt resistors Orange Orange Brown Gold

R32

C24 0µ1F 50V mono-caps (marked 104 for 10 x 10^4 pF)

C25

C26

C27

C28

C2 0µ01F 50V mono-caps (marked 103 for 10 x 10^3 pF)

C5

C7

C17

C22

C23

D1 1N400x White band towards Y1

Sockets 2 14 pin IC sockets for IC4 Notches away from DB9/CN6 connector

Socket 18 pin socket for IC6 Do Not solder DIP IC’s directly into the PCB

Socket 18 pin socket for IC8

Y1 4.00MHz 3 pin ceramic resonator

CN1 2.1mm barrel power connector Tabs may need to be bent to fit holes in PCB

CN6 9 pin DB female connector

C1 100µF 25V negative to edge of PCB

C3 10µF 25V positive to edge of PCB

IC1 LM7805 tab to edge of PCB

Page 40: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

A3 - Schematic Diagram (Eagle)

Page 41: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

A4 - Board Layout Diagram (Eagle)

Page 42: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

A5 - Power Checklist Before installing any ICs, test the board for proper power connections. The following measurements are to be taken on the auxiliary power connector. Before connecting the power perform the following measurement.

Measurement Probe Connection Desired Value

Use a DMM to measure resistance and check for a short circuit from Vcc to ground.

Aux: Pin 1 to Pin 3 > 500KΩ

Now plug in the wall adaptor and connect it to CN1 (2.1mm barrel connector). Measure the following: Vcc (with reference to ground) Aux: Pin 1 to Pin 2 8 < Vcc < 20 Vdc

+5Vdc supply (with reference to ground) Aux: Pin 6 to Pin 2 +5Vdc Measure the following on the PIC IC socket: Voltage on MCLR IC4: Pin 1 to Pin 19 +5Vdc

Supply voltage IC4: Pin 20 to Pin 19 +5Vdc Measure the following on the L293D driver socket: Drive supply voltage IC7: Pin 8 to Pin 4 Vcc

Logic supply voltage IC7: Pin 16 to Pin 13 +5Vdc

ENA voltage IC7: Pin 1 to Pin 4 +5Vdc

ENB voltage IC7: Pin 9 to Pin 12 +5Vdc Measure the following on the MAX232 socket: Supply voltage IC8: Pin 16 to Pin 15 +5Vdc

If, and only if, all of the above power checks are correct continue. Disconnect the power and insert the MAX232 IC into its socket. Pay careful attention to its orientation since connecting it backwards will likely destroy the chip. Power up the MicroBoard again and measure the following on the MAX232 socket: Supply voltage IC8: Pin 16 to Pin 15 +5Vdc

+10 V supply IC8: Pin 2 to Pin 15 +10Vdc

-10 V supply IC8: Pin 6 to Pin 15 -10Vdc The power check is now complete. If all of your measurements were as expected then everything should work fine. You can insert the other ICs into their appropriate sockets and you are ready to program the PIC.

Page 43: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

Appendix B - PIC16F877 MicroBoard Assembly Package This appendix contains a parts kit list, assembly checklist, schematic diagram and board layout diagram that you will use when constructing your PIC16F877 MicroBoard. B1 – Parts Kit List Your PIC16F877 MicroBoard kit should include the following parts. If you are missing any go harass the TA. Qnty Description Part Number 1 100µF 25V electrolytic radial lead (100-220µF 25V or greater) C12 10µF 25V electrolytic radial lead (10-22µF 25V or greater) C3 & one for test 11 0µ1F 50V mono-cap C2, C5, C7, C15, C18, C19,

C24-C28 1 2.1mm barrel power connector CN11 9 pin female right angle DB connector DB091 1N4000 1A 50V rectifier (1N4000-1N4007) D11 LM7805 5V 1A TO-220 voltage regulator IC11 PIC16F877-04 IC41 L293 dual H-bridge driver IC61 MAX232A RS-232 interface chip IC84 4K7Ω ¼ watt resistor R1, R25, R26 & one for test30 470Ω ¼ watt resistor R10-R13, R23, R27-R30,

R35-R38, R43 -R48, R52, R54, R56-R64

1 4.00MHz 3 pin ceramic resonator with integral capacitors Y11 40 pin IC socket Socket for IC4 2 16 pin IC sockets Sockets for IC6 and IC81 Red LED one for test 1 9Vdc 500ma wall adapter 1 MicroBoard Printed Circuit Board

Page 44: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

B2 - Assembly Checklist Soldering the components in this order is recommended (check them off as you go).

Continued on following page…

Part Number

Description Notes

R1 4K7Ω ¼ watt resistors Yellow Violet Red Gold

R25

R26

R35 470Ω ¼ watt resistors Yellow Violet Brown Gold

R47

R43

R38

R11

R10

R12

R13

R36

R45

R37

R44

R54

R52

R56

R64

R57

R63

R58

R62

R59

R61

R60

R46

R30

R48

R29

R28

R27

R23

Page 45: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

C2 0µ1F 50V mono-caps (marked 104 for 10 x 10^4 pF)

C5

C7

C15

C18

C19

C24

C25

C26

C27

C28

D1 1N400x White band towards Y1

Socket 18 pin socket for IC6 Do Not solder DIP IC’s directly into the PCB

Socket 18 pin socket for IC8

Socket 40 pin IC socket for IC4 Notche away from DB9/CN6 connector

Y1 4.00MHz 3 pin ceramic resonator

CN1 2.1mm barrel power connector Tabs may need to be bent to fit holes in PCB

CN6 9 pin DB female connector

C1 100µF 25V negative to edge of PCB

C3 10µF 25V positive to edge of PCB

IC1 LM7805 tab to edge of PCB

Page 46: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

B3 - Schematic Diagram (Eagle)

Page 47: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

B4 - Board Layout Diagram (Eagle)

Page 48: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

B5 - Power Checklist Before installing any ICs, test the board for proper power connections. The following measurements are to be taken on the auxiliary power connectors (labeled RAW and VCC, see board layout diagram). Before connecting the power perform the following measurement.

Measurement Probe Connection Desired Value

Use a DMM to measure resistance and check for a short circuit from Vcc to ground. VCC connector > 500KΩ

Use a DMM to measure resistance and check for a short circuit from RAW to ground. RAW connector > 500KΩ

Now plug in the wall adaptor and connect it to CN1 (2.1mm barrel connector). Measure the following: Raw Supply RAW connector 8 < Vcc < 20 Vdc

+5Vdc supply (with reference to ground) VCC connector +5Vdc Measure the following on the PIC IC socket: Voltage on MCLR IC4: Pin 1 to Pin 12 +5Vdc

Supply voltage IC4: Pin 32 to Pin 12 +5Vdc

Supply voltage IC4: Pin 11 to Pin 12 +5Vdc

Ground IC4: Pin 31 to Pin 12 0Vdc Measure the following on the L293D driver socket: Drive supply voltage IC6: Pin 8 to Pin 4 Vcc

Logic supply voltage IC6: Pin 16 to Pin 13 +5Vdc

ENA voltage IC6: Pin 1 to Pin 4 +5Vdc

ENB voltage IC6: Pin 9 to Pin 12 +5Vdc Measure the following on the MAX232 socket: Supply voltage IC8: Pin 16 to Pin 15 +5Vdc

If, and only if, all of the above power checks are correct continue. Disconnect the power and insert the MAX232 IC into its socket. Pay careful attention to its orientation since connecting it backwards will likely destroy the chip. Power up the MicroBoard again and measure the following on the MAX232 socket: Supply voltage IC8: Pin 16 to Pin 15 +5Vdc

+10 V supply IC8: Pin 2 to Pin 15 +10Vdc

-10 V supply IC8: Pin 6 to Pin 15 -10Vdc The power check is now complete. If all of your measurements were as expected then everything should work fine. You can insert the other ICs into their appropriate sockets and you are ready to program the PIC.

Page 49: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

Appendix C - Soldering Tips Installing Components Before soldering trial fit all components to ensure all hole sizes are correct and components will fit properly. You will likely need to clean the copper on a milled board of tarnish and oils. Cleaning can be accomplished by lightly abrading the surface with at kitchen, non-detergent pad (Scotch/Brillo pad), fine steel wool, or fine sandpaper. Care must be taken not to damage the board. Be careful not to touch the copper surfaces with your hands; fingerprints will contaminate the clean copper surfaces. Some boards may be tin-plated for grater durability. Install components from shortest first to highest last. With this in mind, solder the bias first, then install the resistors and diodes, followed by the IC sockets and monolithic capacitors and then lastly the electrolytic capacitors and connectors. Appendix ? includes assembly checklists for the PIC16F873 and PIC16F877 MicroBoards. It is recommended that you solder the components in that order, checking off the components as you go. Insure that polar circuit elements, such as diodes and electrolytic capacitors, are installed in the proper orientation. If they are not installed properly, this will cause you unnecessary grief and time spent troubleshooting the board. When installing IC sockets ensure that the notched section matches with the orientation of the Ics. The notch indicates the location of pin 1 on the IC. Soldering Remember safety always comes first! Always wear safety glasses when soldering. It is also recommended that you not wear leather pants as the leather material absorbs chemicals and you end up with a very toxic pair of pants. Turn the soldering iron on and allow time for the iron to heat up. While the iron is heating up, ensure that the sponge is damp, as you will need to clean the tip of the iron prior to soldering. To clean the soldering iron tip, simply wipe the heated tip of the iron against the damp sponge. The tip should have a bright silvery appearance after you have cleaned it.

1. Clean the component lead and pad with a flux pen. 2. Insert the component lead into the proper position on the printed circuit board. Don’t pull the

component too tight against the board; the component must be able to expand as the temperature changes.

3. Use the hot soldering iron at a 45° angle. The iron’s tip should come in contact with both the

component lead and the pad on the circuit board. Allow the lead and pad to heat up (a few seconds).

Page 50: PIC MicroBoard Manualee401/resource/manuals/MicroboardRev5.pdf · Hosted mode and will outline some of the methods you can use to “test drive” your board. ... 3.2 Memory Structure

4. Apply the solder to the heated lead and allow it to heat the solder. Try not to touch the solder to the soldering iron. Allow the solder to melt and spread around the whole connection. After the solder flows around and adheres to the part being connected remove the soldering iron.

5. Examine the solder connection. If the connection is cone shaped the proper temperature was reached; bad solder appears grayish and round.

6. Cut the component’s wire leads, leaving a bit of wire protruding from the solder. 7. Examine the area around and remove any excess solder.