Sorry for the long post but this is driving me crazy!I am using a P18F45K22 running at 64MHz int osc. Windows 10 Fully updated, EasyPIC v7, MB v7.6.0. I have aslo tried a P18F44K22 and a couple of other 45K22s so I think I can rule out faulty ICs.
I would like to use both uarts, Uart1 to read data from a PC and Uart2 to write data out to a decoder unit byte by byte, which is what I need to do, as it is received. Same baud rate in and out.
I have done this before using a circular buffer to save a known number of sucessive bytes of received data before processing it and re tranmitting, but simply polling for Rx a byte on Uart 1 and send it to Uart 2 always writes to Uart 1.
I have tried various ways to get round this but it always seems to write to Uart 1.
The startup message did not write correctly at first and gave two copies of "Ready" to Uart 1 until I added the " UART_Set_Active(@UART1_Read, @UART1_Write, @UART1_Data_Ready, @UART1_Tx_Idle)" and "UART_Set_Active(@UART2_Read, @UART2_Write, @UART2_Data_Ready, @UART2_Tx_Idle)" lines at 57 & 61. The message is then sent correctly. I added the same to the byte tx routines for UART1 & 2 but that made no difference.(lines 30 & 35).
I tried writing directly to UART2 TXREG, but still output was to Uart1.
EasyPic v7, Uart1 is on COM3 via a USB to RS232(TTL level) converter to/from the PC and Uart2 is on the in built USB Uart on Com 11. I am using the MB Uart tool on COM3 to read and write to UART1, and Realterm capture program to monitor Uart 2.
The startup message is fine now but sending a simple byte by byte as typing or a string to UART1 always writes back to Uart1 but never to Uart2. It appears that text writes to UART2 are fine, but bytes or chars are not. Of course text is read from an array of bytes, not as a single byte, but I cannot see what the difference is, as the same basic write byte routine is just called repeatedly until the array has all been sent.
Code is based on the Uart1 read/write example supplied with MB V7 with writes to Uart2 added.
Code: Select all
' *
' * Project name:
' UART (Simple usage of UART module library functions)
' * Copyright:
' (c) Mikroelektronika, 2011.
' * Revision History:
' 20110929:
' - initial release (FJ);
' * Description:
' This code demonstrates how to use uart library routines. Upon receiving
' data via RS232, MCU immediately sends it back to the sender.
' * Test configuration:
' MCU: PIC18F45K22
' http://ww1.microchip.com/downloads/en/DeviceDoc/40001412G.pdf
' Dev.Board: EasyPIC7 - ac:UART
' http://www.mikroe.com/easypic/
' **Oscillator: ** Int HS PLL on = 64MHz.
' ** Was :HS-PLL 32.0000 MHz, 8.0000 MHz Crystal
' Ext. Modules: None.
' SW: mikroBasic PRO for PIC
' http://www.mikroe.com/mikrobasic/pic/
' * NOTES:
' - Turn on RX and TX UART switches (SW1.1 and SW2.1). (board specific)
' - Put RX and TX UART jumpers (J3 and J4) in RS-232 or USB position,
' depending on your choice (board specific).
' *
program UART
dim uart_rd,u2_rd as byte
Sub procedure U1_wr(dim inch as byte)
'UART_Set_Active(@UART1_Read, @UART1_Write, @UART1_Data_Ready, @UART1_Tx_Idle) makes no difference
UART1_Write(inch)
end sub
Sub procedure U2_wr(dim inch as byte)
'UART_Set_Active(@UART2_Read, @UART2_Write, @UART2_Data_Ready, @UART2_Tx_Idle) makes no difference
UART2_Write(inch)
' do
' nop 'check TX2 status
' loop until TXSTA2.TRMT
' TXREG2 = inch 'write to register starts transmission
' do
' nop 'check TX2 status
' loop until TXSTA2.TRMT 'wait for Tx to end
end sub
main:
OSCCON = 0x70
PLLEN_bit = true
ANSELC = 0 ' Configure PORTC pins as digital
ANSELD = 0
UART1_Init(9600) ' Initialize UART module at 9600 bps
Delay_ms(100) ' Wait for UART module to stabilize
UART2_Init(9600)
Delay_ms(100) ' Wait for UART module to stabilize
UART_Set_Active(@UART1_Read, @UART1_Write, @UART1_Data_Ready, @UART1_Tx_Idle)
UART1_Write_Text("Ready 1")
UART1_Write(13) ' Line Feed
UART1_Write(10) ' Carriage Return
UART_Set_Active(@UART2_Read, @UART2_Write, @UART2_Data_Ready, @UART2_Tx_Idle) 'without this, always writes to UART1
UART2_Write_Text("Ready 2")
UART2_Write(13) ' Line Feed
UART2_Write(10) ' Carriage Return
while (TRUE) ' Endless loop
if (UART1_Data_Ready() <> 0) then ' If data is received,
uart_rd = UART1_Read() 'read the received data
' Uart1_write(uart_rd)
'Uart2_write(uart_rd)
'Uart2_write(uart_rd) 'any way round writes to Uart1 only
'Uart1_write(uart_rd)
'U1_wr(uart_rd) ' sub routines to allow set up of uarts
U2_wr(uart_rd) 'always writes to UART1 only ???????????
end if
wend
end.
Code: Select all
_UART2_Write:
;__Lib_UART_c67d67.mpas,200 ::
;__Lib_UART_c67d67.mpas,201 ::
L__UART2_Write61:
0x001C 0xB272 BTFSC TXSTA2, 1
0x001E 0xD002 BRA L__UART2_Write62
0x0020 0x0000 NOP
0x0022 0xD7FC BRA L__UART2_Write61
L__UART2_Write62:
;__Lib_UART_c67d67.mpas,202 ::
0x0024 0xFF73C03E MOVFF FARG_UART2_Write_data_, TXREG2
;__Lib_UART_c67d67.mpas,203 ::
L_end_UART2_Write:
0x0028 0x0012 RETURN 0
; end of _UART2_Write
_UART1_Write:
;__Lib_UART_c67d67.mpas,86 ::
;__Lib_UART_c67d67.mpas,87 ::
L__UART1_Write14:
0x002A 0xB2AC BTFSC TXSTA, 1
0x002C 0xD002 BRA L__UART1_Write15
0x002E 0x0000 NOP
0x0030 0xD7FC BRA L__UART1_Write14
L__UART1_Write15:
;__Lib_UART_c67d67.mpas,88 ::
0x0032 0xFFADC03E MOVFF FARG_UART1_Write_data_, TXREG1
;__Lib_UART_c67d67.mpas,89 ::
L_end_UART1_Write:
0x0036 0x0012 RETURN 0
Thanks in advance.