PM Assembler 4.05, Copyright (c) 1995, 2002 microEngineering Labs, Inc.					Page 1

     1					;         *******  bitsc1  *****    ESC for toy car Rx
     2					
     3					;        This design will work only when the "UP" and "DOWN"
     4					;        inputs are connected to points on the Rx that are
     5					;        positive when the Tx button is NOT pushed, and turn
     6					;        negative WHEN the Tx button is pushed.
     7					
     8					;        This version allows the user to set an automatic
     9					;        motor cut off feature to the desired time delay in
    10					;        increments of 15 sec. This is provided for safety
    11					;        as the airplane can easily out fly the range of the
    12					;        Tx.  Setting the delay is done as follows:
    13					
    14					;        1-  When the Rx and wired in BitSC is powered
    15					;        up, there will be aprox 1 sec delay after which
    16					;        the motor will blip once for each 15 sec interval
    17					;        that the BitSC is set for.
    18					
    19					;        2- After the blipping has stopped there is a 2 sec
    20					;        "window" wherein a new number of increments can be
    21					;        entered by pushing the "down" button the number of
    22					;        times equal to the number of 15 sec's desired.
    23					;        The first press of the "down" button holds the
    24					;        "window" open to gather the remaining presses.
    25					
    26					;        3- After the desised number of intervals has been
    27					;        entered, the "up" button is pressed and the unit is
    28					;        ready for normal use.
    29					
    30					;        4- In the event that the "up" button is pressed
    31					;        before the 2 sec window is over the unit will hang
    32					;        waiting for an additional "up" press to be ready.
    33					;        This has the effect of reducing the number of 15
    34					;        sec intervals to just one, but ONLY for that single
    35					;        power up. Thus one can have several intervals of 15
    36					;        sec stored for normal use, but can switch to just
    37					;        a single 15 sec interval when desired.
    38					
    39					;        The BitSC uses a reverse exponential lookup table
    40					;        for motor speed control.
    41					
    42					
    43					
    44					        maclib  'P12F629.inc'
    45					
    46					
    47					        device  PIC12F629,intrc_osc,wdt_off,mclr_off,bod_off,pwrt_off,cpd_
    48					
    49					        data
    50					        org     32
    51					
    52					
    53	0020-				tmp     ds      1               ;general purpose counter
    54	0021-				cnt     ds      1               ;same
    55	0022-				del1    ds      1
    56	0023-				del2    ds      1
    57	0024-				del3    ds      1
    58	0025-				del4    ds      1
PM Assembler 4.05, Copyright (c) 1995, 2002 microEngineering Labs, Inc.					Page 2

    59	0026-				acc     ds      1               ;acumulator for motor channel
    60	0027-				dty     ds      1               ;duty cycle for motor channel
    61	0028-				speed   ds      1               ;motor speed for table lookup
    62	0029-				delnum  ds      1
    63					
    64	=0285				mtr     =       gpio.5          ;output for motor PWM
    65	=0205				down    =       gpio.4          ;input for decrease speed
    66	=0105				up      =       gpio.2          ;input for increase speed
    67	=0014				inputs  =       010100b         ;inputs on gp4 and 2
    68					
    69					        eeprom
    70					        org     0
    71					
    72	0000- 0001			        db      1
    73					
    74					
    75					        code
    76					        org     0
    77					
    78	0000- 1683			        setb    rp0          ;switch to page 1
    79	0001- 23FF			        call    03ffh        ;fetch osccal nomber (FOR 1ST TIME CHIP PRGMI
    80	0002- 0090			        mov     osccal,w     ;move it into osccal (FOR 1ST TIME CHIP PRGMI
    81					;       mov     osccal,#112  ;move cal# previously found at 03FF into oscc
    82					                             ;USE THIS METHOD AFTER 1ST TIME CHIP PROGRAMM
    83	0003- 1283			        clrb    rp0          ;switch back to page 0
    84	0004- 2828			        jmp start
    85					
    86	0005- 0782			table   jmp     pc+w
    87	0006- 3400 3400 342D 3440	        retw    0,0,45,64,78,91,101,111,120,129,137,145,152,159,166,172,17
	000A- 344E 345B 3465 346F
	000E- 3478 3481 3489 3491
	0012- 3498 349F 34A6 34AC
	0016- 34B2 34B8
    88	0018- 34BE 34C3 34C8 34CD	        retw    190,195,200,205,210,215,220,225,230,235,240,245,250,255,25
	001C- 34D2 34D7 34DC 34E1
	0020- 34E6 34EB 34F0 34F5
	0024- 34FA 34FF 34FF 34FF
    89					
    90	0028- 01A0			start   clr     tmp
    91	0029- 01A1			        clr     cnt
    92	002A- 282B			settle  jmp     $+1               ;wait aprox 1 sec
    93	002B- 282C			        jmp     $+1
    94	002C- 282D			        jmp     $+1
    95	002D- 282E			        jmp     $+1
    96	002E- 282F			        jmp     $+1
    97	002F- 2830			        jmp     $+1
    98	0030- 0BA0 282A			        djnz    tmp,settle        ;for processor to settle
    99	0032- 0BA1 282A			        djnz    cnt,settle
   100	0034- 1683			        setb    rp0               ;switch to page 1
   101	0035- 3014 0085			        mov     trisio,#inputs    ;set inputs
   102	0037- 0181			        clr     option            ;enable global pullups
   103	0038- 3014 0095			        mov     wpu,#010100b      ;enable gp2 & gp4 pullups
   104	003A- 1283			        clrb    rp0               ;back to page 0
   105	003B- 3007 0099			        mov     cmcon,#00000111b  ;free gpio.0,1,2 (set comparator to off)
   106	003D- 0185			        clr     gpio
   107	003E- 01A7			        clr     dty
   108	003F- 0103			        clr     w               ;set w to 1st eeprom memory location
   109	0040- 20C2			        call    eread           ;read from eeprom, returning in w
PM Assembler 4.05, Copyright (c) 1995, 2002 microEngineering Labs, Inc.					Page 3

   110	0041- 00A9			        mov     delnum,w        ;transfer to delay number
   111	0042- 00A1			        mov     cnt,w           ;and also to cnt
   112	0043- 2845			        jmp     strtblp         ;jump around delay
   113	0044- 20B9			nxtblip call    dely5           ;wait about a sec
   114	0045- 3064 00A3			strtblp mov     del2,#100       ;blip motor once for each 15 sec delay
   115	0047- 1685			blip    setb    mtr             ;turn motor on
   116	0048- 0BA2 2847			        djnz    del1,blip       ;1st delay loop
   117	004A- 0BA3 2847			        djnz    del2,blip       ;2nd delay loop
   118	004C- 1285			        clrb    mtr             ;turn motor off
   119	004D- 0BA1 2844			        djnz    cnt,nxtblip     ;decrement cnt and repeat if not zero
   120	004F- 300A 00A0			        mov     tmp,#10         ;setup 3rd delay element
   121	0051- 1D05 289F			chksetd jnb     up,setdel       ;check if up button pressed
   122	0053- 0BA2 2851			        djnz    del1,chksetd    ;1st loop
   123	0055- 0BA3 2851			        djnz    del2,chksetd    ;2nd loop
   124	0057- 0BA0 2851			        djnz    tmp,chksetd     ;3rd loop
   125	0059- 1405			        setb    gpio.0          ;optional led at pin 7 can be used to
   126					                                ;indicate programing window is over
   127	005A- 3001 00A8			setspd  mov     speed,#1        ;speed needs value other than zero
   128	005C- 01A2			getsigs clr     del1            ;initialize delay values
   129	005D- 01A3			        clr     del2
   130	005E- 0829 00A5			        mov     del4,delnum
   131	0060- 300B 00A4			setdel3 mov     del3,#11        ;this delay variable gets delnum
   132	0062- 1D05 287B			chksigs jnb     up,incrse       ;checking up button here, go if pushed
   133	0064- 1E05 2881			        jnb     down,decrse     ;checking down button here, go if pushed
   134	0066- 2089			        call    pwm             ;keep up pwm
   135	0067- 0BA2 2862			        djnz    del1,chksigs    ;start of 15 sec timing
   136	0069- 0BA3 2862			        djnz    del2,chksigs
   137	006B- 0BA4 2862			        djnz    del3,chksigs
   138	006D- 0BA5 2860			        djnz    del4,setdel3
   139	006F- 1D05 287B			nxtchk  jnb     up,incrse       ;button press here will leave delay loop
   140	0071- 1E05 2881			        jnb     down,decrse     ;same as above
   141	0073- 2089			        call    pwm             ;keep up pwm
   142	0074- 0828			        mov     w,speed
   143	0075- 2005			        call    table
   144	0076- 00A7			        mov     dty,w
   145	0077- 2094			        call    dely30
   146	0078- 0BA8 286F			        djnz    speed,nxtchk    ;dec speed and loop back to check buttons
   147	007A- 285A			        jmp     setspd          ;timed out, start over
   148					
   149	007B- 0AA8			incrse  inc     speed           ;inc speed index
   150	007C- 3021 0228 1803		        csb     speed,#33       ;compare & skip next if speed < 33
   151	007F- 03A8			        dec     speed           ;if speed = 33 then dec speed
   152	0080- 2884			        jmp     getw            ;jump around decrease speed
   153	0081- 03A8			decrse  dec     speed           ;dec speed index
   154	0082- 1903			        snz                     ;skip next if speed not down to zero
   155	0083- 0AA8			        inc     speed           ;if it is zero then inc it
   156	0084- 0828			getw    mov     w,speed         ;speed goes to w for table lookup
   157	0085- 2005			        call    table           ;get value from table
   158	0086- 00A7			        mov     dty,w           ;and transfer to dty
   159	0087- 2097			        call    dely12          ;delay some
   160	0088- 285C			        jmp     getsigs         ;jump back to check buttons again
   161					
   162					
   163	0089- 0827 07A6			pwm     add     acc,dty         ;add duty to accumulator
   164	008B- 1C03			        sc                      ;if add carried then
   165	008C- 1285			        clrb    mtr             ;skip clearing motor bit
   166	008D- 1803			        snc                     ;if add didn't carry then
   167	008E- 1685			        setb    mtr             ;skip setting motor bit
PM Assembler 4.05, Copyright (c) 1995, 2002 microEngineering Labs, Inc.					Page 4

   168	008F- 0000			        nop                     ;add some time here
   169	0090- 0000			        nop
   170	0091- 0000			        nop
   171	0092- 0000			        nop
   172	0093- 0008			        ret
   173					
   174					
   175	0094- 301E 00A1			dely30  mov     cnt,#30         ;setup cnt for looping
   176	0096- 2899			        jmp     next            ;jump over next setup
   177	0097- 300C 00A1			dely12  mov     cnt,#12         ;setup cnt for looping
   178	0099- 2089			next    call    pwm             ;keep up pwm while looping here for
   179	009A- 0BA0 2899			        djnz    tmp,next        ;about 3/4 sec to slow down constant
   180	009C- 0BA1 2899			        djnz    cnt,next        ;repeating before going back to check
   181	009E- 0008			        ret
   182					
   183	009F- 01A0			setdel  clr     tmp             ;always
   184	00A0- 1D05 28A0			        jnb     up,$            ;wait until signal is hi again
   185	00A2- 0BA1 28A2			        djnz    cnt,$           ;debounce signal
   186	00A4- 1E05 28A9			getbtn  jnb     down,incdel     ;go if signal turns lo
   187	00A6- 1D05 28B1			        jnb     up,finish       ;go if signal turns lo
   188	00A8- 28A4			        jmp     getbtn          ;loop back for to check buttons
   189	00A9- 1E05 28A9			incdel  jnb     down,$          ;wait till signal hi again
   190	00AB- 0BA1 28AB			        djnz    cnt,$           ;debounce signal
   191	00AD- 0AA0			        inc     tmp             ;inc counter
   192	00AE- 0103			        clr     w               ;set eeprom location
   193	00AF- 20C8			        call    ewrite          ;write tmp to eeprom memory
   194	00B0- 28A4			        jmp     getbtn          ;jump back to repeat
   195	00B1- 0820 00A9			finish  mov     delnum,tmp      ;move counter to delay number for use
   196	00B3- 1903			        snz                     ;skip next if not zero
   197	00B4- 0AA9			        inc     delnum          ;inc it if it was zero
   198	00B5- 285A			        jmp     setspd          ;jump back to get button presses
   199					
   200	00B6- 300A 00A0			dely10  mov     tmp,#10
   201	00B8- 28BB			        jmp     nxtdely
   202	00B9- 3005 00A0			dely5   mov     tmp,#5
   203	00BB- 0BA2 28BB			nxtdely djnz    del1,nxtdely
   204	00BD- 0BA3 28BB			        djnz    del2,nxtdely
   205	00BF- 0BA0 28BB			        djnz    tmp,nxtdely
   206	00C1- 0008			        ret
   207					
   208					
   209	00C2- 1683			eread   setb    rp0             ;eeprom read for 12F629 here
   210	00C3- 009B			        mov     eeadr,w
   211	00C4- 141C			        setb    rd
   212	00C5- 081A			        mov     w,eedata
   213	00C6- 1283			        clrb    rp0
   214	00C7- 0008			        ret
   215					
   216					
   217	00C8- 1683			ewrite  setb    rp0             ;eeprom write for 12F629 here
   218	00C9- 009B			        mov     eeadr,w
   219	00CA- 0820 009A			        mov     eedata,tmp
   220	00CC- 151C			        setb    wren
   221	00CD- 3055 009D			        mov     eecon2,#55h
   222	00CF- 30AA 009D			        mov     eecon2,#0AAh
   223	00D1- 149C			        setb    wr
   224	00D2- 189C 28D2			        jb      wr,$
   225	00D4- 1283			        clrb    rp0
PM Assembler 4.05, Copyright (c) 1995, 2002 microEngineering Labs, Inc.					Page 5

   226	00D5- 0008			        ret
