版主
Joined
: 2004/4/30 10:53 最後登入時間
: 2021/11/30 18:11
From CAE, Microchip
Group:
站務管理者 註冊會員
Level : 75 HP : 744 / 1861 MP : 5367 / 108762 EXP : 45
|
送一段RS-232與A/D的範例程式參考看看:
.equ __30F6014, 1 .include "C:\Program Files\MPLAB IDE\dsPIC_Tools\support\inc\p30f6014.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 __U1TXInterrupt ;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 InitADC12 ; Initialize the ADC RCALL Init_USART1 ;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_USART1: clr U1MODE clr U1STA bset U1MODE, #UARTEN ;Enable UART (implies reception) mov #(((FCY/BAUDRATE) / 16) - 1), w0 mov w0, U1BRG ;Initialize BRG
bclr IFS0, #U1TXIF ;Clear the interrupt flag bclr IEC0, #U1TXIE ;Disable ISR processing bset U1STA, #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 ;********************************************************************* InitADC12:
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 U1STA,#UTXBF ;buffer empty then skip bra LoadAscii ; keep looking if full mov w0,U1TXREG ;load the buffer return ; ;------------------------------------------------------------------------------
.end ;End of code in this file
發表於: 2004/7/9 9:59
|