16-Bit Addition Routine
Back to top ; PALPAC Software and Electronics ; 16-bit addition routine written by Louis Christodoulou 2001-03-20. ; This code may be modified to suit the students needs. ; Always make sure that the registers do not clash with reserverved registers on ; newer devices. ; Numbers to be added are in [Byte1Hi:Byte1Lo] and [Byte2Hi:Byte2Lo] and the ; sum in [HiByteTot:LoByteTot]. Carry to bit 17 may be found in carry. org 0x00 Byte1Lo equ 0x20 Byte2Lo equ 0x21 Byte1Hi equ 0x22 Byte2Hi equ 0x23 LoByteTot equ 0x24 HiByteTot equ 0x25 status equ 3 carry equ 0 goto begin Add16Bit movf Byte1Lo,w addwf Byte2Lo,w movwf LoByteTot btfss status,carry goto no_carry incf Byte1Hi,w ADD addwf Byte2Hi,w movwf HiByteTot return no_carry movf Byte1Hi,w goto ADD begin movlw b'11111111' movwf Byte1Lo movlw b'11111111' movwf Byte2Lo movlw b'11111111' movwf Byte1Hi movlw b'11111111' movwf Byte2Hi call Add16Bit forever goto forever end

8-Bit Integer Division Routine
Back to top ; PalPac Software and Electronics ; Written by Louis Christodoulou ; 2001-03-04 ; This is an integer (signed) division routine for 2 eight bit numbers and the answer is ; stored in 8 bits in register Result. The remainder part can be found in the Remainder ; register. The dividend and the divider are placed in file registers 0x10 and 0x11 ; respectively. The two numbers used here are b'00001010'(10) and b'01100100'(100) which ; should display b'0001010'(10) and in hex 0xA. ; PIC16C5x Microcontroller course ; This material may be copied and modified to suit the student's needs. list p=16c84 list r=dec list f=inhx16 Dividend equ 0x10 Divider equ 0x11 same equ 1 StatusReg equ 3 CarryFlag equ 0 Counter equ 0x12 Remainder equ 0x13 Result equ 0x14 org 00 goto begin begin movlw b'00001010' ; load the divider parameter movwf Divider ; store it movlw b'01100100' ; load the dividend parameter movwf Dividend ; store it movlw 8 ; load counter parameter movwf Counter ; store it clrf Result ; clear the result clrf Remainder ; clear the remainder bcf StatusReg,CarryFlag ; clear the carry flag process rlf Dividend,same ; rotate bit into carry rlf Remainder,same ; rotate carry into remainder file movf Divider,w ; load the divider subwf Remainder,w ; subtract it from remainder file store in w rlf Result,same ; rotate carry into result (1) pos (0) neg btfsc Result,0 ; check if last calculation was positive goto calc_remainder ; countdown decfsz Counter,same ; coundown 8 bits goto process ; carry on forever goto forever ; do nothing forever calc_remainder movwf Remainder ; store the remainder in Remainder file goto countdown ; carry on end

4-bit Multiplication Routine using Rotate
Back to top ; PalPac Software and Electronics ; Written by Louis Christodoulou ; 2001-02-17 ; This is an integer multiplication routine for 2 four bit numbers and the answer is displayed ; in 8 bits on port b in binary. The multiplicant and the multiplier are placed in file registers ; 0x10 and 0x11 respectively. ; The two numbers used here are 15 and 15 which should display ; 225 which is binary '11100001' = 0xE1 in hex. ; PIC16C5x Microcontroller course ; This material may be copied and modified to suit the student's needs. list p=16c84 list r=dec list f=inhx16 portb equ 0x6 counter equ 0x12 answer equ 0x9 number1 equ 0x10 number2 equ 0x11 org 00 goto begin begin clrf portb tris portb clrf answer ; clear the answer movlw b'00001111' ; 15, manually initialize number movwf number1 ; save it movlw b'00001111' ; 15, manually initialize number movwf number2 ; save it movlw 4 ; counter parameter movwf counter ; load counter start_calc btfss number2,0 ; test lsd goto rotate_left ; not hi movf number1,w ; lsd hi, load number for addition addwf answer,1 ; answer = answer + result rotate_left rlf number1,1 ; rotate number for 0 insertion lsd bcf number1,0 ; make sure lsd is 0 rrf number2,1 ; get next digit decfsz counter,1 ; countdown goto start_calc ; do over movf answer,w ; prepare answer for display display_answer movwf portb ; display the answer forever goto forever ; do nothing forever end ; end

BCD Conversion from an 8-bit binary number.
Back to top ; PalPac Software and Electronics ; Written by Louis Christodoulou ; 2001-03-04 ; This is an 8-Bit binary to BCD converter. The answer is stored in ; 24 bits in register bigmsd, msd and lsd with leading zeros in each of the ; file register's msb's. (do not confuse this with register msb) ; The parameter to be converted should be placed in the w register and ; convert_bcd should be called. Once convert_bcd has completed, w should ; be loaded with the msd if the original ; number was greater than 99, to create a 3-digit BCD value. If the msd was not ; greater than 9 then the code will not change its value, but instruction cycles ; will be wasted trying to convert the number. ; PIC16C5x Microcontroller course ; This material may be copied and modified to suit the student's needs. list p=16c84 ; processor type list r=dec ; radix list f=inhx16 ; file format compiler directive msd equ 0x10 ; most significant digit lsd equ 0x11 ; least significan digit bigmsd equ 0x12 ; msd for hundreds status equ 3 ; status register carry equ 0 ; carry bit same equ 1 ; org 00 ; origin for PIC16C84 goto begin ; jump over sub-routine convert_bcd clrf msd ; clear the msd movwf lsd ; move the number to convert into lsd ten movlw 10 ; load the constant 10 subwf lsd,w ; subtract 10 from lsd btfss status,carry ; check if answer negative goto done ; conversion complete, jump to done movwf lsd ; save the answer back in lsd and incf msd,same ; increment the msd goto ten ; do the routine again done return ; return to caller convert_big clrf bigmsd ; clear hundreds movwf msd ; move the hundred number to msd hundred movlw 10 ; load the constant 10 subwf msd,w ; subtract 10 from msd btfss status,carry ; check if answer negative goto hundreddone ; conversion complete, jump to hundreddone movwf msd ; save the answer back in msd and incf bigmsd,same ; increment the bigmsd goto hundred ; do the routine again hundreddone return ; return to caller begin movlw 167 ; main program, load the number to convert call convert_bcd ; convert the tens and units movf msd,w ; load the tens into w for hundred conversion (tens>9) call convert_big ; convert the tens into hundreds (tens>9) forever goto forever ; do nothing forever or until reset or power down end ; no more code expected (compiler)

Wake up the RS232 Port on the PIC16F62x. Read a Character in and Send it back to the PC
Back to top ; RS232 Program. mode=9600,n,8,1 ; Written by Louis Christodoulou. ; This program may be modified to suit the students needs. ; The receive port of the RS232 is PortB,1 and the transmit is on PortB,2 ; PIC IS 16F627/8, internal clock @ 4Mhz. INCLUDE "P16f628.INC" LIST R=DEC ORG 00 GOTO BEGIN SETUP BSF STATUS,RP0 ; BANK 1 MOVLW B'00000000' ; INITIALISE PORT A TO OUTPUTS MOVWF 85 ; MOVLW b'00000110' ; SET PORT B 2:1 FOR RS232 PORT MOVWF TRISB MOVLW 25 ; 9K6 MOVWF SPBRG BSF TXSTA,BRGH ; SET BRGH TO HIGH BAUDRATE IN ASYNCHRONOUS MODE BSF TXSTA,TXEN ; ENABLE TRANSMIT BCF TXSTA,SYNC ; ENABLE Asyncronous serial port DEFAULT 0 ON PWR UP BCF TXSTA,TX9 ; TRANSMIT SETUP 8-bit DEFAULT 0 ON PWR UP BSF TXSTA,CSRC ; CLOCK SOURCE SELECT BIT, DONT CARE FOR ASYNCHRONOUS BANKSEL PORTA ; BANK 0 BSF RCSTA,SPEN ; ENABLE Asyncronous serial port DEFAULT 0 ON PWR UP BCF RCSTA,RX9 ; RECEIVE SETUP 8-bit DEFAULT 0 ON PWR UP BCF RCSTA,CREN ; ENABLES CONTINUOUS RECEIVE DEFAULT 0 ON PWR UP BCF RCSTA,ADEN ; IN ASYNC MODE 8-BIT, UNUSED DEFAULT 0 ON PWR UP RETURN WRITECHAR BTFSS PIR1,TXIF GOTO WRITECHAR MOVWF TXREG RETURN READCHAR BSF RCSTA,CREN ; BTFSS PIR1,RCIF ; CHECK FLAG FOR RECEIVED CHARACTER GOTO READCHAR ; NONE YET, SCAN AGAIN MOVF RCREG,W ; LOAD CHARACTER INTO W RETURN BEGIN CALL SETUP ; SET UP COMMUNICATIONS RECEIVE CALL READCHAR ; READ A CHARACTER IN (OR WAIT TILL A CHARACTER IS READ) CALL WRITECHAR ; WRITE THE CHARACTER BACK OUT FOREVER GOTO RECEIVE ; LOOP FOREVER END
Configure the comparator unit on the PIC16F62x with voltage reference.
Connect a variable resistor pin 1 to VCC, middle pin to AN0 input, and pin 3 to GND. Setup a voltage on the internal voltage reference unit and play with the variable resistor until the LED lights up. Measure the voltage and see that it is VRCON<3:0>/24 *Vcc Back to top

; This program reads in a voltage from AN0 (pin 17)and compares it to the internal voltage
; reference unit setup at a pre-determined voltage. 
; The output is tested and sets or clears an LED connected on PORTB bit 0.(pin 6)
; Use a variable resistor connected to vcc, gnd and the middle pin to AN0, and play 
; with the input voltage. You should be able to switch the LED on and off at the 
; specified [input voltage=VREF] and [input voltage <> VREF].
; The students may modify this program to suit their needs.
; clocking is internal, @4.0Mhz on a PIC16F62x micro-controller.
 
			list	r=dec
			include	"p16f628.inc"
ledbit			equ	0
delay10uregister	equ	0x20
			org	0x00
			goto	begin
delay10u		movlw	10
			movwf	delay10uregister
count10u		decfsz	delay10uregister,F
			goto	count10u
			return
setupcmp		movlw	b'00000010'	; 010 = four i/p muxed + vref
			movwf	CMCON
			bsf	STATUS,RP0	; make sure
			bcf	STATUS,RP1	; bank 1
			movlw	b'00000111'	; RA0:RA2 inputs
			movwf	TRISA
			movlw	b'10100110'	; low range, VR3:VR0 = 6
			movwf	VRCON		; Vref = (6/24) x VDD = 1.25 Volts (VDD = 5 Volts)
			bcf	STATUS,RP0	; bank 0
			call	delay10u
			return
begin			call	setupcmp
			bsf	STATUS,RP0	; bank 1
			bcf	TRISB,0		; RB0 output
			bcf	STATUS,RP0	; bank 0
check			call	delay10u
			btfss	CMCON,C1OUT
			goto	clearledbit
			bsf	PORTB,ledbit
forever			goto	check
clearledbit		bcf	PORTB,ledbit
			goto	check
			end