LIST P=PIC16F877
include <P16f877.inc>
org 0x00
__CONFIG _LVP_OFF &_WDT_OFF &_PWRTE_OFF &_CP_OFF & _HS_OSC
reset:
nop
goto start
org 0x20
start:
; configuration
;-----------------------------------
; used_ registers:
MUX_sel EQU H'0021' ; for not transmitting '000000000'
del1 EQU H'0030' ; for delay
del2 EQU H'0031' ; for delay
to_transmit EQU H'0050' ; Frequency data register
led1 EQU H'0070' ; for led delay
demo_tr EQU H'0072' ; demo transmit
loop_mux EQU H'0074' ; demo transmit
banksel ADCON1
movlw 0x07 ; ADC PINS configured as a digital I/O
movwf ADCON1
;UART/*/*/
banksel TRISC
bsf TRISC, 6
bsf TRISC, 7
banksel TXSTA
movlw 0xA7 ; master, transmit EN, Async mode, Transmit Shift Register empty, BRGH =1
movwf TXSTA ; Baud Rate = Fosc/(16(SPBRG+1)) , BRGH (baud rate) ignored in sync
banksel SPBRG
movlw 0x03 ; = d"16" which corresponds to 125Kbps with 8MHz clock
movwf SPBRG
banksel RCSTA
movlw 0x80 ; enable UART
movwf RCSTA
;PWM, timer2/*/*/
banksel TRISC
bcf TRISC, 2 ; make the CCP1 pin an output.
banksel T2CON
movlw 0x04
movwf T2CON ; no post/Prescaler (1:1) , TMR2 ON
banksel PR2 ; output frequency = 15000hz =
movlw 0x84
movwf PR2 ; 1 / [(PR2) + 1] • 4 • TOSC •(TMR2 prescale value)
banksel CCPR1L ;duty cycle =(CCPR1L:CCP1CON<5:4>) • TOSC • (TMR2 prescale value)
movlw 0x42 ; = 267 *1/8MHz = 50% d.c.
movwf CCPR1L
banksel CCP1CON
movlw 0x3C
movwf CCP1CON
;capture, timer1 /*/*/
banksel TRISC
bsf TRISC, 1 ; make the CCP2 pin an input.
banksel T1CON
movlw 0x09 ; Timer1 Input Clock Prescale 1:1, en TMR1
movwf T1CON ; , int clk (fosc/4) , oscillator en
banksel CCP2CON
movlw 0x07
movwf CCP2CON ; Capture mode, every 16th rising edge
banksel PIE2
bcf PIE2, CCP2IE ; avoid false interrupts
;MUX select
banksel TRISD
bcf TRISD, 0
bcf TRISD, 1
bcf TRISD, 2
;LED general delay
bcf TRISD, 4
bcf TRISD, 5
banksel PORTD
bcf PORTD, 4
bcf PORTD, 5
; end configuration --------------------------------------
;************ MAIN PROGRAM ***************
;*****************************************
main:
;MUX init
movlw 0x07
movwf MUX_sel
movlw 0x08
movwf loop_mux
;transmit check /*/*/*/*
movlw 0x05 ; demo check
movwf demo_tr
tra:
CALL DELAY
movf demo_tr, w
movwf to_transmit
CALL transmitting_data
decfsz demo_tr
goto tra
main_loop:
Led_flicker:
movlw 0x30
movwf led1
t2:
CALL DELAY
decfsz led1
goto t2
bcf PORTD,5
bsf PORTD,4
banksel led1
movlw 0x30
movwf led1
t1:
CALL DELAY
decfsz led1
goto t1
bcf PORTD,4
bsf PORTD,5
;---end Led flicker
;capture frequency/*/*
banksel TMR1H
clrf TMR1H
clrf TMR1L
banksel PIR2
bcf PIR2, CCP2IF
check_capture:
btfss PIR2, CCP2IF ; 1 = A TMR1 register capture occurred (must be cleared in software)
goto check_capture
bcf PIR2, CCP2IF
;transmitting data
banksel CCPR2H ; transmit MSB
movf CCPR2H, w
banksel to_transmit
movwf to_transmit
sublw 0xFF ; if data = FF change to FE
btfsc STATUS, Z
CALL data_change
CALL transmitting_data
banksel CCPR2L ; transmit LSB
movf CCPR2L, w
banksel to_transmit
movwf to_transmit
sublw 0xFF ; if data = FF change to FE
btfsc STATUS, Z
CALL data_change
CALL transmitting_data
;MUX select/*/*/
movf MUX_sel,w
banksel PORTD
movwf PORTD
decf MUX_sel, f
decfsz loop_mux
goto main_loop
movlw 0x07
movwf MUX_sel
movlw 0x08
movwf loop_mux
movlw 0xFF ; send FF at end of buffers
movwf to_transmit
CALL transmitting_data
goto main_loop
;******** END MAIN LOOP ********************
transmitting_data:
banksel TXREG
movwf TXREG ; writing to TXREG clears TXIF
trans_wait2:
btfss PIR1, TXIF ; wait for the frame to be transmitted
goto trans_wait2
CALL DELAY
return
data_change:
movlw 0xFE
movwf to_transmit
return
DELAY:
movlw 0xFF
movwf del1
movlw 0xFF
movwf del2
loopa:
decfsz del1, f
goto loopa
loopb:
decfsz del2, f
goto loopb
return
end