Useful Tools

contact us contact tutor/group email to a friend accessibility options report a fault
Microcontrollers

Microcontrollers

Practical Session 3: Programming a Device


This exercise makes use of the Matrix Multimedia (MMM) board and C compiler tools (even though you will not actually be using C to program the PIC just yet).

Having used MPLAB to create, assemble, and simulate your assembly programs, you will want to program the PIC chip in the MMM board. On shipment, the MMM board will have a PIC16F84 device fitted. To program the PIC in the MMM board you will need to create a .hex file and use this to program the PIC. The .hex binary file is the executable file that must be placed into the program memory of the PIC.  The programmer is controlled by software which is part of the C2C development environment.  This means you need to load up the C2C programming environment, then open the .asm file that you created with MPLAB. Then program the MMM board from within the C2C environment.

Here is the procedure.


Installing the C2C Development Environment Software

The small Matrix Mutimedia CD-Rom contains a C programming environment and the software to program the PIC. Before installing the software, read the README file. Then click on the "install C4CPICs" executable to install the software, choosing the FULL installation rather than the MINIMAL option.

Once the software has been installed, click on "Start", then on "Programs", then "C for PIC Micros", then select the ‘pdf’ file "PIC Micro Development Board Documentation" and "Open". This pdf file which can be read using the Adobe Acrobat reader (downloadable free from www.adobe.com). It is advisable to print the contents of the file as it provides useful information about the MMM board. Section 6 of this document describes how to test the MMM board.

If the board is brand new, it will already have a test program in memory and will automatically run this application on power up. You should see all the Port B LEDs flash on and off at an approximately one second intervals. If your MMM board does this, you can assume it is working OK.


Programming a Device

For this section the PIC16F84 will be used. This is similar to the PIC16C54 in many ways but is slightly more complex internally. Externally it is pin-identical. The PIC16F84 is also electrically erasable which makes it ideal for design and demonstration purposes since it can be erased and reprogrammed over and over again.

Note
The target device name for any programs you want to run on the MMM board will have to be changed from PIC16C54 to PIC16F84.
To change the target device to PIC16F84, go to the Options menu and select 'Development Mode'.  Select PIC16F84 using the arrow key.  When PIC16F84 has been selected, click on reset to accept.  The indicator at the bottom of the screen should now display PIC16F84.

 

Edit and assemble the following program using MPLAB.  Simulate the program so the function of the program is understood.

Note:  Although you can enter and assemble this program in the C2C environment, you cannot simulate it. That is why we ask you to use MPLAB.

; Experiment EX3 for the MultiMedia Board.
;
; This assembly program sets portb LEDs all on
; Then proceeds to decrement in binary when sw0 is pressed.
;
; Note that sw0 = 0 when switch not pressed,
; going to 1 when pressed.
;
; The configuration for the MMM board is:
; S2 XT position, S3 OFF, S4 fast position, LCD switch OFF.
;
  LIST p=16F84  
;
; Program Equates
pc EQU 0x02  
status EQU 0x03  
porta EQU 0x05  
portb EQU 0x06  
trisa EQU 0x05 ; address is really 0x85 (bank 1 reg 0x05)
trisb EQU 0x06 ; address is really 0x86 (bank 1 reg 0x06)
rp0 EQU 5 ; bit 5 of status register to switch pages.
w EQU 0 ; d = 0 action is w register
f EQU 1 ; d = 1 action is specified file register.
;
; Main Program
  ORG 0x00  
  GOTO init  
;
init BSF status,rp0 ;select registers page 1
  MOVLW 0x00 ;set port B as all output
  MOVWF trisb ;set trisb register to give outputs
  MOVLW 0xff ;data to set porta as all input.
  MOVWF trisa ;configure porta as all inputs.
  BCF status,rp0 ;select registers page 0
  MOVLW 0xff ;load all 1's into the working register
  MOVWF portb ;turn all PORT B LED's on
; now test for switch-press at SA0
tpa0c  BTFSS porta,0 ;test SA0 (bit 0), skip a line if set
  GOTO tpa0c  
  DECF portb,f ;decrement portb
; again test for switch-press at SA0
tpa0s BTFSC porta,0 ;test SA0 (bit 0), skip a line if clear  
  GOTO tpa0s  
  GOTO tpa0c ;go back to do again.
  END    

First you need to ensure that the PIC is going to be configured correctly. This means that you:
- select the PIC16F84 as the PIC to be programmed.
- select oscillator XT
- select watchdog timer OFF
- select power up timer OFF
- select code protect OFF

To do this, click on START then "PROGRAMS" then within the menu select "MATRIX Multi-media", then "C for PICMicros", and finally select "Config the PICMicro".

The following window should appear:

 

Make sure that the window you see looks like the one shown here. When you are happy with this, click on OK to select your choice.

Now click on START then PROGRAMS then within the menu select MATRIX Multi-media, then C for PICMicros, and finally select C2C Compiler. The following window should appear.

 

Click on File,  then Open, then select the MPLAB folder and find your EX3.asm file.  (You will have to select the .asm file type). When you have opened the file, your active screen should look like the one below.

 

Now select Compile, then assemble (or alternatively, click on the assemble icon [Assemble] button).

The C2C environment will assemble your program and you will see a .lst  list file containing both your source and binary executable code. The Output window should show no errors.

 

Now close the Output window and the List File window.

Make sure that the MMM board is connected to its power supply and switched on, and that the printer cable supplied with the MMM board is plugged into the printer port of the PC and the MMM board.

Now click on the Program button (the one with the microchip symbol next to the Assemble button).  The .hex executable file will be programmed into the PIC and the program should now be ready to run. At this point all the LEDs connected to the PORTB should be turned on.

Pressing SA0 switch on and off should cause the Port B LEDs to decrement in a binary manner.

Pressing the reset button should cause all Port B LEDs to turn on, since you will be resetting the program.


Exercises

Modify the program so that:

  1. A binary up count sequence is produced on Port B for each SA0 operation.
  2. A binary up count sequence is produced on Port B with an approximate 1-second delay interval between values.  (i.e. the count sequence is free running, without the need to operate SA0).

Note: both these exercises can be accomplished without any need for the PICSTART Plus development programmer.

 

So, we are going to be using 'MPLAB' software for producing the machine code and for assembler code-simulation and step-thru debugging facility of the code. And we will use the 'C2C' environment for downloading the machine code to the PIC16F84 chip on the MatrixMultimedia development board. The C2C software can also produce the machine code from the assembler, but it's limitation is that it cannot step through and inspect the individual registers.

 

For simple examples of code to help get an appreciation of more of the PIC Instruction Set please click on the link:-

[Link to Examples of Using Assembler Instructions and appreciation via simulation]

 

[Link to Practical Session 4]

 

[back to top]


Updated 02.07.08 ML

Site Search

Powered by Google
Site Map