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.

Inventing 2 uarts on single UART microcontroller

Status
Not open for further replies.

mik3ca

Member
In the past I tried using two microcontrollers with one UART each to interface to two UARTs but that setup was too much to manage with timing, so now I decided to use one microcontroller and turn a single UART into a dual UART through the use of tristate line drivers (in particular, the chip 74HC125). I wired their enable pins in such a way so that one logic level will enable one UART and disable the other while another logic level does vice-versa.

This is the code I thought of to allow me to control the UARTs but my main concern is that I don't want the UART switch to happen until a point happens where no transmission or reception takes place.

I attempted to do this by making timer 1 execute at 16x the speed of timer 0 because I think 16 is the magic number for serial (16 switch states for a shift register? I don't know). If my idea is wrong, correct me here.

If I decide to change the UART, then I added code which halts the main program until the change happens and a flag is set indicating it.

Please tell me if I'm on the right track, or if there is more efficient code I can use?

Code:
IORAD equ Px.x ;port that selects UART
UART equ 7Fh ;desired UART (setting in memory)
UARTR equ 7Eh ;desired UART recognized

org 0h
ajmp start
org 0Bh ;timer 0 int
push PSW ;save old C
mov C,UART ;load setting
mov IORAD,C ;and put it out to pin
setb UARTR ;and let processes the switch is done
pop PSW ;restore old C
reti
start:
mov A,#1h ;use 56k speed
lcall speedcfg
setb UART ;set by default on power on
setb EA
setb ET0
;send a '1' to first UART
clr TI
mov SBUF,#'1'
jnb TI,$
;switch UART
clr UART
clr UARTR
;wait until its officially switched
jnb UARTR,$
;send a '2' to second UART
clr TI
mov SBUF,#'2'
jnb TI,$
;use 1st uart again
setb UART
clr UARTR
jnb UARTR,$
;attempt to receive char from 1st UART within 256 clock cycles and save to R5
clr RI
mov R6,#0h
rt1:
jnb RI,nr1
mov R5,SBUF ;receive OK
ajmp nx
nr1:
djnz R6,rt1
nx:
;use 2nd uart again
clr UART
clr UARTR
jnb UARTR,$
;attempt to receive char from 2nd UART within 256 clock cycles and save to R4
clr RI
mov R6,#0h
rt2:
jnb RI,nr2
mov R4,SBUF ;receive OK
ajmp nx2
nr2:
djnz R6,rt2
nx2:
;rest of code here


;configure UART speed

speedcfg:
;Turn timers off
clr TR1
clr TR0
;save value
mov R7,A
;setup timer for mode 2 and UART for high speed and serial for 8N1
mov PCON,#80h
mov SCON,#50h
mov TMOD,#22h
;invert value (aka value=256-value) to satisfy mode 2 and store in TH1 + TL1
xrl A,#0FFh
mov TH1,A
mov TL1,TH1
;get saved value and multiply by 16 (aka swap + anl)
mov A,R7
swap A
anl A,#0F0h
;invert again
xrl A,#0FFh
mov TH0,A
mov TL0,TH0
;timer 0 flag is set 16x slower than timer 1 flag
setb TR0
setb TR1
ret
 
circuit.png


Here's my uart expansion circuit. USE2 is connected to a GPIO pin on the micro, and TX and RX are connected to the micro's TXD and RXD respectively.
 
Why don't you just use the RS485 hardware??? 1 to many... Just address each node... You can actually use addressing via the normal UART... If you use 1 more pin you can just chip select.. then two are not transmitting at the same time..
 
Not sure if RS485 works The radio modules I use that require uart only have 2 pins for data. TX and RX. It has no hardware flow control option and thats why I need 2 uarts. one for the micro to connect to radio and one for the micro to connect to PC
 
Not sure if RS485 works The radio modules
Where 'o' where did you mention radio modules... How do you expect reasonable responses if you don't give all the relevant data..

Most radio modules I have used allows hardware flow control to be switched off... However!! Last time I did this I use data switches the 74157 has 4 data switches so hardware can be used...
 
Not sure how 74xx157 will work with me because its two inputs to one output unless I use 2 of the individual multiplexers for transmit and one for receive. If the idea of using 74HC125 (as I have made the circuit board already) does not work, then I will look into 74xx157.
 
Whose radio modules are you using? I use Synapse Wireless radios with RS485 all the time.

RS485 is a PHY specification. Any UART will support it. You just have to use a RS485 translator on each device (something like a MAX485), which any RS485 device would need anyway.

Maxim makes the MAX485, MAX3485 (3.3V), MAX13080E series, and MAX13070 series (3.3V). They're rather inexpensive and available in half and full duplex variants. Each has transmit and receive enable control pins. When these pins are switched to the non-active state, the RS485 pair is tri-stated. You can have multiple translators on one device, then make active the control pins for the bus you wish to communicate with.

Even better would be to use a FT232 USB to UART translator for the PC to uC connection, which does have flow control pins (it actually breaks out an entire DB9 connector complete with CTS/RTS/DTR/DSR, etc). Then use a 485 translator for the uC to radio connection
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top