Hello,
I'm playing around with UART on a 18F2550 after using 16F chips previously, I set up the hardware UART as with the 16F without any problem, I send a value it returns the same value.
I then tried to return the value using software uart on PORTB,7 but it sends back different values which makes me think there is something wrong with the clock speed possibly? I am using a 4MHz crystal and the XT_XT configuration.
I'm using the following code to implement the software UART at a baud rate of 9600
Is there something I'm missing, something thats different in the 18F series compared to the 16F which is causing the problem?
I'm playing around with UART on a 18F2550 after using 16F chips previously, I set up the hardware UART as with the 16F without any problem, I send a value it returns the same value.
I then tried to return the value using software uart on PORTB,7 but it sends back different values which makes me think there is something wrong with the clock speed possibly? I am using a 4MHz crystal and the XT_XT configuration.
I'm using the following code to implement the software UART at a baud rate of 9600
Rich (BB code):
baud ; AT 2400 BAUD THE PERIOD IS 416.6 US
; CLK = 4MHz
movlw D'16' ; 1 US (BAUD RATE CONSTANT)
movwf COUNT ; 1 US
baud1
decfsz COUNT,F ; 1 US (+ 1 US MORE IF SKIP)
goto baud1 ; 2 US
; FALL THRU...AFTER 1+1+3x68+1 = 207 US
half_baud
movlw D'16' ; 1 US
movwf COUNT ; 1 US
hbaud1
decfsz COUNT,F ; 1 US (+ 1 US MORE IF SKIP)
goto hbaud1 ; 2 US
retlw 0 ; ...AFTER 1+1+3x68+1 = 207 US (X2=414 US)
outch_n ; THIS ROUTINE USES 8 DATA BITS
movwf SERBUF ; SERBUF CONTAINS CHARACTER TO XMT
movlw 8 ; THE CHARACTER HAS 8 BITS
movwf TEMP
bcf PORTB,7 ; SET START-BIT TO A "SPACE"
call baud ; WAIT ONE BAUD TIME
outch_n1
rrf SERBUF,F ; ROTATE THE FIRST BIT INTO CARRY
btfss STATUS,0 ; TEST THE CARRY BIT
bcf PORTB,7 ; IF BIT IS 0 SET OUTPUT PIN TO A "0" (SPACE)
btfsc STATUS,0 ; TEST THE CARRY BIT AGAIN
bsf PORTB,7 ; IF BIT IS 1 SET OUTPUT PIN TO A "1" (MARK)
call baud ; ONE BAUD-BIT DELAY
decfsz TEMP,F ; IF COUNT IS ZERO THEN XMIT A STOP BIT
goto outch_n1 ; ...ELSE XMIT NEXT BIT
rrf SERBUF,F ; ROTATE CARRY, GET THE MSB BACK INTO BIT 7
bsf PORTB,7 ; SET PIN TO A 1 (A "MARK") FOR THE STOP BIT
call baud ; FIRST BAUD-BIT DELAY
call baud ; SECOND BAUD-BIT DELAY
retlw 0 ; RETURN WITH THE CHARACTER IN SERBUF