University of Brighton Introduction to Electronic CAD with Microchip PIC Microcontrollers & Proteus VSM Tutor Martin Bates 2010
University of Brighton
Introduction to
Electronic CAD with Microchip PIC Microcontrollers & Proteus VSM
TutorMartin Bates
2010
Introduction
This course was devised for the University of Brighton Product Development Centre based in St Leonards, Sussex UK for delivery from 2010. It assumes that Proteus VSM Version 7 ECAD software has been installed.
Proteus VSM is a leading electronic design package for embedded applications, and recognised third-party design tool for the Microchip PIC development system, MPLAB. The combination of these tools provides an integrated microcontroller circuit design package with full interactive on-screen circuit simulation (ISIS) and professional PCB design (ARES). It is ideal for developing small-scale microcontroller solutions for industrial control and instrumentation, and upgrading from outdated discrete digital and analogue designs.
The purpose of this short course is to introduce the software package, allow participants to evaluate Proteus VSM against alternative design packages, and provide an insight into current trends in ECAD. It will be illustrated by numerous specific examples of programming and interfacing the PIC in assembler and C drawn from the books listed below.
It will allow you to:
Create a project in MPLAB Design a PIC microcontroller application circuit Write a program in assembly language or C Test the application using Proteus embedded co-simulation tools Download and run an test program on a demo target system
The follow-up sessions were tailored to the participant’s particular interests, offering the following topics:
PIC MCU architecture and assembler programming Interfacing the PIC to standard I/O devices: switches, LEDs, keypad, 7-
segment display, LCD display (serial & parallel) PIC system design, with analogue inputs, serial interfaces RS232, I2C and
SPI and memory expansion C Programming essentials (CCS PIC C): basic program elements, program
structure, I/O functions and data processing Further C programming, including serial interfacing and system design PCB design and prototyping using ARES layout software and a CIF PCB mill
The sample applications are designed around the popular PIC 16F877A, a standard 8-bit PIC MCU with a full range of peripheral interfaces. Test programs will also be demonstrated for target systems using:
Microchip PICstart, PICkit2 & ICD2 programming & debugging interfaces Microchip 44-pin and LPC demo target boards Microchip PICDEM Mechatronics board Selected original designs
Reference & Resources
Websites
www.picmicros.org.ukSupport site for books on PIC microcontrollers by Martin BatesExample applications, information & presentations can be downloaded
www.microchip.comManufacturer of the PIC microcontroller and MPLAB development system MPLAB may be downloaded free of charge & PIC data downloaded
www.labcenter.co m Design and supply of Proteus VSM electronic design softwareA demonstration version of Proteus may be downloaded, or licensed version purchased
www.ccsinfo.comSupplier of CCS PIC C complier (PCM)
Books
PIC Microcontrollers, an Introduction to MicroelectronicsElsevier webpage
Interfacing PIC Microcontrollers, Embedded Design by Interactive SimulationElsevier webpage
Programming 8-bit PIC Microcontrollers in C, with Interactive Hardware SimulationElsevier webpage
1 Sample Application
Open up ISIS (Proteus schematic capture and simulation module) and select the sample application
C:\Program Files\Labcenter Electronics\Proteus 7 Professional\SAMPLES\VSM for PIC16\EPE Graphics LCD Demo\GEPE456.DSN*
This is a typical microcontroller application with an LCD display and very few other components. The demo circuits will run, but cannot be modified.
Press the play control (lower left) to run the simulation, and the on-screen active push button to start change the display output (small square is drawn)
Press the pause control and the assembler source code is displayed. The step control button allows the program to be executed one instruction at a time.
The circuit is created using parts from a component library, seen in the DEVICES window. The toolbar on the left provides other tools for making the schematic.
When the drawing is complete, the program is attached via the SOURCE menu. Assembler source code can be written within ISIS, and the PIC assembler utility MPASMWIN selected as the code generation tool.
Activity 1
Load the design GEPE456.DSN, run, step the source code and investigate how the program is attached to the microcontroller unit (MCU).
* Note MPASMWIN.EXE, the assembler for PIC programs, has a limited filepath length, and will report an error in this case where none exists. Typically, it will not work with files on the desktop; move your project files to a location nearer to the root of the drive in use.
2 ISIS Schematic Capture
This section describes the procedure for entering the hardware circuit diagram.
2.1 If ISIS is already running, select New File and save the blank design in the folder of your choice as BAR1.DSN.
2.2 Add a microcontroller to the DEVICES list by clicking on button [P] and select Microprocessor ICs, PIC 16 Family, PIC 16F877A. Add a green bar graph display (Optoelectronics, Bargraph Displays) and an 8-way resistor pack (Resistors, Resistor Packs) from the device libraries. The resistor values are set by double clicking and editing the device properties to Component Value ‘180R’ and Model Type ANALOGUE.
2.3 Select the components in turn and place them by left clicking on the schematic. They can be deleted by right click (select), right click (delete).Components can be flipped or rotated before placement (buttons lower left).Connect up by left clicking on the component leads.Note that we are in default Component Mode on the left hand Mode Toolbar.
2.4 Select Terminal Mode, and place a ground terminal for the resistor pack common connection. You may need to move the bar graph label by selecting and dragging. Note that the power connections are implicit, and no clock components are needed for simulation purposes.
2.5 The schematic can be zoomed in using the mouse scroll wheel, and zoom reset using the Zoom to View Entire Sheet button.
2.6 Save the design as shown below.
3 Program Creation
This section describes how to edit, assemble and attach the program to the MCU.
3.1 Select from the main menu Source, Add/Remove Source Files.Click on New button, and navigate to the application folder in which the design is saved and save as BAR1.TXT, and respond Yes to create new file.In the Code Generation Tool dialogue, select MPASM.The BAR1.ASM file can now be selected from the source menu, when an edit window appears.
3.2 Enter the program listed below, saving it in the project folder:
; BAR1.ASM; Your Name & date; Test program for ISIS system
PROCESSOR 16F877
CLRWTRIS 06
loop INCF 06GOTO loop
END
3.3 To assemble the program, select from the Source menu, Build All. Hopefully, a message indicating success will appear. If not, check for typos.
3.4 To attach the program, double click on the MCU, browse for the Program File BAR1.HEX and select it. It is described as a C Object Code file.
3.5 Click on the Run control and the bar graph should flash to indicate the simulation is active. When paused, the source code should be displayed.If not, select it in the Debug menu.
4 PIC Internal Architecture
The PIC uses a Harvard architecture which separates the program and data busses, RISC instruction set and instruction pipelining to improve program execution rate.
16F887 File Register Set
5 PIC Instruction Set
F = Any file register (specified by number or label), example is 0C W = Working register, W L = Literal value (follows instruction), example is 0F9 * = Use of these instructions not now recommended by manufacturer
Operation Example
MoveMove data from F to W MOVF 0C,WMove data from W to F MOVWF 0CMove literal into W MOVLW 0F9
RegisterClear W ( reset all bits and value to 0 ) CLRWClear F ( reset all bits and value to 0 ) CLRF 0CDecrement F ( reduce by 1 ) DECF 0CIncrement F ( increase by 1 ) INCF 0CSwap the upper and lower four bits in F SWAPF 0CComplement F value (invert all bits) COMF 0CRotate bits Left through Carry Flag RLF 0CRotate bits Right through Carry Flag RRF 0CClear ( reset to zero ) the bit specified ( eg bit 3) BCF 0C,3Set ( to 1) the bit specified (eg bit 3) BSF 0C,3
ArithmeticAdd W to F ADDWF 0CAdd F to W ADDWF 0C,WAdd L to W ADDLW 0F9Subtract W from F SUBWF 0CSubtract W from F, placing result in W SUBWF 0C,WSubtract W from L, placing result in W SUBLW 0F9
LogicAND the bits of W and F, result in F ANDWF 0CAND the bits of W and F, result in W ANDWF 0C,WAND the bits of L and W, result in W ANDLW 0F9OR the bits of W and F, result in F IORWF 0COR the bits of W and F, result in W IORWF 0C,WOR the bits of L and W, result in W IORLW 0F9Exclusive OR the bits of W and F, result in F XORWF 0CExclusive OR the bits of W and F, result in W XORWF 0C,WExclusive OR the bits of L and W XORLW 0F9
Test & SkipTest a bit in F and Skip next instruction if it is Clear ( =0 ) BTFSC 0C,3Test a bit in F and Skip next instruction if it is Set ( = 1) BTFSS 0C,3Decrement F and Skip next Instruction if it is now Zero DECFSZ 0CIncrement F and Skip next Instruction if it is now Zero INCFSZ 0C
JumpGo To a Labelled Line in the Program GOTO start Jump to the Label at the start of a Subroutine CALL delayReturn at the end of a Subroutine to the next instruction RETURNReturn at the end of a Subroutine with L in W RETLW 0F9Return from Interrupt Service Routine to next instruction RETFIE
ControlNo Operation - delay for 1 cycle NOPGo into Standby Mode to save power SLEEPClear Watchdog Timer to prevent automatic reset CLRWDTLoad Port Data Direction Register from W* TRIS 06Load Option Control Register from W* OPTION
The result of Arithmetic and Logic operations can generally be stored in W instead of the file register by adding ‘ ,W ’ to the instruction. General Purpose Register 1, address 0C, represents all file registers (00 - 4F). Literal value 0F9 represents all values 00 - FF. Bit 3 is used to represent File Register Bits 0 - 7. For MOVE instructions data is copied to the destination but retained in the source register.
6 Program Development
The application can be further developed by adding inputs to control the sequence.
6.1 To create a new application file set, save a copy of the application BAR1 as BAR2.DSN in a new folder called BAR2. Open the source code, rename it BAR2.ASM and save in the new folder. Now replace the attached source code, and Build All to create BAR2.HEX; attach this to the MCU. Check that it runs as before.
6.2 Modify the hardware with a switch and pull-up resistor on RD0. A power terminal is also needed to provide a notional +5V.
6.3 Amend the program as shown below, save, assemble and run.
; BAR2.ASM; Your Name & date; Demonstrates input and delay;*********************************************************************
PROCESSOR 16F877 ; Specify MCU for assembler
CLRW ; Initialise Port B for outputTRIS 06
stop BTFSC 08,0 ; Wait for button pressedGOTO stop
CLRF 06 ; Clear and count while button pressedloop BTFSC 08,0
GOTO stop ; and stop when releasedINCF 06
CLRF 20 ; Delay routinedelay DECFSZ 20
GOTO delay
GOTO loop ; Repeat main loop
END ; Terminate assembler
The program demonstrates input and delay operations.
Note the following:
Header block Comment delimiter (;) MCU type declaration Port register addresses (Port B = 06, Port D = 08) Port initialisation for output (TRIS) Branch instruction (BTFSC) and bit identification (register 08, bit 0) Decrement register and branch (DECFSZ) Data registers start at 20h
Activity 2
Download the PIC 16F877 data sheet from www.microchip.com and note the internal architecture and register set (W, SFRs and GPRs) (see below)
7 Virtual Instruments
Virtual instruments are available to check the system outputs. The oscilloscope, logic analyser and counter/timer are shown below.
8 Program Debugging
8.1 Source code debugging is carried out by pausing the program and selecting PIC CPU, Source Code from the Debug menu. The program can then be single stepped and the outputs checked after each instruction.
8.2 Normally, the Step Into button is used, Step Out and Step Over being used with subroutines.
8.3 Breakpoints can be set and the program run between them to skip to areas of interest or, in this case, run through the delay routine quickly. The breakpoint at the last instruction in the loop allows the total loop time to be displayed on the counter/timer, and the delay routine could be adjusted for a specified value of output period.
Activity 3
Adjust the delay loop value in program BAR2 for an output period of 1.00 ms by substituting a delay counter value of 0A6. Confirm the delay period by setting a suitable breakpoint and checking the execution clock reading between after one cycle.
9 Program Download
The program machine code PROGNAME.HEX is downloaded within PROGNAME.OBJ which contains memory location info, configuration code etc.
Originally, PICs had to be transferred from the application board into a programmer ZIF socket for programming. ICD allows the chip to programmed in circuit via an ICD connector (6 pins). This is connected to the PGC (clock) and PGD (data) pins of the MCU, plus MCLR (Reset) and supply pins, so that no external supply is needed. Baseline and older mid-range devices needed a special header for this.
The ICD connector needs to be designed into the application hardware.
TargetBoard
PICkit2ProgrammerModule
USB link
DownloadSoftware
10 Microchip LPC (Low Pin Count) Demo Board
The LPC board has a PIC 16F690, 4 LED outputs, push button and analogue pot inputs, ICPD header, expansion connector and small prototyping area.
Schematic
11 LPC Test Program Source Code
;; LPC2.ASM; M Bates 27-3-09 Ver 2.1; Test program for LPC demo board; Push button rotates a LED, pot controls the speed;;*********************************************************************
PROCESSOR 16F690 ; Specify MCU for assembler__CONFIG 30D4 ; MCU configuration bitsINCLUDE "P16F690.INC" ; Standard register labels
LOCO EQU 20 ; GPR labelsHICO EQU 21
; Initialise registers................................................
BANKSEL ANSEL ; Select Bank 2CLRF ANSEL ; Port C digital I/OBSF ANSEL,0 ; except AN0 Analogue inputCLRF ANSELH ; Port C digital I/O
BANKSEL TRISC ; Select Bank 1CLRF TRISC ; Initialise Port C for outputMOVLW B'00000000' ; Analogue input setup codeMOVWF ADCON1 ; Left justify result, ref=Vdd
BANKSEL PORTC ; Select bank 0CLRF PORTC ; Clear display outputsMOVLW B'00000001' ; Analogue input setup codeMOVWF ADCON0 ; f/8, RA0, done, enable
; Start main loop...............................................
stop CLRF PORTC ; LEDs offBTFSC PORTA,3 ; Wait for button pressedGOTO stop
BSF PORTC,0 ; Switch on LED0loop BTFSC PORTA,3 ; Stop when button released
GOTO stopRLF PORTC ; Rotate output LED
BSF ADCON0,1 ; start ADC..wait BTFSC ADCON0,1 ; ..and wait for finish
GOTO waitMOVF ADRESH,W ; store result high byteMOVWF HICO
INCF HICOCALL slowGOTO loop ; Repeat main loop
; Subroutine....................................................
slow CLRF LOCO ; delay blockfast DECFSZ LOCO
GOTO fastDECFSZ HICOGOTO slowRETURN
END ; Terminate assembler..........
Activity 4
Download the LPC2 design files from www.picmicros.org.uk, test in ISIS and download to the target hardware using PICkit2 programming module. Run the test program in the target system and confirm correct operation. The push button steps through the LEDs, and the pot controls the speed. Analyse the program, noting the initialisation and operation of the analogue input.
12 PICDEM Mechatronics Board
This is very useful demo system for control applications. It has DC and stepper motors with current driver interface, temperature and light sensors and a 3.5 digit LCD built around a PIC 16F917 MCU. It can be programmed via the ICD (RJ45) or ICSP (6-pin SIL) connector.
Simulation Schematic
Activity 5
Download the simulation version of the PICDEM board from www.picmicros.org.uk(Download Demo Files) and test (suitable Proteus licence required) using the simulation versions of the programs. Download the hardware test programs to the target system and confirm correct operation of the real hardware. Note the modifications required for the simulation version.