I'm trying to migrate a code i have for PIC 18F47Q10 to PIC 18F47Q43. I manage to make all needed updates (mostly declare different names for PPS and CLC's) and code is compiled without errors, but for some reason the hw UART port doesn't seem to work (i got no response to either output or input).
This is the code i use, regarding USART setup, for PIC 18F47Q10 which works ok:
#chip 18F47q10, 64
#option explicit
#startup InitPPS, 85
#define PPSToolPart 18f47q10
function
#define USART2_BAUD_RATE 115200
#define BUFFER_SIZE 2900
#define USART2_DELAY 0 ms
#define USART2_BLOCKING
Sub InitPPS
UNLOCKPPS
RX2PPS = 0x0009 'RB1 > RX2
RB2PPS = 0x000B 'TX2 > RB2
LOCKPPS
End Sub
On Interrupt UsartRX2Ready Call readUSART
And this is the modified code for PIC 18F47Q43:
#chip 18F47q43, 64
#option explicit
#startup InitPPS, 85
#define PPSToolPart 18f47q43
function
#define USART2_BAUD_RATE 115200
#define BUFFER_SIZE 2900
#define USART2_DELAY 0 ms
#define USART2_BLOCKING
Sub InitPPS
UNLOCKPPS
RB2PPS = 0x0023 // TX2 > RB2
U2RXPPS = 0x0009 // RB1 > RX2
LOCKPPS
End Sub
On Interrupt UART2ReceiveInterrupt Call readUSART
For the conversion i used PPS and CLC Designer tools, and for the name of the Interrupt routine (to place received byte into a ring buffer) i consulted the 18F47Q43.dat file in gcbasic\chipdata folder.
And this is the ASM code i got for INITUSART function, for PIC 18F47Q10:
The PPS and the ASM all looks correct. I was comparing here to MPLAB.
So do the following. Do the basic work ?
Remove the interrupt. Use a simple get data send data. Does that work ?
#chip 18F47q43, 64
#option explicit
#startup InitPPS, 85
#define PPSToolPart 18f47q43
#define USART2_BAUD_RATE 115200
#define USART2_DELAY OFF
#define USART2_BLOCKING
Sub InitPPS
UNLOCKPPS
// PPS is correct.
RB2PPS = 0x0023 // TX2 > RB2
U2RXPPS = 0x0009 // RB1 > RX2
LOCKPPS
End Sub
// Not really needed but for testing
Comport = 2
Do
If USART2HasData Then
// Send the RX to the TX
HserPrint HSerReceiveFrom(2), 2
End If
Loop
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The PPS and the ASM all looks correct. I was comparing here to MPLAB.
So do the following. Do the basic work ?
Remove the interrupt. Use a simple get data send data. Does that work ?
#chip 18F47q43, 64
#option explicit
#startup InitPPS, 85
#define PPSToolPart 18f47q43
#define USART2_BAUD_RATE 115200
#define USART2_DELAY OFF
#define USART2_BLOCKING
Sub InitPPS
UNLOCKPPS
// PPS is correct.
RB2PPS = 0x0023 // TX2 > RB2
U2RXPPS = 0x0009 // RB1 > RX2
LOCKPPS
End Sub
// Not really needed but for testing
Comport = 2
Do
If USART2HasData Then
// Send the RX to the TX
HserPrint HSerReceiveFrom(2), 2
End If
Loop
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I connect the board with PIC to an amstrad cpc. Using a usb2serial cable (which is verified that works ok) i tried this code instead:
RESTART:IfUSART2HasDataThendata_buf=HSerReceive2// Send the RX to the TXhsersenddata_buf,2wait500ms;HserPrintHSerReceiveFrom(2),2EndIfgotorestart
The good news is that whenever i send a character to the Amstrad (e.g. PIC's usart port), i get a respond back, BUT not with the character i sent, instead i always get ÿÿ characters as respond to PC terminal...
Exactly the same behavior i got whe i use HserPrint HSerReceiveFrom(2), 2 instead of hsersend data_buf,2.
port speed is set to 115200bps at both ends, and it's the only speed in terminal that i get a respond, so most probable baud rate is correctly set.
Does this give any clue?
Last edit: ikonsgr74 2025-08-08
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I just try the above code but without the wait. I try to send many characters and USART responds back with the same number of characters, but again are completely irrelevant and not the same every time! For example:
I sent 3 times '12345' and i got in response:
¿ÿ÷¿
o»÷÷
ßû¿
Even when i send i single same char i got a different respond each time, for example i sent 3 times '2' and got:
÷
¿
ÿ
Last edit: ikonsgr74 2025-08-08
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If i remove all the "Set the UART2 module to the options selected in the user interface." section, and left only the 'do loop', i got no response at all.
Last edit: ikonsgr74 2025-08-09
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well, fortunately i have a PICKI4 which reflashes this test program in half a second, so i don't think it would take long :-)
Btw, thanks for the explanation about INTON/INTOFF i didn't thought that since there is no interrupt command in source code it will be disable :-)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
btw, i discover another potential bug in the 18F47Q43 asm code: there is no assembly code produced for INTON/INTOFF gcb commands. For example this is the asm i get for 18F47Q410:
Note: as you can see the setting of CLC registers need more instructions with Q43, as you can't set them directly as with Q10 but instead you have to use the extra CLCSELECT all the time... I really can't understand what were thinking in Microchip.... :-)
Last edit: ikonsgr74 2025-08-09
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Can you write the existing values to eeprom then read the chip to share the results? You will be reading the EEProm with your PK4 to share with me what the USART library is setting. I am reading the important registers - hopefully this will show some differences.
Sorry, for delays. I am travelling today! And, the signal is very poor when in the car!
Ok, i've added the extra code for writing eeprom but it doesn't seem to write anything. The bytes i read from eeprom before pugging PIC to the test board are the same with the bytes after ( first 2 eeprom bytes are set to '0' and all others to 'FF')
Last edit: ikonsgr74 2025-08-09
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The issue is not the configuration but the USART functions. There was a change that set the comport to a Constant. As Constants cannot change the comport being selected was essentially hard coded. I think I know why the happended but this should not have passed testing.
Attached is an update usart.h file. it is an updated version of usart.h with the following change:
Removed dependency on SCRIPT_DEFAULT_COMPORT constant. Subroutines like HSerGetNum, HSerPrint, and others now use comport as the default value for the optional comport parameter, enabling runtime flexibility for selecting USART ports.
This change aims to make the library more flexible for multi-USART setups while maintaining compatibility with existing code.
Purpose: Tests basic USART2 functionality on a PIC 18F27Q43 microcontroller.
Key Functionality:
- Configures USART2 at 115,200 baud with blocking mode and no delay.
- Initializes PPS for USART2 (TX2 on RB2, RX2 on RB1).
- Continuously checks for incoming data on USART2 and echoes it back to the transmitter.
- Sets Comport = 2 explicitly for USART2 operations.
Focus: Verifies basic send/receive functionality of USART2, acting as a simple echo test for serial communication.
ComplexTest.gcb
Purpose: Tests advanced USART2 functionality with a buffered, interrupt-driven data transfer system on a PIC 18F27Q43 microcontroller.
Key Functionality:
- Configures USART2 at 115,200 baud with blocking mode and no delay.
- Initializes Peripheral Pin Select (PPS) for USART2 (TX2 on RB2, RX2 on RB1).
- Implements a ring buffer (2,900 bytes) to store incoming USART2 data.
- Uses an interrupt handler (UART2ReceiveInterrupt) to read incoming data into the buffer.
- Continuously checks the buffer and sends stored data to USART2.
- Prints version information and USART control register values (U1CON0, U1CON1, U1CON2, U2CON0, U2CON1, U2CON2) for both USART1 and USART2 to verify configuration.
- Demonstrates dynamic comport switching (USART1 and USART2) for printing diagnostic data.
Focus: Tests robust serial communication with interrupt-driven buffering, suitable for high-throughput or real-time applications.
Comparison
Complexity: ComplexTest.gcb is more sophisticated, incorporating a ring buffer and interrupt handling for efficient data management, while SimpleTest.gcb focuses on straightforward data echoing.
Testing Scope: ComplexTest.gcb tests both USART1 and USART2 diagnostics and buffering, whereas SimpleTest.gcb is limited to USART2 echo functionality.
Use Case: ComplexTest.gcb is suited for validating complex serial applications; SimpleTest.gcb is a minimal test for basic USART operation.
I'm trying to migrate a code i have for PIC 18F47Q10 to PIC 18F47Q43. I manage to make all needed updates (mostly declare different names for PPS and CLC's) and code is compiled without errors, but for some reason the hw UART port doesn't seem to work (i got no response to either output or input).
This is the code i use, regarding USART setup, for PIC 18F47Q10 which works ok:
And this is the modified code for PIC 18F47Q43:
For the conversion i used PPS and CLC Designer tools, and for the name of the Interrupt routine (to place received byte into a ring buffer) i consulted the 18F47Q43.dat file in gcbasic\chipdata folder.
And this is the ASM code i got for INITUSART function, for PIC 18F47Q10:
And for PIC 18F47Q43:
Last edit: ikonsgr74 2025-08-08
The PPS and the ASM all looks correct. I was comparing here to MPLAB.
So do the following. Do the basic work ?
Remove the interrupt. Use a simple get data send data. Does that work ?
The PPS and the ASM all looks correct. I was comparing here to MPLAB.
So do the following. Do the basic work ?
Remove the interrupt. Use a simple get data send data. Does that work ?
I connect the board with PIC to an amstrad cpc. Using a usb2serial cable (which is verified that works ok) i tried this code instead:
The good news is that whenever i send a character to the Amstrad (e.g. PIC's usart port), i get a respond back, BUT not with the character i sent, instead i always get ÿÿ characters as respond to PC terminal...
Exactly the same behavior i got whe i use HserPrint HSerReceiveFrom(2), 2 instead of hsersend data_buf,2.
port speed is set to 115200bps at both ends, and it's the only speed in terminal that i get a respond, so most probable baud rate is correctly set.
Does this give any clue?
Last edit: ikonsgr74 2025-08-08
I just try the above code but without the wait. I try to send many characters and USART responds back with the same number of characters, but again are completely irrelevant and not the same every time! For example:
I sent 3 times '12345' and i got in response:
¿ÿ÷¿
o»÷÷
ßû¿
Even when i send i single same char i got a different respond each time, for example i sent 3 times '2' and got:
÷
¿
ÿ
Last edit: ikonsgr74 2025-08-08
Baud Rate calcs.
MPLAB 64Mhz at 115k
~~~
// BRGL 138;
U2BRGL = 0x8A;
;U2BRGH=SPBRGH_TEMP2
banksel U2BRGH
clrf U2BRGH,BANKED = 0
;U2BRGL=SPBRGL_TEMP2
movlw 137
movwf U2BRGL,BANKED = 137
chip 18F47q43, 64
option explicit
~~~
YES! That worked straight!
Ok.
Isolate the issue by removing the special init. Leave the do-loop. Does the standard init work?
If i remove all the "Set the UART2 module to the options selected in the user interface." section, and left only the 'do loop', i got no response at all.
Last edit: ikonsgr74 2025-08-09
To understand the root cause. I need you to put back each line, one at a time, one by one. Which line makes this work?
Once I know what line is required then I can investigate in the serial initialization code.
This is exactly what I would be doing to understand the root cause. A bit of a pain, and a slow process, but it will determine what is wrong.
Well, fortunately i have a PICKI4 which reflashes this test program in half a second, so i don't think it would take long :-)
Btw, thanks for the explanation about INTON/INTOFF i didn't thought that since there is no interrupt command in source code it will be disable :-)
btw, i discover another potential bug in the 18F47Q43 asm code: there is no assembly code produced for INTON/INTOFF gcb commands. For example this is the asm i get for 18F47Q410:
And this is the asm for 18F47Q43 for exactly the same code:
Last edit: ikonsgr74 2025-08-09
This is because the 'On Interrupt....' is not present.
Ok,i finish tests. The minimum group of registers needed for proper function of UART port are:
Any of the above removed, UART stops functioning. All others don't seem to affect functioning of the UART.
Thanks.
Can you write the existing values to eeprom then read the chip to share the results? You will be reading the EEProm with your PK4 to share with me what the USART library is setting. I am reading the important registers - hopefully this will show some differences.
Sorry, for delays. I am travelling today! And, the signal is very poor when in the car!
Evan
This shows the method to write the EEProm.
Ok, i've added the extra code for writing eeprom but it doesn't seem to write anything. The bytes i read from eeprom before pugging PIC to the test board are the same with the bytes after ( first 2 eeprom bytes are set to '0' and all others to 'FF')
Last edit: ikonsgr74 2025-08-09
They may be zero. Let us check the EE write operations.
EPWRITE 0, 0x55
EPWRITE 1, 0x44
EPWRITE 3, 0x33
Comment out the following.
// EPWRITE 0, U2CON0
// EPWRITE 1, U2CON1
// EPWRITE 3, U2CON2
Do you see 0x55, 0x44, 0x33 ?
eprom byte 0 and 1 have 0x55, 0x44 but all others are 0xff
So most probable U2CON0 and U2CON1 are set to '0' by init uart routine, but for some reason the 3rd byte is not written to eeprom...
I tried to write eprom byte 0 with the value of U2CON2 and it also gave me '0', so it seems that all 3 values are set to '0' from the UART init
Ok. let me look at the library again. Please look for an update post from me when i have completed my review.
ok, thanks.
Can I check ? You are on the latest build?
Inspect USART.H. Last change 24/12/2024 ? Yes?
The issue is not the configuration but the USART functions. There was a change that set the comport to a Constant. As Constants cannot change the comport being selected was essentially hard coded. I think I know why the happended but this should not have passed testing.
Attached is an update usart.h file. it is an updated version of usart.h with the following change:
Updated Default Comport Handling (August 10, 2024):
This change aims to make the library more flexible for multi-USART setups while maintaining compatibility with existing code.
Please replace your existing USART.H, and test
Here are my test programs.
Summary of ComplexTest.gcb and SimpleTest.gcb
SimpleTest.gcb
Purpose: Tests basic USART2 functionality on a PIC 18F27Q43 microcontroller.
Key Functionality:
- Configures USART2 at 115,200 baud with blocking mode and no delay.
- Initializes PPS for USART2 (TX2 on RB2, RX2 on RB1).
- Continuously checks for incoming data on USART2 and echoes it back to the transmitter.
- Sets
Comport = 2
explicitly for USART2 operations.Focus: Verifies basic send/receive functionality of USART2, acting as a simple echo test for serial communication.
ComplexTest.gcb
Purpose: Tests advanced USART2 functionality with a buffered, interrupt-driven data transfer system on a PIC 18F27Q43 microcontroller.
Key Functionality:
- Configures USART2 at 115,200 baud with blocking mode and no delay.
- Initializes Peripheral Pin Select (PPS) for USART2 (TX2 on RB2, RX2 on RB1).
- Implements a ring buffer (2,900 bytes) to store incoming USART2 data.
- Uses an interrupt handler (
UART2ReceiveInterrupt
) to read incoming data into the buffer.- Continuously checks the buffer and sends stored data to USART2.
- Prints version information and USART control register values (
U1CON0
,U1CON1
,U1CON2
,U2CON0
,U2CON1
,U2CON2
) for both USART1 and USART2 to verify configuration.- Demonstrates dynamic comport switching (USART1 and USART2) for printing diagnostic data.
Focus: Tests robust serial communication with interrupt-driven buffering, suitable for high-throughput or real-time applications.
Comparison
ComplexTest.gcb
is more sophisticated, incorporating a ring buffer and interrupt handling for efficient data management, whileSimpleTest.gcb
focuses on straightforward data echoing.ComplexTest.gcb
tests both USART1 and USART2 diagnostics and buffering, whereasSimpleTest.gcb
is limited to USART2 echo functionality.ComplexTest.gcb
is suited for validating complex serial applications;SimpleTest.gcb
is a minimal test for basic USART operation.