This is a multi-part message in MIME format. ------=_NextPart_000_003A_01C4399E.7C7E8960 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit I would try running the attached sample code as I found it of great help writing my own code. It is for the pic16f628 but shouldn't be a problem. z ----- Original Message ----- From: "Hoang Viet Cuong" To: Sent: 14 May 2004 23:27 Subject: [PIC:]Can't send characters to HyperTerminal from PiC16F877 > Hi, > > > > I'm new to PIC programming and trying to write a program to send a string to > the hyperterminal, through RS232 interface. After initializing RS232 > communicating, the PIC can receive characters from the hyperterminal and > echo back to the screen. And inside interrupt service, I can print string to > the hyperterminal (In my program, I try to print "BANK" and "TEST" to the > PC). However, right after initialsing the PIC, I cant send characters to PC > (In my program I try to send BANK to the HT). I don't know why there is > difference between sending characters to HT from within interrupt code and > from "normal" code (i.e. the code not handling interrupt)? Can anybody help > me with this one? Thank a lot. > > > > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; > > ; RS232 communicating > > LIST P=16F877, F=INHX8M > > #include > > __config _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_OFF & _LVP_OFF & > _BODEN_ON & _WDT_OFF & _HS_OSC & _PWRTE_ON > > > > ;--------------------------------------------------------------------------- > - > > ;Macros to select the register bank > > ;Many bank changes can be optimized when only one STATUS bit changes > > > > Bank0 MACRO ;macro to > select data RAM bank 0 > > bcf STATUS,RP0 > > bcf STATUS,RP1 > > ENDM > > > > Bank1 MACRO ;macro to > select data RAM bank 1 > > bsf STATUS,RP0 > > bcf STATUS,RP1 > > ENDM > > > > Bank2 MACRO ;macro to > select data RAM bank 2 > > bcf STATUS,RP0 > > bsf STATUS,RP1 > > ENDM > > > > Bank3 MACRO ;macro to > select data RAM bank 3 > > bsf STATUS,RP0 > > bsf STATUS,RP1 > > ENDM > > > > Send macro send_char > > movlw send_char > > call _RSxmit > > endm > > > > ;;;;;;;;;;Variables;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; > > cblock 0x70 > > W_Temp > > STATUS_Temp > > endc > > > > ;;;;;;;;;Reset and Interrupt Vectors;;;;;;;;;;;;;;;;;;;;;;;;;; > > org 0x0000 > ;Reset vector > > goto Start > > org 0x0004 > ;Interrupt vector > > goto IntService > > > > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; > > ; Program begins here > > > > Start > > call Initial_RS232 > > Send 'B' > > Send 'A' > > Send 'N' > > Send 'K' > > Loop > > goto Loop ; Loop > forever > > > > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; > > ; Initialisation subroutine > > Initial_RS232 > > clrf PIR1 > > clrf PIR2 ;Clear > interrupt flags registers > > clrf PORTA > > clrf PORTB > > clrf PORTC > > clrf PORTD > > clrf PORTE ;Clear > ports to output all 0's > > bsf STATUS, RP0 ; Set RAM Bank 1 > > movlw .7 > > movwf ADCON1 > ;PortA and PortE all digital I/O > > clrf TRISA ;Port A > all outputs > > clrf TRISB ;Port B > all outputs > > movlw .192 ;Port C > all outputs except for UART RX TX > > movwf TRISC > > clrf TRISD ;Port D > all outputs > > clrf TRISE ;Port E > all outputs > > movlw .36 > ;TX On, Async mode, high speed 9600bps, 8 bit trasnmission > > movwf TXSTA > > movlw .129 > ;9600bps at 20Mhz > > movwf SPBRG > > movlw .32 > > movwf PIE1 > ;Enable USART recieve interrupt > > bcf STATUS, RP0 ;Set > RAM Bank 0 > > clrf ADCON0 > > movlw .144 > > movwf RCSTA ;RX > On continous recieve > > movf RCREG, W > ;Empty USART RX FIFO > > movf RCREG, W > > movf RCREG, W > > movlw .192 > > movwf INTCON > ;Set GIE and periferal interrupts > > return > > > > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; > > ; USART interrupts handled here > > IntService > > movwf W_Temp ; > Save a copy of W > > swapf STATUS, W ; Swap status > and save to W > > clrf STATUS > ; Bank 0 > > movwf STATUS_Temp ; Save STATUS > > > > Send 'B' > > Send 'A' > > Send 'N' > > Send 'K' > > > > btfss PIR1, RCIF ; Check > Interrupt flag > > goto End_Int ; If No, go > to EndInt > > btfss RCSTA, FERR ; Check for > framing error > > goto No_Framing_Err > > movf RCREG, W ; Remove the > framing by reading FIFO > > goto End_Int > > No_Framing_Err > > btfss RCSTA, OERR ; Check if it is > overun error > > goto Receive_data > > bcf RCSTA, CREN ; > Remove overrun error by resetting USART > > nop > > bsf RCSTA, CREN > > goto End_Int > > > > clrf STATUS > > btfsc PIR1, TXIF ; Test for > TXIF transmit interrupt > > ; bsf STATUS, RP0 ; > Change to bank 1 if TXIF set > > ; btfsc PIE1, TXIE ; Test if > interrupt enabled if TXIF set > > goto Trans_data > > goto End_Int > > > > Receive_data > > movf RCREG, W > > movwf PORTB > > ;movwf TXREG > > Send 'T' > > Send 'E' > > Send 'S' > > Send 'T' > > > > goto End_Int > > Trans_data > > clrf STATUS > > movlw 'T' > > movwf TXREG > > End_Int > > swapf STATUS_Temp, W ; Swap STATUS_TEMP > into W > > movwf STATUS ; > Move W into STATUS register > > swapf W_Temp, f ; Swap W_Temp > > swapf W_Temp, w ; Swap W_Temp > into W > > retfie > ; return from interrupt > > > > _RSxmit > > Bank1 > > _RSbusy > > btfss TXSTA,TRMT ; check, if previous transmission > > goto _RSbusy ; has been terminated > > Bank0 > > movwf TXREG ; send next char > > RETURN > > end > > > -- > http://www.piclist.com#nomail Going offline? Don't AutoReply us! > email listserv@mitvma.mit.edu with SET PICList DIGEST in the body > -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body ------=_NextPart_000_003A_01C4399E.7C7E8960 Content-Type: text/plain; name="uart.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="uart.txt" ;*******************************************************************=20 ; Function: Sends alive message, then echoes characters at 9600 bps=20 ; Processor: PIC16F628 at 4 MHz using internal RC oscillator=20 ; Hardware: Testboard K4=20 ; Filename: 628uart.asm=20 ; Author: Lars Petersen, oz1bxm@qsl.net=20 ; Website: www.qsl.net/oz1bxm/PIC/pic.htm=20 ; Credit: Tony Nixon's test program at=20 ; www.piclist.com/techref/microchip/16f877/setup.htm=20 ;*******************************************************************=20 LIST P=3D16F628, R=3DDEC ; Use the PIC16F628 and decimal = system=20 #include "P16F628.INC" ; Include header file=20 __config _INTRC_OSC_NOCLKOUT & _LVP_OFF & _WDT_OFF & _PWRTE_ON = & _BODEN_ON=20 CBLOCK 0x20 ; Declare variable addresses starting at = 0x20=20 dataL=20 ENDC=20 ORG 0x000 ; Program starts at 0x000=20 ;=20 ; --------------------------------=20 ; SET ANALOG/DIGITAL INPUTS PORT A=20 ; --------------------------------=20 ;=20 movlw 7=20 movwf CMCON ; CMCON=3D7 set comperators off=20 ;=20 ; ----------------=20 ; INITIALIZE PORTS=20 ; ----------------=20 ;=20 movlw b'00000000' ; set up portA=20 movwf PORTA=20 movlw b'00000100' ; RB2(TX)=3D1 others are 0=20 movwf PORTB=20 bsf STATUS,RP0 ; RAM PAGE 1=20 movlw 0xFF=20 movwf TRISA ; portA all pins input=20 movlw b'11110010' ; RB7-RB4 and RB1(RX)=3Dinput, others = output=20 movwf TRISB=20 ; ------------------------------------=20 ; SET BAUD RATE TO COMMUNICATE WITH PC=20 ; ------------------------------------=20 ; Boot Baud Rate =3D 9600, No Parity, 1 Stop Bit=20 ;=20 movlw 0x19 ; 0x19=3D9600 bps (0x0C=3D19200 bps)=20 movwf SPBRG=20 movlw b'00100100' ; brgh =3D high (2)=20 movwf TXSTA ; enable Async Transmission, set brgh=20 bcf STATUS,RP0 ; RAM PAGE 0=20 movlw b'10010000' ; enable Async Reception=20 movwf RCSTA=20 ;=20 ; ------------------------------------=20 ; PROVIDE A SETTLING TIME FOR START UP=20 ; ------------------------------------=20 ;=20 clrf dataL=20 settle decfsz dataL,F=20 goto settle=20 movf RCREG,W=20 movf RCREG,W=20 movf RCREG,W ; flush receive buffer=20 ;=20 ; ---------=20 ; MAIN LOOP=20 ; ---------=20 ;=20 call message ; send "16F628 alive"=20 loop call receive ; wait for a char=20 call send ; send the char=20 goto loop=20 ;=20 ; -------------------------------------------=20 ; RECEIVE CHARACTER FROM RS232 AND STORE IN W=20 ; -------------------------------------------=20 ; This routine does not return until a character is received.=20 ;=20 receive btfss PIR1,RCIF ; (5) check for received data=20 goto receive=20 movf RCREG,W ; save received data in W=20 return=20 ;=20 ; -------------------------------------------------------------=20 ; SEND CHARACTER IN W VIA RS232 AND WAIT UNTIL FINISHED SENDING=20 ; -------------------------------------------------------------=20 ;=20 send movwf TXREG ; send data in W=20 TransWt bsf STATUS,RP0 ; RAM PAGE 1=20 WtHere btfss TXSTA,TRMT ; (1) transmission is complete if hi=20 goto WtHere=20 bcf STATUS,RP0 ; RAM PAGE 0=20 return=20 ;=20 ; -------=20 ; MESSAGE=20 ; -------=20 ;=20 message movlw '1'=20 call send=20 movlw '6'=20 call send=20 movlw 'F'=20 call send=20 movlw '6'=20 call send=20 movlw '2'=20 call send=20 movlw '8'=20 call send=20 movlw ' '=20 call send=20 movlw 'a'=20 call send=20 movlw 'l'=20 call send=20 movlw 'i'=20 call send=20 movlw 'v'=20 call send=20 movlw 'e'=20 call send=20 movlw 0x0D ; CR=20 call send=20 movlw 0x0A ; LF=20 call send=20 return=20 END -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body ------=_NextPart_000_003A_01C4399E.7C7E8960--