пример например :)
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)

миниатюрный аудио-видеорекордер mAVR

Отправлено zet557 15 июня 2004 г. 22:24
В ответ на: Как rs232 програмно на PIC организовать??? отправлено Oleg_n 15 июня 2004 г. 22:06

;
Title "Simple Bit Bang RS232."
;
list P = 16F676
include "P16f676.inc"


__CONFIG _CPD_OFF & _CP_OFF & _BODEN & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & H'11FF'

__idlocs H'1234'

;
; CRYSTAL SPEED = 4000000Hz
;
; TX pin = RC4
; RX pin = RC5
;
CBLOCK 0x20
;
BitWait ; bit period delay counter
TxSend ; transmit register
RxHold ; receive register
BCount ; bit Tx/Rx counter
w_tmp
s_tmp
;
ENDC
;

intE macro
bsf INTCON,INTE
bsf INTCON,GIE
endm
intD macro
bcf INTCON,GIE
endm


; -------------
; PROGRAM START
; -------------
;
#define TX_pin PORTC,4
#define RX_pin PORTC,5

org 0x0000 ; startup = 0000h
goto Start
org 0x0004 ; startup = 0004h
inter
movwf w_tmp
swapf STATUS, W
movwf s_tmp

call in_ra

swapf s_tmp, W
movwf STATUS
swapf w_tmp, F
swapf w_tmp, W

; btfsc INTCON, T0IF
; bcf INTCON, T0IF ;clear TMR0 overflow interrupt flag
; btfsc INTCON, INTF

retfie
;

in_ra
btfsc PORTA,2
goto if1_ra
goto if0_ra
if1_ra
bsf PORTC,2
goto ex_ra
if0_ra
bcf PORTC,2
goto ex_ra
;ex_ra

btfsc PORTA,0
goto if1_ra0
goto if0_ra0
if1_ra0
bsf PORTC,2
goto ex_ra
if0_ra0
bcf PORTC,2
goto ex_ra
ex_ra
return

Start
include ini676.inc
movlw 0x00
goto WaitRX
; ------------------------------------
; PROVIDE A SETTLING TIME FOR START UP
; ------------------------------------
;
clrf BitWait
settle
decfsz BitWait,F
goto settle
;
; ---------------------------------------------
; TRANSMIT A CHARACTER 'C' WHEN 'T' IS RECEIVED
; ---------------------------------------------
;
WaitRX
call RxRoutine ; wait for a character to be received
xorlw 'T'
btfss STATUS,Z
goto WaitRX ; not 'T'

movlw 'H' ; send 'Hi' back
call TxRoutine
movlw 'i'
call TxRoutine
goto WaitRX
;
; ---------------
; RECEIVE ROUTINE
; ---------------
;
RxRoutine
; intD
clrf STATUS
InRX
btfsc RX_pin ; wait for start bit
goto InRX
;
call HBDelay2 ; wait 1/2 bit length - 2 cycles
movlw 9h
movwf BCount
;
RxLoop
btfsc RX_pin ; start bit is lost during routine
goto RxHiBit
;
bcf STATUS,C ; receive start bit and 8 data bits
goto RxLoBit
;
RxHiBit
bsf STATUS,C
nop
RxLoBit
rrf RxHold,F
call HBDelay4 ; wait 1/2 bit length - 4 cycles
call HBDelay5 ; wait 1/2 bit length - 5 cycles
decfsz BCount,F
goto RxLoop
;
movf RxHold,W
; intE
return ; data returned in W Reg
;
; ----------------
; TRANSMIT ROUTINE
; ----------------
;
TxRoutine
movwf TxSend ; temp store data to be sent
bcf TX_pin ; do start bit
call HBDelay5
call HBDelay2
;
movlw 8h ; 8 data bits to send
movwf BCount
;
TxLoop
rrf TxSend,F
btfsc STATUS,C
goto TxHiBit
;
nop
bcf TX_pin
goto DoneBit
;
TxHiBit
bsf TX_pin
goto $ + 1
;
DoneBit
call HBDelay5 ; wait 1 bit length
call HBDelay5
decfsz BCount,F
goto TxLoop
;
call DoRet
nop
bsf TX_pin ; stop bit
call HBDelay1
call HBDelay
DoRet return
;
; -----------------------------
; HALF BIT DELAY FOR 19200 BAUD
; -----------------------------
;
HBDelay nop
HBDelay1 nop
HBDelay2 goto $ + 1
HBDelay4 nop
HBDelay5 movlw 0x05
movwf BitWait
BW decfsz BitWait,F
goto BW
nop
return
;
;
end

Составить ответ  |||  Конференция  |||  Архив

Ответы



Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание  |||  Без кадра

E-mail: info@telesys.ru