View previous topic :: View next topic |
Author |
Message |
mayur.k.vadukul
Joined: 07 Jul 2022 Posts: 40
|
Baud rate Issues for UART2 on PIC18F65J11 |
Posted: Thu Apr 27, 2023 7:25 am |
|
|
I have been using UART1 & UART2 with PIC18F66K22 device and I have been doing the following to get the right baud rates for our systems:-
#use rs232(UART2, baud=9600, ERRORS, STREAM = FM_UART) //Set 2nd UART to 9600 Baudrate
#use rs232(UART1, baud=4322, ERRORS, ENABLE=DATA_DIR, DISABLE_INTS, STREAM = BUS_DATA) //For Data Bus
The code was running as expected and there were no issues.
I then changed the microprocessor from PIC18F66K22 to PIC18F65J11 (Pin Compatible) , and I have changed the device on my board keeping the code exactly the same as above. The code compiles too, but for some reason the baud rate for UART2 is unexpected. I don’t understand what could be the difference. _________________ MVadukul |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Thu Apr 27, 2023 7:34 am |
|
|
I don't use those PICs but....
It could be a compiler 'bug'
Post your version and someone with it can confirm/deny the 'bug' .
You should dump the listing to see what bits are being configured in the 'baud rate' register. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Thu Apr 27, 2023 7:40 am |
|
|
What do you mean 'unexpected'?. Slow, fast, etc. etc..
What is the master oscillator frequency in both cases?.
Post the actual #PIN_SELECT lines for the UART.
What compiler version?. |
|
|
mayur.k.vadukul
Joined: 07 Jul 2022 Posts: 40
|
baud |
Posted: Thu Apr 27, 2023 7:44 am |
|
|
@ temtronic, see the listing below:-
.................... #use rs232(UART2,baud=9600,ERRORS, STREAM = FM_UART) //Set 2nd UART to 9600 Baudrate
*
0B6E: BTFSS FA4.4
0B70: BRA 0B6E
0B72: MOVWF F62
0B74: RETURN 0
@temtronic,
It is faster than 9600. the bit rate for working 9600 is 104uSec and non working code is 32uSec.
I don't understand what do you mean by #PIN_SELECT lines?
The compiler version is 5.109. _________________ MVadukul |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Thu Apr 27, 2023 7:49 am |
|
|
Both of your chips are not PPS chips. Very surprising for a J chip.
However you haven't answered the oscillator question?. |
|
|
mayur.k.vadukul
Joined: 07 Jul 2022 Posts: 40
|
Ttelmah |
Posted: Thu Apr 27, 2023 7:52 am |
|
|
On both occasion the crystal is 16MHz and it is run at 64MHz using PLL.
#use delay(clock=64MHz,crystal=16MHz,restart_wdt) _________________ MVadukul |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Thu Apr 27, 2023 8:02 am |
|
|
Also the baud rate setting code is not at the #USE line, but at the start
of main. Look there.
The J11, is not rated to run at 64MHz. 40MHz max. Probably why it is
not working right.... |
|
|
mayur.k.vadukul
Joined: 07 Jul 2022 Posts: 40
|
Baud rate Issues for UART2 on PIC18F65J11 |
Posted: Thu Apr 27, 2023 9:39 am |
|
|
I am sorry but I do not understand your comments. _________________ MVadukul |
|
|
mayur.k.vadukul
Joined: 07 Jul 2022 Posts: 40
|
Baud rate Issues for UART2 on PIC18F65J11 |
Posted: Thu Apr 27, 2023 9:42 am |
|
|
I have also tried disabling the PLL and ran the system at 16MHz crystal and it still did not work.
On scope, i tried delay function, checked timer interrupt and UART1. It all worked fine but the UART2 doesn't want to know. I happened to have the code in XC8 compiler as well and it is working fine. So there got to be the bug in the compiler. Do you know any other method to set the baud rate? _________________ MVadukul |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri Apr 28, 2023 1:43 am |
|
|
OK.
The point I was making, was you posted the stuff generated by the
#use as if this could affect your UART. However this is just the transmit
and receive code (just the start of it posted). The UART _setup_ does
not appear here, but at the start of the main in the listing. So:
Code: |
.................... void main()
0004: CLRF TBLPTRU
0006: BCF RCON.IPEN
0008: MOVLW 40
000A: MOVWF OSCTUNE
000C: CLRF OSCCON
000E: MOVF OSCCON,W
0010: CLRF rs232_errors
0012: BSF F7C.3
0014: MOVLW 40
0016: MOVWF SPBRG2
0018: MOVLW 03
001A: MOVWF F7D
001C: MOVLW A6
001E: MOVWF TXSTA2
0020: MOVLW 90
0022: MOVWF RCSTA2
|
The code starting at 0014, is where it sends the values to the BRG
registers.
This shows what the setting is doing.
Now, on your chip, UART1 & UART2 have different settings for the
BRG. Uart2 only has an 8bit BRG generator, while Uart1 has a 16bit
generator. On Uart2, there just is a prescaler controlled by the BRGH bit.
The setup code is treating UART2, like UART1, and loading the high
byte into the location F7D (001A). This doesn't exist for this UART.
Now the timings you were getting, were also wrong because of your clock
rate, which the chip didn't support. It looks as if the chip was actually
running at 32MHz, since it is loading a divider of 65, and a prescaler
of 16. 32uSec for the bit time gives a baud rate of 31250. and this times
65*16 -> 32000000.
So:
Code: |
#define CLOCK 16000000 //set this to suit what your clock really is
#define BAUD 9600 //select the baud rate required
#define BRG16 (CLOCK/(16*BAUD))-1
#define BRG64 (CLOCK/(64*BAUD))-1
#BYTE SPBRG2=getenv("SFR:SPBRG2")
#BYTE TXSTA2=getenv("SFR:TXSTA2")
#bit BRGH=TXSTA2.2
//Then at the start of your main
void main(void)
{
#IF BRG16>255 //here we are going to have to select /64
SPBRG2=BRG64; //set UART2 for 9600bps
BRGH=0;
#ELSE //here /16
SPBRG2=BRG16; //set UART2 for 9600bps
BRGH=1;
#ENDIF
|
If you run at a legitimate clock rate this will correct the BRG settings. |
|
|
mayur.k.vadukul
Joined: 07 Jul 2022 Posts: 40
|
Baud rate Issues for UART2 on PIC18F65J11 |
Posted: Fri Apr 28, 2023 1:58 am |
|
|
Thanks for the in depth insight into the issue.
I have tried running the device with 16MHz as well. However, that did not work too. More so, I have use delay_ms (), function and check on the scope whether it produces right results to ensure that the clock is how i have set up. My UART1 works well as I have checked on the scope and so does my timers routine. (checked on scope too). All of these have been tried using both 16MHz & 64MHz. I understand the chip defines it to run with 40MHz max but not sure whether that is the highest frequency of the physical crystal or the actual clock after PLL. Anyway, for the purposes of the testing I have disabled PLL and ran purely as 16MHz as stated above and it still did not work.
I will try adding assembly code to get around and see what happens.
However, when I contacted CCS support, they sent me new pch.dll file as they admitted there is bug in the compiler for the microprocessor I am using. Now, when I pasted, in hurry i forgot to back up the original file and as a result now my CCS compiler program does not run due to CRG mismatch. :(
As soon as I get any response from the CCS, I will keep you posted of the findings and will be interesting to implement assembly lines too.
Many thanks and fingers crossed. _________________ MVadukul |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Apr 29, 2023 1:34 am |
|
|
From CCS to me:
Quote: |
Yes, this was reported to us yesterday and we sent the user a fix.
If anyone else has trouble they can e-mail us for a fixed dll until
we make the next release.
I think only this part (maybe family) is affected.
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Apr 29, 2023 9:44 am |
|
|
On overclocking, there is a lot to this.
First individual instructions and peripherals may simply not work.
Then the chip will run hot.
Then it may well stop operating at temperatures well below it's rated limits.
Long term this can destroy the chip.
As I said, in fact what your chip was doing was the PLL was failing to work
at this rate. It was not able to clock at the 64MHz rate, and was locking to
32MHz.
Small overclock levels (perhaps 10%), will almost certainly work fine.
Levels over perhaps 20% are likely to have/cause problems. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sun Apr 30, 2023 5:17 am |
|
|
OK, I'm real curious...
What device has a baudrate of 4432 ?? |
|
|
|