piclist 1996\03\22\092504a >
Thread: Simulator vs. Actual
face BY : Rob Martin email (remove spam text)

       I know this is rather extensive but I know I must be missing something
fundamental here or something.  I have written a program that basically is
supposed to do one of three things, depending on which pins on PortB get the
Once a signal occurs, the RTCC is started and the program goes back to sleep
different delays for each task.  The following is how I think I have things

PortB, bit 7 - do task3
PortB, bit 5 - do task2
PortB, bit 4 - do task1

Task2 should be the default incase something goes weird (the failsafe you
might say)

I used Parallax coding (mostly) and assembler to generate the object file.
I also use the Parallax simulator to check to code out and it does exactly what
I think it should do when I step through the instructions.  When I program the
16C71 and put it on the PIC proto board and change the level on on of the PortB
pins, it acts nothing like I expect !

Can somebody HELP !

__________________ CODE ____________________________________

       DEVICE  pic16c71,hs_osc,wdt_off,pwrt_off,protect_off
       ID 'TEST'

DATA    EQU     0Ch     ;leave 4 bytes of data for each value
                       ;12 bytes are used for ch0 - ch2 : 0Ch - 17h
TIME    EQU     18h
CHAN    EQU     23h     ;Channel count
LOC     EQU     24h     ;Location in data array
TEMP    EQU     25h
LASTRB  EQU     26h     ;Last status of PortB to watch for change
TIMECNT EQU     28h     ;Counter for Timing Routines
VARI    EQU     2Fh     ;Loop variable for Division
       org     0
       goto    start

;Interrupt handler
       org     4
       sb      INTCON.2        ;If RTCC interrupt then inc. seconds
       goto    NextInt
       goto    TimeInt         ;Routine to set time
NextInt sb      RBIF            ;If bit 0 set then Port B Transition
       goto    IntEnd
       goto    TaskInt         ;Routine to select task
IntEnd  reti

start   mov     TIMECNT,#76     ;Initialize counter for timer
       clr     TIME            ;Initialize TIME to zero
       clrb    INTCON.5        ;disable RTCC Interrupt
       clrb    GIE             ;disable global interrupt
       call    InitIntB        ;Initialize PortB for Interrupts
       sleep                   ;put to sleep until Interrupt

TaskInt mov     LASTRB,PORTB
       mov     w,PORTB
       mov     TEMP,w
       xor     LASTRB,w
       snb     LASTRB.4
       goto    Task1
       snb     LASTRB.7
       goto    Task3
       goto    Task2

TimeInt dec     TIMECNT         ;RTCC needs to overflow 76 times for 1 Sec.
       cse     TIMECNT,#0      ;If TIMECNT is Zero the increment seconds
       goto    EndTime
       inc     TIME            ;Increment Time Counter
       xor     PORTB,#00000001b
       mov     TIMECNT,#76     ;Reset counter for timer
EndTime clrb    INTCON.2

       mov     LASTRB,PORTB    ;save PortB values to LASTRB
       clrb    RBIF            ;Clear Port B Interrupt Flag
       call    InitportB
       call    InitIntRTCC     ;Initialize RTCC for clock interrupts
       clrb    RBIE            ;Disable Port B Interrupts
T3Wait  clrb    PORTB.1         ;make sure LED is off
       setb    PORTB.3         ;set bit 3 to turn LED
       setb    PORTB.2         ;PORTB, bit 2 is LED output
       mov     w,#100
mloop2  mov     VARI,w
       mov     w,#250           ;need to loop approx. 25,000 times for 20
mS at 20.0 MHz
mloop1  mov     TEMP,w
       movsz   w,--TEMP
       goto    mloop1
       movsz   w,--VARI
       goto    mloop2
       clrb    PORTB.2
       mov     w,#100
mloop4  mov     VARI,w
       mov     w,#250
mloop3  mov     TEMP,w
       movsz   w,--TEMP
       goto    mloop3
       movsz   w,--VARI
       goto    mloop4
       clrb    PORTB.3         ;clear bit 3 to turn off LED
       csb     TIME,#10        ;lube for 10 seconds before resetting
       goto    start           ;
       goto    T3Wait          ;Loop around before re-starting

Task2   mov     LASTRB,PORTB    ;save PortB values to LASTRB
       clrb    RBIE            ;Disable Port B Interrupts
       call    InitportB
       call    InitIntRTCC     ;Initialize RTCC for clock interrupts
       clrb    RBIF            ;clear flag
NoWait  csb     TIME,#255       ;wait for 2 seconds before resetting
       goto    start           ;
       goto    NoWait          ;Loop around before re-starting

Task1   mov     LASTRB,PORTB    ;save PortB values to LASTRB
       clrb    RBIE            ;Disable Port B Interrupts
       call    InitportB
       call    InitConst       ;Initialize Constants
       call    InitIntRTCC     ;Initialize RTCC for clock interrupts
       call    InitAD
       call    StartAD
       clrb    RBIF            ;clear flag
update  mov     INDIRECT,ADRES
       cse     CHAN,#2         ;Only sample 3 of the four A/D channels
       goto    NextAd          ;else do next channel
; This is where the stuff to go with Task 1 is
       csa     TIME,#30        ;If TIME gets to 30 then no more activity
       goto    NextAd          ;NO, get next channel and continue
       goto    start           ;YES, end of task1 and start over

NextAd  call    NextChan        ;select next channel
       call    SetupDelay      ;set up > = 10uS
       clrb    ADIF            ;clear flag
       setb    GO_DONE         ;set bit to start new a/d conversion
loop    snb     ADIF            ;a/d done?
       goto    update          ;yes then update
       goto    loop            ;wait till done
;InitConst, initializes some constants
       clr     VAVG
       clr     VAVG+1
       clr     LASTVAV
       clr     LASTVAV+1
       clr     MAXLV
       clr     TIME
       clr     FLAGS
       clr     NUMPEAK
       clr     NUMLUBE
       clr     MAXV
       clr     MAXL
       clr     MAXL+1
; InitportB, initializes port b as outputs
       mov     !RB,#0          ;make PortB RB0-RB3 outputs
       mov     w,PORTB         ;get PortB values, ends mismatch condition
       clr     PORTB
       clrb    RBIF            ;clear flag
; InitIntB, initializes port b as Interrupts
       setb    RP0             ;set to Page 1
       mov     !RA,#255        ;make PortA digital I/O
       mov     !RB,#240        ;make RB0-RB3 outputs and RB4-RB7 inputs
       mov     OPTION,#00110111b       ;Set Option Register
       clrb    RP0             ;set back to Page 0
       clr     PORTB           ;clear PortB
       clr     LASTRB          ;clear last PortB reading
       clr     PORTA           ;clear PortA
       clr     TMR0            ;clear RTCC counter
       clrb    RBIE            ;disable mask
       mov     w,PORTB         ;read PortB
       clrb    RBIF            ;clear flag
       setb    RBIE            ;enable mask
       setb    GIE             ;enable global interrupts
;InitIntRTCC, Initializes RTCC for interrupts for real-time clock
       setb    RP0                     ;set to Page 1
       mov     OPTION,#00010111b       ;Set Option Register
                                       ;RTCC Internal & 1:256 pre-scaler
       clrb    RP0                     ;set back to Page 0
       setb    INTCON.5                ;enable RTCC Interrupt
       setb    GIE
;InitAD, initializes and sets up the A/D hardware.
       mov     !RA,#255        ;Sets Port A as inputs
       mov     ADCON0,#192     ;Set up for internal RC Oscillator (11000000b)
       setb    RP0             ;Enable Page 1
       mov     ADCON1,#0       ;Enable RA0-RA3 as A/D inputs
       clrb    RP0             ;Enable Page 0
;StartAD, starts up the ADC
       setb    ADON            ;Power up the ADC
       mov     DATA,#0Ch       ;Set first value to start address
       mov     FSR,DATA        ;and load into indirect register
       clr     ADRES           ;Clear the ADC Result
       clrb    ADIF            ;clear flag
       setb    GO_DONE         ;set bit to start new a/d conversion
ADloop  snb     ADIF            ;a/d done?
       goto    ADloop          ;wait till done
;NextChan, selects the next channel to be sampled in a "round-robin" format.
ncstart mov     w,#08
       add     ADCON0,w        ;00 is ch0 and 01 is ch1, etc.
       clrb    ADCON0.5        ;clear carry bit
       clr     CHAN
       clr     TEMP
       snb     CHS0
       setb    CHAN.0          ;make CHAN the next channel number
       snb     CHS1
       setb    CHAN.1
       cje     CHAN,#3,chret   ;skip if CH3
       cjne    CHAN,#0,chcont  ;is CHAN equal to CH0
       mov     TEMP,#-12       ;if Equal to CH0 then delete 12 bytes
       call    NextScan        ;else select next scan location
       add     FSR,LOC         ;add byte (scan) location
chcont  add     TEMP,#4         ;then add 4 more bytes to last FSR
       add     FSR,TEMP        ;add channel location
chret   goto    ncstart
;NextScan, selects the next location for the data to be stored in the
;4 byte array and sets FSR accordingly
       sub     FSR,loc         ;decrement FSR back to base address
       inc     loc             ;increment byte location
       cjne    loc,#4,sccont   ;Skip if less than 5
       clr     loc             ;set back to 0
;This routine is a software delay of 10uS for the a/d setup.
;At 4Mhz clock, the loop takes 3uS, so initialize TEMP with
;a value of 3 to give 9uS, plus the move etc should result in
;a total time of > 10uS.
;At 20Mhz clock, the loop takes 2uS, so initialize TEMP with
;a value of 5 to give 10uS, plus the move etc. should result in
;a total time of > 10uS.
       mov     w,#05
       mov     TEMP,w
       movsz   w,--TEMP
       goto    sdloop

       END     ;End of Program

Rob Martin
Association of American Railroads
Transportation Technology Center
Pueblo, CO 81001

See also: www.piclist.com/techref/index.htm?key=simulator+actual
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Simulator vs. Actual

month overview.

new search...