Sub routines and time delays with 16f88

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
richardmarks
Posts: 3
Joined: Thu Apr 10, 2008 6:29 pm
Contact:

Sub routines and time delays with 16f88

Post by richardmarks »

I've recently started teaching Microprocessor programming to A Level students. They need only do some basic programming (thank goodness) but one part of it has to be calling a sub routine. The sub routine can be already written for them in a template so basically I need to write the sub routine for them. It would be ideal if this sub routine was a time delay command as they need to use both a sub routine and a time delay command in their project (killing two birds with one stone).

The hardware we are using is a Version 3 MCU Development Board with a 16f88 chip. Previously we used an older board with a 16f84 chip and the template we used worked. Obviously the syntax has changed for the newer chip. Has anybody got any advice they could give to create/modify the code I need. The time delay previously used is commented out in the template below and I have patched together the best template I can with my limited knowledge

;*******************************************************************
; TITLE: New 16F88 project
; AUTHOR: Name
; DATE: Todays Date
;*******************************************************************

;State here what the program does:

;*********************************************************************
; DEFINITIONS
;*********************************************************************
; Configuration data
; PICmicro MCU type: 16F88
; Oscillator: RC mode, slow, VR1 fully anticlockwise (min.rate)
; LCD display: off
; 7-segment display: off
; Version 2 board settings: J14 links: Digital
;*********************************************************************

;****** REGISTER USAGE ******

;
; The following line embeds configuration data into the PICmicro

STATUS EQU H'03' ;name program location 3 as STATUS
TRISA EQU H'05' ;name program location 5(port A direction register) as TRISA
PORTA EQU H'05' ;name program location 5(port A data register) as PORTA
TRISB EQU H'06' ;name program location 6(port B direction register) as TRISB
PORTB EQU H'06' ;name program location 6(port B data register ) as PORTB

W EQU 0 ;Working
F EQU 1 ;File
C EQU 0 ;Carry
Z EQU 2 ;Zero

temp1 EQU H'11' ;Temporary register 1
temp2 EQU H'11' ;Temporary register 2
count EQU H'11' ;Counter register

;__CONFIG H'3FFB' ; This configuration will bet set via the PPP software

;*********************************************************************
; VECTORS
;*********************************************************************

ORG 0 ; Reset vector
GOTO 5 ; goto start of program
ORG 4 ; Interrupt vector
GOTO 5 ; goto start of program
ORG 5 ; Start of program memory

;*********************************************************************
; SUBROUTINES
;*********************************************************************

;By convention, subroutines are placed after the vectors so that they
;are at the bottom of memory. This avoids memory bank-switching for
;small programs.

; Time delay subroutine 'onems'=1ms,'tenths'=0.1s
;onems movlw H'0F8'
; movwf temp1
;again1 nop
; decfsz temp1,1
; goto again1
; nop
; return
;tenth movlw H'064'
; movwf temp2
;again2 call onems
; decfsz temp2,1
; goto again2
; return


;*********************************************************************
; MAIN PROGRAM
;*********************************************************************


CLRF PORTB ; clear Port B data pins
BSF STATUS,5 ; Page 1
CLRF PORTB ; set all Port B as output
BCF STATUS,5 ; Page 0

CLRF 6 ; set all Port B pins to logic 0
BSF 3,5 ; instruct program that a Page 1
; command comes next
CLRF 6 ; set all Port B pins as outputs
BCF 3,5 ; instruct program that a Page 0
; command comes next


; Pupils enter their program here

LOOPIT BSF PORTB,7 ;EXAMPLE CODE
BSF PORTB,6
BCF PORTB,7
BCF PORTB,6
GO TO LOOPIT


;*********************************************************************

END ; FINAL STATEMENT. Enter must be pressed after this to signify the end of the program

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Sub routines and time delays with 16f88

Post by Steve »

I can think of 2 required changes:

1) You must turn off the ADCs on the 16F88 so that the pins on porta (and some on portb) can function as digital i/o. To do this, set register "ansel" to zero (address = 9Bh, so it's in bank1)

2) The crystal used in the new board is 19.6608MHz, but in the old board it was 3.2768MHz. So your delay routine will need to be 6 times as long.

richardmarks
Posts: 3
Joined: Thu Apr 10, 2008 6:29 pm
Contact:

Re: Sub routines and time delays with 16f88

Post by richardmarks »

Thanks for replying Steve

Even though I'm teaching this to A level students my knowledge is less than basic.
I'm assuming that the code below will be okay for the ANSEL register

STATUS EQU H'03' ;name program location 3 as STATUS
TRISA EQU H'05' ;name program location 5(port A direction register) as TRISA
PORTA EQU H'05' ;name program location 5(port A data register) as PORTA
TRISB EQU H'06' ;name program location 6(port B direction register) as TRISB
PORTB EQU H'06' ;name program location 6(port B data register ) as PORTB
ANSEL EQU 9BH ;allows pins on Port A and some on Port B to function as digital i/o


I then took out the semi colons from infront of the time delay sub routine and used the command in a basic program. No errors show when sending it via ASM-IDE but the program I have written never runs. This happened previously before trying to change the ANSEL register. Any ideas??

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Sub routines and time delays with 16f88

Post by Steve »

ANSEL EQU H'9B'

.
.
.

BSF STATUS,5 ; Page 1
CLRF PORTB ; set all Port B as output
CLRF ANSEL ; set all i/o as digital <-------- add this line in your program
BCF STATUS,5 ; Page 0

richardmarks
Posts: 3
Joined: Thu Apr 10, 2008 6:29 pm
Contact:

Re: Sub routines and time delays with 16f88

Post by richardmarks »

Many thanks for your help Steve.

The ANSEL register command has also sorted out a problem I had where the LED's for PORTB 6 and 7 were switching off when I used any other parts of PortB. I also played around with moving the Sub Routine. I placed it after the main part of the program the pupils would write and I was then able to CALL it properly. I'm just using several NOP commands now to create a time delay in the Sub Routine.

Thanks again

Post Reply