La clé pour la serrure

Description

Et oui dans une serrure il faut une clé

Source / Exemple :


;*****************************************************************
;
;                TITLE           "CLECARD.ASM"
;                LIST           P=16f84
; cr‚er le    :	15/06/2000
; par         : Deaubonne Patrick
; modifier le :	13/08/2001
; par         : Deaubonne Patrick
;*****************************************************************

                opt list
				device pic16f84
                config CP=off,OSC=xt

		1 EQU 1

		IO_PIN  EQU     007H    ;SET INPUT / OUTPUT PIN
		IO_MASK EQU     07FH

		INDIR	EQU	00H	; Indirect pointer
		RTCC	EQU	01H	; Real time clock / counter
		PCL 	EQU	02H	; Program counter low byte
		STATUS	EQU	03H	; Status register
		CY  	EQU	0	; Carry/borrow bit
		DC  	EQU	1	; Digit carry
		Z   	EQU	2	; Zero flag
		PD  	EQU	3	; Power down
		T0  	EQU	4	; Time-out bit
		RP0 	EQU	5	; Register page select bits
		RP1 	EQU	6	; Not used in 16C84
		RP2 	EQU	7	; Not used in 16C84
		FSR 	EQU	04H	; Indirect pointer
		PORTA	EQU	05H	; PORT A
		PORTB	EQU	06H	; PORT B
		EEDATA	EQU	08H	; Eeprom data
		EEADR	EQU	09H	; Eeprom address
		PCLATH	EQU	0AH	; Program counter high byte
		INTCON	EQU	0BH	; Interrupt control
		GIE 	EQU	07H	;  global interrupt enable bit
		RBIE 	EQU	03H	;  Timer 0 interrupt enable bit
		TMR0 	EQU	05H	;  Timer 0 interrupt enable bit

;  if picasm doesn't like 08?H for register addresses (TRISA/B EECON1/2)
;  change to 05H, 06H, 08H and 09H :
		OPTION_REG EQU  01H
		TRISA	EQU	05H	; Data direction port A
		TRISB	EQU	06H	; Data direction port B
		EECON1	EQU	08H	; Eeprom control
		EECON2	EQU	09H	; Eeprom control
		RD  	EQU	0	;  eeprom rd bit
		WR  	EQU	1	;  eeprom wr bit
		WREN	EQU	2	;  eeprom wr enable bit
		WRERR	EQU	3	;  eeprom ....
		EEIF	equ	4	;  eeprom ....
		W   	EQU	0	; W reg. as destination
		F   	EQU	1	; F file as destination

;		TMP		1	equ	0CH	; reg 0ch tmp
		BITCNT  equ	0DH	; bit#
		ADDRL	equ	3EH	; eeprom address
		ADDRH	equ	3FH	; eeprom address
		RCVSTAT	equ	10H	; RCVSTAT bit 0:= last byte to tx
					;         bit 1:= last byte to rx
		ADR		equ 11H
		tmp		equ 15H
		MYDAT		equ 12H

		SDATA   EQU     4       ;SERIAL EEPROM DATA LINE
		SCLK    EQU     5       ;SERIAL EEPROM CLOCK LINE
		CLK		equ		6		; CLK pic
		SDA		equ		7		; SDA pic

		BUF		equ 16h
		tmp1	equ	37h
		tmp2	equ 38h

		ORG	000H

;-----------------------------------------------------------------------------
;  Program Start	Send Atr and Read data from decoder
;-----------------------------------------------------------------------------
START
		goto	MyStart
		nop
		nop
		nop
		retfie
		retfie
		retfie
		retfie
GetTyp
		retlw	2
GetUser
		retlw	0

Mystart

		clrf	PORTA
		clrf	PORTB
		bsf 	STATUS,RP0	; bank 1 acces … TRISA
		movlw	0x10
		TRIS    PORTA
INTXX
		bcf 	INTCON,GIE	; disable all interrupts
;		bcf 	INTCON,TMR0	; disable Timer interrupts
;		bcf 	INTCON,RBIE	; disable Timer interrupts
		clrf	INTCON
		movlw	0xcf
		TRIS	portb	;
		movlw	0x7f
		movwf	OPTION_REG
		bcf 	OPTION_REG,7
		bcf     STATUS,RP0

SLOOP		; read until controlbyte 0xa*  is received

	clrf	ADDRH
	call	GetTyp		; Lit le No du type
	movwf	ADDRL	; sauve dans adresse basse
	bcf		status,cy	; efface la retenue
	rlf		ADDRL,f	; x2
	rlf		ADDRH,f
	rlf		ADDRL,f	; x4
	rlf		ADDRH,f
	rlf		ADDRL,f	; x8
	rlf		ADDRH,f
	movlw   BUF
	movwf	FSR
	clrf	adr
	call	Nextline
	goto	fin

NextLine

	call 	BSTOP
	call	ReadLine
	call 	writeBuf
fin
	call	BSTOP	 ;  send BSTOP
	movlw	0x4F
	tris portb

SendKey
SendLoop3
	movlw	128 ; tempo 128 boucle
	movwf	tmp1

	BSF 	PORTB,SDA	; set data
SendLoop3a
	; tempo Data haut de 128 boucles
	decfsz	tmp1,f
	goto	SendLoop3a

	; Adresse buffer
	movlw	BUF
	; adressage indirect
	movwf	FSR
	; 8 caractŠres … transmettres
	movlw	8
	movwf	tmp

SendLoop1
	movlw	32
	movwf	tmp1
	BSF 	PORTB,SDA	; set data
SendLoop1a
	; tempo Data haut de 32 boucles
	decfsz	tmp1,f
	goto	SendLoop1a

	; 8 bits par caractŠres
	movlw	8
	movwf	tmp1
	movf	indir,w
	movwf	tmp2
	incf	FSR,f

	; stzrt bit

	BCF 	PORTB,SDA	; set data low  for this bit
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	BSF 	PORTB,SDA	; set data low  for this bit
	nop
	nop
	nop
	nop
	nop
	nop
	nop
SendLoop2
	rlf		tmp2,f
	btfsc	status,cy
	BSF 	PORTB,SDA	; set data high for this bit
	btfss	status,cy
	BCF 	PORTB,SDA	; set data low  for this bit
	decfsz	tmp1,f
	goto	SendLoop2

	decfsz	tmp,f
	goto	SendLoop1
goto SendKey

forever
	BCF 	PORTB,SDA	; set data low for ever
	goto	forever

ReadLine:

RDNEXT		;                ;do {  /* only max 16 byte page write */
		movf	ADDRL,w
		call	READ_EE ; read from eeprom
		movf	EEDATA,w
		movwf	INDIR
		incf	FSR,f
RDNEXT2
		call	RX ; read from eeprom
		movf	EEDATA,w
		movwf	INDIR
		incf	FSR,f
		decfsz	tmp,f
		goto	RDnext2
		goto	BSTOP	 ;  send BSTOP

;movf	EEDATA,w
;movwf	MyDat
;clrf	ADR
;call	WritePicEEPROM
;incf	ADR,f
;movf	Indir,w
;movwf	MyDat
;call	WritePicEEPROM
;goto fin

writeBuf:

	movlw	Buf
writeBuf3
	movwf	FSR
	MOVLW	8
	movwf	tmp

writeBuf2:
	movf	INDIR,w
	movwf	MyDat
	call	WritePicEEPROM
	incf	ADR,f
	incf	FSR,f
	decfsz	tmp,f
	goto	writebuf2
	bsf 	3,5         ; Bank 1
	bcf 	0bH, 7      ; Disable INTs.
	bcf 	3,5         ; Bank 0
	return

writePicEEPROM
	bcf 	3, 5        ; Bank 0
	movf	MYDAT,W
	movwf	8           ; EEDATA=W
	movf	ADR,W
	movwf	9           ; Address to read
	bsf 	3,5         ; Bank 1
	bcf 	8, 4        ; clear EE int flag
	bcf 	0bH, 7      ; Disable INTs.
	bsf 	8, 2        ; Enable Write
	movlw	055h        ;
	movwf	9           ; Write 55h
	movlw	0AAh        ;
	movwf	9           ; Write AAh
	bsf 	8, 1        ; Set WR bit, Begin Write
	bsf 	0bH, 7      ; Enable INTs
	btfsc	8, 1        ; Test End write
	goto	$-1         ; loop while not end
	bcf 	8, 2        ; Disable Write
	bcf 	3,5         ; Bank 0
	return

;
; following code borrowed from multi215.asm +
; - read modified to do sequential read
;
;######################################################
;	EEPROM READ ROUTINE
;######################################################
;		Used reg: EEADR, EEDATA,RCVSTAT,W
;		IN  : W= READ ADRESS
;		OUT : W= BYTE

READ_EE 	MOVWF	EEADR	       ; store byte in reg EEADR

READ_EE_DIR	CALL	BSTART		; generate start bit
		BCF	STATUS,CY			; WRITE SEQUENCE
		CALL	CONTROL_BYTE	; CALC CONTROL BYTE AND TRANSMIT
		MOVF	EEADR,W 		; get word address....
READ_EE1	CALL	TX			; and send it

READ_CURRENT	CALL	BSTART		; generate start bit
		BSF	STATUS,CY	; READ SEQUENCE
		CALL	CONTROL_BYTE	; CALC CONTROL BYTE AND TRANSMIT
		CALL	RX		; read 1 byte from serial EE
;		btfsc	RCVSTAT,1	; if lastbyte then
;		CALL	BSTOP		;   send stop bit to end transmission
;		MOVF	EEDATA,W	; GET RECEIVED BYTE
		RETURN

;
;**********************************************
;	GENERATE CONTROL BYTE
;**********************************************
CONTROL_BYTE	RLF	ADDRH,W		; => CY=1
		ANDLW	B'00001111'     ;MASK OUT UPPER NIBBLE
		IORLW	B'10100000'     ;COMPLETE BYTE 1010AAAD
					;A=ADRESS D=DIRECTION

					;SEND CONTROL BYTE

;**********************************************
;	TRANSMIT DATA SUBROUTINE
;**********************************************

TX		MOVWF	EEDATA		;STORE BYTE TO TX
		MOVLW	B'11001111'     ; set data,clock as outputs
		TRIS PORTB

		CALL    TX4		;TRANSMIT 8 TIMES
		CALL    TX4
		GOTO 	BITIN

TX4		CALL    TXLP
		CALL    TXLP
		CALL    TXLP
TXLP	 	RLF	EEDATA,F	; ROTATE BIT TO CARRY
                GOTO    BITOUT_R        ; send the bit to serial EE

;**********************************************
;	RECEIVE DATA SUBROUTINE
;**********************************************
RX 		MOVLW	B'11011111'    ; make SDATA an input line/SCLK = OUT
;		Call MyTrisb
		TRIS PORTB
		CALL    RX4
		CALL    RX4
		btfss   RCVSTAT,1      ; if not last byte then
		bcf	STATUS,CY      ;   set Ack=0
		btfsc	RCVSTAT,1      ; else
		BCF     STATUS,CY      ;   SET ACK BIT=1
		GOTO    BITOUT         ; TO FINISH TRANSMISSION

RX4		CALL	RXLP
		CALL 	RXLP
		CALL	RXLP

RXLP    CALL    BITIN_R         ; READ A BIT
	 	RLF	EEDATA,F	; SHIFT CARRY TO BYTE
   		RETURN

;*********************************************
;	START BIT SUBROUTINE
;*********************************************

BSTART		BSF	PORTB,SDATA	; make sure data is high
		MOVLW	B'11001111'
		TRIS PORTB		; set data and clock lines for output
		BSF	PORTB,SCLK	; set clock high
		NOP
		BCF	PORTB,SDATA	; data line goes low during
		NOP
		GOTO	BC_END		; high clock for start bit

;**********************************************
;	STOP BIT SUBROUTINE
;**********************************************

BSTOP		BCF	PORTB,SDATA	; make sure data line is low
		MOVLW	B'11001111'
		TRIS PORTB		; set data/clock lines as outputs
		NOP
		BSF	PORTB,SCLK	; set clock high
			NOP
		BSF	PORTB,SDATA	; data goes high while clock high
					; for stop bit
B_END		NOP
BC_END		BCF	PORTB,SCLK	; set clock low again
		RETURN

;**********************************************
;	BITOUT ROUTINE , SEND BIT WHEN CARRY SET
;**********************************************

BITOUT		MOVLW	B'11001111'     ; set data,clock as outputs
;		Call MyTrisb
		tris PORTB
BITOUT_R	BTFSS	STATUS,CY	; check for state of data bit to xmit
		BcF	PORTB,SDATA	; set data line low
		BTFSc	STATUS,CY
		BsF	PORTB,SDATA	; high? set data line high

CLKOUT		BSF	PORTB,SCLK	; set clock line high
		GOTO	B_END

;**********************************************
;	BITIN ROUTINE
;**********************************************
BITIN		MOVLW	 B'11011111'    ; make SDATA an input line/SCLK = OUT
;		Call MyTrisb
		tris PORTB
		bcf	OPTION_REG,7	; make sure weak pull-up is on
BITIN_R		BSF	 PORTB,SCLK	; set clock line high
		NOP
		BSF	 STATUS,CY	; assume input bit is high
		BTFSS	 PORTB,SDATA	; read the data bit
		BCF	 STATUS,CY	; input bit was low ,clear CARRY
		NOP
		GOTO	 BC_END
MyTrisB
	bsf	3,5
	tris	06
	bcf	3,5
	return

		END

Conclusion :


http://perso.wanadoo.fr/pat.deaubonne/DOWNLOAD/Serrure_A_WaferCard/CleCard.asm.txt

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.