⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pickit.asm

📁 pic16c745 的 firmware的一个usb样例,供大家参考
💻 ASM
📖 第 1 页 / 共 2 页
字号:
	call	PopByte		; load LSB
	movwf	AWord
	clrf	AWord+1		; clear high order byte
	movlw	AWord+1
	movwf	FSR
	movf	AWord,w
	call	Write14		; send byte to device
	
	call	BeginProgramCycle ; wait 8ms between BeginProgramCycle
	call	Delay		  ;  and Delay
	call	IncrementPC	; increment PC to prepare for next write
	return

;******************************************************************************
; IncrementPC
;  Issues Increment Program Counter command once. ProgramCntr is incremented to
;  coinside with PIC12F629/675 programm counter.
;   Inputs:	none
;   Outputs:	none
;******************************************************************************
IncrementPC
	incf	ProgramCntr,f	; Increment 16 bit program counter tracker
	btfsc	STATUS,Z
	incf	ProgramCntr+1,f
	movlw	INCRADDRESS	; send increment address command
	call	SendCommand
	return

;******************************************************************************
; IncrementPCmulti
;  Issues Increment Program Counter command the number of times specifed in the
;  word to follow this command.	
;   Inputs:	1 word containing number of times to increment 
;   Outputs:	none
;******************************************************************************
IncrementPCmulti
	call	PopByte		; pop LSB
	movwf	counter
	call	PopByte		; pop MSB
	movwf	counter+1	
	iorwf	counter,w	; is 16 bit counter a zero?
	btfsc	STATUS,Z
	return			; yes, then return
	incf	counter+1,f	; no, then increment MSB once (if this isn't 
				;  done the following loop will not execute the
				;  appropriate amount of times)
IncrementLoop
	incf	ProgramCntr,f	; increment device program counter tracker
	btfsc	STATUS,Z
	incf	ProgramCntr+1,f
	movlw	INCRADDRESS	; send increment address command
	call	SendCommand	
	decfsz	counter,f	; repeat if necessary
	goto	IncrementLoop
	decfsz	counter+1,f
	goto	IncrementLoop
	return
	
;******************************************************************************
; BeginProgramCycle
;  Program device...  Sends the command and delays ~4mS before returning
;   Inputs:	none
;   Outputs:	none
;******************************************************************************
BeginProgramCycle
	movlw	BEGINPROGRAMMING ; send the Program command to the device
	call	SendCommand
	call	Delay		; 4ms delay
	return

;******************************************************************************
; ReadProgram
;  Read Program memory, send the data to the USB In Buffer
;   Inputs:	none
;   Outputs:	4 words of program memory
;******************************************************************************
ReadProgram
	movlw	4		; read 4 words at a time
	movwf	counter	
ReadProgLoop
	movlw	READPGMMEM	; send read program memory command
	call	SendCommand
	call	Read14		; get the word 
	movwf	AWord
	movf	INDF,w
	movwf	AWord+1
	movf	AWord,w
	call	TxByte		; send LSB to transfer buffer
	movf	AWord+1,w
	call	TxByte		; send MSB to transfer buffer
	call	IncrementPC	; increment program counter
	decfsz	counter,f	; repeat 4 times
	goto	ReadProgLoop
	return

;******************************************************************************
; ReadEEData
;  Read Data memory, send the data to the USB In Buffer
;   Inputs:	none
;   Outputs:	8 bytes of data memory
;******************************************************************************
ReadEEData
	movlw	8		; read 8 bytes at a time
	movwf	counter
ReadEELoop
	movlw	READDATAMEM	; send read data memory command
	call	SendCommand
	call	Read14		; get the byte
	call	TxByte		; send the byte to the transfer buffer
	call	IncrementPC	; increment program counter
	decfsz	counter,f	; repeat 8 times
	goto	ReadEELoop
	return

;******************************************************************************
; CalcChecksum
;  Calculate the checksum of the device, with the size of the device specified
;  by the host.
;   Inputs:	4 bytes
;		(one word specifing length of program memory)
;		(one word specifing length of data memory)
;   Outputs:	3 bytes
;		(one word containing program memory checksum, including
;		 configuation bits, or code protected checksum)
;		(one byte containing data memory checksum)
;******************************************************************************
CalcChecksum
	call	EnterProgramMode
	clrf	CheckSum
	clrf	CheckSum+1	; clear the sum
	clrf	CheckSum+2
	call	Delay
CheckConfigWord
	call	WriteConfig	; point PC to Configuration memory
	movlw	7
	movwf	counter		
CheckConfigWordLoop
	call	IncrementPC	; bump PC to 2007 (location of Config word)
	decfsz	counter,f
	goto	CheckConfigWordLoop	
	
	movlw	READPGMMEM	; Get Configuration Word
	call	SendCommand
	call	Read14
	movwf	CheckSum	; move low order byte into checksum
	movf	INDF,w
	andlw	0x01		; mask with one
	movwf	CheckSum+1	; move high order byte into checksum
	
;	btfsc	CheckSum+1,0	; is data code protection bit set?
;	goto	CheckSumNoProtect ; no, calculate non-code protect checksum
	btfsc	CheckSum,7	; is program memory code protection enabled?
	goto	CheckSumNoProtect ; no, calculate non-code protect checksum

CheckSumProtect			; if yes to both then computate checksum 	
				;  for code protected part
	call	ExitProgramMode	; Must cycle out and into programming
				;  mode to initialize Program Counter
	call	Delay
	call	EnterProgramMode
	call	Delay			
	call	WriteConfig	; Jump to Configuration Memory again
	clrf	AWord
	clrf	AWord+1
	movlw	4
	movwf	counter
CheckSumProtectLoop
	movlw	READPGMMEM	
	call	SendCommand
	call	Read14	
	andlw	0x0F
	iorwf	AWord,f
	decf	counter,f
	btfsc	STATUS,Z
	goto	CheckSumProtectEnd
	call	IncrementPC
	movlw	4		
	movwf	counter+1
	bcf	STATUS,C	; clear carry flag			
LoopDLoop
	rlf	AWord,f		; shift left four bits
	rlf	AWord+1,f	; shift left four bits
	decfsz	counter+1,f
	goto	LoopDLoop
	goto	CheckSumProtectLoop
CheckSumProtectEnd	
	movf	AWord,w
	addwf	CheckSum,f
	btfsc	STATUS,C
	incf	CheckSum+1,f
	movf	AWord+1,w
	addwf	CheckSum+1,f
	call	PopByte		; clear command parameters from buffer
	call	PopByte
	call	PopByte
	call	PopByte
	goto	EndCheckSum	

CheckSumNoProtect
	call	ExitProgramMode	; Must cycle out and into programming
				;  mode to initialize Program Counter
	call	EnterProgramMode
	call	Delay
	
	call	PopByte		; get the length of memory to checksum
	movwf	counter		
	call	PopByte
	movwf	counter+1
	incf	counter+1,f	
ProgCheckSumLoop
	call	ReadCheckSum	; read word, sum into checksum	
	call	IncrementPC	; bump PC
	decfsz	counter,f	
	goto	ProgCheckSumLoop
	decfsz	counter+1,f
	goto	ProgCheckSumLoop
DataCheckSum
	call	ExitProgramMode	; Must cycle out and into programming
				;  mode to initialize Program Counter
	call	EnterProgramMode
	call	Delay
	
	call	PopByte		; get the length of memory to checksum
	movwf	counter		
	call	PopByte
	movwf	counter+1
	incf	counter+1,f	
DataCheckSumLoop
	call	ReadDataCheckSum ; read word, sum into checksum	
	call	IncrementPC	 ; bump PC
	decfsz	counter,f	
	goto	DataCheckSumLoop
	decfsz	counter+1,f
	goto	DataCheckSumLoop
	
EndCheckSum
	call	ExitProgramMode	; Exit Programming Mode
	movf	CheckSum,w	; now send the checksum back to host
	movwf	TxBuffer
	movf	CheckSum+1,w
	movwf	TxBuffer+1
	movf	CheckSum+2,w
	movwf	TxBuffer+2	
	movlw	TxBuffer
	movwf	FSR
	goto	TxLoop		; send data to host

; Read a program word and sum it into the checksum
ReadCheckSum
	movlw	READPGMMEM
	call	SendCommand
	call	Read14
	addwf	CheckSum,f	; add low order byte into checksum
	btfsc	STATUS,C	; if it overlows, add one to high
	incf	CheckSum+1,f	;  order byte
	movf	INDF,w
	addwf	CheckSum+1,f	; add high order byte into checksum
	return
	
; Read a data byte and sum it into the checksum
ReadDataCheckSum
	movlw	READDATAMEM
	call	SendCommand
	call	Read14
	addwf	CheckSum+2,f
	return


;******************************************************************************
; ReportVersion
;  Report the Version number
;   Inputs:	none
;   Outputs:	none
;******************************************************************************	
ReportVersion
	movlw	MAJORVERSION
	movwf	TxBuffer
	movlw	MINORVERSION
	movwf	TxBuffer+1
	movlw	DOTVERSION
	movwf	TxBuffer+2
	movlw	TxBuffer
	movwf	FSR
	goto	TxLoop		; send data to host

;******************************************************************************
; PowerControl
;  Turn Vdd On and Off. Turn 5kHz pwm on and off.
;   Inputs:	1 byte
;   Outputs:	none
;******************************************************************************
PowerControl	
	call	PopByte
	movwf	temp
	btfss	temp,0
	call	VddOff		; turn Vdd off
	btfsc	temp,0
	call	VddOn		; turn Vdd on
	btfss	temp,1
	call	PWMOff		; turn 5kHz square wave off
	btfsc	temp,1
	call	PWMOn		; turn 5kHz square wave on
	return

;******************************************************************************
; Delay
;  Generic Delay function.  Uses a TMR0 to create 4ms delay.  
;******************************************************************************
Delay
	movlw	.162
	movwf	TMR0		; preload TMR0 so it overflows after 4ms
	bcf	INTCON,T0IF
DelayLoop
	btfss	INTCON,T0IF	; if TMR0 flag is set return, else loop
	goto	DelayLoop
	return

;******************************************************************************
; TxInit
;  Initialize transmit variables
;******************************************************************************
TxInit
	movlw	TxBuffer
	movwf	TxBufferPtr
	return

;******************************************************************************
; TxByte
;  Takes the Byte in Wreg and queues it up for the USB In.  When the buffer is 
;  full it sends it up the USB link via PutEP1.  If the put isn't successful, 
;  it waits until the buffer is free.
;   Inputs:	1 byte in w
;   Outputs:	none  
;******************************************************************************
TxByte
	movwf	temp		; save 1 stbyte
	movf	TxBufferPtr,w
	movwf	FSR
	movf	temp,w
	movwf	INDF
	incf	TxBufferPtr,f	; bump the pointer to the next position
	movf	TxBufferPtr,w
	xorlw	TxBuffer+8	; is the buffer full?
	btfss	STATUS,Z
	return			; no: return for the next byte

	movlw	TxBuffer	; initialize TxBufferPtr
	movwf	TxBufferPtr	
	movwf	FSR		; point to TxBuffer
TxLoop
	movlw	8
	pagesel PutEP1
	call	PutEP1		; send the buffer 
	pagesel TxLoop
	banksel TxBuffer
	btfss	STATUS,C	; was it successful?
	goto	TxLoop		; no, try again.
	return			

;******************************************************************************
; SendCommand
;  Sends a 6 bit command to the host.  
;   Inputs:	1 byte in w
;   Outputs:	none
;******************************************************************************
SendCommand
	movwf	Command		; store command
	movlw	.6		; preload loop counter to 6
	movwf	LoopCnt		
	bcf	INTCON, GIE 	; disable global interrupts

CommandLoop
	bcf	PORTC,6		; pull clock pin low
	bsf	TempPORTC,7	; preset bit for clock pin in PORTC temp reg.
	rrf	Command,f	; rotate command bit into PORTC temp register
	rrf	TempPORTC,w	
	movwf	PORTC		; copy PORTC temp register to PORTC
	decfsz	LoopCnt,f	; if done 6 times then command is sent and exit
	goto	CommandLoop

EndCommandLoop
	bcf	PORTC,6		; clear clock pin
	bsf	INTCON,GIE	; re-enable global interrupts
	return

;******************************************************************************
; Write14
;  Write 14 bits to program memory or data memory  
;   Inputs:	2 bytes 
;		(1 byte in w)
;		(1 byte pointed to by FSR)
;   Outputs:	none
;******************************************************************************
Write14
	movwf	TempWord	; move LSB to tempory register
	movf	INDF,w
	movwf	TempWord+1	; move MSB to subsequent tempory register
	movlw	.14		; load loop counter with 14
	movwf	LoopCnt
	bcf	INTCON, GIE	; disable interrupts
	bsf	PORTC,6		; set clock pin high
	nop
	
WriteLoop
	bcf	PORTC,6		; drop clock pin low
	bsf	TempPORTC,7	; preset bit for clock high in PORTC temp reg.
	rrf	TempWord+1,f	; rotate next data bit into PORTC temp register
	rrf	TempWord,f
	rrf	TempPORTC,w	
	movwf	PORTC		; copy PORTC temp register to PORTC
	decfsz	LoopCnt,f	; if 14th time then exit
	goto	WriteLoop
EndWriteLoop
	bcf	PORTC,6		; toggle clock pin
	movlw	b'01000000'	; clear data pin
	movwf	PORTC
	nop
	clrf	PORTC		; clear clock pin
	bsf	INTCON, GIE	; enable interrupts
	return

;******************************************************************************
; Read14
;  Read program memory or data memory  
;   Inputs:	none
;   Outputs:	2 bytes 
;		(1 byte in w)
;		(1 byte pointed to by FSR)
;******************************************************************************
Read14
	bcf	INTCON,GIE	; disable interrupts
	banksel	TRISC
	bsf	TRISC,7		; put data pin in tri-state mode
	banksel	LoopCnt
	movlw	.14		; load loop counter with 14
	movwf	LoopCnt
	clrf	TempWord	; clear storage word for memory
	clrf	TempWord+1
	bsf	PORTC,6		; toggle clock pin
	nop
	bcf	PORTC,6
	nop

ReadLoop	
	bsf	PORTC,6		; set clock pin
	rlf	PORTC,w		; rotate data pin state into storage word
	rrf	TempWord+1,f
	rrf	TempWord,f
	bcf	PORTC,6		; clear clock pin
	decfsz	LoopCnt,f	; if 14th time then exit
	goto	ReadLoop

EndReadLoop
	bsf	PORTC,6		; set clock pin
	bsf	STATUS,RP0	; Bank1
	bcf	TRISC,7		; make data pin an output again
	bcf	STATUS,RP0	; Bank0
	clrf	PORTC		; clear clock and data pins
	bsf	INTCON,GIE	; re-enable interrupts
	bcf	STATUS,C	; shift storage word right two bits
	rrf	TempWord+1,f
	rrf	TempWord,f
	rrf	TempWord+1,f
	rrf	TempWord,f
	movlw	TempWord+1	; move LSB of read data into w
	movwf	FSR	
	movf	TempWord,w	; move MSB of read data into INDF
	return

;******************************************************************************
; PopInit
;  Initialize receive registers.  
;******************************************************************************
PopInit
	movlw	RcvBuffer
	movwf	RcvBufferPtr
	return

;******************************************************************************
; PopByte
;  Pop one byte out of USB receive buffer.
;   Inputs:	none
;   Outputs:	1 byte in w 
;		carry flag (1 = success, 0 = unsuccessful)
;******************************************************************************
PopByte
	movf	FSR,w		; save FSR register
	movwf	FSRSave
	
	movf	RcvBufferPtr,w	; is the receive buffer pointer no
	xorlw	RcvBuffer+9	;  longer pointing to the buffer?
	btfsc	STATUS,Z
	goto	QueueEmpty	; yes, go do something else...
	
	movf	RcvBufferPtr,w	; no, then get the next command or parameter
	movwf	FSR
	movf	INDF,w	
	movwf	temp
	incf	RcvBufferPtr,f  ; increment the buffer pointer
	bsf	STATUS,C	; set carry to signal successful
	goto	PopByteDone
	
QueueEmpty
	movlw	RcvBuffer	; initialize receive buffer pointer
	movwf	RcvBufferPtr
	bcf	STATUS,C	; clear carry to signal unsuccessful
PopByteDone
	movf	FSRSave,w	; restore FSR register
	movwf	FSR
	movf	temp,w		; move command or parameter to w and exit
	return
	
	end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -