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

📄 four.s

📁 我自己编译使用的dspic源代码.简单实用.
💻 S
字号:
;******************************************************************************
;	AsmAdc12Usart.s is a simple program written in assembly to run on a 
;	ICDdemo board and degigned to exercise the ADC and USART
;	The program does the following:
;	This program does the following:											
;	1.	The potentiometer connected to AN2 is sampled and converted	
;	 	by the 12 bit ADC on the dsPIC30F6012.								
;	2. The 12 bit value is then converted to a 3 digit bcd code		
;		which corresponds to the hex value of the conversion			
;	3.	The 3 digits are then loaded to a buffer and transmitted		
;		4.	The user can connect a RS232 cable to Hyperterm and 		
;		the 12 bit value can be viewed on the PC desktop at 2400		
;		baud, 8 bits 1 stop/start no parity.
;******************************************************************************

;                                                                             *

;    Author              : 	Stan D'Souza                                    *

;    Company             :    Microchip                                       *

;    Filename            :  AsmAdc12Usart.s                                         *

;    Date                :  06/27/03                                        *

;    File Version        :  1.10                                              *

;                                                                             *

;    Other Files Required: p30F6012.gld, p30f6012.inc                         *

;    Tools Used:MPLAB GL : 6.00                                               *

;               Compiler : 1.10                                               *

;               Assembler: 1.10                                               *

;               Linker   : 1.10                                               *

;------------------------------------------------------------------------------

	.equ __30F4011, 1
	.include "p30f4011.inc"


;------------------------------------------------------------------------------
;Program Specific Constants (literals used in code)

	.equiv	FCY, #1000000			;Instruction Cycle Frequency
	.equiv	BAUDRATE, 2400			;Operating Baud Rate
	.equiv	DelayConst, FCY/2000


;------------------------------------------------------------------------------
;Global Declarations:

		.global __reset			;Declare the label for the start of code
      .global __U2TXInterrupt ;Declare USART1 TX ISR name global



;
;Assign ram space for register Flag
		.section	.nbss
		.align	2
DelayReg:	.space	2

;------------------------------------------------------------------------------
;Code Section in Program Memory

	.text				;Start of Code section


__reset:       
			mov	#__SP_init, W15	;Initalize the Stack Pointer
			mov	#__SPLIM_init, W0
			mov	W0,SPLIM	;Initialize the Stack Pointer Limit Register
			nop			;Add NOP to follow SPLIM initialization
			clr	W0		;Initialize Working registers to 0x0000
			mov	W0,W14 ;clr working registers w0 to w14
			repeat	#12
			mov	W0,[++W14]
			clr	W14

;------------------------------------------------------------------------------

			RCALL	InitADC10			; Initialize the ADC
			RCALL Init_USART2			;initialize USART1 for Tx interrupt at 2400 baud

Again:
			bset	ADCON1,#SAMP		; start sampling ...
			mov	#10,w0				; delay for 
			mov	w0,DelayReg			; for 10 mS
 			rcall DelayNmSec			; 		/
 			bclr	ADCON1,#SAMP		; start Converting
ADCdone:
			btss	IFS0,#ADIF			; conversion done?
			bra	ADCdone				; no then keep checking
			rcall	SendADC				; Load buffer and transmit
			bra	Again					;repeat again

;
;-----------------------------------------------------------------------------

;Subrotuinte to Init Ports pins connected to LED1 to LED4

;------------------------------------------------------------------------------

Init_PORTS:
	
	clr	LATD
	mov	#0xFF0F,W0				; set RD7 to RD4 as outputs
	mov	W0,TRISD
	return
;
;InitUSART1, subroutine initializes the USART 
;
;
Init_USART2:
	clr U2MODE
	clr U2STA
	bset U2MODE, #UARTEN                   	;Enable UART (implies reception)
	mov #(((FCY/BAUDRATE) / 16) - 1), w0       
	mov w0, U2BRG                         	;Initialize BRG 

	bclr IFS0, #U2TXIF			;Clear the interrupt flag
	bclr IEC0, #U2TXIE			;Disable ISR processing
	bset U2STA, #UTXEN         ;Enable Transmission 
	return 
;
;
;*******************************************************************
;		Below is the code required to setup the ADC registers for :
;		1. 1 channel conversion (in this case RB2/AN2)
;		2. Manual Sample start
;		3. User specified sampling delay (100mS in this case)
;		4. Manual Stop Sampling and start converting
;		5. Manual check of Conversion complete 
;
;		The code is as per Figure 18-3 in the Ref. manual																
;*********************************************************************
InitADC10:

	mov	#0xfffb,w0			;all PORTB = Digital; RB2 = analog
	mov	w0,ADPCFG
	clr	ADCON1				; SAMP bit = 0 ends sampling ...
									; and starts converting
	mov	#0x0002,w0			; Connect RB2/AN2 as CH0 input  ..
	mov	w0,ADCHS				; in this example RB2/AN2 is the input
	clr	ADCSSL
	mov	#0x0002,w0			; Manual Sample, Tad = internal 2 Tcy
	mov	w0,ADCON3
	clr	ADCON2
 	bset	ADCON1,#ADON		; turn ADC ON
	return
;
;------------------------------------------------------------------------------
;DelayNmSec, delays the value in the DelayReg in mSecs at a given Mip rate
;
DelayNmSec:
	do		#DelayConst,DIL
	nop
DIL:	nop
	dec	DelayReg
	bra	nz,DelayNmSec
	return
;
;-----------------------------------------------------------------------------
;SendADC, sends 5 charaters to the U1TXREG buffer for transmission.  The
;first 3 values are the hex code for the 12 bit ADC value. The last two are 
;the CR and LF characters
;
SendADC:
	mov	ADCBUF0,w0		;get ADC value
	swap	w0					;swap low and high bytes
;	rcall	GetAscii			;get the ascii value
	rcall	LoadAscii		;load the ascii value
	mov	ADCBUF0,w0		;get ADC value
	swap.b	w0				;swap low and high nibbles of LSB
;	rcall	GetAscii			;get Ascii value
	rcall	LoadAscii		; load ascii value
	mov	ADCBUF0,w0		;get ADC value
;	rcall	GetAscii			;get ascii value
	rcall	LoadAscii		;load ascii value
;	mov	#0x000A,w0		;load a CR
;	rcall	LoadAscii		;	/
;	mov	#0x000D,w0		;load LF
;	rcall	LoadAscii		;
	return
;
;GetAscii, takes the value in w0 and uses the 4 LS bits and
;converts them to a hex value (0 to F) in w0.  Next it
;converts the hex value in w0 to an ascii displayable character
GetAscii:
	mov	#0x000F,w1		;mask all but the low 4 bits
	and	w0,w1,w0			;			/
	cp.b	w0,#10			;compare with 10
	bra	n,Add30hex		;< 10 the add 30 hex
	add	#0x37,w0			;>= 10 then add 0x37 to make into ascii
								; character (A to F)
	return
Add30hex:
	add	#0x30,w0			;add 0x30 to make ascii number (0 to 9)
	return
;
;LoadAscii, takes the ascii value in w0 and loads it into
;the U1RXBUF if there is room in the buffer.
LoadAscii:
	btsc	U2STA,#UTXBF	;buffer empty then skip
	bra	LoadAscii		; keep looking if full
	mov	w0,U2TXREG		;load the buffer
	return
;
	
	
		
;------------------------------------------------------------------------------

	.end				;End of code in this file

⌨️ 快捷键说明

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