- 
	
	
	
		HSER baudrate 
		Hello,
 I'm not very familiar with the hardware serial port. So far I have used define.
 But when I tried this:
 
	Code: 
 HSerBaud var byte
 IF HSerBaud=0 THEN
 DEFINE HSER_BAUD 300
 ENDIF
 IF HSerBaud=1 THEN
 DEFINE HSER_BAUD 2400
 ENDIF
 IF HSerBaud=$BD THEN
 DEFINE HSER_BAUD 9600
 ENDIF
 
 I got an error, Redefininig symbor that can not be redefined...
 I do not know what registers should be set to change the baud rate.
 If you can help, so I would not read the whole datasheet. I'm using PIC18LF14K50.
 Thanks;)
 
 
- 
	
	
	
		Re: HSER baudrate 
		That's is correct as far as I know, you cannot redefine it in running  code, you can however select it in the beginning as your PIC starts.  Look at the code example in the lower 1/2 of this article. I show you  how to do it there.
 
 http://www.picbasic.co.uk/forum/content.php?r=171-LCD-serial-backpacks
 
 
 
- 
	
	
	
		Re: HSER baudrate 
		If you need to change the baud rate during run time do not use defines.  Deal with the registers directly.
 
	Code: 
 X VAR WORD
 START:
 RCSTA.4 = 0 : RCSTA.4 = 1
 RCSTA=$90:TXSTA=$24:SPBRG=129
 HSERIN [dec X]
 HSEROUT [DEC X.7,13]
 blah
 blah
 
 
 
 
- 
	
	
	
		Re: HSER baudrate 
		:D or there's Dave's way . . . 
 
- 
	
	
	
		Re: HSER baudrate 
		When changing baudrate on the fly, you want to turn off the USART before though.
 
 RCSTA=0
 SPBRG=xyz
 TXSTA = whatever, assuming it's value change from baudrate to baudrate
 RCSTA=$90
 
 
- 
	
	
	
		Re: HSER baudrate 
		Thanks, guys.
 Both examples helped me. Just to see which registers should be set. Today I'll try ..
 
 
- 
	
	
	
		Re: HSER baudrate 
		Get the latest version of my PICMultiCalc on this forum, it will give you a good kick. 
 
- 
	
	
	
		Re: HSER baudrate 
		Everything works great. The calculator has really helped me.
 Here's the code if someone needs it.
 
	Code: 
              IF HSerBaud=0 THEN
 RCSTA = 0
 SPBRG = 207 ' 300 Baud @ 4MHz, 0.0%
 RCSTA = $90 ' Enable serial port & continuous receive
 TXSTA = $20 ' Enable transmit, BRGH = 0
 endif
 IF HSerBaud=1 THEN
 RCSTA = 0
 SPBRG = 25  ' 2400 Baud @ 4MHz, 0.17%
 RCSTA = $90 ' Enable serial port & continuous receive
 TXSTA = $20 ' Enable transmit, BRGH = 0
 endif
 IF HSerBaud=$BD THEN
 RCSTA = 0
 SPBRG = 25  ' 9600 Baud @ 4MHz, 0.16%
 RCSTA = $90 ' Enable serial port & continuous receive
 TXSTA = $24 ' Enable transmit, BRGH = 1
 endif
 
 
 
 
- 
	
	
	
		Re: HSER baudrate 
		http://bravellir.com/gallery/d/5420-2/woohoo.gif
 For safety sake, I would set RCSTA at the end, just after TXSTA.
 
 
- 
	
	
	
		Re: HSER baudrate 
		Done. But I don't see what difference it makes.
 Can someone explain?
 
 
- 
	
	
	
		Re: HSER baudrate 
		It shouldn't make so much difference, but it's a bit more logical to me
 
 1. You disable the USART
 2. You set the baudrate and mode
 3. You Enable the USART
 
 Open the datasheet, and see what else you do when you write 0 or $90 to RCSTA, it does more than just enable/disable the USART.
 
 
- 
	
	
	
		Re: HSER baudrate 
		I looked into datasheet, and saw to change a bunch of thing. I just need to reset and set the seventh bit RCSTA to  turn off and on UART
 Is there a way to use the parity bit. Suppose 2400.8, E, 1, or 9600.8, E, 1 As I see hardware support and the ninth bit. The PBP I saw that there define the ninth bit.
 If I add this to the beginning of the code
 DEFINE HSER_EVEN 1
 DEFINE HSER_BITS 9
 How will changes in RCSTA, TXSTA,SPBRG  registers affect hserin or serout.
 
 
- 
	
	
	
		Re: HSER baudrate 
		RCSTA, TXSTA, SPBRG will override the DEFINE and configure the PIC USART.  It doesn't affect Serout, Serout2 or DEBUG.  Those are software solution.
 
 Again check out the USART section about Parity.
 
 
- 
	
	
	
		parity 
		I continue where I left off ...
 This is code:
 
	Code: 
          define OSC 4
 OSCCON=%01010110
 
 DEFINE HSER_CLROERR 1 'Hser clear overflow automatically
 '        SPBRG = 25  ' 9600 Baud @ 4MHz, 0.16%
 '        TXSTA = $24 ' Enable transmit, BRGH = 1
 '        RCSTA = $90 ' Enable serial port & continuous receive
 DEFINE HSER_BAUD 9600        'Set Baud rate to 9600bps
 DEFINE HSER_BITS 9        'Set to 9 bit mode
 DEFINE HSER_EVEN 1        'Set Even Parity
 
 TRISB.7 = 0 'Make Tx output
 TRISB.5 = 1 'Make Rx input
 
 main:
 pause 1000
 HSerOut ["Parity:)"]
 goto main
 
 Use this or not
 
	Code: 
 DEFINE HSER_BITS 9        'Set to 9 bit mode
 DEFINE HSER_EVEN 1        'Set Even Parity
 
 Always get a 9600, N, 1. PIC never send parity bit.
 I'm banging head all day, but I can't catch what I missed.
 
 
- 
	
	
	
		Re: HSER baudrate 
		Problem solved, thanks to Darrel.
 I find solution in this topic.