Settings for remappable uarts

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
dangerous
Posts: 748
Joined: 08 Mar 2005 16:06
Location: Nottinghamshire, UK

Settings for remappable uarts

#1 Post by dangerous » 28 Apr 2022 15:49

Hi, Can anyone help please?
I am trying to set up a PIC18F45K42 to use uarts on port C 6 & 7 and D 6 & 7, to replace a PIC 18F45K22 in an already designed board.
It has to run at 64MHz from the internal oscillator, and that seems to be OK but I cannot get the PPS remappable function to accept what I am telling it:

Code: Select all

 Unlock_IOLOCK()
    PPS_Mapping_NoLock(22, _INPUT, _RX2_DT2)   '; // RX - RP22.
    PPS_Mapping_NoLock(23, _OUTPUT, _TX2_CK2)  '; // TX - RP23.
    Lock_IOLOCK()
    UART2_Remappable_Init(9600)                     ' Initialize UART module at 9600 bps
Line 2 produces errors "Identifier _RXD2_DT2 not defined."

Changing to

Code: Select all

 PPS_Mapping_NoLock(22, _INPUT, _RX2)   '; // RX - RP22.
    PPS_Mapping_NoLock(23, _OUTPUT, _TX2)
gives "Identifier _Tx2 not defined"

Any ideas why?

Also do I call the uarts as UART1_Read() etc or do I have to use UART1_remappable_Read(), and chage to UART2_remappable_Read() for the other uart?

Any help would be appreciated.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Settings for remappable uarts

#2 Post by filip » 29 Apr 2022 14:48

Hi,

Try something like this :
PPS_Mapping(_RC6, _OUTPUT, _TX1PPS); //RC6
PPS_Mapping(_RC7, _INPUT, _RX1); //RC7
Regards,
Filip.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Settings for remappable uarts

#3 Post by janni » 01 May 2022 15:18

dangerous wrote:
28 Apr 2022 15:49
Also do I call the uarts as UART1_Read() etc or do I have to use UART1_remappable_Read(), and chage to UART2_remappable_Read() for the other uart?
Take a look here.

BTW, the 'pin numbers' you used as arguments in PPS_Mapping_NoLock can be determined by a simple function, like

Code: Select all

' determines 'Remappable pin number' for PPS_Mapping and PPS_Mapping_NoLock
' from port letter and pin number, for example PPSpin("B",6).
' May also be used directly in input pin assignments, like RX1PPS=PPSpin("C",7)
sub function PPSpin(dim port as char, dim pin as byte) as byte
  result=255
  if ord(port)>=65 then
   if ord(port)<=70 then
    if pin<=7 then
      result=ord(port)-65
      result=swap(result)>>1+pin
    end if
   end if
  end if
 end sub 'PPSpin

dangerous
Posts: 748
Joined: 08 Mar 2005 16:06
Location: Nottinghamshire, UK

Re: Settings for remappable uarts

#4 Post by dangerous » 03 May 2022 12:41

Thanks janni and Filip,

Janni,the code in your link does not work for the 18F45K42 (the definition RX1DTPPS and RX2DTPPS are not declared according to the error messages). It seems to accept U1RXPPS =_RC7 U2RXPPS =_RD7. (Does not seem to work though)

Filip PPS_Mapping(_RC6, _OUTPUT, _TX1PPS) ' //RC6 PPS_Mapping(_RC7, _INPUT, _RX1) ' //RC7 Throws no error.

Neither works. I have tried to adapt the suggestions from here and loaded the revised mcl file given: viewtopic.php?f=93&t=72327&p=291823&hil ... le#p291823

I am using EasyPic7 and portc 6 & 7 are mapped to the RS232 port, which appears as COM6 in the MB Usart terminal. Nothing is received. Tx does n ot seem to do anything either. With a scope on RA6, clock = 16MHz, so it seems that the clock is working at 64MHz.

Also I cannot debug with the EP7, ICD not supported in MikroProg for this MCU message is displayed. Is there a fix for this?

With the code below, 'UART1_Write_Text("Hello") 'throws error "incompatible type, c0mplex type to simple type". Individal chars seem to be accepted. (still does not work)

Code: Select all

    
Program UART
dim uart_rd as byte
Dim message as byte[5]


main:
    OSCCON1 = 0x60
    OSCFRQ = 0x08                         '64MHZ
    ANSELA = 0                                                                  ' let's keep life simple and make all digital
    ANSELB = 0                                                                  ' all digital   - Rx will not work if analogue
    ANSELC = 0                                                                  ' all digital
    ANSELD = 0                                                                  ' all digital
    ANSELE = 0                                                                  ' all digital
    TRISA = 0xff                                                                ' all input
    TRISB = 0xff                                                                ' all input
    TRISC = 0xff                                                                ' all input
    TRISD = 0xff                                                                ' all input
    TRISE = 0xff                                                                ' all input
    Delay_ms(3000)                                                              ' give plenty of power-up settle time

'
' leave portC as default RX RC7, TX RC6
     RC6PPS = 0                     '
     RC7PPS = 0
'     need to remap to port d 6 & 7
'
    PPS_Mapping(_RC6, _OUTPUT, _TX1PPS)     ' //RC6
    PPS_Mapping(_RC7, _INPUT, _RX1)        ' //RC7
'
      Lock_IOLOCK()
      trisc = 0x80               'bit 7 is input (RX)
     'trisd = 0x80

     UART1_Remappable_Init(9600)                     ' Initialize UART module at 9600 bps
    'UART2_Remappable_Init(9600)
    Delay_ms(100)   
    UART_Set_Active(@UART1_Read, @UART1_Write, @UART1_Data_Ready, @UART1_Tx_Idle)
    'UART_Set_Active(@UART2_Read, @UART2_Write, @UART2_Data_Ready, @UART2_Tx_Idle)
     Uart1_Remappable_Write("H")
     Uart1_Write("e")
     Uart1_Write("l")
     Uart1_Write("l")
     Uart1_Write("o")
    ' UART1_Write_Text("Hello")          'throws error  incompatible type
     UART1_Write(13)                      ' Line Feed
     UART1_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)             ' and send data via UART
    end if
  wend
end.
Getting a bit lost here.

Any help woudl be appreciated as I am trying to replace a 18F45K22 and need to sort the UARTS out as we are using both UART1 and UART2.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Settings for remappable uarts

#5 Post by janni » 03 May 2022 15:43

dangerous wrote:
03 May 2022 12:41
Janni,the code in your link does not work for the 18F45K42 (the definition RX1DTPPS and RX2DTPPS are not declared according to the error messages). It seems to accept U1RXPPS =_RC7 U2RXPPS =_RD7. (Does not seem to work though)
I wasn't directing you to code - the link was to my post about USART library routines, like Read or Write as you asked about it.

I've checked and the statements PPS_Mapping(_RC6, _OUTPUT, _TX1PPS) and PPS_Mapping(_RC7, _INPUT, _RX1) produce valid assembly code. Though the simulator doesn't emulate MOVFFL assembly instruction, it should work fine in real environment (have you checked the USART jumpers on your EasyPIC board?).
Of course, you can avoid the PPS library by using the same constants in direct assignments (and saving 2k of code memory)

Code: Select all

TRISC.6=0
TRISC.7=1
Unlock_IOLOCK    ' not needed directly after reset
U1RXPPS=_RC7     ' not really necessary as it's the default
RC6PPS=_TX1PPS
Lock_IOLOCK
Also, UART1_Write_Text("Hello") does not produce the error you described - which compiler version did you use?

And this

Code: Select all

' leave portC as default RX RC7, TX RC6
     RC6PPS = 0                     '
     RC7PPS = 0
has not the effect you intended - it removes the default RX assignment to RC7.

dangerous
Posts: 748
Joined: 08 Mar 2005 16:06
Location: Nottinghamshire, UK

Re: Settings for remappable uarts

#6 Post by dangerous » 04 May 2022 08:32

Thanks janni,

I will have another try today. Uart1 is set to RC6 and RC7 on the EP 7 board then runs tyo the PC via a USB - Serial converter. That worked fine with the 45K22 so I am pretty sure that is OK. The compiler is MB V7.6.0 (latest version I believe).

Uart1_write_text ("Hello") has always worked with other PICs, but not on this MCU for some reason. Maybe I will re-install the program and see if that helps.

So far, I cannot get UART1 to work even when I change nothing (Default setting), after setting all inputs to digital with ANSEL and trisb to RC7 as input and initialising UART1_Remappable_Init (9600).

I have established that the oscillator is correct and driving 16MHz out on RA6 so hopefully the baudrate is OK.

More work needed!

I really need to know how to re-map UART2 from portB to portD (as in 45K22 device) if I ever get UART1 to work.

This was supposed to be a quick fix to replace a 45K42! Wrong again!

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Settings for remappable uarts

#7 Post by janni » 04 May 2022 14:48

dangerous wrote:
04 May 2022 08:32
Uart1_write_text ("Hello") has always worked with other PICs, but not on this MCU for some reason.
This is certainly a signal that something is wrong as I have no problem with compilation for this processor.
I really need to know how to re-map UART2 from portB to portD (as in 45K22 device)
That's actually simple, just do the same as with Port C

Code: Select all

TRISC.6=0
TRISC.7=1
Unlock_IOLOCK    ' not needed directly after reset
U1RXPPS=_RC7     ' not really necessary as it's the default
RC6PPS=_TX1PPS
U2RXPPS=_RD7    
RD6PPS=_TX2PPS
Lock_IOLOCK
This was supposed to be a quick fix to replace a 45K42! Wrong again!
Yeah, this processor is quite complicated with lots of features and I'm not sure the compiler hasn't got some yet uncovered problems with it. I've verified its work some time ago and I think the bugs found in first implementation were fixed but I have not used mE's libraries.

Try this simple USART setting and transmission

Code: Select all

  U1CON0=0x00   ' RX,TX disabled, normal speed, async 8-bit
  U1CON1=0x00   ' disabled,
  U1CON2=0x00   ' not inverted, 1 stop bit, no flow control, checksum disabled
  U1BRG=416     ' 9600 bod @64MHz, -0.08% error
  U1ERRIE=0x00  ' no interrupts
  U1ON_bit=1    ' enable serial port 1
  
  U1TXEN_bit=1  ' enable transmission
  Uart1_Write_Txt("Hello")
where the write function is

Code: Select all

sub procedure Uart1_Write_Txt(dim byref txt as string)
 dim ii,len as byte
 len=Length(txt)
 ii=0
 while ii<len
  while U1FIFO.U1TXBF wend
  U1TXB=txt[ii]
  inc(ii)
 wend
end sub 'Uart1_Write
You can do the same with second UART just by changing 1 to 2 in registers' names.

dangerous
Posts: 748
Joined: 08 Mar 2005 16:06
Location: Nottinghamshire, UK

Re: Settings for remappable uarts

#8 Post by dangerous » 04 May 2022 15:51

Thanks jani, tried that, and to my suprise it works on both uarts now. The suprise was to find a solution after 3 days of frustration!

The rejection of Uart1_write_text("Hello") was solved by re-installing MB (V7.6.0) No idea why that was getting upset.
Yeah, this processor is quite complicated with lots of features
Not going to argue with that at all!

I had sort of used the same code as you to set Uart2 up, but then found writing to either UART always wrote to uart2. Very strange.

The other thing that is a bit annoying is that the WDT is enabled by default. Just like the Inputs being analogue by default for some years, here's another quirk to remember. (Good old Microchip)

At least I can get to work making the other parts of it work as a 45K22 now.

Thanks again. Very much appreciated.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Settings for remappable uarts

#9 Post by janni » 04 May 2022 16:26

dangerous wrote:
04 May 2022 15:51
The other thing that is a bit annoying is that the WDT is enabled by default.
That's only true if processor configuration is so set. And, indeed, it is set by default in mE's processor definition file.
Just like the Inputs being analogue by default for some years, here's another quirk to remember. (Good old Microchip)
This is actually sensible as unconnected digital inputs may cause drastic increase in processor current consumption. Note that pins intended as outputs are in practice also 'unconnected inputs' during reset and later, until redefined in code.

Post Reply

Return to “mikroBasic PRO for PIC General”