Header

Free PIC resources

[Front page] [BBC Mecca] [Links] [PIC micros] [Enviromyths] [The dump] [Email]

On this page


Side by side comparison of MPASM and !PICbasic

To show just how easy it is to port a program downloaded from the internet into a form that PICbasic can understand (or vice-versa) these two panels show a side by side comparison of some 'traffic lights' software for a PIC16C84

Code from MPASMCode from PICbasic
; WRITTEN BY Nigel Gardner
; COPYRIGHT Bluebird Electronics
; Web http://www.bluebird-electronics.co.uk

; RESONATOR 4.00 MHz XT
; INSTRUCTION CLOCK 1.00 MHz T= 1uS

; ***** equates *****

tmr0 equ 1 ; timer 0
pcl equ 2 ; program counter
optreg equ 81h ; option on page 1
trisa equ 85h ; port direction register page 1
trisb equ 86h ; port direction register page 1
status equ 3 ; status register

time equ .156 ; 156 * 32ms = approx 5 seconds

porta equ 5
auto equ 0 ; manual auto switch
step equ 1 ; sequence step switch

portb equ 6
red1 equ 0 ; set a of lights
yel1 equ 1
green1 equ 2
red2 equ 3 ; set b of lights
yel2 equ 4
green2 equ 5

count equ 0ch ; general purpose counter

list p=16c84




org 00 ; reset vector




goto init

;************************ subroutines ********************

; test here to see if manual mode or delay if automatic

delay
btfsc porta,auto ; test for auto switch on
goto dly1 ; automatic mode

lp1
btfsc porta,step ; if manual mode, then wait for
goto delay ; button press but check if auto
btfss porta,step ; and then release before continuing
goto $-1 ; to next sequence
retlw 0

; long delay 32ms * value in w register

dly1
movlw time ; load timer
movwf count ; use this register temporarily
del1
clrf tmr0
btfss tmr0,7 ; test tmr0 bit 7 128*256us = 32.768ms
goto $-1
decfsz count,f ; decrement, until zero
goto del1
retlw 0

; *********** initalise ports and tmr0 *************

init
clrf portb
bsf status,5 ; set up page 1
movlw 00h
movwf trisb ; port b as outputs
movlw 0fh
movwf trisa ; port a as inputs
movlw b'10000111' ; tmr0 pre-scalar /256 pullup off
movwf optreg ; 256us per count internal clock
bcf status,5 ; set up page 0

; ********* program begins here **********************

main
bsf portb,red1
bsf portb,green2
movlw time ; delay time
call delay

bsf portb,yel1
bcf portb,green2
bsf portb,yel2
movlw time ; delay time
call delay

bcf portb,yel1
bcf portb,red1
bsf portb,green1
bsf portb,red2
movlw time ; delay time
call delay

bcf portb,green1
bsf portb,yel1
bsf portb,yel2
movlw time ; delay time
call delay

bcf portb,yel1
bcf portb,yel2
bcf portb,red2
goto main


end
REM WRITTEN BY Nigel Gardner
REM COPYRIGHT Bluebird Electronics
REM Web http://www.bluebird-electronics.co.uk

REM RESONATOR 4.00 MHz XT
REM INSTRUCTION CLOCK 1.00 MHz T= 1uS

REM ***** equates *****

tmr0 = 1 :REM timer 0
pcl = 2 :REM program counter
optreg = &81 :REM option on page 1
trisa = &85 :REM port direction register page 1
trisb = &86 :REM port direction register page 1
status = 3 :REM status register

time = 156 :REM 156 * 32ms = approx 5 seconds

porta = 5
auto = 0 :REM manual auto switch
step = 1 :REM sequence step switch

portb = 6
red1 = 0 :REM set a of lights
yel1 = 1
green1 = 2
red2 = 3 :REM set b of lights
yel2 = 4
green2 = 5

count = &0C :REM general purpose counter

W=0
F=1
DIM code% 512
FOR pass = 4 TO 7 STEP 3

P%=00 :REM reset vector
O%=code%

[OPT pass

goto init

\************************ subroutines ********************

\ test here to see if manual mode or delay if automatic

.delay
btfsc porta,auto \ test for auto switch on
goto dly1 \ automatic mode

.lp1
btfsc porta,step \ if manual mode, then wait for
goto delay \ button press but check if auto
btfss porta,step \ and then release before continuing
goto P%-1 \ to next sequence
retlw #0

\ long delay 32ms * value in w register

.dly1
movlw #time \ load timer
movwf count \ use this register temporarily
.del1
clrf tmr0
btfss tmr0,7 \ test tmr0 bit 7 128*256us = 32.768ms
goto P%-1
decfsz count,F \ decrement, until zero
goto del1
retlw #0

\ *********** initalise ports and tmr0 *************

.init
clrf portb
bsf status,5 \ set up page 0
movlw #&00
movwf trisb \ port b as outputs
movlw #&0F
movwf trisa \ port a as inputs
movlw #%10000111 \ tmr0 pre-scalar /256 pullup off
movwf optreg \ 256us per count internal clock
bcf status,5 \ set up page 0

\ ********* program begins here **********************

.main
bsf portb,red1
bsf portb,green2
movlw #time \ delay time
call delay

bsf portb,yel1
bcf portb,green2
bsf portb,yel2
movlw #time \ delay time
call delay

bcf portb,yel1
bcf portb,red1
bsf portb,green1
bsf portb,red2
movlw #time \ delay time
call delay

bcf portb,green1
bsf portb,yel1
bsf portb,yel2
movlw #time \ delay time
call delay

bcf portb,yel1
bcf portb,yel2
bcf portb,red2
goto main

]
NEXT

Some PIC projects to try

VUbars

There is a simple 'get you working' project to try which takes the input voltage on line ADC0 and converts that into a VU bar style LED display.

K2

There is a slightly more complex project that acts as a maximum/minimum temperature display with screen saver and user settable alarm point which could then be used to (for example) open a window or buzz a buzzer. It was called K2 after the mountain, and comes complete with diagrams.

CRCcalc

There is a software only project which calculates the 16 bit CRC of a block of data in the PIC and uses the same polynomial code as the BBC micro's tape filing system. This is a good first project to try with the simulator, PICtracer, since it doesn't require any external stimuli.

Software RS232

There is a skeleton of an RS232 serial comms terminal to build (includes diagram). Using the RTS/CTS flow control mechanism it's maximum baud rate is software definable. For each character it receives it echos back "Rx:<letter><cr><lf>" as a demonstration of its use.


©2006 SPROW [Updated 17-Apr-2006] | home
Valid HTML 4.01