Program description

What does it do?

The Math program demonstrates literal (constant) and register-based (variable) math instructions.

New instructions

During this activity, you will learn about these microcontroller instructions:

addlw 'add literal to W' - adds the contents of a constant specified in the program to the W register.
sublw 'subtract W from literal' - subtracts the contents the W register from a constant supplied by the program.
addwf 'add W to file register' - adds the contents of the W register to the contents of a file register. The result of this addition can be left in the W register or in the file (RAM) register by appending ,W or ,F to the instruction, respectively.

Math programming activity

The Math program demonstrates the two primary ways of performing addition and subtraction operations in the PIC microcontroller.

What you should know before starting

Microcontroller related information

The only two mathematical operations that mid-range PIC microcontrollers are capable of are addition and subtraction. Each math operation can be performed using constant or variable data, so there are actually a total of four math instructions: addlw, addwf, sublw and subwf.

In the PICmicro, math operations always make use of the W register. A number always has to be loaded into W before a math operation can proceed. This first number can be a constant supplied by the program, or a variable quantity stored in the RAM file registers. The math instructions perform their operations between the number already in W and a second number that can also be supplied by either the program or the contents of a file register.

Program requirements

To use this program you will need:

An assembled CHRP board, microcontroller, and power supply, a programming cable, and a Windows PC with the MPLAB IDE software and downloader software as described in the Output activity.

Create the program

The entire ANALOGUE.ASM program is shown below. Create an Analogue project in MPLAB, copy this code into it, and build the program.


;MATH.ASM 	v2.0	Last modified on December 10, 2008
;===============================================================================
;Description:	Demonstrates PIC math instructions.

;Start of MPLAB and processor configuration.

	include	"p16f886.inc"		;Include processor definitions 

	__config _CONFIG1, _DEBUG_OFF & _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTOSCIO
	__config _CONFIG2, _WRT_OFF & _BOR40V


;End of MPLAB and processor configuration.

;Start of hardware register equates

num1		equ	20h		;File register to store a number
num2		equ	21h		;File register to store another number

;End of register equates

	org	00h			;Start of program memory

		goto	initPorts	;Jump to initialize routine

	org	05h

initPorts	;Set Ports B and C to support CHRP digital circuitry.

		banksel	ANSELH		;Switch register banks
		movlw	01010111b	;Enable Port B pull-ups, TMR0 internal
		movwf	OPTION_REG	;clock, and 256 prescaler
		clrf	ANSELH		;Set all PORTB pins to digital
		clrf	TRISB		;Set all PORTB pins as outputs for LEDs
		banksel	TRISC		;Switch register banks
		movlw	10110000b	;Setup serial input and output pins,
		movwf	TRISC		;and set motor outputs
		banksel	PORTB		;Return to PORTB register bank

		movlw	6		;Preload number registers for later use
		movwf	num1
		movlw	4
		movwf	num2

conCon		;Demonstrate math operations using two constants.

		movlw	7		;Load W with the first constant and
		addlw	5		;add the second constant to W

		movlw	4		;Load W with the first constant and
		sublw	13		;subtract it from the second constant

regCon		;Demonstrate math operations between a constant and the contents
		;of a file register.

		movf	num1,W		;Copy the contents of num1 into W and
		addlw	8		;add a constant to it

		movlw	2		;Load W with a constant and
		addwf	num1,W		;add the contents of num1, keeping the
					;result in W
		addwf	num1,f		;Now, add contents of num1 to W again,
					;overwriting the contents of num1

regReg		;Demonstrate math operations between the contents of two
		;file registers.

		movf	num1,W		;Copy the contents of num1 into W and
		addwf	num2,W		;add contents of num2 to W

		movf	num1,W		;Copy the contents of num1 to W again
		addwf	num2,f		;and add to num2, overwriting num2

		sleep			;Stop at end of program

	org	1F00h			;Start of bootloader code area
	res	256			;Reserve memory for bootloader

	end
		

You won't need to download this program to your CHRP board—just run it in the MPLAB Sim debugger. Set up a watch window to watch the contents of the W register and file registers 20h and 21h. Step through the program and observe the contents of the registers after each step.

How the program works

Um, well, by this point you should be able to follow the code and figure out its operation.

Test your knowledge

  1. Add two numbers that produce a sum equal to or less than 255. What is the state of the Z, DC and C bits in the Status register?
  2. Add two numbers that produce a sum equal to 256. What is the state of the Z, DC and C bits this time?
  3. Add two numbers that produce a sum greater than 256. What is the state of the Z, DC and C bits after this addition?

Apply your skills

  1. make it subract