IGNITION SWITCH


Home


See more projects using micros:
Elektor,EPE,Silicon Chip
 

This project is a discussion on how to program PIC10F200 chips.
We are not selling the program or the project. Go to Mike McLaren - Micro Application Consultants for details on these things.
We are just teaching how to write a program PIC10F200 chips.
We have specialised in presenting programs for the simplest PIC chip: the PIC12F629 and the PIC10F200 is even smaller in size and pin-count.
It has 3 in-out lines and one input-only line. It has 256 locations for your program and the 16 General Purpose Registers are located between 10h and 1Fh.
It has only a 2-level stack and one 8-bit timer. All the other features are the same as the PIC12F629 and if you want to produce something extremely small, this chip is the challenge for you.


The first IGNITION SWITCH program has been written by a very qualified programmer and shows what can be done with this 6 pin chip, but the program also contains a number of difficult-to-understand instructions, with some of them coming from a list that only expert programmers understand.
The second IGNITION SWITCH program has been written by Colin Mitchell and is much easier to understand. if you study them both, you will gain a lot of understanding on how to create a program.


Here's what the circuit does:
Put your Ignition Switch and Starter into one unit. The push-switch has a blue LED to indicate when the ignition is "On."  Push and hold the button while the starter turns over and release when the engine starts. The ignition stays "On." Push again to stop the engine. You've seen it on new, top-end luxury cars. . .now it can be in your car, too.

IGNITION SWITCH for PIC10F200

;******************************************************************
;*                                                                *
;*  Filename: Ignition 10F200 v2.asm                              *
;*    Author: Mike McLaren, K8LH   (k8lh@arrl.net)                *
;*      Date: 30-Sep-09                                           *
;*                                                                *
;*                                                                *
;*  10F200 Ignition Switch Experiment                             *
;*                                                                *
;*                                                                *
;*     MPLab: 8.14    (tabs=8)                                    *
;*     MPAsm: 5.21                                                *
;*                                                                *
;******************************************************************

        include "p10f200.inc"
        list    st=off

        __CONFIG   _MCLRE_OFF & _CP_OFF & _WDT_OFF

        radix   dec

;--< hardware >----------------------------------------------------

#define ignition  GPIO,0        ; GP0 = active hi ignition relay
#define starter   GPIO,1        ; GP1 = active hi starter relay

;--< variables >---------------------------------------------------

        cblock  0x10
temp                            ; delay subsystem
swnew                           ; fresh switch sample (b3 bit)
swold                           ; switch state latch (b3 bit)
mstmr                           ; msec timer
swtmr                           ; 1 sec switch timer
        endc

#define running swold,0         ; running flag

;--< macros >------------------------------------------------------

clock   equ     4               ; 4-MHz clock
usecs   equ     clock/4         ; cycles/usec multiplier

inDlyCy macro   pCycles         ; 0..1027 cycle range
        local   loop            ;
     if pCycles > 3
        movlw   pCycles/4       ;
loop    movwf   temp            ; 4-cycle loop
        decfsz  temp,W          ;
        goto    loop            ;
     endif
     if pCycles%4 >= 2
        goto    $+1             ; 2 cycles
     endif
     if pCycles&1 == 1
        nop                     ; 1 cycle
     endif
        endm

;******************************************************************
;  main program                                                   *
;******************************************************************
        org     0x000
start
        movwf   OSCCAL          ;
        movlw   b'10011110'     ; 10011110
                                ; 1-------, IOC off
                                ; -0------, weak pullups ON
                                ; --0-----, T0CS source Fosc/4
                                ; ---1----, T0SE edge hi>lo
                                ; ----1---, PSA prescale WDT
                                ; -----110, PS prescaler 64
        option                  ;
        movlw   b'00001000'     ;
        tris    GPIO            ; GP3 input, all others outputs
        clrf    GPIO            ; set output latches to '0'
        clrf    swold           ; clear switch state latch
        clrf    swtmr           ; clear 1 second timer
        clrc                    ; clear Carry

newsample
        call    dbdelay         ; 32-msec debounce delay
        comf    GPIO,W          ; sample active lo GP3 switch
        movwf   swnew           ; save fresh sample
newpress
        btfsc   swnew,3         ; swnew.3 == 1 and
        btfsc   swold,3         ; swold.3 == 0 (new press)?
        goto    newrelease      ; no, branch, else
        bsf     swold,3         ; update swold.3 state latch
        call    beep1           ; do a single 32-msec beep and
        movlw   1000/32         ; start 1 second timer
        movwf   swtmr           ;
newrelease
        btfss   swnew,3         ; swnew.3 == 0 and
        btfss   swold,3         ; swold.3 == 1 (new release)?
        goto    timeout         ; no, branch, else
        bcf     swold,3         ; update swold.3 state latch
        bcf     starter         ; turn GP1 'starter' off
        movf    swtmr,W         ; timed out "long" press?
        bz      newsample       ; yes, branch (done), else
        clrf    swtmr           ; turn off 1 second timer
        movlw   b'00000001'     ; use GP0 pin mask to
        xorwf   GPIO,F          ; toggle GP0 'ignition' output
        bcf     running         ; clear "running" flag
timeout
        movf    swtmr,F         ; switch timer running?
        bz      newsample       ; no, branch, else
        decfsz  swtmr,F         ; is it timed out?
        goto    newsample       ; no, branch, else
        call    beep2           ; do a double beep
        btfsc   ignition        ; ignition == 1 and
        btfsc   running         ; running == 0 (allow start)?
        goto    newsample       ; no, branch, else
        bsf     running         ; set "running" flag
        bsf     starter         ; turn GP1 'starter' on
        goto    newsample       ;

;******************************************************************
;  subroutines  This subroutine creates a beep as well as a delay *
; The setc instruction determines if the speaker bit is toggled   *
;******************************************************************
beep2
        call    beep1           ; 32-msec delay with beep
        call    dbdelay         ; 32-msec delay
beep1
        setc                    ; 32-msec delay with beep
dbdelay
        movlw   32              ; C = 0, delay, C = 1, beep
        movwf   mstmr           ; mstmr = 32 msecs
dbloop
        movlw   b'00000100'     ; mask for GP2 'spkr' pin
        skpnc                   ; beep task? no, skip, else
        xorwf   GPIO,F          ; toggle the speaker pin
        inDlyCy(1000*usecs-6)   ; 1-msec minus 6 cycles
        decfsz  mstmr,F         ; done? yes, skip, else
        goto    dbloop          ; loop
        clrc                    ;
        retlw   0               ;

;******************************************************************
        end

The following program is much simpler. It has been made simple by allowing the micro to
advance down the program and loop a number of instructions, looking for a "switch press"
or "switch release." 

IGNITION SWITCH for PIC10F200

;******************************************************************
;*                                                                *
;*  Filename: Ignition 10F200 v2.asm                              *
;*    Author: Colin Mitchell          	                          *
;*      Date: 11-Feb-2010                                         *
;*                                                                *
;*                                                                *
;*  10F200 Ignition Switch Project                                *
;*                                                                *
;*                                                                *
;******************************************************************

        include "p10f200.inc"
        list    st=off

        __CONFIG   _MCLRE_OFF & _CP_OFF & _WDT_OFF

        radix   dec

;--< hardware >----------------------------------------------------

#define ignition  GPIO,0        ; GP0 = active hi ignition relay
#define starter   GPIO,1        ; GP1 = active hi starter relay
#define spkr      GPIO,2        ; GP2 = piezo speaker
#define Sw        GPIO,3        ; GP3 = input - switch

;--< variables >---------------------------------------------------

        		;general purpose registers 10h to 1Fh
temp   equ    10h       ; used in delay 
DelA   equ    11h       ; used in delay 
DelB   equ    12h       ; used in delay 


;******************************************************************
;  main program                                                   *
;******************************************************************
        org     0x000
SetUp		
        movlw   b'10011110'     ;10011110                     
                                ;-0------, weak pullups ON     
        option                  ;
        movlw   b'00001000'     ;
        tris    GPIO            ;GP3 input, all others outputs
        clrf    GPIO            ;make outputs LOW
                      

Start
        call    Delay1          ;20mS debounce delay
        btfsc   GPIO,3          ;Is Sw LOW? - pushed
        goto	Start           ;loop
	call    beep1   	;40mS beep
	bsf	GPIO,0  	;activate ignition  relay
	call    Delay1 
	bsf	GPIO,1  	;activate starter relay

   ;Turn off starter relay
	call	Delay2		;delay 1 second
	btfss   GPIO,3          ;Is Sw released? - HIGH
	goto	$-2
	bcf	GPIO,1  	;de-activate starter relay
	call    beep2   	; 40mS beeps

   ;Turn off Ignition
		
	call	Delay2		;delay 1 second
	btfsc   GPIO,3          ;Is Sw LOW? - pushed
	goto	$-2
	call	Delay2		;delay 1 second
	btfsc   GPIO,3          ;Is Sw LOW? - pushed
	goto	$-5
	bcf	GPIO,0  	;de-activate ignition  relay
	call    beep2   	;40mS beeps
	call	Delay2		;delay 1 second
	call	Delay2		;delay 1 second
	call	Delay2		;delay 1 second
	call	Delay2		;delay 1 second
	call	Delay2		;delay 1 second
	goto	SetUp
;******************************************************************
;  subroutines                                                    *
;******************************************************************

beep2   call    beep1           ; 40mS beep
	call	Delay1  	; 20mS delay
        ;(micro will go here after delay1 and execute beep1)

beep1   movlw   .40             
	movwf	temp
	goto	$+1
	decfsz	DelA,1
	goto	$-2
	movlw   b'00000100'     ;spkr pin
        xorwf   GPIO,F 		;spkr bit will toggle
	decfsz	temp,1
	goto	$-6	
        retlw   00

Delay1	movlw   .20      	; 20mS delay       
	movwf	temp
	goto	$+1
	decfsz	DelA,1
	goto	$-2	
	decfsz	temp,1
	goto	$-4	
        retlw   00  
 
		;1 Second delay
	
Delay2  movlw	.50
	movwf	DelB
	call	Delay1
	decfsz	DelB,1
        goto	$-2
	retlw	00 

;******************************************************************
        end

If you are going to burn the program yourself, you will need a PICkit-2 programmer.
You should read about programming PIC chips by looking on Talking Electronics website, under
Elektor,EPE,Silicon Chip. and see Multi Chip Programmer.

If you have any other questions, contact Colin Mitchell via email.


  2/1/2010