gpasm-0.11.8 alpha servo.asm 3-10-2004 16:18:08 PAGE 1 LOC OBJECT CODE LINE SOURCE TEXT VALUE 00001 ; CPU configuration 00002 processor 16f84 00003 include 00001 LIST 00002 ; P16F84.INC Standard Header File, Version 2.00 Microchip Technology, Inc. 00136 LIST 002007 3FF2 00004 __config _HS_OSC & _WDT_OFF & _PWRTE_ON 00005 00006 ; variables 00007 00008 ; delay code is 4 cycles long, at 4Mz one instruction is 1us, so we can 00009 ; execute 250 4 cycle delay loops per ms 00010 00011 ; the first time we wait for just under 1ms (load the counter with 248) after 00012 ; activating the servo. then we use the position value to set the delay length 00013 ; (in 4us increments) when the delay finishes, we activate the servo. the 00014 ; result is a 1-2 ms pulse corresponding to the position variable 00015 00016 ; the servo needs updating every 20ms or so 00017 0000000C 00018 counter EQU H'0c' ; servo delay counter register 0000000D 00019 position EQU H'0d' ; servo position register 00000011 00020 position2 EQU H'11' 00000001 00021 servopin EQU H'01' ; servo i/o pin 00000002 00022 servopin2 EQU H'02' 00000001 00023 inputpin EQU H'01' ; mode switch i/o pin 0000000E 00024 longcounter EQU H'0e' ; long delay counter 0000000F 00025 random EQU H'0f' ; random state 00000010 00026 randomcounter EQU H'10' ; delay for random positioning 00027 0000 00028 main: 0000 3000 00029 movlw B'00000000' Warning [224] : Use of this instruction is not recommended. 0001 0065 00030 tris PORTA ; init port A as output 0002 30FF 00031 movlw B'11111111' Warning [224] : Use of this instruction is not recommended. 0003 0066 00032 tris PORTB ; init port B as input 00033 0004 3000 00034 movlw D'00' ; init variables 0005 008C 00035 movwf counter 0006 008D 00036 movwf position 0007 008E 00037 movwf longcounter 0008 0090 00038 movwf randomcounter 0009 304F 00039 movlw D'79' ; random seed 000A 008F 00040 movwf random 00041 000B 00042 servo: 00043 000B 30F8 00044 movlw D'248' ; load counter with cycles for 000C 008C 00045 movwf counter ; 1 ms servo delay 000D 1485 00046 bsf PORTA,servopin ; activate servo output 000E 2032 00047 call delay ; wait for 1ms to pass 000F 080D 00048 movf position,0 ; load w with servo position 00049 ; 00 and FF=extremes 128 = centre gpasm-0.11.8 alpha servo.asm 3-10-2004 16:18:08 PAGE 2 LOC OBJECT CODE LINE SOURCE TEXT VALUE 0010 008C 00050 movwf counter ; and store in Counter for next delay 0011 2032 00051 call delay ; keep servo active for position delay 0012 1085 00052 bcf PORTA,servopin ; deactivate servo output 00053 00054 0013 3008 00055 movlw D'8' ; wait 10ms ish 0014 008E 00056 movwf longcounter 0015 202C 00057 call longdelay 00058 00059 00060 00061 00062 0016 30F8 00063 movlw D'248' ; load counter with cycles for 0017 008C 00064 movwf counter ; 1 ms servo delay 0018 1505 00065 bsf PORTA,servopin2 ; activate servo output 0019 2032 00066 call delay ; wait for 1ms to pass 001A 0811 00067 movf position2,0 ; load w with servo position 00068 ; 00 and FF=extremes 128 = centre 001B 008C 00069 movwf counter ; and store in Counter for next delay 001C 2032 00070 call delay ; keep servo active for position delay 001D 1105 00071 bcf PORTA,servopin2 ; deactivate servo output 00072 00073 00074 001E 3008 00075 movlw D'8' 001F 008E 00076 movwf longcounter 0020 202C 00077 call longdelay 00078 Message [305] : Using default destination of 1 (file). 0021 0A91 00079 incf position2 00080 0022 2823 00081 goto randompos 00082 0023 00083 randompos: 0023 0390 00084 decf randomcounter,f 0024 1D03 00085 btfss STATUS,Z ; skip if it's zero 0025 280B 00086 goto servo 0026 3020 00087 movlw D'32' 0027 0090 00088 movwf randomcounter 0028 2036 00089 call rnd 0029 080F 00090 movf random,w 002A 008D 00091 movwf position ; load position with random pos 002B 280B 00092 goto servo 00093 002C 00094 longdelay: 002C 30F8 00095 movlw D'248' ; load counter with cycles for 002D 008C 00096 movwf counter ; 1 ms servo delay 002E 2032 00097 call delay ; wait for 1ms to pass 002F 0B8E 00098 decfsz longcounter,f ; counter=counter-1 0030 282C 00099 goto longdelay 0031 0008 00100 return 00101 0032 00102 delay: gpasm-0.11.8 alpha servo.asm 3-10-2004 16:18:08 PAGE 3 LOC OBJECT CODE LINE SOURCE TEXT VALUE 0032 0000 00103 nop ; pad loop 0033 0B8C 00104 decfsz counter,f ; counter=counter-1 0034 2832 00105 goto delay 0035 0008 00106 return 00107 0036 00108 rnd: 0036 301D 00109 movlw 01DH 0037 1003 00110 clrc 0038 0D8F 00111 rlf random,f 0039 1803 00112 skpnc 003A 068F 00113 xorwf random,f 003B 3400 00114 retlw 0 gpasm-0.11.8 alpha servo.asm 3-10-2004 16:18:08 PAGE 4 SYMBOL TABLE LABEL VALUE C 00000000 DC 00000001 EEADR 00000009 EECON1 00000088 EECON2 00000089 EEDATA 00000008 EEIE 00000006 EEIF 00000004 F 00000001 FSR 00000004 GIE 00000007 INDF 00000000 INTCON 0000000B INTE 00000004 INTEDG 00000006 INTF 00000001 IRP 00000007 NOT_PD 00000003 NOT_RBPU 00000007 NOT_TO 00000004 OPTION_REG 00000081 PCL 00000002 PCLATH 0000000A PORTA 00000005 PORTB 00000006 PS0 00000000 PS1 00000001 PS2 00000002 PSA 00000003 RBIE 00000003 RBIF 00000000 RD 00000000 RP0 00000005 RP1 00000006 STATUS 00000003 T0CS 00000005 T0IE 00000005 T0IF 00000002 T0SE 00000004 TMR0 00000001 TRISA 00000085 TRISB 00000086 W 00000000 WR 00000001 WREN 00000002 WRERR 00000003 Z 00000002 _CP_OFF 00003FFF _CP_ON 0000000F _HS_OSC 00003FFE _LP_OSC 00003FFC _PWRTE_OFF 00003FFF _PWRTE_ON 00003FF7 gpasm-0.11.8 alpha servo.asm 3-10-2004 16:18:08 PAGE 5 LOC OBJECT CODE LINE SOURCE TEXT VALUE _RC_OSC 00003FFF _WDT_OFF 00003FFB _WDT_ON 00003FFF _XT_OSC 00003FFD __16F84 00000001 counter 0000000C delay 00000032 inputpin 00000001 longcounter 0000000E longdelay 0000002C main 00000000 position 0000000D position2 00000011 random 0000000F randomcounter 00000010 randompos 00000023 rnd 00000036 servo 0000000B servopin 00000001 servopin2 00000002 MEMORY USAGE MAP ('X' = Used, '-' = Unused) 0000 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXX---- 2000 : -------X-------- ---------------- ---------------- ---------------- All other memory blocks unused. Program Memory Words Used: 61 Errors : 0 Warnings : 2 reported, 0 suppressed Messages : 1 reported, 0 suppressed