PDA

View Full Version : 115200 Baud to 9600 Baud Translator



ScottC
- 19th February 2005, 16:00
Has anyone done a translation table for serial commands? What I want to do is receive data in at 115200 and send out a different string at 9600. maybe 8 to 10 different commands. Is it Possible?

Seems it is. I use Pic Basic Pro. I have done serial in, and serial out only, but never both at the same time, yet alone at different baud rates. I am still kinda new at this, and only delt with PIC16F84. If anyone can recommend a PIC, that would be great too!

From what I can gather so far, looks like I'll use a 16F873 and a 22.1184 XTAL.

Any ideas on how to go about this?

Thanks,

ScottC

mister_e
- 19th February 2005, 16:51
depending of all your requirement, you can also whatever PIC with a internal USART like pic16f628, pic16F870 or elses.

Use a 20MHZ will be enough to have a great 115200 baud reception.

The only thing you'll have to do is to store every data receive in diferent variable or an array. once it's don send all of them serial. You'll be able to change the internal USART baudrate on the spot by changing only the SPBRG register.

ScottC
- 19th February 2005, 18:16
Steve,

Thanks for the info. I will try just that. It will be a few days as I need to order some PICs.

I have never worked with a UART before so that part should be interesting.

Scott

ScottC
- 25th February 2005, 06:44
I don't understand why I am getting errors on this. Is my array set up right?

If I replace BAUDIN with N2400 on SERIN2 line, I only get bad expresion during the compile
Why?
If I can get this to run at 9600, I would like to increase the input to 115200. What should I put in for the OSC? 20MHZ, and run a 22.1184MHZ xtal?

Would like to use a 16F84A or 16F873A with UART
Any Ideas?

Thanks,

ScottC

DEFINE OSC 4 ' 20MHz oscillator
RSDATA VAR BYTE[7] ' Data input array
BAUDIN CON 16468 ' 115200,8,N,1 Serial In
BAUDOUT CON 16468 ' 9600,8,N,1 for Serial Out
TX VAR PORTA.0 ' Pin for TX
RX VAR PORTA.1 ' Pin for RX
'********************************************

TRISA = %01000010

'***************************** READ SERIAL PORT

LOOP:

SerIn2 RX,BAUDIN,[WAITSTR RSDATA\7] 'serial data in on PortA.1

Bruce
- 25th February 2005, 16:35
You get the error because N2400 hasn't been defined.

Declare N2400 as a constant first.

N2400 CON 16780 ' <-- Baud modes shown in manual for serin2/serout2

Now you can use N2400 with serin2 to set the baud mode.

Note: Look at the DEBUG / DEBUGIN commands if you want high-speed serial without a hardware USART, odd value or fast crystal. With DEBUGIN you can easily get 115200bps with a 4MHz crystal. You can't with serin2.

You can use the 16F84A running @20MHz with DEBUGIN receivng at 115200, and serout2 re-sending at 9600, but you'll want a crystal speed > 4MHz for serin2 @ 9600 if you want reliability.

ScottC
- 26th February 2005, 03:06
Bruce,

Thanks for the info, but to run up the rate, what number do I use for 115200?
The manual only shows values up to 9600 baud using 4 mhz xtal. Or is there a way to calculate that number or do you "FOOL" PBP by substituting a different xtal?

Scott

Bruce
- 26th February 2005, 04:42
As shown in the manual, to find the value for a given baud rate, use the equation (1000000/baud)-20. This doesn't work for serin2/serout2 @ 115200 bps.

With DEBUGIN it's simple.

DEFINE DEBUGIN_REG PORTC
DEFINE DEBUGIN_BIT 7
DEFINE DEBUGIN_MODE 0 '1 = Inverted, 0 = true
DEFINE DEBUG_BAUD 115200

Yes. There are ways to trick PBP into working at higher data rates with serin2 & serout2, but then everything else in your program is whacked timing wise since PBP adjusts timing for everything else based on the defined osc speed.

ScottC
- 18th March 2005, 14:55
I have been messing with this for a bit and no luck. I am using a 16F873A with UART. I see a burst of data on my scope, but PC does not see it. I also know that I have to invert the signal due to the nature of HSEROUT.

Is what I have code wise correct?

Any Ideas? I am using a 22.1184MHz Xtal. code is below.
Just using this as a test to see if I can get 115200 baud to work. Then I'll try to concour the above issue posted.


DEFINE OSC 20 ' 20MHz oscillator
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h
DEFINE HSER_BAUD 115200
DEFINE HSER_SPBRG 2
TRISA = 0
TRISB = 0
TRISC = 0

PORTB.0 = 1

START:

HSerout ["12345",10]
Pause 1000

GoTo START
End

Thanks,

Scott

Ceug2005
- 18th March 2005, 15:37
try at lower speed
this what i use , not tested uper 9600

define OSC 20
DEFINE HSER_TXSTA 20h ' enable the transmit register
DEFINE HSER_BAUD 2400
.
.
.' +/-
HSEROUT [dec sign0,10,dec tempc0,13]

better to use an MAX 232 to conect at COMx

Ingvar
- 18th March 2005, 16:49
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_SPBRG 11

Ingvar
- 18th March 2005, 17:07
.... and mode 97 should give 9600 baud with SEROUT2. However, you would probably do yourself a favour by switching to a 20MHz crystal since PBP likes that better, all timings will be correct. 115200 baud works quite well with 20MHz and HSEROUT.

DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_SPBRG 10

.... will give you 115200 with 20MHz.

ScottC
- 19th March 2005, 06:24
Well, at last I have success! Thanks to all who pitched in to make this happen. I adjusted the values in the define statements as suggested.

I am still using 22.1184 XTAL. Here is the test code below.

Thanks again.......

Scott


DEFINE OSC 20 ' 20MHz oscillator (22.1184MHz)
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 115200
DEFINE HSER_SPBRG 11
TRISA = 0
TRISB = 0
TRISC = 0

START:

HSerout ["HELLO"]
Pause 500

GoTo START
End

ScottC
- 21st March 2005, 14:38
Well, interesting. Now I am trying to take in the character at 9600 and resend at 115200. When I do this code below, my results are not what I want!

For example when I send the number 1, I receive an "a", send 2, I get an "f"
3 a "g", ect till I send 6, the sequence jumps to "n", "o".. Really strange.

I don't know if it is a timing issue or not???? Puzzled.

It was mentioned above that I could change the baud rate on the fly, How is that done? Could the issue be I have different baud rates on different pins, seems it should not matter. I'm lost..

ScottC

DEFINE OSC 20 ' 20MHz oscillator
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 115200 '115200 Baud Out
DEFINE HSER_SPBRG 11
DEFINE DEBUGIN_REG PORTB ' 9600,8,N,1 Serial In
DEFINE DEBUGIN_BIT 7
DEFINE DEBUG_BAUD 9600
DEFINE DEBUGIN_MODE 1
B7 VAR BYTE

'**********************************************

TRISA = 0
TRISB = %00000001
TRISC = 0

'********************************* READ SERIAL PORT

loop: While PORTB.7 = 1 'Wait for start bit
Wend

DebugIn [B7] ' B0 = input character
Pause 500

print: HSerout [B7]

GoTo loop ' Forever

Bruce
- 21st March 2005, 15:32
Are you're still using the 22.1184MHz crystal?

DEBUGIN bit timing is calculated for 20MHz with DEFINE OSC 20.

ScottC
- 21st March 2005, 17:43
yes,

I am still using 22.1184 XTAL. I undersatand what you are saying, from what info I gathered, this or 11.something, 1/2 of 22.1184, or an 18.432 xtal is what you want for error free reception. I am still new at this so I'm still in the learnig curve. But it makes sense what you are saying. I am going to try changing the SPBRG later in the program using DEFINE HSER_BAUD 9600, and DEFINE HSER_SPBRG 143 to change the valuse. I compiled it, have not tested yet, I'll get to in a bit see if that works. What do you think?

Scott

Bruce
- 21st March 2005, 18:07
With HSERIN/HSEROUT you can change the data-rate by writing directly to SPBRG.


Here:
SPBRG = 129 ' 9600 @20MHz
HSEROUT ["TEST AT 9600",13,10]
PAUSE 10
SPBRG = 64 ' 19200 @20MHz
PAUSE 5000 ' Time to switch terminal program from 9600 to 19200
HSEROUT ["TEST AT 19200",13,10]
PAUSE 5000
GOTO Here

You can't change DEFINEs at run time, so just write to SPBRG directly if you want to change data-rates with HSEROUT/HSERIN.

With DEBUG/DEBUGIN the data-rate & pins used are fixed, you can't change data-rates on the fly, and you'll want your osc frequency as close as possible to whatever you have it DEFINEed as.

Ingvar
- 22nd March 2005, 10:16
OK, think about it .... you're running your pic at 22.1184MHz, PBP thinks it's running at 20MHz. You'll actually run 22.1184/20=1.10592 times too fast. You need to lower your baudrate by this factor to get DEBUGIN to run at the correct speed. 9600/1.10592=8681 should do the trick.