📄 yy.txt
字号:
bsf PORTC,OE ; the decoder to store data
;possibly need to set RST here or hard wire it to Vcc
;start the ADC for the first time
banksel ADCON0
bsf ADCON0,GO ;Set the GO bit for the ADC (ie. start AD conversion)
startDataCollection
; banksel buffersFilled
; clrf buffersFilled
banksel buffer
clrf buffer
clrf buffer+1
clrf buffer+2
clrf buffer+3
clrf buffer+4
clrf buffer+5
;now wait for AD interup to deal with ADdone
banksel UserStatus
btfss UserStatus,ADxfinish ;loop until ADx is finished, then go on
goto $-1
;possibly convert adcValuex
;put adcvaluex into buffer
banksel adcValuex
movf adcValuex,w ;move adcvaluex into W
banksel buffer
movlw buffer+2 ;put adcValuex into the 3rd buffer address
;read the Decoder
pagesel ReadDecoder
call ReadDecoder ; values are stored in DecHighbit and DecLowbit respectively
;put Decoder result into buffer
banksel DecHighbit
movf DecHighbit,w ;move DecHighbit into W
banksel buffer
movlw buffer ;put DecHighbit into the 1st buffer address
banksel DecLowbit
movf DecLowbit,w ;move DecLowbit into W
banksel buffer
movlw buffer+1 ;put DecLowbit into the 2nd buffer address
;wait for adc to finish with the y value
banksel UserStatus
btfsc UserStatus,ADyfinish ;loop until ADy is finished, then go on.
goto $-1
;possibly convert adcValuey
;put adcvaluey into buffer
banksel adcValuey
movf adcValuey,w ;move adcvaluey into W
banksel buffer
movlw buffer+3 ;put adcValuey into the 4th buffer address
;clear User Status ADC bits
banksel UserStatus
bcf UserStatus,ADyfinish ;clear the ADy finish bit
bcf UserStatus,ADxfinish ;clear the ADx finish bit
;send data
sendData
pagesel PutEP1
bankisel buffer ;put the buffer address into the FSR
movlw buffer
movwf FSR ; point FSR to our buffer
movlw 0x4 ; send 4 bytes from buffer
call PutEP1
btfss STATUS,C ; loop here until packet goes through
goto sendData
;start adc
banksel ADCON0
bsf ADCON0,GO ;Set the GO bit for the ADC (ie. start AD conversion)
;clear buffer
banksel buffer
clrf buffer
clrf buffer+1
;recieve data
receiveData:
bankisel buffer
pagesel GetEP1
movlw buffer
movwf FSR ; point FSR to our buffer
call GetEP1 ; If data is ready, it will be copied.
Pagesel receiveData
btfss STATUS,C ; was there any data for us?
goto receiveData ; Nope, loop here until there is
;set PWM
banksel buffer
btfss buffer+1,2 ;test bit 2 of buffer+1. <2>=1 (reverse), <2>=0 (forward)
goto forward
reverse:
pagesel setPWMreverse
call setPWMreverse
pagesel startDataCollection
goto startDataCollection ;restart process
forward:
pagesel setPWMforward
call setPWMforward
pagesel startDataCollection
goto startDataCollection ;restart process
;******************************
;end of main program
;******************************
;******************************
;Function Definitions
;******************************
setPWMreverse:
banksel UserStatus
btfsc UserStatus,direction ;if direction already equals reverse
goto setReverse ; goto setReverse, otherwise clearForward
clearForward:
banksel CCPR2L
clrf CCPR2L ;clear MSByte of forward PWM duty
movlw 0xcf ;move '1100 1111' to w
andwf CCP2CON,f ;clear bis <5:4> of CCP2CON (ie LSbits of PWM duty)
bsf UserStatus,direction ;set direction equal to reverse
setReverse:
;set the most significant byte
banksel buffer
movlw buffer ;move most signifigant 8 bits to w
banksel CCPR1L
movwf CCPR1L ;place in register CCPR1L
;set the least significant two bits
banksel buffer
swapf buffer+1,f ;buffer+1= 'xx?? xxxx'
movlw 0x30 ;w = '0011 0000'
andwf buffer+1,f ;buffer+1= '00?? 0000'
movlw 0x0f ;w = '0000 1111'
iorwf buffer+1,w ;w = '00?? 1111'
banksel CCP1CON
movwf CCP1CON ;set least sig. bits of PWM(CCP1CON<5:4>)
; and leave the pwm module initialized(CCP1CON<3:0>)
return
; btfss buffer+1,0 ;test bit 0 of least sig two bits
; goto clear0r
;
;set0r:
; banksel CCP1CON
; bsf CCP1CON,4
; pagesel test1r
; goto test1r
;
;clear0r:
; banksel CCP1CON
; bcf CCP1CON,4
;
;test1r:
; banksel buffer
; btfss buffer+1,1 ;test bit 1 of least sig two bits
; goto clear1r
;
;set1r:
; banksel CCP1CON
; bsf CCP1CON,5
; return
;
;clear1r:
; banksel CCP1CON
; bcf CCP1CON,5
; return
;end setPWMreverse
setPWMforward:
banksel UserStatus
btfss UserStatus,direction ;if direction already equals forward
goto setForward ; goto setForward, otherwise clearReverse
clearReverse:
banksel CCPR1L
clrf CCPR1L ;clear MSByte of reverse PWM duty
movlw 0xcf ;move '1100 1111' to w
andwf CCP1CON,f ;clear bis <5:4> of CCP1CON (ie LSbits of PWM duty)
bcf UserStatus,direction ;set direction equal to forward
setForward:
;set the most significant byte
banksel buffer
movlw buffer ;move most signifigant 8 bits to w
banksel CCPR2L
movwf CCPR2L ;place in register CCPR2L
;set the least significant two bits
banksel buffer
swapf buffer+1,f ;buffer+1= 'xx?? xxxx'
movlw 0x30 ;w = '0011 0000'
andwf buffer+1,f ;buffer+1= '00?? 0000'
movlw 0x0f ;w = '0000 1111'
iorwf buffer+1,w ;w = '00?? 1111'
banksel CCP2CON
movwf CCP2CON ;set least sig. bits of PWM(CCP2CON<5:4>)
; and leave the pwm module initialized(CCP1CON<3:0>)
return
;end setPWMforward
ReadDecoder:
;function: enables the data latch and reads the high bit then the low bit
; values are stored in DecHighbit and DecLowbit respectively. Then
; clears the data latch and returns.
banksel PORTC
bcf PORTC,OE ;clearing OE and SEL enables the data latch and
bcf PORTC,SEL ; selects the high bit at the same time.
nop ;waiting one cycle to allow data to reach port.
;Note: High bit must be read first. see Decoder spec HCTL 2016
;PORTB in same bank as PORTC
movf PORTB,w ;copy value on port b to working register
banksel DecHighbit
movwf DecHighbit ;save value in variable
banksel PORTC
bsf PORTC,SEL ;setting SEL selects low bit and moves it to the bus
nop ;waiting one cycle to allow data to reach port.
movf PORTB,w ;copy value on port b to working register
banksel DecLowbit
movwf DecLowbit ;save value in variable
banksel PORTC
bsf PORTC,OE ;setting OE releases the data latch on the decoder
; and allows it to continue with its operation
return
;end ReadDecoder
ADIntRoutine:
;Depeding on the UserStatus, this function copies the AD result into either
; the first (x) result or the second (y) result and udates the UserStatus byte
; if both are already completed it does nothing
bcf PIR1,ADIF
;ADx
banksel UserStatus
btfsc UserStatus,ADxfinish ;if ADx is finished(set = finished): go to ADy
goto ADy ;else: skip this line
banksel ADCON0
bsf ADCON0,CHS0 ;Set the Analog Channel Pin to pin 1
banksel ADRES ;Go to the ADC result register
movf ADRES,w ;Get the value
banksel adcValuex
movwf adcValuex ;Store ADC value x in register
banksel UserStatus
bsf UserStatus,ADxfinish ;set ADxfinish bit
banksel inner
clrf inner ;wait for the ADC to get new value
bsf inner,7 ;128*3=384 (64 us)
incfsz inner,f
goto $-1
banksel ADCON0
bsf ADCON0,GO ;Set the GO bit for the ADC (ie. start AD conversion)
return
ADy btfsc UserStatus,ADyfinish ;if ADy is also finished return
return
banksel ADCON0 ;Go to the other ADC bank
bcf ADCON0,CHS0 ;Set the Analog Channel Pin to pin 0
banksel ADRES ;Go to the ADC result register
movf ADRES,w ;Get the value
banksel adcValuey
movwf adcValuey ;Store ADC value y in register
banksel UserStatus
bsf UserStatus,ADyfinish ;set the ADy finish bit
return
;end ADIntRoutine
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -