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

📄 pickit.asm

📁 pic16c745 的 firmware的一个usb样例,供大家参考
💻 ASM
📖 第 1 页 / 共 2 页
字号:
;******************************************************************************
;Software License Agreement                                         
;                                                                    
;The software supplied herewith by Microchip Technology             
;Incorporated (the "Company") is intended and supplied to you, the  
;Company抯 customer, for use solely and exclusively on Microchip    
;products. The software is owned by the Company and/or its supplier,
;and is protected under applicable copyright laws. All rights are   
;reserved. Any use in violation of the foregoing restrictions may   
;subject the user to criminal sanctions under applicable laws, as   
;well as to civil liability for the breach of the terms and         
;conditions of this license.                                        
;                                                                    
;THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,  
;WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED  
;TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A       
;PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,  
;IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR         
;CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.       
;******************************************************************************
;******************************************************************************
;Filename:	PICkit.asm
;Author:	Reston Condit
;Date:		23 January 2003  	
;Version:	1.00
;Description:  	
;  This firmware is written for the PICkit(TM) 1 FLASH Starter Kit. 
;
;******************************************************************************
;Revision History:
;  none
;******************************************************************************
;Include files:

#include <p16c745.inc>
#include "usb_defs.inc"
#include "VppCntrl.inc"
#include "pid.inc"

;******************************************************************************

	errorlevel  -302	; suppress message 302 from list file

	__CONFIG  _H4_OSC & _WDT_OFF & _PWRTE_OFF & _CP_OFF

unbanked	udata_shr
W_save		res	1	; register for saving W during ISR

bank0	udata
Status_save	res	1	; registers for saving context 
PCLATH_save	res	1	;  during ISR
FSR_save	res	1
FSRSave		res	1
PIRmasked	res	1
USBMaskedInterrupts  res  1
Command		res	1
counter		res	2
LoopCnt		res	1
TempPORTC	res	1
AWord		res	2
RcvBuffer	res	8
RcvBufferPtr	res	1
TxBuffer	res	8
TxBufferPtr	res	1
temp		res	1
CheckSum	res	3
TempWord	res	2
ProgramCntr	res	2

	extern	InitUSB
	extern	PutEP1
	extern	GetEP1
	extern	RemoteWakeup
	extern	USBReset
	extern	USBActivity
	extern	USBStall
	extern	USBError
	extern	USBSleep
	extern	TokenDone
	extern	USB_dev_req
	extern	finish_set_address

STARTUP	code
	pagesel	Main
	goto	Main
	nop
	
;******************************************************************************
; Interrupt Service Routine
;******************************************************************************
InterruptServiceVector
	movwf	W_save		; save W
	movf	STATUS,W
	clrf	STATUS		; force to page 0
	movwf	Status_save	; save STATUS
	movf	PCLATH,w
	movwf	PCLATH_save	; save PCLATH
	movf	FSR,w
	movwf	FSR_save	; save FSR

PERIPHERALTEST
	btfss	INTCON,PEIE	; is there a peripheral interrupt?
	goto	EndISR		; all done....

TEST_PIR1
	bsf 	STATUS, RP0	; Bank1
	movf	PIE1,w
	bcf	STATUS, RP0	; Bank0
	andwf	PIR1,w		; mask the enables with the flags
	movwf	PIRmasked

	pagesel TryADIF
	btfss	PIRmasked,USBIF	; USB interrupt flag
	goto	TryADIF
	bcf	PIR1,USBIF
	
	banksel UIR
	movf	UIR,w
	andwf	UIE,w
	banksel USBMaskedInterrupts
	movwf	USBMaskedInterrupts

	pagesel	USBActivity
	btfsc	USBMaskedInterrupts,ACTIVITY	; Is there activity on the bus?
	call	USBActivity

	pagesel	USBReset
	btfsc	USBMaskedInterrupts,USB_RST	; is it a reset?
	call	USBReset	; yes, reset the SIE

	pagesel TryADIF
	btfss	USBMaskedInterrupts,TOK_DNE	; is it a Token Done?
	goto	TryADIF		; no, skip the queueing process

CheckFinishSetAddr
	banksel UIR
	bcf	UIR, TOK_DNE	; clear Token Done
	bcf	STATUS,RP0	; bank 2
	movf	USB_dev_req,w	; Are we waiting for the In transaction 
	xorlw	SET_ADDRESS	;  ack-ing the end of the set address?
	btfss	STATUS,Z
	goto	TryADIF		; no, skip the rest.. just queue the USTAT 
				;  register
	pagesel finish_set_address ; yes, finish setting the address
	call	finish_set_address
	clrf	STATUS		; bank 0

TryADIF

;	btfsc   PIRmasked,ADIF		; AD Done?
;	nop
;	btfsc	PIRmasked,RCIF
;	nop
;	btfsc	PIRmasked,TXIF
;	nop
;	btfsc	PIRmasked,CCP1IF
;	nop
	pagesel	DoSwitcher
	banksel	PIRmasked
	btfss	PIRmasked,TMR2IF
	goto	TEST_PIR2
	movf	PORTC,w		; toggle RC1
	xorlw	0x02
	movwf	PORTC
	call	DoSwitcher
;	btfsc	PIRmasked,TMR1IF
;	nop
TEST_PIR2
;	banksel	PIR2
;	movf	PIR2,w
;	banksel	PIE2
;	andwf	PIE2,w
;	banksel	PIRmasked
;	movwf	PIRmasked
;	btfsc	PIRmasked,CCP2IF
;	nop

;******************************************************************************
; End ISR, restore context and return to the Main program
;******************************************************************************
EndISR
	clrf	STATUS		; select bank 0
	movf	FSR_save,w	; restore the FSR
	movwf	FSR
	movf	PCLATH_save,w	; restore PCLATH
	movwf	PCLATH
	movf	Status_save,w	; restore Status
	movwf	STATUS
	swapf	W_save,f	; restore W without corrupting STATUS
	swapf	W_save,w
	retfie

PORT1	code

;******************************************************************************
; ServiceUSB
;    Services any outstanding USB interrupts.
;    This should be called from the main loop.
;******************************************************************************
ServiceUSB
	banksel	UIR
	movf	UIR,w		; move UIR to bank0 variable
	banksel	USBMaskedInterrupts
	movwf	USBMaskedInterrupts

	pagesel	USBError	; service USB error flags
	btfsc	USBMaskedInterrupts,UERR
	call	USBError

	pagesel	USBSleep	; service USB IDLE flag
	btfsc	USBMaskedInterrupts,UIDLE
	call	USBSleep

	pagesel	USBStall	; service USB stall flag
	btfsc	USBMaskedInterrupts,STALL
	call	USBStall

	pagesel	TokenDone	; service TOKEN Done interrupts
	btfsc	USBMaskedInterrupts,TOK_DNE
	call	TokenDone

	pagesel	ServiceUSB	; page0
	clrf	STATUS		; bank0
	return

;******************************************************************************
; Main
;  Initializes registers then waits for USB communication.  
;     
;******************************************************************************
Main
	movlw	.30		; delay 16 uS to wait for USB to reset
	movwf	W_save		; SIE before initializing registers
	decfsz	W_save,f	; W_save is merely a convienient register
	goto	$-1		; to use for the delay counter.

	banksel	TRISA		; Bank 1
	clrf	TRISA		; Set PORTA as all outputs
	clrf	TRISB		; Set PORTB as all outputs
	movlw	0xC2		
	movwf	TRISC		; Outputs:RC0,RC2-RC5 Inputs:RC1,RC6,RC7
	movlw	0x07
	movwf	OPTION_REG	; TMR0 Prescaler: 1:256 Overflows at 10.9mS
	banksel PORTA
	clrf	PORTA		; Clear all ports
	clrf	PORTB
	clrf	PORTC

	pagesel	InitUSB		
	call	InitUSB		; Initialize USB communication.
	 
	pagesel	VppInit
	call	VppInit		; Initiate Vpp PWM
	call	VppOff		; Turn Vpp off
	call	VppHigh		; Set Vpp to 13v (when it's turned on)
	call	VddOn		; Turn Vdd on
	call	PopInit		; Initialize Popbyte variables
	call	TxInit		; Initialize transmit variables

MainLoop
	call	ServiceUSB	; see if there are any USB tokens to process

	ConfiguredUSB		; macro to check configuration status
	btfss	STATUS,Z	; Z = 1 when configured
	goto	MainLoop

	bankisel RcvBuffer		
	banksel	RcvBuffer	; move the address for 'Buffer' into
	movlw	RcvBuffer	;  the FSR pointer
	movwf	FSR
	movlw	8		; look for eight bytes
	pagesel	GetEP1
	call	GetEP1		; see if there are any commands to process
	pagesel	MainLoop
	btfss	STATUS,C	
	goto	MainLoop	; continue with main loop

MainLoopCont
	call	PopByte		; get the next command on the queue
	movwf	Command
	btfss	STATUS,C
	goto	MainLoop	; nothing there

	call	ServiceUSB	; see if there are any USB tokens to process
	call	Dispatch
	goto	MainLoopCont
	
;******************************************************************************
;#defines    
;******************************************************************************
; ASCII commands from sent by GUI
#define	ENTERPROGRAM	A'P'	; Enter programming mode
#define	EXITPROGRAM	A'p'	; Exit programming mode
#define WRITEPGM	A'W'	; Write program memory
#define WRITECONFIG	A'C'	; Set Program Counter to 0x2000 (Config Mem)
#define	WRITEDATA	A'D'	; Write data memory
#define	ERASEPGM	A'E'	; Bulk erase program memory 
#define	ERASEDATA	A'e'	; Bulk erase data memory
#define	INCADDRESS	A'I'	; Increment address
#define	READPGM		A'R'	; Read program memory
#define	READDATA	A'r'	; Read data memory
#define	CHECKSUM	A'S'	; Calculate checksum
#define	GETVERSION	A'v'	; Get firmware version number
#define	STUFFBYTE	A'Z'	; Stuff byte
#define	POWERCTRL	A'V'	; Turn Vdd on/off; Turn 5kHz square wave on/off

; Command Mapping from the PIC12F629/675 programming specification
#define	LOADCONFIG		B'00000000'
#define	LOADPGM			B'00000010'
#define LOADDATA		B'00000011'
#define READPGMMEM		B'00000100'
#define READDATAMEM		B'00000101'
#define	INCRADDRESS		B'00000110'
#define	BEGINPROGRAMMING	B'00001000'
#define	BULKERASEPGM		B'00001001'
#define	BULKERASEDATA		B'00001011'

; Firmware version revision
#define  MAJORVERSION	0x00
#define  MINORVERSION	0x09
#define  DOTVERSION	0x00

;******************************************************************************
; Dispatch
;  Interprets the command and calls the appropriate function to execute the 
;  command.     
;******************************************************************************
Dispatch
	movf	Command,w		
	xorlw	ENTERPROGRAM
	btfsc	STATUS,Z
	goto	EnterProgramMode

	movf	Command,w
	xorlw	EXITPROGRAM
	btfsc	STATUS,Z
	goto	ExitProgramMode

	movf	Command,w
	xorlw	ERASEPGM
	btfsc	STATUS,Z
	goto	EraseProgram

	movf	Command,w
	xorlw	ERASEDATA
	btfsc	STATUS,Z
	goto	EraseEEData
	
	movf	Command,w
	xorlw	WRITEPGM
	btfsc	STATUS,Z
	goto	WriteProgram

	movf	Command,w
	xorlw	WRITEDATA
	btfsc	STATUS,Z
	goto	WriteEEData

	movf	Command,w
	xorlw	WRITECONFIG
	btfsc	STATUS,Z
	goto	WriteConfig
	
	movf	Command,w
	xorlw	INCADDRESS
	btfsc	STATUS,Z
	goto	IncrementPCmulti

	movf	Command,w
	xorlw	READPGM
	btfsc	STATUS,Z
	goto	ReadProgram
	
	movf	Command,w
	xorlw	READDATA
	btfsc	STATUS,Z
	goto	ReadEEData
	
	movf	Command,w
	xorlw	CHECKSUM
	btfsc	STATUS,Z
	goto	CalcChecksum

	movf	Command,w
	xorlw	GETVERSION
	btfsc	STATUS,Z
	goto	ReportVersion
	
	movf	Command,w
	xorlw	POWERCTRL
	btfsc	STATUS,Z
	goto	PowerControl
	
	return		; command not recognized, ignore it.

;******************************************************************************
; EnterProgramMode
;  Issues Enter Program Mode command to PIC12F629/675. Clears ProgramCntr.
;   Inputs:	none
;   Outputs:	none
;******************************************************************************
EnterProgramMode
	bsf	STATUS,RP0
	movlw	0x02		
	movwf	TRISC		; RC1 is input; RC0,RC2-RC7 are outputs
	bcf	STATUS,RP0
	movlw	.85		; Clear ProgramCntr (see later in function)
	movwf	ProgramCntr
	clrf	ProgramCntr+1
	movlw	0x3F		; force clock and data pin low
	andwf	PORTC,f
	call	Delay		; 4 ms delay
	call	Delay 
	call	VppOn		; turn on Vpp
	decfsz	ProgramCntr	; wait 85x3 instructions (~40us)
	goto	$-1		; ProgramCntr is clear upon leaving function
	call	VddOn		; turn on Vdd
	return

;******************************************************************************
; EnterProgramMode
;  Issues Exit Program Mode command to PIC12F629/675.
;   Inputs:	none
;   Outputs:	none
;******************************************************************************
ExitProgramMode
	bsf	STATUS,RP0
	movlw	0xC2		
	movwf	TRISC		; outputs:RC0,RC2-RC5 inputs:RC1,RC6,RC7
	bcf	STATUS,RP0
	call	VddOff		; turn off Vdd
	call	VppOff		; Turn off Vpp
	return

;******************************************************************************
; EraseProgram
;  Issues Bulk Erase Program Memory command.
;   Inputs:	none
;   Outputs:	none
;******************************************************************************
EraseProgram
	movlw	BULKERASEPGM
	call	SendCommand
	call	Delay
	call	Delay
	return

;******************************************************************************
; EraseEEData
;  Issues Bulk Erase Data Memory command.
;   Inputs:	none
;   Outputs:	none
;******************************************************************************
EraseEEData
	movlw	BULKERASEDATA
	call	SendCommand
	call	Delay
	call	Delay
	return

;******************************************************************************
; WriteConfig
;  Issues Write Configuration command. IT DOES NOT PROGRAM THE CONFIGURATION 
;  MEMORY.  Send Write Program Memory command to do that.  ProgramCNTR is set
;  to 0x2000.
;   Inputs:	2 bytes (both are ingored)
;   Outputs:	none
;******************************************************************************
WriteConfig
	clrf	ProgramCntr
	movlw	0x20
	movwf	ProgramCntr+1
	movlw	LOADCONFIG	; Load Data for Configuration memory
	call	SendCommand	;  send the Write Command to the device
	clrf	AWord		; Use Load Config Data to change modes 	
	clrf	AWord+1		;  not to actually program Config memory.
	movlw	AWord+1		;  Use Load Program Data to program config
	movwf	FSR		;  memory.
	movf	AWord,w
	call	Write14	
	return		

;******************************************************************************
; WriteProgram
;  Issues Write Program Memory command.  Pops a word out of the endpoint buffer
;  to write. 
;   Inputs:	1 word program memory
;   Outputs:	none
;******************************************************************************
WriteProgram
	movlw	LOADPGM		; Load Data for Program memory
	call	SendCommand	; send the Write Command to the device
	call	PopByte		; Pop low order byte, move to AWord
	movwf	AWord
	call	PopByte		; Pop high order byte, move to AWord+1
	movwf	AWord+1
	movlw	AWord+1
	movwf	FSR
	movf	AWord,w
	call	Write14		; send word to device
	
	call	BeginProgramCycle ; wait just the 4ms in BeginProgramCycle
	call	IncrementPC	; increment PC to prepare for next write
	return

;******************************************************************************
; WriteEEData
;  Issues Write Data Memory command.  Pops a byte out of the endpoint buffer to
;  write.
;   Inputs:	1 byte data memory
;   Outputs:	none
;******************************************************************************
WriteEEData
	movlw	LOADDATA	; Load Data for EE memory
	call	SendCommand	; send the Write Command to the device

⌨️ 快捷键说明

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