----- Original Message ----- From: "Tony Nixon" To: Sent: Friday, October 25, 2002 9:04 AM Subject: Re: 16f628 echo chars to pc > al wrote: > > > > We were given this code and told it will echo characters back to a pc. > > Told that it works. > > > Try this.... > > ; > Title "Simple serial" > ; > list P = 16F628 > ; > include "P16f628.inc" > ; > ; ------------------ > ; CONFIGURATION FUSE > ; ------------------ > ; > __CONFIG _CP_OFF & _HS_OSC & _WDT_OFF & _PWRTE_OFF & _BODEN_OFF & > _LVP_OFF & _MCLRE_ON > ; > ; > ; ------------- > ; REGISTER LIST > ; ------------- > ; > CBLOCK 0x20 > > RxHold > DelayH > DelayL > > ENDC > ; > ; --------------------- > ; YOUR CODE BEGINS HERE > ; --------------------- > ; > org 0x0000 > > > clrf PORTA ; assumes nothing connected to ports > movlw b'00000100' ; except serial port > movwf PORTB > bsf STATUS,RP0 > clrf TRISA > movlw b'00000010' > movwf TRISB > ; > ; ------------------------------------ > ; SET BAUD RATE TO COMMUNICATE WITH PC > ; ------------------------------------ > ; Boot Baud Rate = 19200, No Parity, 1 Stop Bit > ; > bsf STATUS,RP0 > movlw 0x0C ; 19200 baud > movwf SPBRG > movlw b'00100100' ; brgh = high (2) > movwf TXSTA ; enable Async Transmission, set brgh > movlw b'10010000' ; enable Async Reception > bcf STATUS,RP0 > movwf RCSTA > ; > ; ----------------------- > ; DISABLE THE COMPARATORS > ; ----------------------- > ; > movlw b'00000111' ; disable comparators > movwf CMCON > > call Del12mS > > > MainLoop call Receive ; wait for reception > movwf TXREG ; echo it back > goto MainLoop > > ; > ; ---------------------------- > ; RECEIVE CHARACTER FROM RS232 > ; ---------------------------- > ; This routine does not return until a character is received. > > Receive btfss PIR1,RCIF ; (5) check for received data > goto Receive > > movf RCREG,W > return > ; > ; ---------- > ; 12mS Delay > ; ---------- > ; > Del12mS clrf DelayL > movlw 11h > movwf DelayH > InDel12 decfsz DelayL > goto InDel12 > decfsz DelayH > goto InDel12 > return > > ; > ; > end > > -- Thanks for that. Modified my orginal program. Any suggestions on better ways or cleaner code. Also how do you use weak pull ups. I am not clear on this, or how you set it. All help greatly appreciated. Thank you. Al ; ;*********************************************************** ; PIC16f628 EXAMPLE CODE FOR a PIC usart demo ; ; ; FREQUENCY: 4MHz ; ;************************************************************ ; This program demonstrates basic functionality of the USART. ; ; Port B is connected to 8 LEDs. //excluding the one the usart is on ; When the PIC16f628 receives a byte of data from ; the USART, the value is displayed on the LEDs and ; is retransmitted to the host computer. ; ; Set terminal program to 9600 baud, 1 stop bit, no parity ; ; Get this working show it Echo'ing to a PC, then make it do something usefull list p=16f628 ; set processor type list n=0 ; supress page breaks in list file #include __CONFIG _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _WDT_OFF & _CP_OFF & _LVP_OFF & _PWRTE_ON ;************************************************************ ; Reset and Interrupt Vectors org 0h ; Reset Vector goto Start org 4h ; Interrupt vector goto IntVector ;************************************************************ ; Program begins here org 5h ; Beginning of program EPROM Start movlw 0 BANKSEL TRISB movlw b'00000110' movwf TRISB ;0=output 1=input ;!BUT for UART use RB1 and RB2 MUST be programmed as an INPUT! BANKSEL PORTB movwf PORTB ; Clear PORTB output latches movlw 0 BANKSEL TRISA movlw b'00000000' movwf TRISA BANKSEL PORTA movwf PORTA ; Now let us set the baud rate BANKSEL SPBRG movlw D'25' ; 9600 baud @4MHz movwf SPBRG ; ; Now we must set up the type of transmission we want BANKSEL TXSTA movlw b'00100100' ;Transmit enabled and high baud rate selected movwf TXSTA BANKSEL RCSTA movlw b'10010000' ;Serial port enabled and continuous recieve enabled movwf RCSTA BANKSEL PIR1 bcf PIR1,RCIF ;Recieve interrupt flag cleared BANKSEL PIE1 bsf PIE1,RCIE ;recieve interrupt enabled BANKSEL INTCON bsf INTCON,PEIE ;Peripheral interrupt enabled bsf INTCON,GIE ;Global interrupt enabled BANKSEL RCREG ;-------Send Dummy Char to start UART (must do ..alex)------------------ movf RCREG,W ;clear uart receiver movf RCREG,W ; including fifo movf RCREG,W ; which is two deep. movlw 0 ;any character will do. movwf TXREG ;send out dummy character ; to get transmit flag valid! ;************************************************************ ; Main loop Main goto Main ; loop to self doing nothing ;************************************************************ ; Interrupt Service Routine IntVector ; save context (WREG and STATUS registers) if needed in some real application. BANKSEL PIR1 btfss PIR1,RCIF ; Did USART cause interrupt? goto OtherInt ; No, some other interrupt bcf PIR1,RCIF movlw 06h ; Mask out unwanted bits andwf RCSTA,W ; Check for errors btfss STATUS,Z ; Was either error status bit set? goto RcvError ; Found error, flag it movf RCREG,W ; Get input data movwf PORTA ; Display on LEDs - note not enough pins on one port ; we need to get some spare pins from bot ports A and port B to do this (changed to A) movwf TXREG ; Echo character back goto ISREnd ; go to end of ISR, restore context, return RcvError bcf RCSTA,CREN ; Clear receiver status bsf RCSTA,CREN movlw 0FFh ; Light all LEDs movwf PORTB goto ISREnd ; go to end of ISR, restore context, return OtherInt ; Find cause of interrupt and service it before returning from ; interrupt. If not, the same interrupt will re-occur as soon ; as execution returns to interrupted program. retfie ISREnd ; Restore context if needed in some real application. retfie end -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.