; ; *************************************************************************** ; *** Bubble Software Parallax to PIC Source Converter. Copyright 1999. *** ; *** http://www.picnpoke.com email: sales@picnpoke.com *** ; *************************************************************************** ; ; SEROUT port (in w), pin, baud, polarity, data ; Transmits data serially at the specified rate and polarity out the specified port ; pin. Data bytes to be transmitted must be arranged in memory starting at the ; address "buffer-1" and working downward (e.g., 30,29,28...). The number of ; bytes to transmit must be stored at the address labeled "buffer." A companion ; routine, BIN_ASC, converts 16-bit numbers to this form, optionally adding a ; decimal point. See the listing for Serout ...(format # data). ; Serout uses the common data format of no parity, 8 data ; bits, 1 stop bit (N81). ; Device data and reset vector P = pic16c55 #include <16c55.inc> ; processor assembler definitions _CONFIG _xt_osc & _wdt_off & _protect_off reset start buffer equ d'31' ; String storage at end of memory. B2400 equ d'30' ; Bit delay for 2400 baud at 4MHz. B1200 equ d'62' ; Bit delay for 1200 baud at 4MHz. B600 equ d'126'; Bit delay for 600 baud at 4MHz. B300 equ d'254'; Bit delay for 300 baud at 4MHz. org 8 baud Res d'1' ; Baud rate. pin Res d'1' ; Pin number (0-7). port Res d'1' ; Port number (0-2). temp1 Res d'1' ; Temporary counter for Serout. temp2 Res d'1' ; Temporary counter for Serout. temp3 Res d'1' ; Temporary counter for delay. polarity equ PA2; Polarity: 0=true, 1=inverted. ; Note that the polarity flag is the unused page-address bit of the PIC 16C5x ; series. If you port this program to another type of PIC, or another routine uses ; this bit, you may have to reassign it. org 0 ; Table to convert pin number (0-7) into bit mask (00000001b to 10000000b). Pinz ADDWF pcl RETLW d'1' RETLW d'2' RETLW d'4' RETLW d'8' RETLW d'16' RETLW d'32' RETLW d'64' RETLW d'128' ; Subroutine used by Serout to send a bit with the specified polarity (true or ; inverted) and timing (300, 600, 1200, or 2400 bps). emitBit MOVF port,w ; Point to the output port. MOVWF fsr MOVF baud,w ; Load baud rate into temp counter. MOVWF temp3 MOVF status,w ; Copy carry bit (data bit) into w. BTFSC status,pa2 ; If polarity = 1 then invert w.0. XORLW d'1' ANDLW d'1' ; Copy w.0 back to carry bit. BCF status,c ; (This roundabout method of inverting BTFSC status,z ; carry is needed since "XOR status,w" BSF status,c ; doesn't work properly.) ; This loop deserves some explanation. The first time through, it sets or clears ; the serial output bit. Thereafter, it's just a delay loop. emitBit_loop MOVF pin,w ; Get the pin mask. BTFSS status,c ; Data in carry bit. IORWF indirect ; If carry = 0 then set bit. COMF pin,w BTFSC status,c ANDWF indirect ; Else if carry = 1 then clear bit. GOTO $+1 ; Two-cycle nops for time delay. GOTO $+1 DECFSZ temp3 ; Number of trips through loop GOTO emitBit_loop RETLW 0h ; set by baud rate in temp3. ; Start of demo program. Upon power up or reset, the PIC transmits the string ; "OK" via pin 2 of port ra at 2400 baud, inverted polarity start CLRF 5h ; High is a start bit, so hold ra low. MOVLW d'0' ; All outputs. TRIS 5h MOVLW d'2' ; Put test string "OK" MOVWF buffer MOVLW 'O' ; into the buffer, starting with the MOVWF buffer-1 MOVLW 'K' ; number of characters. MOVWF buffer-2 BSF status,pa2 ; Inverted for direct connection. MOVLW B2400 ; 2400 baud. MOVWF baud MOVLW d'2' ; Pin 2. MOVWF pin MOVLW d'0' ; of port ra. MOVWF port CALL Serout ; Send the data. GOTO $ ; Endless loop ; Upon entry, the desired pin must already be set up as an output. ; The w register contains a number representing the output port (0-2) for ; RA through RC. Variable "pin" contains the pin number (0-7). The ; variable baud must contain a number representing the baud rate. ; The polarity flag must be set (1) for true, or cleared (0) for inverted ; output. A data string consisting of the number of bytes, followed by the ; bytes arranged downward in memory, must be at the location "buffer." ; Upon return, the data in the buffer will be unchanged, but the 0th entry ; (representing the string length) will have been decremented to 0. Serout MOVLW 5h ; Add offset for port RA. ADDWF port MOVF pin,w CALL Pinz ; Get bit mask from the table. MOVWF pin ; Put the mask into pin. MOVLW buffer-1 ; Point to first data address. MOVWF temp1 Serout_xmit BCF status,c ; Start bit into carry CALL emitBit ; Send start bit. MOVLW d'8' ; Send 8 data bits. MOVWF temp2 Serout_bits MOVF temp1,w ; Point to data byte. MOVWF fsr RRF indirect ; Rotate bit 0 into carry. CALL emitBit ; Send the data bit. DECFSZ temp2 ; All 8 bits sent? No: send more bits. GOTO Serout_bits MOVF temp1,w ; Point to the data byte. MOVWF fsr RRF indirect ; Rotate it back into original position. BTFSS status,pa2 ; If it was inverted, invert it again to COMF indirect ; restore original. BSF status,c ; Move stop bit into carry CALL emitBit ; Send the stop bit. DECF temp1 ; Point to next data byte. DECFSZ buffer ; All bytes sent? No: send more bytes. GOTO Serout_xmit RETLW 0h ; Yes: return. end