Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Uart pic

Status
Not open for further replies.
hi, i want to use the UART on the PIC16F690. TO START i just want to send some data from its TX pin to the RX PIN, so i can understand how it works. The code below is meant to send it recieve it and display it. The display works fine i just can recieve/transmitt data. can anyone point me in the right direction.

Code:
; DELAY EQUATES
CNT           EQU 0X25         ;ADDRESS FOR COUNTER
CNT2          EQU 0X26         ;ADDRESS FOR COUNTER2
CNT3          EQU 0X27         ;ADDRESS FOR COUNTER3
CNT4          EQU 0X28         ;ADDRESS FOR COUNTER4
; LCD EQUATES AND DEFINES
#DEFINE     DDRE   TRISB,7     ;DATA DIRECTION
#DEFINE     DDRRS  TRISB,4     ;DATA DIRECTION
#DEFINE     E      PORTA,2     ;ENABLE LINE ON LCD
#DEFINE     RS     PORTB,4     ;INSTRUCTION/DATA SELECT
#DEFINE 	RW	   PORTB,6
TEMP_LCD    EQU 0X3A           ;STORAGE FOR LCD
LCD         EQU PORTC          ;LCD DATA PORT
CLRSCR      EQU B'00000001'    ;COMMAND FOR CLRSCR
HOME        EQU B'00000010'    ;SENDS CURSOR TO HOME
ON          EQU B'00001100'    ;TURNS ON THE DISPLAY
OFF         EQU B'00001000'    ;TURNS THE DISPLAY OFF
LEFT        EQU B'00011000'    ;LEFT SHIFT
RIGHT       EQU B'00011100'    ;RIGHT SHIFT
LIN2        EQU B'11000000'    ;SETS TO LINE 2



;----------------------------------------
	; VECTOR FOR NORMAL START UP.
        ORG     0
        GOTO    START

; MAIN PROGRAM STARTS HERE:
START   CLRW			; CLEAR W.	
        MOVWF   PORTA           ; ENSURE PORTA IS ZERO BEFORE WE ENABLE IT.
        MOVWF   PORTB           ; ENSURE PORTB IS ZERO BEFORE WE ENABLE IT.
		movwf 	PORTC
		BSF     STATUS,RP1
		movlw	H'00'
		movwf 	ANSEL
		movwf 	ANSELH
		BCF     STATUS,RP0
        BSF     STATUS,RP0      ; SELECT BANK 1
        MOVLW   H'00'           ; MASK FOR PORTA FOR ALL OUTPUTS.
        MOVWF   TRISA           ; SET TRISA REGISTER.
        MOVLW   H'00'           ; MASK FOR PORTB FOR ALL OUTPUTS.
        MOVWF   TRISB           ; SET TRISB REGISTER.
		MOVLW	H'00'
		MOVwf	TRISC
        BCF     STATUS,RP0      ; RESELECT BANK 0.
        
        CALL	INIT_LCD	; Initialse the LCD
		bsf 	PORTA,2
        MOVLW   CLRSCR		; Clear the screen
        CALL    COMMAND		; Initiate the command in the W reg
		
        MOVLW   'H'		; send h
        CALL    DISPLAY		; Display the w reg on the LCD .;kv 
	
		MOVLW   CLRSCR		; Clear the screen
        CALL    COMMAND		; Initiate the command in the W reg
		MOVLW	'H'
		CALL 	send
		clrw

		MOVLW	H'FF'
		MOVWF	CNT3
COUNT3: MOVLW	0xff
		CALL 	DELAY
		DECFSZ CNT3,1
		GOTO COUNT3
			
		CALL	receive
		call 	DISPLAY



	goto		$	; Stop here	
	
	
	
;**************************lcdv6******************************
;Porta 1 for the enable and porta 2 for rs/ portb for lcd data
;*************************************************************
;************************************************************
; Initialize the LCD 
;************************************************************
INIT_LCD:
movlw .120
CALL	SEC_DELAY	           ; '' ''
MOVLW B'00110000'                  ;tell display its two lines
CALL COMMAND                       ;output to the screen
movlw .40
CALL SEC_DELAY
MOVLW B'00110000'
CALL COMMAND
MOVLW .100
CALL MDELAY
MOVLW B'00110000'
CALL COMMAND
MOVLW .100
CALL MDELAY
MOVLW B'00111100'					;FUNCTION SET
CALL COMMAND


MOVLW ON                           ;turn on display
CALL COMMAND                       ;output to the screen
MOVLW 0X01
CALL COMMAND
MOVLW B'00000110'                  ;shift along every write
CALL COMMAND                       ;output to the screen
RETURN
;************************************************************
; This routine is to set the lcd to the second line 
;************************************************************
SECOND_LINE:
MOVLW LIN2                     ;set to line 2
CALL COMMAND                   ;output to the screen
RETURN
;************************************************************
; THis executes a command in the W reg
;************************************************************
COMMAND:
BCF RS  
BCF	RW                       ;command coming
MOVWF LCD                      ;PUT OUT ONTO PORT
MOVLW .200                     ;delay time in microsecs
CALL MDELAY                    ;wait a while
MOVLW .250                     ;delay time in microsecs
CALL MDELAY                    ;WAIT
BSF E                          ;make sure the enable line is up 
MOVLW .200                     ;delay time
CALL MDELAY                    ;wait a while
MOVLW .250                     ;delay time in microsecs
CALL MDELAY                    ;WAIT
BCF E                          ;clock the data in
MOVLW .200                     ;delay time
CALL MDELAY                    ;wait a while
MOVLW .250                     ;delay time in microsecs
CALL MDELAY                    ;WAIT
BSF RS                         ;return to a data input
RETURN
;************************************************************
; This controls the module. When you call this 
; routine, the contents of the 'w' register is 
; displayed on the LCD screen. It is written in a 
; module like this for portability!! 
;************************************************************
DISPLAY:
BSF RS     
BCF	RW                    ;make sure it will accept data
MOVWF LCD                      ;send data to lcd
MOVLW .250                     ;delay time in microsecs
CALL MDELAY                    ;WAIT
MOVLW .250                     ;delay time in microsecs
CALL MDELAY                    ;WAIT
BSF E                          ;make sure the enable line is up 
MOVLW .250                     ;delay time
CALL MDELAY                    ;wait a while
MOVLW .250                     ;delay time in microsecs
CALL MDELAY                    ;WAIT
BCF E                          ;clock the data in
MOVLW .250                     ;delay time
CALL MDELAY                    ;wait a while
MOVLW .250                     ;delay time in microsecs
CALL MDELAY                    ;WAIT
RETURN


	
	
;*************************************************************
; 1 MICROSECOND DELAY 
;*************************************************************
MDELAY:
MOVWF CNT          ;LOAD DELAY TIME
COUNT:
NOP         ;TWO NO OPERATIONS
NOP                ;TO OBTAIN THE DELAY
DECFSZ CNT,1       ;DECREMENT THE COUNTER
GOTO COUNT         ;GOTO COUNT IF NOT FINISHED
RETURN


;*************************************************************
; quarter us delay 
;*************************************************************
DELAY:
MOVWF	CNT2
COUNT2: MOVLW	0xff
		CALL MDELAY
		DECFSZ CNT2,1
		GOTO COUNT2
RETURN

; ------------------------------------ 
; SET BAUD RATE TO COMMUNICATE WITH PC 
; ------------------------------------ 
; Boot Baud Rate = 9600, No Parity, 1 Stop Bit 
; 
UART_SET_UP:
        movlw 0x19              ; 0x19=9600 bps (0x0C=19200 bps) 
        movwf SPBRG 
        movlw b'00100100'       ; brgh = high (2) 
        movwf TXSTA             ; enable Async Transmission, set brgh 

        bcf STATUS,RP0          ; RAM PAGE 0 

        movlw b'10010000'       ; enable Async Reception 
        movwf RCSTA 
	    return 

; 
; ------------------------------------------- 
; RECEIVE CHARACTER FROM RS232 AND STORE IN W 
; ------------------------------------------- 
; This routine does not return until a character is received. 
; 
receive btfss PIR1,RCIF         ; (5) check for received data 
        goto receive 

        movf RCREG,W            ; save received data in W 
        return 
; 
; ------------------------------------------------------------- 
; SEND CHARACTER IN W VIA RS232 AND WAIT UNTIL FINISHED SENDING 
; ------------------------------------------------------------- 
; 
send    movwf TXREG             ; send data in W 

TransWt bsf STATUS,RP0          ; RAM PAGE 1 
WtHere  btfss TXSTA,TRMT        ; (1) transmission is complete if hi 
        goto WtHere 

        bcf STATUS,RP0          ; RAM PAGE 0 
        return 
		end
 
In order to configure pins RB6/SCK/SCL and
RB7/TX/CK as the Universal Synchronous
Asynchronous Receiver Transmitter:
• SPEN (RCSTA<7>) bit must be set (= 1),
• TRISB<6> bit must be set (= 1), and
• TRISB<7> bit must be set (= 1).
The operation of the EUSART module is controlled
through three registers:
• Transmit Status and Control (TXSTA)
• Receive Status and Control (RCSTA)
• Baud Rate Control (BAUDCTL)
• Baud Rate registers (SPBRGH:SPBRG)
See Registers 12-1, 12-2 and 12-3 for more detail.

I think you missed that part
 
hi Ed,
Number of errors.
You call a SEC_DELAY which dosnt exist.
You never call UART_SET_UP

You dont specify a crystal freq, so I cannot be sure of your delays.

Sort these out and repost.
 
I recognize some of that code. (I had to debug the very same code to test my USART application)
 
hi Ed,
Look at this modified version of your program, it runs in Oshonsoft.
ie: TXD out, RXD in
The UART sub and TXD sub's have been changed.

The delays need working on.:)
 

Attachments

  • forum1.asm
    6.7 KB · Views: 128
Eric It's like digging threw a box of parts you never know want you'll find. Then you have them canned .inc file for the compiler. I don't see how the code ever compiled But they say it did. You need Oshonsoft 101, C18 101. Hi-tech c 101, etc LOL
 
Eric It's like digging threw a box of parts you never know want you'll find. Then you have them canned .inc file for the compiler. I don't see how the code ever compiled But they say it did. You need Oshonsoft 101, C18 101. Hi-tech c 101, etc LOL

hi Burt,
As you say, been there, done that...:)

I am using some OP's program codes to check out the latest upgraded version of the Oshonsoft IDE Simulator.
 
Sorry i didn't mean to post that bit of code, was very tiered last night. This is the edited code. It seems to get stuck in the receive sub-routine, probably because no byte is ever recieved. I have ommitted the LCD code because that runs fine.
I can't think what could be going wrong i stole this code from a text book. Eric i'm sorry i wanted to see the link you sent but i don't have the software.

Code:
 ORG     0
        GOTO    START

; MAIN PROGRAM STARTS HERE:
START   CLRW			; CLEAR W.	
        MOVWF   PORTA           ; ENSURE PORTA IS ZERO BEFORE WE ENABLE IT.
        MOVWF   PORTB           ; ENSURE PORTB IS ZERO BEFORE WE ENABLE IT.
		movwf 	PORTC
		BSF     STATUS,RP1
		movlw	H'00'
		movwf 	ANSEL
		movwf 	ANSELH
		BCF     STATUS,RP0
        BSF     STATUS,RP0      ; SELECT BANK 1
        MOVLW   H'00'           ; MASK FOR PORTA FOR ALL OUTPUTS.
        MOVWF   TRISA           ; SET TRISA REGISTER.
        MOVLW   B'00100000'           ; MASK FOR PORTB FOR ALL OUTPUTS.
        MOVWF   TRISB           ; SET TRISB REGISTER.
		MOVLW	H'00'
		MOVwf	TRISC
        BCF     STATUS,RP0      ; RESELECT BANK 0.


        CALL	INIT_LCD	; Initialse the LCD
        MOVLW   CLRSCR		; Clear the screen
        CALL    COMMAND		; Initiate the command in the W reg
	
		
		CALL 	UART_SETUP
	

	
		MOVLW	'H'
		CALL 	SEND
	
		
		CALL	RECEIVE
		MOVLW	'H'
		CALL	DISPLAY
	
       
	

		
   
		
       

		goto		$	; Stop here	

; ------------------------------------------- 
; RECEIVE CHARACTER FROM RS232 AND STORE IN W 
; ------------------------------------------- 
; This routine does not return until a character is received. 
; 
RECEIVE btfss PIR1,RCIF         ; (5) check for received data 
        goto RECEIVE 

        movf w           ; save received data in W 
        return 
; 
; ------------------------------------------------------------- 
; SEND CHARACTER IN W VIA RS232 AND WAIT UNTIL FINISHED SENDING 
; ------------------------------------------------------------- 
; 
SEND    BCF PIR1,RCIF
		movwf TXREG             ; send data in W 
;WtHere  btfss TXSTA,TRMT        ; (1) transmission is complete if hi 
;       goto WtHere 
        return 	
	
UART_SETUP:
        movlw 0x19              ; 0x19=9600 bps (0x0C=19200 bps) 
		bsf	STATUS,RP0
        movwf SPBRG 
        movlw b'00100100'       ; brgh = high (2) 
        movwf TXSTA             ; enable Async Transmission, set brgh 

        bcf STATUS,RP0          ; RAM PAGE 0 

        movlw b'10010000'       ; enable Async Reception 
        movwf RCSTA 
	    return
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top