News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Need some help for 18f25Q10

Started by Maxi, Jul 26, 2023, 01:24 PM

Previous topic - Next topic

Maxi

hi, this serial settings I use for 18F25K22
but not work on 18F25Q10
serial characters come wrong out.

what is my mistake? some one help me?
I read out portb.0 1000ms, so osc setting correct I think.

Device 18F25Q10
Declare Xtal=8

OSCCON1 = $60               ' NOSC HFINTOSC, NDIV 1
OSCCON3 = $00               ' CSWHOLD may proceed, SOSCPWR Low power
OSCEN   = $00               ' MFOEN disabled, LFOEN disabled, ADOEN disabled, SOSCEN disabled, EXTOEN disabled, HFOEN disabled
OSCFRQ  = $03               ' HFFRQ 8_MHz
OSCTUNE = $00

    '-------------------------------------------------------------------------------
    '**** Added by Fuse Configurator ****
    ' Use the Fuses Tab to change these settings

    Config_Start
    FEXTOSC = OFF              'Oscillator not enable
    RSTOSC = HFINTOSC_64MHZ    'HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:
    CLKOUTEN = OFF             'CLKOUT function is disable
    CSWEN = On                 'Writing to NOSC and NDIV is allowed
    FCMEN = On                 'Fail-Safe Clock Monitor enable
    MCLRE = INTMCLR         ; If LVP = 0, MCLR pin (RE3) is an input; If LVP =1, MCLR pin (RE3) is MCLR
    PWRTE = On                 'Power up timer is on
    LPBOREN = OFF              'Low power BOR is DISABLE
    BOREN = SBORDIS            'Brown-out Reset enabled , SBOREN bit is ignore
    BORV = VBOR_190            'Brown-out Reset Voltage (VBOR) set to 1.90
    ZCD = OFF                  'ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCO
    PPS1WAY = OFF               
    STVREN = OFF            ; Stack full/underflow will not cause Reset
    XINST = OFF                'Extended Instruction Set and Indexed Addressing Mode disable
    WDTCPS = WDTCPS_31         'Divider ratio 1:65536' software control of WDTP
    WDTE = OFF                 'WDT Disable
    WDTCWS = WDTCWS_7          'window always open (100%)' software control' keyed access not require
    WDTCCS = SC                'Software Contro
    WRT0 = OFF                 'Block 0 (000800-003FFFh) not write-protecte
    WRT1 = OFF                 'Block 1 (004000-007FFFh) not write-protecte
    WRT2 = OFF                 'Block 2 (008000-00BFFFh) not write-protecte
    WRT3 = OFF                 'Block 3 (00C000-00FFFFh) not write-protecte
    WRTC = OFF                 'Configuration registers (300000-30000Bh) not write-protecte
    WRTB = OFF                 'Boot Block (000000-0007FFh) not write-protecte
    WRTD = OFF                 'Data EEPROM not write-protecte
    SCANE = OFF             ; Scanner module is NOT available for use, SCANMD bit is ignored
    LVP = OFF               ; HV on MCLR/VPP must be used for programming
    Cp = On                   'UserNVM code protection enable
    CPD = On                  'DataNVM code protection enable
    EBTR0 = OFF                'Block 0 (000800-003FFFh) not protected from table reads executed in other block
    EBTR1 = OFF                'Block 1 (004000-007FFFh) not protected from table reads executed in other block
    EBTR2 = OFF                'Block 2 (008000-00BFFFh) not protected from table reads executed in other block
    EBTR3 = OFF                'Block 3 (00C000-00FFFFh) not protected from table reads executed in other block
    EBTRB = OFF                'Boot Block (000000-0007FFh) not protected from table reads executed in other block
    Config_End

    '**** End of Fuse Configurator Settings ****
    '-------------------------------------------------------------------------------
   
    TRISA=%00010111:TRISB=%10000100:TRISC=%00000110
    PORTA=%00000000:PORTB=%00000000:PORTC=%00000000
     
    RC1STA.7=1
     
    Declare Create_Coff=On
   
    CM1CON0.7=0
    CM2CON0.7=0
    WPUB=%00000000
   
    ADCON0=%00000011
    ADCON2=%10101010

    ANSELA=%00000101
    ANSELB=%00000100
    ANSELC=%01000000

RC1STA.7=1   
'---------------------------------------------------
'8mhz, 57600
Declare Hserial_Baud=57600
Declare Hserial_RCSTA=$90
Declare Hserial_TXSTA=$24
Declare Hserial_SPBRG=34
Declare Hserial_Clear=On
SP1BRGH = 0
BAUD1CON.3 = 1
'---------------------------------------------------
HRSOut "test1234"

L1:
Toggle PORTB.0
DelayMS 1000
GoTo L1


tumbleweed

Remove this and see if it works:
OSCEN   = $00

That's telling it to disable all the internal osc unless requested by a peripheral.

TimB

#2
Try setting up the PPS pins

eg

on my device its

    Proc InitPPS()
        PPS_UnLock()
        RC6PPS = 0x09  'TX1(RC6) Output from EUSART1
        RXPPS = 0x17  'RX1(RC7) Input to EUSART1
        RB6PPS = 0xB  'TX2(RB6) Output from EUSART2
        RB7PPS = 0xF  'RX2(RB7) Input to EUSART2
        PPS_Lock()
    EndProc

Also this is my set up for 64mhz and 38400

        PIE3bits_TX1IE = 0
        PIR3bits_TX1IF = 0
        Wreg = RC1Reg
        Tx1Reg = Wreg

        PIE3bits_TX2IE = 0
        PIR3bits_TX2IF = 0
        Wreg = RC2Reg
        Tx2Reg = Wreg

        ' TX/RX 1
        SPBRGH1 = 0                                                             'EUSARTx Baud Rate Generator Register High Byte
        SPBRG1 = $67                                                            'EUSARTx Baud Rate Generator Register Low Byte

         ' TX/RX 2
        SPBRGH2 = 0                                                             'EUSARTx Baud Rate Generator Register High Byte
        SPBRG2 = $67                                                            'EUSARTx Baud Rate Generator Register Low Byte



Maxi

guys, thank you but problem continue

tim, I try pps but this controller have 1 serial out already
18F26Q10 have double

the last code here, I measure serial out and baudrate show 6820 !
picture and code here


Device 18F25Q10
Declare Xtal=8

OSCCON1 = $60               ' NOSC HFINTOSC, NDIV 1
OSCCON3 = $00               ' CSWHOLD may proceed, SOSCPWR Low power
'OSCEN   = $00               ' MFOEN disabled, LFOEN disabled, ADOEN disabled, SOSCEN disabled, EXTOEN disabled, HFOEN disabled
OSCFRQ  = $03               ' HFFRQ 8_MHz
OSCTUNE = $00

    '-------------------------------------------------------------------------------
    '**** Added by Fuse Configurator ****
    ' Use the Fuses Tab to change these settings

    Config_Start
    FEXTOSC = OFF              'Oscillator not enable
    RSTOSC = HFINTOSC_64MHZ    'HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:
    CLKOUTEN = OFF             'CLKOUT function is disable
    CSWEN = On                 'Writing to NOSC and NDIV is allowed
    FCMEN = On                 'Fail-Safe Clock Monitor enable
    MCLRE = INTMCLR         ; If LVP = 0, MCLR pin (RE3) is an input; If LVP =1, MCLR pin (RE3) is MCLR
    PWRTE = On                 'Power up timer is on
    LPBOREN = OFF              'Low power BOR is DISABLE
    BOREN = SBORDIS            'Brown-out Reset enabled , SBOREN bit is ignore
    BORV = VBOR_190            'Brown-out Reset Voltage (VBOR) set to 1.90
    ZCD = OFF                  'ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCO
    PPS1WAY = OFF               
    STVREN = OFF            ; Stack full/underflow will not cause Reset
    XINST = OFF                'Extended Instruction Set and Indexed Addressing Mode disable
    WDTCPS = WDTCPS_31         'Divider ratio 1:65536' software control of WDTP
    WDTE = OFF                 'WDT Disable
    WDTCWS = WDTCWS_7          'window always open (100%)' software control' keyed access not require
    WDTCCS = SC                'Software Contro
    WRT0 = OFF                 'Block 0 (000800-003FFFh) not write-protecte
    WRT1 = OFF                 'Block 1 (004000-007FFFh) not write-protecte
    WRT2 = OFF                 'Block 2 (008000-00BFFFh) not write-protecte
    WRT3 = OFF                 'Block 3 (00C000-00FFFFh) not write-protecte
    WRTC = OFF                 'Configuration registers (300000-30000Bh) not write-protecte
    WRTB = OFF                 'Boot Block (000000-0007FFh) not write-protecte
    WRTD = OFF                 'Data EEPROM not write-protecte
    SCANE = OFF             ; Scanner module is NOT available for use, SCANMD bit is ignored
    LVP = OFF               ; HV on MCLR/VPP must be used for programming
    Cp = On                   'UserNVM code protection enable
    CPD = On                  'DataNVM code protection enable
    EBTR0 = OFF                'Block 0 (000800-003FFFh) not protected from table reads executed in other block
    EBTR1 = OFF                'Block 1 (004000-007FFFh) not protected from table reads executed in other block
    EBTR2 = OFF                'Block 2 (008000-00BFFFh) not protected from table reads executed in other block
    EBTR3 = OFF                'Block 3 (00C000-00FFFFh) not protected from table reads executed in other block
    EBTRB = OFF                'Boot Block (000000-0007FFh) not protected from table reads executed in other block
    Config_End

    '**** End of Fuse Configurator Settings ****
    '-------------------------------------------------------------------------------
   
    TRISA=%00010111:TRISB=%10000100:TRISC=%00000110
    PORTA=%00000000:PORTB=%00000000:PORTC=%00000000
     
    RC1STA.7=1
     
    Declare Create_Coff=On
   
    CM1CON0.7=0
    CM2CON0.7=0
    WPUB=%00000000
   
    ADCON0=%00000011
    ADCON2=%10101010

    ANSELA=%00000101
    ANSELB=%00000100
    ANSELC=%01000000
   
    'RB6PPS = 0xB  'TX2(RB6) Output from EUSART2
    'RB7PPS = 0xF  'RX2(RB7) Input to EUSART2

RC1STA.7=1   
'---------------------------------------------------
'8mhz, 57600
Declare Hserial_Baud=57600
Declare Hserial_RCSTA=$90
Declare Hserial_TXSTA=$24
Declare Hserial_SPBRG=34
Declare Hserial_Clear=On
SP1BRGH = 1
BAUD1CON.3 = 1
'---------------------------------------------------
HRSOut "test1234"

L1:
Toggle PORTB.0
DelayMS 1000
GoTo L1



Dompie

Could ANSELC=%01000000 be a problem (RC6 analog)?

Johan

Maxi

Quote from: Dompie on Jul 27, 2023, 10:30 AMCould ANSELC=%01000000 be a problem (RC6 analog)?

Johan
no johan
serial port pins B6-B7

okmn

Quote from: Maxi on Jul 27, 2023, 08:27 AMguys, thank you but problem continue

tim, I try pps but this controller have 1 serial out already
18F26Q10 have double

the last code here, I measure serial out and baudrate show 6820 !
picture and code here


Device 18F25Q10
Declare Xtal=8

OSCCON1 = $60              ' NOSC HFINTOSC, NDIV 1
OSCCON3 = $00              ' CSWHOLD may proceed, SOSCPWR Low power
'OSCEN  = $00              ' MFOEN disabled, LFOEN disabled, ADOEN disabled, SOSCEN disabled, EXTOEN disabled, HFOEN disabled
OSCFRQ  = $03              ' HFFRQ 8_MHz
OSCTUNE = $00

    '-------------------------------------------------------------------------------
    '**** Added by Fuse Configurator ****
    ' Use the Fuses Tab to change these settings

    Config_Start
    FEXTOSC = OFF              'Oscillator not enable
    RSTOSC = HFINTOSC_64MHZ    'HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:
    CLKOUTEN = OFF            'CLKOUT function is disable
    CSWEN = On                'Writing to NOSC and NDIV is allowed
    FCMEN = On                'Fail-Safe Clock Monitor enable
    MCLRE = INTMCLR        ; If LVP = 0, MCLR pin (RE3) is an input; If LVP =1, MCLR pin (RE3) is MCLR
    PWRTE = On                'Power up timer is on
    LPBOREN = OFF              'Low power BOR is DISABLE
    BOREN = SBORDIS            'Brown-out Reset enabled , SBOREN bit is ignore
    BORV = VBOR_190            'Brown-out Reset Voltage (VBOR) set to 1.90
    ZCD = OFF                  'ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCO
    PPS1WAY = OFF             
    STVREN = OFF            ; Stack full/underflow will not cause Reset
    XINST = OFF                'Extended Instruction Set and Indexed Addressing Mode disable
    WDTCPS = WDTCPS_31        'Divider ratio 1:65536' software control of WDTP
    WDTE = OFF                'WDT Disable
    WDTCWS = WDTCWS_7          'window always open (100%)' software control' keyed access not require
    WDTCCS = SC                'Software Contro
    WRT0 = OFF                'Block 0 (000800-003FFFh) not write-protecte
    WRT1 = OFF                'Block 1 (004000-007FFFh) not write-protecte
    WRT2 = OFF                'Block 2 (008000-00BFFFh) not write-protecte
    WRT3 = OFF                'Block 3 (00C000-00FFFFh) not write-protecte
    WRTC = OFF                'Configuration registers (300000-30000Bh) not write-protecte
    WRTB = OFF                'Boot Block (000000-0007FFh) not write-protecte
    WRTD = OFF                'Data EEPROM not write-protecte
    SCANE = OFF            ; Scanner module is NOT available for use, SCANMD bit is ignored
    LVP = OFF              ; HV on MCLR/VPP must be used for programming
    Cp = On                  'UserNVM code protection enable
    CPD = On                  'DataNVM code protection enable
    EBTR0 = OFF                'Block 0 (000800-003FFFh) not protected from table reads executed in other block
    EBTR1 = OFF                'Block 1 (004000-007FFFh) not protected from table reads executed in other block
    EBTR2 = OFF                'Block 2 (008000-00BFFFh) not protected from table reads executed in other block
    EBTR3 = OFF                'Block 3 (00C000-00FFFFh) not protected from table reads executed in other block
    EBTRB = OFF                'Boot Block (000000-0007FFh) not protected from table reads executed in other block
    Config_End

    '**** End of Fuse Configurator Settings ****
    '-------------------------------------------------------------------------------
   
    TRISA=%00010111:TRISB=%10000100:TRISC=%00000110
    PORTA=%00000000:PORTB=%00000000:PORTC=%00000000
   
    RC1STA.7=1
   
    Declare Create_Coff=On
   
    CM1CON0.7=0
    CM2CON0.7=0
    WPUB=%00000000
   
    ADCON0=%00000011
    ADCON2=%10101010

    ANSELA=%00000101
    ANSELB=%00000100
    ANSELC=%01000000
   
    'RB6PPS = 0xB  'TX2(RB6) Output from EUSART2
    'RB7PPS = 0xF  'RX2(RB7) Input to EUSART2

RC1STA.7=1   
'---------------------------------------------------
'8mhz, 57600
Declare Hserial_Baud=57600
Declare Hserial_RCSTA=$90
Declare Hserial_TXSTA=$24
Declare Hserial_SPBRG=34
Declare Hserial_Clear=On
SP1BRGH = 1
BAUD1CON.3 = 1
'---------------------------------------------------
HRSOut "test1234"

L1:
Toggle PORTB.0
DelayMS 1000
GoTo L1




are you meauring as ttl level or rs232?
if rs232  then you should select  "inverted rs232" box
i lived like that a problem in the pass.

Maxi

Quote from: okaman on Jul 27, 2023, 11:04 AM
Quote from: Maxi on Jul 27, 2023, 08:27 AMguys, thank you but problem continue

tim, I try pps but this controller have 1 serial out already
18F26Q10 have double

the last code here, I measure serial out and baudrate show 6820 !
picture and code here


Device 18F25Q10
Declare Xtal=8

OSCCON1 = $60              ' NOSC HFINTOSC, NDIV 1
OSCCON3 = $00              ' CSWHOLD may proceed, SOSCPWR Low power
'OSCEN  = $00              ' MFOEN disabled, LFOEN disabled, ADOEN disabled, SOSCEN disabled, EXTOEN disabled, HFOEN disabled
OSCFRQ  = $03              ' HFFRQ 8_MHz
OSCTUNE = $00

    '-------------------------------------------------------------------------------
    '**** Added by Fuse Configurator ****
    ' Use the Fuses Tab to change these settings

    Config_Start
    FEXTOSC = OFF              'Oscillator not enable
    RSTOSC = HFINTOSC_64MHZ    'HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:
    CLKOUTEN = OFF            'CLKOUT function is disable
    CSWEN = On                'Writing to NOSC and NDIV is allowed
    FCMEN = On                'Fail-Safe Clock Monitor enable
    MCLRE = INTMCLR        ; If LVP = 0, MCLR pin (RE3) is an input; If LVP =1, MCLR pin (RE3) is MCLR
    PWRTE = On                'Power up timer is on
    LPBOREN = OFF              'Low power BOR is DISABLE
    BOREN = SBORDIS            'Brown-out Reset enabled , SBOREN bit is ignore
    BORV = VBOR_190            'Brown-out Reset Voltage (VBOR) set to 1.90
    ZCD = OFF                  'ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCO
    PPS1WAY = OFF             
    STVREN = OFF            ; Stack full/underflow will not cause Reset
    XINST = OFF                'Extended Instruction Set and Indexed Addressing Mode disable
    WDTCPS = WDTCPS_31        'Divider ratio 1:65536' software control of WDTP
    WDTE = OFF                'WDT Disable
    WDTCWS = WDTCWS_7          'window always open (100%)' software control' keyed access not require
    WDTCCS = SC                'Software Contro
    WRT0 = OFF                'Block 0 (000800-003FFFh) not write-protecte
    WRT1 = OFF                'Block 1 (004000-007FFFh) not write-protecte
    WRT2 = OFF                'Block 2 (008000-00BFFFh) not write-protecte
    WRT3 = OFF                'Block 3 (00C000-00FFFFh) not write-protecte
    WRTC = OFF                'Configuration registers (300000-30000Bh) not write-protecte
    WRTB = OFF                'Boot Block (000000-0007FFh) not write-protecte
    WRTD = OFF                'Data EEPROM not write-protecte
    SCANE = OFF            ; Scanner module is NOT available for use, SCANMD bit is ignored
    LVP = OFF              ; HV on MCLR/VPP must be used for programming
    Cp = On                  'UserNVM code protection enable
    CPD = On                  'DataNVM code protection enable
    EBTR0 = OFF                'Block 0 (000800-003FFFh) not protected from table reads executed in other block
    EBTR1 = OFF                'Block 1 (004000-007FFFh) not protected from table reads executed in other block
    EBTR2 = OFF                'Block 2 (008000-00BFFFh) not protected from table reads executed in other block
    EBTR3 = OFF                'Block 3 (00C000-00FFFFh) not protected from table reads executed in other block
    EBTRB = OFF                'Boot Block (000000-0007FFh) not protected from table reads executed in other block
    Config_End

    '**** End of Fuse Configurator Settings ****
    '-------------------------------------------------------------------------------
   
    TRISA=%00010111:TRISB=%10000100:TRISC=%00000110
    PORTA=%00000000:PORTB=%00000000:PORTC=%00000000
   
    RC1STA.7=1
   
    Declare Create_Coff=On
   
    CM1CON0.7=0
    CM2CON0.7=0
    WPUB=%00000000
   
    ADCON0=%00000011
    ADCON2=%10101010

    ANSELA=%00000101
    ANSELB=%00000100
    ANSELC=%01000000
   
    'RB6PPS = 0xB  'TX2(RB6) Output from EUSART2
    'RB7PPS = 0xF  'RX2(RB7) Input to EUSART2

RC1STA.7=1   
'---------------------------------------------------
'8mhz, 57600
Declare Hserial_Baud=57600
Declare Hserial_RCSTA=$90
Declare Hserial_TXSTA=$24
Declare Hserial_SPBRG=34
Declare Hserial_Clear=On
SP1BRGH = 1
BAUD1CON.3 = 1
'---------------------------------------------------
HRSOut "test1234"

L1:
Toggle PORTB.0
DelayMS 1000
GoTo L1




are you meauring as ttl level or rs232?
if rs232  then you should select  "inverted rs232" box
i lived like that a problem in the pass.

hello okaman
the main problem is baudrate
18F25Q10 set internal 8mhz osc, serial settings 57600 baud
but serial out 6820 baud now
(serial port work but not 57600)

tumbleweed

If you want to use UART2 then you need to use the declares and registers for that UART, and not serial1

Maxi

Quote from: tumbleweed on Jul 27, 2023, 03:29 PMIf you want to use UART2 then you need to use the declares and registers for that UART, and not serial1

no, 18F25Q10 have 1 serial out

trastikata

Config_Start
....
    FEXTOSC = OFF
    RSTOSC = HFINTOSC_64MHZ   
....
Config_End
+
Declare Xtal = 8

Maxi

Quote from: trastikata on Jul 27, 2023, 04:21 PMConfig_Start
....
    FEXTOSC = OFF
    RSTOSC = HFINTOSC_64MHZ   
....
Config_End
+
Declare Xtal = 8

its correct I think
you choose 64mhz int osc then divide it 8 and you have 8mhz int osc. (OSCFRQ  = $03)
also, my 1000ms loop toggled 1 seceond correct out

trastikata

Quote from: Maxi on Jul 27, 2023, 04:42 PMits correct I think
you choose 64mhz int osc then divide it 8 and you have 8mhz int osc. (OSCFRQ  = $03)
also, my 1000ms loop toggled 1 seceond correct out

I just saw that I missed the OSCCONx code before the fuse configuration. You are right, it should be 8MHz - just one clarification, you are not using the clock postscaller but setting HFINTOSC to 8Hz.

Anyway I saw you set BRGH = 1 with SPBRG = 34, this gives a baud of 14286, not 57.6k

Maxi

Quote from: trastikata on Jul 27, 2023, 05:32 PM
Quote from: Maxi on Jul 27, 2023, 04:42 PMits correct I think
you choose 64mhz int osc then divide it 8 and you have 8mhz int osc. (OSCFRQ  = $03)
also, my 1000ms loop toggled 1 seceond correct out

I just saw that I missed the OSCCONx code before the fuse configuration. You are right, it should be 8MHz - just one clarification, you are not using the clock postscaller but setting HFINTOSC to 8Hz.

Anyway I saw you set BRGH = 1 with SPBRG = 34, this gives a baud of 14286, not 57.6k

yes you are right but I trying "BRGH = 0" before (check my first msg)
I dont know how resolve this problem :(

top204

#14
You are over-confusing the problem you are having.

Looking at your original code snippet above, it seems you are telling the compiler that it is using 8MHz, but setting the device for 64MHz. You are also using the declares that are no longer required, and the settings in them are loaded in to the SFRs, so if you have the Baud generator set to a specific resolution, but have not set the other SFRs to state what Baud generator resolution is being used, and expecting the compiler to calculate the Baud rate with an unknown Baud generator resolution, problems will happen. However, the code snippet's listing is not in any logical sequence, so it is difficult to scan and see a pattern.

The compiler sorts out all the SFR (Special Function Register) manipulations for a Baud rate required with a specific device frequency, and on devices that have PPS (Peripheral Pin Select), it manipulates them based upon what pins are stated in the declares. So the older declares are no longer required, but can be used if the user knows what values to place into a USART's SFRs for a particular Baud rate with a particular Baud generator resolution, and a particular device frequency. But it is better to manipulate the SFRs within a procedure to setup a peripheral for a particular specification.

Below is a demonstration showing how straightforward it is to setup USART1 for the commands that use it:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' A simple demonstration of setting up the USART1 peripheral
'
    Device = 18F25Q10                   ' Tell the compiler what device to compile for
    Declare Xtal = 64                   ' Tell the compiler what frequency the device will be operating at (in MHz)
'
' Setup USART1
'
    Declare HSerial1_Baud = 57600
    Declare HSerout1_Pin  = PORTC.6
    Declare HSerin1_Pin   = PORTC.7

'-------------------------------------------------------------------------------------
' The main program starts here
'
Main:
    Do                                  ' Create a loop
        HRsoutLn "Hello World"
        DelayMs 255
    Loop                                ' Do it forever

'-------------------------------------------------------------------------------------
' Setup the config fuses to use the internal oscillator at 64MHz on a PIC18FXXQ10 device
'
Config_Start
    FEXTOSC = Off                       ' Oscillator not enabled
    RSTOSC = HFINTOSC_64MHZ             ' HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1
    MCLRE = INTMCLR                     ' If LVP = 0, MCLR pin (RE3) is an input. If LVP =1, MCLR pin (RE3) is MCLR
    WDTE = Off                          ' WDT Disabled
    CLKOUTEN = Off                      ' CLKOUT function is disabled
    CSWEN = Off                         ' The NOSC and NDIV Bits cannot be changed by user software
    FCMEN = Off                         ' Fail-Safe Clock Monitor disabled
    PWRTE = Off                         ' Power up timer disabled
    LPBOREN = Off                       ' Low power BOR is disabled
    BOREN = Off                         ' Brown-out Reset disabled
    ZCD = Off                           ' ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
    PPS1WAY = Off                       ' PPSLOCK bit can be set and cleared repeatedly (subject to the unlock sequence)
    STVREN = Off                        ' Stack full/underflow will not cause Reset
    XINST = Off                         ' Extended Instruction Set and Indexed Addressing Mode disabled
    WDTCCS = SC                         ' Software Control
    WRT0 = Off                          ' Block 0 (000800-001FFF) not write-protected
    WRT1 = Off                          ' Block 1 (002000-003FFF) not write-protected
    WRT2 = Off                          ' Block 2 (004000-005FFF) not write-protected
    WRT3 = Off                          ' Block 3 (006000-007FFF) not write-protected
    WRTC = Off                          ' Configuration registers (300000-30000B) not write-protected
    WRTB = Off                          ' Boot Block (000000-0007FF) not write-protected
    WRTD = Off                          ' Data EEPROM not write-protected
    SCANE = Off                         ' Scanner module is NOT available for use, SCANMD bit is ignored
    LVP = Off                           ' HV on MCLR/VPP must be used for programming
    Cp = Off                            ' User NVM code protection disabled
    CPD = Off                           ' Data NVM code protection disabled
    EBTR0 = Off                         ' Block 0 (000800-001FFF) not protected from table reads executed in other blocks
    EBTR1 = Off                         ' Block 1 (002000-003FFF) not protected from table reads executed in other blocks
    EBTR2 = Off                         ' Block 2 (004000-005FFF) not protected from table reads executed in other blocks
    EBTR3 = Off                         ' Block 3 (006000-007FFF) not protected from table reads executed in other blocks
    EBTRB = Off                         ' Boot Block (000000-0007FF) not protected from table reads executed in other blocks
Config_End

In the assembler code produced by the compiler, you can see the setups performed for the USART1 peripheral that are run before a user's program starts:

; UART1_ACTUALBAUD = 57971.02
; UART1_BAUDERROR = 0.64
    bsf BAUD1CON,PP_BRG16
    movlw 0x44
    movwf SP1BRG
    clrf SP1BRGH
    movlw 0x20
    movwf TX1STA
    movlw 0x90
    movwf RC1STA
; CONFIGURE HRSOUT1 PPS
    movlw _PPS_FN_TX1
    movff WREG,RC6PPS
; CONFIGURE HRSIN1 PPS
    movlw 0x17
    movff WREG,RX1PPS

Below is the above program running in the Proteus simulator with the correct Baud rate:
18F25Q10_USART1_Demo.jpg

Maxi

#15
Quote from: top204 on Jul 28, 2023, 09:59 AMYou are over-confusing the problem you are having.

Looking at your original code snippet above, it seems you are telling the compiler that it is using 8MHz, but setting the device for 64MHz. You are also using the declares that are no longer required, and the settings in them are loaded in to the SFRs, so if you have the Baud generator set to a specific resolution, but have not set the other SFRs to state what Baud generator resolution is being used, and expecting the compiler to calculate the Baud rate with an unknown Baud generator resolution, problems will happen. However, the code snippet's listing is not in any logical sequence, so it is difficult to scan and see a pattern.

The compiler sorts out all the SFR (Special Function Register) manipulations for a Baud rate required with a specific device frequency, and on devices that have PPS (Peripheral Pin Select), it manipulates them based upon what pins are stated in the declares. So the older declares are no longer required, but can be used if the user knows what values to place into a USART's SFRs for a particular Baud rate with a particular Baud generator resolution, and a particular device frequency. But it is better to manipulate the SFRs within a procedure to setup a peripheral for a particular specification.

Below is a demonstration showing how straightforward it is to setup USART1 for the commands that use it:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' A simple demonstration of setting up the USART1 peripheral
'
    Device = 18F25Q10                   ' Tell the compiler what device to compile for
    Declare Xtal = 64                   ' Tell the compiler what frequency the device will be operating at (in MHz)
'
' Setup USART1
'
    Declare HSerial1_Baud = 57600
    Declare HSerout1_Pin  = PORTC.6
    Declare HSerin1_Pin   = PORTC.7

'-------------------------------------------------------------------------------------
' The main program starts here
'
Main:
    Do                                  ' Create a loop
        HRsoutLn "Hello World"
        DelayMs 255
    Loop                                ' Do it forever

'-------------------------------------------------------------------------------------
' Setup the config fuses to use the internal oscillator at 64MHz on a PIC18FXXQ10 device
'
Config_Start
    FEXTOSC = Off                       ' Oscillator not enabled
    RSTOSC = HFINTOSC_64MHZ             ' HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1
    MCLRE = INTMCLR                     ' If LVP = 0, MCLR pin (RE3) is an input. If LVP =1, MCLR pin (RE3) is MCLR
    WDTE = Off                          ' WDT Disabled
    CLKOUTEN = Off                      ' CLKOUT function is disabled
    CSWEN = Off                         ' The NOSC and NDIV Bits cannot be changed by user software
    FCMEN = Off                         ' Fail-Safe Clock Monitor disabled
    PWRTE = Off                         ' Power up timer disabled
    LPBOREN = Off                       ' Low power BOR is disabled
    BOREN = Off                         ' Brown-out Reset disabled
    ZCD = Off                           ' ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
    PPS1WAY = Off                       ' PPSLOCK bit can be set and cleared repeatedly (subject to the unlock sequence)
    STVREN = Off                        ' Stack full/underflow will not cause Reset
    XINST = Off                         ' Extended Instruction Set and Indexed Addressing Mode disabled
    WDTCCS = SC                         ' Software Control
    WRT0 = Off                          ' Block 0 (000800-001FFF) not write-protected
    WRT1 = Off                          ' Block 1 (002000-003FFF) not write-protected
    WRT2 = Off                          ' Block 2 (004000-005FFF) not write-protected
    WRT3 = Off                          ' Block 3 (006000-007FFF) not write-protected
    WRTC = Off                          ' Configuration registers (300000-30000B) not write-protected
    WRTB = Off                          ' Boot Block (000000-0007FF) not write-protected
    WRTD = Off                          ' Data EEPROM not write-protected
    SCANE = Off                         ' Scanner module is NOT available for use, SCANMD bit is ignored
    LVP = Off                           ' HV on MCLR/VPP must be used for programming
    Cp = Off                            ' User NVM code protection disabled
    CPD = Off                           ' Data NVM code protection disabled
    EBTR0 = Off                         ' Block 0 (000800-001FFF) not protected from table reads executed in other blocks
    EBTR1 = Off                         ' Block 1 (002000-003FFF) not protected from table reads executed in other blocks
    EBTR2 = Off                         ' Block 2 (004000-005FFF) not protected from table reads executed in other blocks
    EBTR3 = Off                         ' Block 3 (006000-007FFF) not protected from table reads executed in other blocks
    EBTRB = Off                         ' Boot Block (000000-0007FF) not protected from table reads executed in other blocks
Config_End

In the assembler code produced by the compiler, you can see the setups performed for the USART1 peripheral that are run before a user's program starts:

; UART1_ACTUALBAUD = 57971.02
; UART1_BAUDERROR = 0.64
    bsf BAUD1CON,PP_BRG16
    movlw 0x44
    movwf SP1BRG
    clrf SP1BRGH
    movlw 0x20
    movwf TX1STA
    movlw 0x90
    movwf RC1STA
; CONFIGURE HRSOUT1 PPS
    movlw _PPS_FN_TX1
    movff WREG,RC6PPS
; CONFIGURE HRSIN1 PPS
    movlw 0x17
    movff WREG,RX1PPS

Below is the above program running in the Proteus simulator with the correct Baud rate:
18F25Q10_USART1_Demo.jpg

hi thank you
is it possible to use this controller at 8MHZ intenal OSC and 57600K baudrate?
I need 8MHZ

NOTE:
in ppi file have 3 line for int osc
RSTOSC = HFINTOSC_64MHZHFINTOSC ; with HFFRQ = 64 MHz and CDIV = 1:1
RSTOSC = HFINTOSC_16MHZHFINTOSC ; with HFFRQ = 16 MHz and CDIV = 1:1
RSTOSC = HFINTOSC_1MHZHFINTOSC  ; with HFFRQ = 4 MHz and CDIV = 4:1

trastikata

Quote from: Maxi on Jul 28, 2023, 12:44 PMis it possible to use this controller at 8MHZ intenal OSC and 57600K baudrate?
I need 8MHZ

Try this modification of your code ...

Device = 18F26Q10

Declare Xtal = 8

'-------------------------------------------------------------------------------
    '**** Added by Fuse Configurator ****
    ' Use the Fuses Tab to change these settings

    Config_Start
    FEXTOSC = OFF              'Oscillator not enable
    RSTOSC = HFINTOSC_64MHZ    'HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:
    CLKOUTEN = OFF             'CLKOUT function is disable
    CSWEN = On                 'Writing to NOSC and NDIV is allowed
    FCMEN = On                 'Fail-Safe Clock Monitor enable
    MCLRE = INTMCLR         ; If LVP = 0, MCLR pin (RE3) is an input; If LVP =1, MCLR pin (RE3) is MCLR
    PWRTE = On                 'Power up timer is on
    LPBOREN = OFF              'Low power BOR is DISABLE
    BOREN = SBORDIS            'Brown-out Reset enabled , SBOREN bit is ignore
    BORV = VBOR_190            'Brown-out Reset Voltage (VBOR) set to 1.90
    ZCD = OFF                  'ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCO
    PPS1WAY = OFF               
    STVREN = OFF            ; Stack full/underflow will not cause Reset
    XINST = OFF                'Extended Instruction Set and Indexed Addressing Mode disable
    WDTCPS = WDTCPS_31         'Divider ratio 1:65536' software control of WDTP
    WDTE = OFF                 'WDT Disable
    WDTCWS = WDTCWS_7          'window always open (100%)' software control' keyed access not require
    WDTCCS = SC                'Software Contro
    WRT0 = OFF                 'Block 0 (000800-003FFFh) not write-protecte
    WRT1 = OFF                 'Block 1 (004000-007FFFh) not write-protecte
    WRT2 = OFF                 'Block 2 (008000-00BFFFh) not write-protecte
    WRT3 = OFF                 'Block 3 (00C000-00FFFFh) not write-protecte
    WRTC = OFF                 'Configuration registers (300000-30000Bh) not write-protecte
    WRTB = OFF                 'Boot Block (000000-0007FFh) not write-protecte
    WRTD = OFF                 'Data EEPROM not write-protecte
    SCANE = OFF             ; Scanner module is NOT available for use, SCANMD bit is ignored
    LVP = OFF               ; HV on MCLR/VPP must be used for programming
    Cp = On                   'UserNVM code protection enable
    CPD = On                  'DataNVM code protection enable
    EBTR0 = OFF                'Block 0 (000800-003FFFh) not protected from table reads executed in other block
    EBTR1 = OFF                'Block 1 (004000-007FFFh) not protected from table reads executed in other block
    EBTR2 = OFF                'Block 2 (008000-00BFFFh) not protected from table reads executed in other block
    EBTR3 = OFF                'Block 3 (00C000-00FFFFh) not protected from table reads executed in other block
    EBTRB = OFF                'Boot Block (000000-0007FFh) not protected from table reads executed in other block
    Config_End

    '**** End of Fuse Configurator Settings ****
    '-------------------------------------------------------------------------------

    Declare HSerout1_Pin  = PORTC.6
    Declare HSerin1_Pin   = PORTC.7

   
    OSCCON1 = %01100011             ' NOSC HFINTOSC, NDIV 8
   
    TRISA=%00010111:TRISB=%10000100:TRISC=%00000110
    PORTA=%00000000:PORTB=%00000000:PORTC=%00000000
     
    PPS_UnLock()
    RC6PPS = 0x09  'TX1(RC6) Output from EUSART1
    RXPPS = 0x17  'RX1(RC7) Input to EUSART1
    PPS_Lock()
     
    CM1CON0.7=0
    CM2CON0.7=0
    WPUB=%00000000
   
    ADCON0=%00000011
    ADCON2=%10101010

    ANSELA=%00000101
    ANSELB=%00000100
    ANSELC=%00000000
   
    '8mhz, 57600 --> 57143 --> 0.8% Error
    '16-bit/Asynchronous FOSC/[4 (n+1)]
    BAUD1CON.3 = 1      'BRG16 = 1
    TX1STA.2 = 1        'BRGH = 1     
    SPBRG = 34
   
    RC1STA.7 = 1   
    RC1STA.5 = 1
    TX1STA.5 = 1
   
While 1 = 1
    HRSOut "test1234"
    Toggle PORTB.0
    DelayMS 1000
Wend

Maxi

Quote from: trastikata on Jul 28, 2023, 05:58 PM
Quote from: Maxi on Jul 28, 2023, 12:44 PMis it possible to use this controller at 8MHZ intenal OSC and 57600K baudrate?
I need 8MHZ

Try this modification of your code ...

Device = 18F26Q10

Declare Xtal = 8

'-------------------------------------------------------------------------------
    '**** Added by Fuse Configurator ****
    ' Use the Fuses Tab to change these settings

    Config_Start
    FEXTOSC = OFF              'Oscillator not enable
    RSTOSC = HFINTOSC_64MHZ    'HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:
    CLKOUTEN = OFF             'CLKOUT function is disable
    CSWEN = On                 'Writing to NOSC and NDIV is allowed
    FCMEN = On                 'Fail-Safe Clock Monitor enable
    MCLRE = INTMCLR         ; If LVP = 0, MCLR pin (RE3) is an input; If LVP =1, MCLR pin (RE3) is MCLR
    PWRTE = On                 'Power up timer is on
    LPBOREN = OFF              'Low power BOR is DISABLE
    BOREN = SBORDIS            'Brown-out Reset enabled , SBOREN bit is ignore
    BORV = VBOR_190            'Brown-out Reset Voltage (VBOR) set to 1.90
    ZCD = OFF                  'ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCO
    PPS1WAY = OFF               
    STVREN = OFF            ; Stack full/underflow will not cause Reset
    XINST = OFF                'Extended Instruction Set and Indexed Addressing Mode disable
    WDTCPS = WDTCPS_31         'Divider ratio 1:65536' software control of WDTP
    WDTE = OFF                 'WDT Disable
    WDTCWS = WDTCWS_7          'window always open (100%)' software control' keyed access not require
    WDTCCS = SC                'Software Contro
    WRT0 = OFF                 'Block 0 (000800-003FFFh) not write-protecte
    WRT1 = OFF                 'Block 1 (004000-007FFFh) not write-protecte
    WRT2 = OFF                 'Block 2 (008000-00BFFFh) not write-protecte
    WRT3 = OFF                 'Block 3 (00C000-00FFFFh) not write-protecte
    WRTC = OFF                 'Configuration registers (300000-30000Bh) not write-protecte
    WRTB = OFF                 'Boot Block (000000-0007FFh) not write-protecte
    WRTD = OFF                 'Data EEPROM not write-protecte
    SCANE = OFF             ; Scanner module is NOT available for use, SCANMD bit is ignored
    LVP = OFF               ; HV on MCLR/VPP must be used for programming
    Cp = On                   'UserNVM code protection enable
    CPD = On                  'DataNVM code protection enable
    EBTR0 = OFF                'Block 0 (000800-003FFFh) not protected from table reads executed in other block
    EBTR1 = OFF                'Block 1 (004000-007FFFh) not protected from table reads executed in other block
    EBTR2 = OFF                'Block 2 (008000-00BFFFh) not protected from table reads executed in other block
    EBTR3 = OFF                'Block 3 (00C000-00FFFFh) not protected from table reads executed in other block
    EBTRB = OFF                'Boot Block (000000-0007FFh) not protected from table reads executed in other block
    Config_End

    '**** End of Fuse Configurator Settings ****
    '-------------------------------------------------------------------------------

    Declare HSerout1_Pin  = PORTC.6
    Declare HSerin1_Pin   = PORTC.7

   
    OSCCON1 = %01100011             ' NOSC HFINTOSC, NDIV 8
   
    TRISA=%00010111:TRISB=%10000100:TRISC=%00000110
    PORTA=%00000000:PORTB=%00000000:PORTC=%00000000
     
    PPS_UnLock()
    RC6PPS = 0x09  'TX1(RC6) Output from EUSART1
    RXPPS = 0x17  'RX1(RC7) Input to EUSART1
    PPS_Lock()
     
    CM1CON0.7=0
    CM2CON0.7=0
    WPUB=%00000000
   
    ADCON0=%00000011
    ADCON2=%10101010

    ANSELA=%00000101
    ANSELB=%00000100
    ANSELC=%00000000
   
    '8mhz, 57600 --> 57143 --> 0.8% Error
    '16-bit/Asynchronous FOSC/[4 (n+1)]
    BAUD1CON.3 = 1      'BRG16 = 1
    TX1STA.2 = 1        'BRGH = 1     
    SPBRG = 34
   
    RC1STA.7 = 1   
    RC1STA.5 = 1
    TX1STA.5 = 1
   
While 1 = 1
    HRSOut "test1234"
    Toggle PORTB.0
    DelayMS 1000
Wend

@trastikata this is works
many many thanks