This page created with Cool Page.  Click to get your own FREE copy of Cool Page!
Making PIC Instruments and Controllers.
Ancillary information
06 Jan '09

Chapter 5
  Controlling the output, reading the input.

Program 5.1 Controlling (blinking) an LED (Blinking an LED [We are using the
rightmost LED on the bargraph])
CLEAR ; clear memory locations
DEFINE OSC 4 ; osc speed
LOOP: ; main loop
HIGH PORTD.0 ; turns LED connected to D0 ON
PAUSE 500 ; delay 0.5 seconds
LOW PORTD.0 ; turns LED connected to D0 OFF
PAUSE 500 ; delay 0.5 seconds
GOTO LOOP ; go back to Loop and repeat operation
END ; all programs must end with END

Program 5.2 Blinking eight LEDs one after the other on a bargraph
CLEAR ; clear memory
DEFINE OSC 4 ; Osc speed
LED_ID VAR BYTE ; call out the two variables
A VAR BYTE ; as 8 bit bytes
TRISD =%00000000 ; set PORTD to all outputs
;
MAINLOOP: ; this loop is executed forever
A=1 ; initialize the counter to 1
FOR LED_ID = 1 TO 8 ; do it for the 8 LEDs
PORTD=A ; puts number in PORTD
PAUSE 100 ; pause so you can see the display
A=A * 2 ; multiply by 2 moves lit LED left 1
; position
NEXT LED_ID ; go up and increment counter
GOTO MAINLOOP ; do it all forever
END ; always end with END

Program 5.3 Turns on an LED and dims the one next to it (Doing it this way lets
you compare the brightness of the two LEDs)
CLEAR ; always start with a CLEAR statement
DEFINE OSC 4 ; osc speed
TRISD = %11111100 ; set only PORTD pin 0 and 1 to outputs
X VAR BYTE ; declare X as a variable
PORTD.1=1 ; turned PORTD.1 ON & compare to PORTD.0
;
LOOP: ; start of loop
FOR X = 1 TO 255 STEP 2 ; set up loop for X
PWM PORTD.0, X, 3 ; vary the duty cycle
PAUSE 200/X ; pauses longer for the dimmer values.
NEXT X ; end of loop for X
GOTO LOOP ; return and do it again
END ; all programs with an END statement

Program 5.4 Displaying and blinking “HELLO WORLD” in the LCD display
(Continued )
DEFINE LCD_RWBIT 2 ; read/write bit ]
DEFINE LCD_BITS 8 ; width of data path ] Can be 4 or 8
DEFINE LCD_LINES 2 ; lines in display ]
DEFINE LCD_COMMANDUS 2000; delay in micro seconds ]
DEFINE LCD_DATAUS 50 ; delay in micro seconds ]
;
; Set the port directions. We are setting (must set) all of PORTD
; and all of PORTE as outputs even though PORTE has only three
; lines. (The low nibble in PORTD can be set as inputs if we use
; a 4 high bit path to feed the LCD.)
;
PAUSE 500 ; allow for LCD startup
TRISD = %00000000 ; set all PORTD lines to output
TRISE = %00000000 ; set all PORTE lines to output
; set the Analog-to-Digital control register
ADCON1=%00000111 ; needed for the 16F877A - see above and
; below - this makes all of ports A and E
; digital.
LOOP: ; the main loop of the program
LCDOUT $FE, 1 ; clear screen, go to position 1
PAUSE 250 ; pause 0.25 seconds
LCDOUT “HELLO” ; print
LCDOUT $FE, $C0 ; go to second line, first position
LCDOUT “WORLD” ; print
PAUSE 250 ; pause 0.25 seconds
GOTO LOOP ; repeat
END ; all programs must end in END

Program 5.5 Writing to the LCD display in FULL binary, hexadecimal, and
decimal
CLEAR ; clear memory
DEFINE OSC 4 ; osc speed
DEFINE LCD_DREG PORTD ; define LCD connections
DEFINE LCD_DBIT 4 ; define LCD connections
DEFINE LCD_RSREG PORTE ; define LCD connections
DEFINE LCD_RSBIT 0 ; define LCD connections
DEFINE LCD_EREG PORTE ; define LCD connections
DEFINE LCD_EBIT 1 ; define LCD connections
ADCON1=%00000110 ; make PORTA and PORTE digital
LOW PORTE.2 ; LCD R/W low (write) We will do no reading
PAUSE 500 ; wait for LCD to start up
;
NMBR VAR BYTE ; assign variable
;
TRISD = %00000000 ; D7- -D0 = all outputs
NMBR = %10101010 ; this is decimal 170
;
LCDOUT $FE, 1 ; clear the LCD
LCDOUT $FE, $80, BIN8 NMBR,” “,HEX2 NMBR, “ “, DEC5 NMBR,” “
;display
END ; end program

Program 5.6 Displaying the potentiometer wiper position on the LCD and the
LED bargraph
CLEAR ; define LCD connections
DEFINE OSC 4 ; osc speed
DEFINE LCD_DREG PORTD ; define LCD connections
DEFINE LCD_DBIT 4 ; define LCD connections
DEFINE LCD_RSREG PORTE ; define LCD connections
DEFINE LCD_RSBIT 0 ; define LCD connections
DEFINE LCD_EREG PORTE ; define LCD connections
DEFINE LCD_EBIT 1 ; define LCD connections
ADCON1=%00000110 ; Make PORTA and PORTE digital
LOW PORTE.2 ; LCD R/W low (set it to write only)
PAUSE 500 ; wait for LCD to start up
;
NUMB VAR BYTE ; assign variable
;
TRISD = %00000000 ; D7 to D0 are all made outputs
A2D_VALUE VAR BYTE ; create A2D_Value to store result
TRISA = %11111111 ; set PORTA to all input
ADCON1 = %00000010 ; set PORTA analog input
LCDOUT $FE, 1 ; clear the LCD
; define ADCIN parameters
DEFINE ADC_BITS 8 ; set number of bits in result
DEFINE ADC_CLOCK 3 ; set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ; set sampling time in uS
;
LOOP: ; start loop
ADCIN 0, A2D_VALUE ; read channel 0 to A2D_Value
LCDOUT $FE, $80, “VALUE= “, HEX2 A2D_VALUE, “ “, DEC5
A2D_VALUE
LCDOUT $FE, $C0, BIN8 A2D_VALUE ;
PORTD=A2D_VALUE ; the pause must come right after setting
; PORTD and before PORTD is used again
PAUSE 250 ; try setting PORTD before the LCDOUT
GOTO LOOP ; do it forever
END ; end progra

Program 5.7 Generates a short tone on the piezo speaker (Note that line C2 is
HPWM Channel 1)
CLEAR ; clear memory
DEFINE OSC 4 ; osc speed
PWM PORTC.2, 127, 100 ; beep command
END ; end the program

Program 5.8 LED dimming using the PWM command
CLEAR ; clear RAM
DEFINE OSC 4 ; osc speed
TRISD = %11111110 ; set only PORTD pin 1 to output
X VAR BYTE ; declare x as a variable
;
LOOP: ; start loop
FOR X = 0 TO 255 STEP 5 ; ] in this loop the value
PWM PORTD.0, X, 3 ; ] x represents the brightness
NEXT X ; ] of the LED at PORTD.0
GOTO LOOP ; repeat loop
END ; end program

Program 5.9 Generates a tone on the piezo speaker (There is no looping in this
program)
CLEAR ; clears memory
DEFINE OSC 4 ; osc speed
DEFINE CCP1_REG PORTC ; port to be used by HPWM 1
DEFINE CCP1_BIT 2 ; pin to be used by HPWM 1
; since no timer is defined,
; Timer1 will be used;
HPWM 1,127,2500 ; the tone command
PAUSE 100 ; pause .1 sec to hear tone
END ; end program to stop tone.

Program 5.10 Generates telephone key tones on the piezo speaker (555-1212)
CLEAR ; clear memory
DEFINE OSC 4 ; osc speed
DTMFOUT PORTC.2, [5, 5, 5, 1, 2, 1, 2] ; telephone tones
END ; end program

Program 5.11 Servo Position Control for an R/C servo from PORTB buttons
(This program uses a servo on Jumper J7)
CLEAR ; clear memory
DEFINE OSC 4 ; osc speed
DEFINE LCD_DREG PORTD ; define LCD connections
DEFINE LCD_DBIT 4 ; define LCD connections
DEFINE LCD_RSREG PORTE ; define LCD connections
DEFINE LCD_RSBIT 0 ; define LCD connections
DEFINE LCD_EREG PORTE ; define LCD connections
DEFINE LCD_EBIT 1 ; define LCD connections
POS VAR WORD ; servo position variable
CENTERPOS VAR WORD ; servo position variable
MAXPOS.VAR WORD ; servo position variable
MINPOS VAR WORD ; servo position variable
POSSTEP VAR BYTE ; servo position step variable
SERVO1 VAR PORTC.1 ; alias servo pin Use J7 for servo
POS=0 ; set variables
CENTERPOS =1540 ; set variables
MAXPOS =2340 ; set variables
MINPOS =740 ; set variables
POSSTEP =5 ; set variables
ADCON1 = %00000111 ; PORTA and PORTE to digital
LOW PORTE.2 ; LCD R/W low = write
PAUSE 100 ; wait for LCD to startup
OPTION_REG = $01111111 ; enable PORTB pullups
LOW SERVO1 ; servo output low
GOSUB CENTER ; center servo
LCDOUT $FE, 1 ; clears screen only
;
MAINLOOP: ; main program loop
PORTB = 0 ; PORTB lines low to read buttons
TRISB = $11111110 ; enable first row of buttons on kybd
IF PORTB.4 = 0 THEN GOSUB LEFT; check if any button is pressed
IF PORTB.5 = 0 THEN GOSUB CENTER ; and make a move
IF PORTB.6 = 0 THEN GOSUB RIGHT ; accordingly
LCDOUT $FE, $80, “POSITION = “, DEC4 POS , “ “ ;
SERVO1 = 1 ; start servo pulse
PAUSEUS POS ;
SERVO1 = 0 ; end servo pulse
PAUSE 16 ; servo update rate about 60 Hz
GOTO MAINLOOP ; do it all forever
;
LEFT: ; move servo left
IF POS < MAXPOS THEN POS = POS + POSSTEP ;
RETURN ;
;
RIGHT: ; move servo right
IF POS > MINPOS THEN POS = POS – POSSTEP ;
RETURN ;
;
CENTER: ; center servo
POS = CENTERPOS ;
RETURN ;
END ; end program

Program 5.12 Use servo on jumper pins at the J7 Servo position control, with
added functions
CLEAR ; clear memory
DEFINE OSC 4 ; osc speed
DEFINE LCD_DREG PORTD ; define LCD connections
DEFINE LCD_DBIT 4 ;
DEFINE LCD_RSREG PORTE ;
DEFINE LCD_RSBIT 0 ;
DEFINE LCD_EREG PORTE ;
DEFINE LCD_EBIT 1 ;
DEFINE ADC_BITS 8 ; set number of bits in result
DEFINE ADC_CLOCK 3 ; set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ; set sampling time in uS
TRISA = %11111111 ; set PORTA to all input
TRISD = %00000000 ; set all PORTD lines to outputs
ADCON1 = %00000111 ; PORTA and PORTE to digital
LOW PORTE.2 ; LCD R/W line low (W)
A2D_VALUE VAR BYTE ; create A2D_Value to store result
A2D_VALUE1 VAR BYTE ; create A2D_Value to store result
A2D_VALUE2 VAR BYTE ; create A2D_Value to store result
POS VAR WORD ; servo positions
CENTERPOS VAR WORD ; center position
MAXPOS VAR WORD ; max position
MINPOS VAR WORD ; min position
POSSTEP VAR BYTE ; step length
PAUSE 500 ; wait .5 second
SERVO1 VAR PORTC.1 ; alias servo pin
ADCIN 0, A2D_VALUE ; read channel 0 to A2D_Value
OPTION_REG = $7F ; enable PORTB pull ups
LOW SERVO1 ; servo output low
GOSUB CENTER ; center servo
LCDOUT $FE, 1 ; clears screen only
PORTB = 0 ; PORTB lines low to read buttons
TRISB = %11111110 ; enable first button row
;
MAINLOOP: ; main program loop
; check any butn pres’d to move servo
IF PORTB.4 = 0 THEN GOSUB LEFT ; handle left move
IF PORTB.5 = 0 THEN GOSUB CENTER ; handle centering
IF PORTB.6 = 0 THEN GOSUB RIGHT ; handle right move
ADCIN 0, A2D_VALUE ; read channel 0 to A2D_Value
ADCIN 1, A2D_VALUE1 ; read channel 1 to A2D_Value 1
ADCIN 3, A2D_VALUE2 ; read channel 2 to A2D_Value 2
MAXPOS =2350 –127 + A2D_VALUE1 ; max position relationship defined
MINPOS =750 +127-A2D_VALUE1 ; min position relationship defined
CENTERPOS=POS-127 + A2D_VALUE ; center position relationship defined
SERVO1 = 1 ; start servo pulse
PAUSEUS POS ; pulse length
SERVO1 = 0 ; end servo pulse
LCDOUT $FE, $80, “POS=”, DEC POS-127 + A2D_VALUE , “ “,DEC_
A2D_VALUE,” “,DEC A2D_VALUE1,” “ ,DEC POSSTEP,” “ ;
PAUSE 16 ; servo update rate about 60 Hz
GOTO MAINLOOP ; do it all forever
LEFT: ; move servo left
IF POS < MAXPOS THEN POS = POS + POSSTEP
RETURN ;
RIGHT: ; move servo right
IF POS > MINPOS THEN POS = POS - POSSTEP
RETURN ;
CENTER: ; center servo
POS = 1540-127 + A2D_VALUE ;
RETURN ;
END ; end program

Program 5.13 Reading a switch (Program reads SW1 and turns LED on PORTD.0
ON while it is down)
CLEAR ; clear memory
DEFINE OSC 4 ; osc speed
TRISB = %11110000 ; set the PORTB directions
PORTB = %11111110 ; Set only B0 made low.
; See page 31 of the datasheet re:
; the pull ups on PORTB
OPTION_REG.7=0 ; bit 7 of the OPTION_REG sets the pull ups
; when cleared
TRISD = %11111110 ; set only PORTD.0 to an output.
PORTD.0=0 ; initialize this LED to OFF
;
MAINLOOP: ;
IF PORTB.4=1 THEN ; check for first column being low
PORTD.0=0 ; if it is low turn D0 OFF
ELSE ;
PORTD.0=1 ; if not turn it ON
ENDIF ;
GOTO MAINLOOP ; repeat.
END ;

Program 5.14 Read keyboard (Reading the keyboard rows and columns)
(Continued)
LOW PORTE.2 ; LCD R/W low (write)
PAUSE 500 ; wait for LCD to start up
;
READING VAR BYTE ; define the variables
ALPHA VAR BYTE ;
BUFFER VAR BYTE ;
; set up port B pull ups
OPTION_REG.7 = 0 ; enable PORTB pull ups to make B4-B7 high
TRISB = %11110000 ; make B7-B4 inputs, B3-B0 outputs
BUFFER=%11111111 ; no key has been pressed for display
; set up the initial LCD readings
LCDOUT $FE, 1 ; clear the LCD
LCDOUT $FE, $C0, “ROW=”,BIN4 (BUFFER & $0F),” COL=”,
BIN4 BUFFER >>4
;
LOOP: ;
PORTB =%00001110 ; set line B0 low so we can read row 1
FOR ALPHA = 1 TO 4 ; only need to look at 4 rows
LCDOUT $FE, $80, BIN8 PORTB,” SCANVIEW B” ; see bits scanned
IF (PORTB & $F0)$F0 THEN ; as soon as one of the bits in B4
; to B7 changes we immediately
; have to store the value of PORTB
BUFFER =PORTB ; in a safe place.
GOSUB SHOWKEYPRESS ;
ELSE ;
ENDIF ;
PAUSE 50 ; pause lets us see the scan but
; it also means hold a key down
; for over 50 usecs to have it
; register. Pause be removed
; after you have seen the bits
; scanning on the LCD
PORTB= PORTB <<1 ; move bits left one place for next
; line low
PORTB= PORTB + 1 ; put 1 back in LSBit, the right bit
NEXT ALPHA ;
GOTO LOOP ;
;
SHOWKEYPRESS: ;
LCDOUT $FE, $C0, “ROW=”, BIN4 (BUFFER & $0F),_
“ COL=”, BIN4 BUFFER >>4 ;
RETURN ;
END ;

Program 5.15 Reading the keyboard (Reading the keyboard rows and columns
and show key number)
CLEAR ; clear memory
DEFINE OSC 4 ; osc speed
DEFINE LCD_DREG PORTD ; define LCD connections
DEFINE LCD_DBIT 4 ;
DEFINE LCD_RSREG PORTE ;
DEFINE LCD_RSBIT 0 ;
DEFINE LCD_EREG PORTE ;
DEFINE LCD_EBIT 1 ;
ADCON1 = 7 ; make PORTA and PORTE digital
LOW PORTE.2 ; LCD R/W low (write)
PAUSE 200 ; wait for LCD to start
; define the variables
BUFFER VAR BYTE ;
ALPHA VAR BYTE ; counter for rows
COLUMN VAR BYTE ;
ROW VAR BYTE ;
SWITCH BYTE ;
; set up PORTB pullups
OPTION_REG.7 = 0 ; enable PORTB pullups to make B4-B7 high
TRISB = %11110000 ; make B7-B4 inputs, B3-B0 outputs
; set up the initial LCD readings
LCDOUT $FE, 1 ; clear the LCD
LOOP: ;
PORTB =%00001110 ; set line B0 low so we can read row 1 only
FOR ALPHA = 1 TO 4 ; need to look at 4 rows
IF (PORTB & $F0)<>$F0 THEN ;
; as soon as one of the bits in
; B4 to B7 changes we; immediately
; have to store the value of PORTB
BUFFER =PORTB ;
GOSUB SHOWKEYPRESS ;
ELSE ;
ENDIF ;
PORTB= PORTB << 1 ; move bits left one place for
; next line low
PORTB= PORTB + 1 ; put 1 back in LSBit, the right bit
NEXT ALPHA ;
GOTO LOOP ;
;
SHOWKEYPRESS: ;
BUFFER = BUFFER ^ %11111111 ; flips all the bits in the buffer
; print the first line
LCDOUT $FE, $80, “ROW=”,BIN4 (BUFFER & $0F),” _
COL=”, BIN4 BUFFER >>4 ;
COLUMN=(NCD BUFFER) –4 ; calculate column
ROW=NCD (BUFFER &$0F) ; calculate row
SWITCH=((ROW-1) * 4) +COLUMN ; calculate switch number
; print the second line
LCDOUT $FE, $C0, “ROW=”, DEC ROW, “ COL=”, DEC COLUMN, _
“ SW=”, DEC SWITCH, “ “ ;
RETURN ;
END ;

Program 5.16 Potentiometer readings (Displaying the value of the potentiometer
in all formats)
DEFINE OSC 4 ; osc speed
LOOP: ; begin loop
ADCON0.2 = 1 ; start conversion
NOT_DONE: ; marker if not done
PAUSE 5 ;
IF ADCON0.2 = 1 THEN NOT_DONE ; wait for low on bit-2 of ADCON0, conv
A2D_VALUE = ADRESH ; move high byte of result to A2D_Value
LCDOUT $FE, 1 ; clear screen
LCDOUT “VALUE: “, DEC A2D_VALUE,” “ ; display the decimal value
PAUSE 100 ; wait 0.1 second
GOTO LOOP ; do it forever
DEFINE LCD_DREG PORTD ; define LCD registers and bits
DEFINE LCD_DBIT 4 ;
DEFINE LCD_RSREG PORTE ;
DEFINE LCD_RSBIT 0 ;
DEFINE LCD_EREG PORTE ;
DEFINE LCD_EBIT 1 ;
A2D_VALUE VAR BYTE ; create A2D_Value to store result
;
TRISA = %11111111 ; wet PORTA to all input
TRISD = %00000000 ; wet PORTD to all output
ADCON0 = %11000001 ; configure and turn on A/D Module
ADCON1 = %00000010 ; set PORTA analog and LEFT justify
PAUSE 500 ; wait 0.5 second for LCD startup
;
LOOP: ;
ADCON0.2 = 1 ; start conversion
NOT_DONE: ;
IF ADCON0.2 = 1 THEN NOT_DONE ; wait for low on bit-2 of ADCON0,
; conversion finishes
A2D_VALUE = ADRESH ; move high byte of result to A2D_Value
LCDOUT $FE, 1 ; clear screen
LCDOUT “DEC VALUE= “, DEC A2D_VALUE,” “ ; Display 3 values
LCDOUT $FE, $C0, “HEX=”, HEX2 A2D_VALUE,” “,”BIN=”, BIN8_
A2D_VALUE,” “ ;
PORTD=A2D_VALUE ; displays value in bargraph
PAUSE 100 ; wait 0.1 second
GOTO LOOP ; do it forever
END ; end program

Program 5.17 Display potentiometer settings (Reading and displaying all three
potentiometers values in decimal format)
CLEAR ; define LCD connections
DEFINE OSC 4 ; osc speed
DEFINE LCD_DREG PORTD ;
DEFINE LCD_DBIT 4 ;
DEFINE LCD_RSREG PORTE ;
DEFINE LCD_RSBIT 0 ;
DEFINE LCD_EREG PORTE ;
DEFINE LCD_EBIT 1 ;
LOW PORTE.2 ; LCD R/W line low (W)
PAUSE 500 ; wait .5 second for LCD startup
; the next 3 defines are needed for
; the ADCIN command
DEFINE ADC_BITS 8 ; set number of bits in result
DEFINE ADC_CLOCK 3 ; set internal clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ; set sampling time in uS
;
TRISA = %11111111 ; set PORTA to all input
TRISD = %00000000 ; set all PORTD lines to outputs
ADCON1 = %00000110 ; PORTA and PORTE to digital
A2D_Value0 VAR BYTE ; create A2D_Value to store result 1
A2D_Value1 VAR BYTE ; create A2D_Value to store result 2
A2D_Value2 VAR BYTE ; create A2D_Value to store result 3
;
LCDOUT $FE, 1 ; clear the display
;
MAINLOOP: ; main program loop
; check potentiometer values
ADCIN 0, A2D_VALUE0 ; read channel 0 to A2D_Value0
ADCIN 1, A2D_VALUE1 ; read channel 1 to A2D_Value1
ADCIN 3, A2D_VALUE2 ; read channel 2 to A2D_Value2
LCDOUT $FE, $80, DEC A2D_VALUE0,” “,DEC A2D_VALUE1,” “ ,DEC_
A2D_VALUE2,” “ ;
PAUSE 10 ;
GOTO MAINLOOP ; do it all forever
END ; end program

Program 5.18 Servo/Potentiometers (Three potentiometers controlling one
servo; connect the servo to Jumper J7 for this program)
CLEAR ;
DEFINE OSC 4 ; osc speed
DEFINE LCD_DREG PORTD ; define LCD connections
DEFINE LCD_DBIT 4 ;
DEFINE LCD_RSREG PORTE ;
DEFINE LCD_RSBIT 0 ;
DEFINE LCD_EREG PORTE ;
DEFINE LCD_EBIT 1 ;
LOW PORTE.2 ; LCD R/W line low (W)
DEFINE ADC_BITS 8 ; set number of bits in result
DEFINE ADC_CLOCK 3 ; set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ; set sampling time in uS
TRISA = %11111111 ; set PORTA to all input
TRISD = %00000000 ; set all PORTD lines to outputs
ADCON1 = %00000111 ; PORTA and PORTE to digital
A2D_VALUE VAR BYTE ; create A2D_Value to store result
A2D_VALUE1 VAR BYTE ; create A2D_Value1 to store result
A2D_VALUE2 VAR BYTE ; create A2D_Value2 to store result
POS VAR WORD ; servo positions
CENTERPOS VAR WORD ;
MAXPOS VAR WORD ;
MINPOS VAR WORD ;
POSSTEP VAR BYTE ;
PAUSE 500 ; wait .5 second
SERVO1 VAR PORTC.1 ; alias servo pin
ADCIN 0, A2D_VALUE ; read channel 0 to A2D_Value
OPTION_REG = $01111111 ; enable PORTB pullups
LOW SERVO1 ; servo output low
GOSUB CENTER ; center servo
LCDOUT $FE, 1 ; clears screen only
PORTB = 0 ; PORTB lines low to read buttons
TRISB = %11111110 ; enable first button row
; main program loop
MAINLOOP: ; check any but. pressed to move servo
IF PORTB.4 = 0 THEN GOSUB LEFT ;
IF PORTB.5 = 0 THEN GOSUB CENTER ;
IF PORTB.6 = 0 THEN GOSUB RIGHT ;
ADCIN 0, A2D_VALUE ; read channel 0 to A2D_Value
ADCIN 1, A2D_VALUE1 ; read channel 1 to A2D_Value 1
ADCIN 3, A2D_VALUE2 ; read channel 2 to A2D_Value 2
MAXPOS =1500 + A2D_VALUE1*3 ;
MINPOS =1500 - A2D_VALUE1*3 ;
CENTERPOS=1500+3*(A2D_VALUE-127) ;
POSSTEP =A2D_VALUE2/10 +1 ;
SERVO1 = 1 ; start servo pulse
PAUSEUS POS ;
SERVO1 = 0 ; end servo pulse
LCDOUT $FE, $80, “POS=”, DEC POS , “ “` ;
LCDOUT $FE, $C0, DEC A2D_VALUE,” “,DEC A2D_VALUE1,” “ ,
DEC_ POSSTEP,” “
PAUSE 10 ; servo update rate about 60 Hz
GOTO MAINLOOP ; do it all forever
; move servo left
LEFT: IF POS < MAXPOS THEN POS = POS + POSSTEP
RETURN ;
; Move servo right
RIGHT: IF POS > MINPOS THEN POS = POS - POSSTEP
RETURN ;
; center servo
CENTER: POS = 1500+3*(A2D_VALUE-127) ;
RETURN ;
END ; end program






Program listings are listed in detail so you can see the code
Listings are by chapter
There are no listings in Chapter 1 to 4 or after Chapter 22