News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Usart2 not work

Started by krizpin, Aug 27, 2023, 07:30 AM

Previous topic - Next topic

krizpin

Good morning.
 I have a problem with usart2, I can't get it to work. I have it configured the same as usart1 but usart2 does not send data. can anybody help me????
I have prepared a very simple code to see it
thank you


Device = 18F25K22
Xtal 16
'-------------------------------------------------------------------------------
'**** Added by Fuse Configurator ****
' Use the Fuses Tab to change these settings

Config_Start
  FOSC = INTIO67     ;Internal oscillator bloc
  PLLCFG = OFF       ;Oscillator used directl
  PRICLKEN = ON      ;Primary clock enable
  FCMEN = OFF        ;Fail-Safe Clock Monitor disable
  IESO = OFF         ;Oscillator Switchover mode disable
  PWRTEN = ON        ;Power up timer enable
  BOREN = SBORDIS    ;Brown-out Reset enabled in hardware only (SBOREN is disabled
  BORV = 190         ;VBOR set to 1.90 V nomina
  WDTEN = OFF        ;Watch dog timer is always disabled. SWDTEN has no effect
  WDTPS = 32768      ;1:3276
  CCP2MX = PORTC1    ;CCP2 input/output is multiplexed with RC
  PBADEN = OFF       ;PORTB<5:0> pins are configured as digital I/O on Rese
  CCP3MX = PORTB5    ;P3A/CCP3 input/output is multiplexed with RB
  HFOFST = ON        ;HFINTOSC output and ready status are not delayed by the oscillator stable statu
  T3CMX = PORTC0     ;T3CKI is on RC
  P2BMX = PORTB5     ;P2B is on RB
  MCLRE = EXTMCLR    ;MCLR pin enabled, RE3 input pin disable
  STVREN = ON        ;Stack full/underflow will cause Rese
  LVP = ON           ;Single-Supply ICSP enabled if MCLRE is also
  XINST = OFF        ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode
  DEBUG = OFF        ;Disable
  CP0 = OFF          ;Block 0 (000800-001FFFh) not code-protecte
  CP1 = OFF          ;Block 1 (002000-003FFFh) not code-protecte
  CP2 = OFF          ;Block 2 (004000-005FFFh) not code-protecte
  CP3 = OFF          ;Block 3 (006000-007FFFh) not code-protecte
  CPB = OFF          ;Boot block (000000-0007FFh) not code-protecte
  CPD = OFF          ;Data EEPROM not code-protecte
  WRT0 = OFF         ;Block 0 (000800-001FFFh) not write-protecte
  WRT1 = OFF         ;Block 1 (002000-003FFFh) not write-protecte
  WRT2 = OFF         ;Block 2 (004000-005FFFh) not write-protecte
  WRT3 = OFF         ;Block 3 (006000-007FFFh) not write-protecte
  WRTC = OFF         ;Configuration registers (300000-3000FFh) not write-protecte
  WRTB = OFF         ;Boot Block (000000-0007FFh) not write-protecte
  WRTD = OFF         ;Data EEPROM not write-protecte
  EBTR0 = OFF        ;Block 0 (000800-001FFFh) not protected from table reads executed in other block
  EBTR1 = OFF        ;Block 1 (002000-003FFFh) not protected from table reads executed in other block
  EBTR2 = OFF        ;Block 2 (004000-005FFFh) not protected from table reads executed in other block
  EBTR3 = OFF        ;Block 3 (006000-007FFFh) 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 ****
'-------------------------------------------------------------------------------
OSCCON=%01110111

'***** CONFIGURACION CANALES ANALOGICOS/DIGITALES ********
'*********************************************************


TRISB.6=1   'Registro TX2 ,debe estar a nivel logico "1" .Comunicacion PIC A NEXTION
TRISB.7=1   'Registro RX2 ,debe estar a nivel logico "1" .Comunicacion PIC A NEXTION

TRISC.6=1   'Registro TX1 ,debe estar a nivel logico "1" .Comunicacion RS485
TRISC.7=1   'Registro RX1 ,debe estar a nivel logico "1" .Comunicacion RS485

ANSELA=0    'CONFIGURADO COMO DIGITAL
ANSELB=0    'CONFIGURADO COMO DIGITAL
anselc=0  'CONFIGURADO COMO DIGITAL


'******************************************
'******** CONFIGURACION LCD ***************
Declare LCD_Type 0
Declare LCD_DTPin PORTA.0
Declare LCD_RSPin PORTA.4
Declare LCD_ENPin PORTA.5
Declare LCD_Lines 4



 '******************************************
'******** CONFIGURACION EUSART1 *********** 
'******************************************
Declare Hserial_Baud = 9600
declare hrsout1_pin = portc.6
declare hrsin1_pin =  portc.7    'set the RX pin
Declare Hserial_Clear = On
PIE1.5=1     'HABILITA INTERRUPCION EUSART1 RECEPCION
Symbol bandera_rx1 = PIR1.5
Clear bandera_rx1



 '******************************************
'******** CONFIGURACION EUSART2 ***********     
'******************************************
Declare Hserial2_Baud = 9600
declare hrsout2_pin = portb.6
declare hrsin2_pin =  portb.7    'set the RX pin
Declare Hserial2_Clear = On
PIE3.5=1     'HABILITA INTERRUPCION EUSART1 RECEPCION
Symbol bandera_rx2 = PIR3.5
Clear bandera_rx2



do

    hrsout2 "HELLO"," "
    delayms 10
    hrsout "HELLO"," "
    delayms 80
loop

end

RGV250

Hi,
With the Tris statements you are setting the pins to inputs, not sure why it works for one though.
I did not think you needed to set those anyway as the compiler does it.

Bob

krizpin

according to the data sheet you have to have them at "1" anyway even if you don't configure them, the usart2 still doesn't work

trastikata

Did you try it in an actual board or it is the SIM that's not working?

krizpin

I did not try it on the real board, only in proteus

trastikata

#5
As RGV250 said you have to clear the corresponding TRIS bit to make given pin an output.

QuoteFor all modes of EUSART operation, the TRIS control bits corresponding to the RXx/DTx and TXx/CKx pins may be set to '1'. The EUSART control will automatically reconfigure the pin from input to output, as needed.

You were probably referring to this paragraph, but I don't think (I am not sure though) that port direction change occurs automatically to Output every time you write in the TX register. It'd be a change that happens automatically only when you enable the UART TX module.

Since in the generated code the UART module configuration happens before the user code, you are effectively reverting the pin to input, I think.

As for why UART1 works, I don't know either.


krizpin

Could it be a compiler problem? I don't know what to think anymore, I've tried to change the tris pin and nothing

tumbleweed

Quote from: krizpin on Aug 27, 2023, 11:14 AMCould it be a compiler problem?
I don't see anything wrong with the generated code. The code for USART2 is pretty much the same as that for USART1.

In addition to what the others have pointed out, you can get rid of these lines (automatically done by the compiler)
ANSELA=0    'CONFIGURADO COMO DIGITAL
ANSELB=0    'CONFIGURADO COMO DIGITAL
anselc=0  'CONFIGURADO COMO DIGITAL

Also, I'd get rid of these two:
PIE1.5=1     'HABILITA INTERRUPCION EUSART1 RECEPCION
PIE3.5=1     'HABILITA INTERRUPCION EUSART1 RECEPCION
Although interrupts aren't enabled, setting the bits without an interrupt handler isn't a good idea

Try it on real hardware... it's likely a problem with Proteus.


RGV250

Hi,
It is strange, I put a scope on the VSM and there is something coming out on USART2 but a lot more than USART1 so I an wondering if it is a peripheral although I cannot see what.

Bob

krizpin

I'm going to try another version of proteus out of curiosity, otherwise I'll have to test it by hardware to see if I can see that data arrives on the lcd
my head is crazy xdddd

krizpin

I just tried another version of proteus and it does the same thing. usart 1 perfect but usart2 does not stop flashing the TX pin but does not show data.

trastikata

Could you run this code through your simulation and post the result?

Device = 18F25K22
Xtal 16
'-------------------------------------------------------------------------------
'**** Added by Fuse Configurator ****
' Use the Fuses Tab to change these settings

Config_Start
  FOSC = INTIO67     ;Internal oscillator bloc
  PLLCFG = OFF       ;Oscillator used directl
  PRICLKEN = On      ;Primary clock enable
  FCMEN = OFF        ;Fail-Safe Clock Monitor disable
  IESO = OFF         ;Oscillator Switchover mode disable
  PWRTEN = On        ;Power up timer enable
  BOREN = SBORDIS    ;Brown-out Reset enabled in hardware only (SBOREN is disabled
  BORV = 190         ;VBOR set to 1.90 V nomina
  WDTEN = OFF        ;Watch dog timer is always disabled. SWDTEN has no effect
  WDTPS = 32768      ;1:3276
  CCP2MX = PORTC1    ;CCP2 input/output is multiplexed with RC
  PBADEN = OFF       ;PORTB<5:0> pins are configured as digital I/O on Rese
  CCP3MX = PORTB5    ;P3A/CCP3 input/output is multiplexed with RB
  HFOFST = On        ;HFINTOSC output and ready status are not delayed by the oscillator stable statu
  T3CMX = PORTC0     ;T3CKI is on RC
  P2BMX = PORTB5     ;P2B is on RB
  MCLRE = EXTMCLR    ;MCLR pin enabled, RE3 input pin disable
  STVREN = On        ;Stack full/underflow will cause Rese
  LVP = On           ;Single-Supply ICSP enabled if MCLRE is also
  XINST = OFF        ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode
  Debug = OFF        ;Disable
  Cp0 = OFF          ;Block 0 (000800-001FFFh) not code-protecte
  CP1 = OFF          ;Block 1 (002000-003FFFh) not code-protecte
  CP2 = OFF          ;Block 2 (004000-005FFFh) not code-protecte
  CP3 = OFF          ;Block 3 (006000-007FFFh) not code-protecte
  CPB = OFF          ;Boot block (000000-0007FFh) not code-protecte
  CPD = OFF          ;Data EEPROM not code-protecte
  WRT0 = OFF         ;Block 0 (000800-001FFFh) not write-protecte
  WRT1 = OFF         ;Block 1 (002000-003FFFh) not write-protecte
  WRT2 = OFF         ;Block 2 (004000-005FFFh) not write-protecte
  WRT3 = OFF         ;Block 3 (006000-007FFFh) not write-protecte
  WRTC = OFF         ;Configuration registers (300000-3000FFh) not write-protecte
  WRTB = OFF         ;Boot Block (000000-0007FFh) not write-protecte
  WRTD = OFF         ;Data EEPROM not write-protecte
  EBTR0 = OFF        ;Block 0 (000800-001FFFh) not protected from table reads executed in other block
  EBTR1 = OFF        ;Block 1 (002000-003FFFh) not protected from table reads executed in other block
  EBTR2 = OFF        ;Block 2 (004000-005FFFh) not protected from table reads executed in other block
  EBTR3 = OFF        ;Block 3 (006000-007FFFh) 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 ****
'-------------------------------------------------------------------------------
OSCCON=%01110111

'***** CONFIGURACION CANALES ANALOGICOS/DIGITALES ********
'*********************************************************


TRISB.6=0   'Registro TX2 ,debe estar a nivel logico "1" .Comunicacion PIC A NEXTION
TRISB.7=1   'Registro RX2 ,debe estar a nivel logico "1" .Comunicacion PIC A NEXTION

TRISC.6=0   'Registro TX1 ,debe estar a nivel logico "1" .Comunicacion RS485
TRISC.7=1   'Registro RX1 ,debe estar a nivel logico "1" .Comunicacion RS485

ANSELA=0    'CONFIGURADO COMO DIGITAL
ANSELB=0    'CONFIGURADO COMO DIGITAL
ANSELC=0  'CONFIGURADO COMO DIGITAL


'******************************************
'******** CONFIGURACION LCD ***************
Declare LCD_Type 0
Declare LCD_DTPin PORTA.0
Declare LCD_RSPin PORTA.4
Declare LCD_ENPin PORTA.5
Declare LCD_Lines 4




RCSTA1.7 = 1 'SPEN: Serial port1 Enable bit
RCSTA2.7 = 1 'SPEN: Serial port2 Enable bit
RCSTA1.2 = 0 'FERR: Framing Error bit clear
RCSTA2.2 = 0 'FERR: Framing Error bit clear
RCSTA1.1 = 0 'OERR: Overrun Error bit clear
RCSTA2.1 = 0 'OERR: Overrun Error bit clear
BAUDCON1.3 = 0 '8-Bit Baud Rate Register Enable bit
BAUDCON2.3 = 0 '8-Bit Baud Rate Register Enable bit 
TXSTA1.2 = 0   'BRGH: Low speed
TXSTA2.2 = 0   'BRGH: Low speed
SPBRGH1 = 0   'EUSARTx Baud Rate Generator Register High Byte
SPBRGH2 = 0   'EUSARTx Baud Rate Generator Register High Byte
SPBRG1 = 25   'EUSARTx Baud Rate Generator Register Low Byte
SPBRG2 = 25   'EUSARTx Baud Rate Generator Register Low Byte
TXSTA1.5 = 1  'TXEN: Transmit Enable bit
TXSTA2.5 = 1  'TXEN: Transmit Enable bit

Dim i As Byte

Do
    Uart2Tx("HELLO ")
    DelayMS 10
    Uart1Tx("HELLO ")
    DelayMS 80
Loop

Proc Uart1Tx(sTx As String * 6)
    For i = 0 To 5
        While PIR1.4 = 0 : Wend
        TXREG1 = sTx[i]
    Next
EndProc

Proc Uart2Tx(sTx As String * 6)
    For i = 0 To 5
        While PIR3.4 = 0 : Wend
        TXREG2 = sTx[i] 
    Next
EndProc

End

Pepe

Add SPBRGH2 = 0 for your program to work
but the uart2 instead of transmitting at 9600, transmits at 600 baud

RGV250

#13
Hi Pepe,
I tried it and it works, any explanation why it does?
I also removed the declare baud for both and it works.

I also tried Trastikata's one and that works but you cannot have a space in the text. I changed it to HELLO 1 / HELLO 2 and altered the length in the proc but only got HELLO

Changed to the original do / loop and that works without the proc and you can send text with spaces.

Bob

trastikata

#14
Quote from: RGV250 on Aug 27, 2023, 04:50 PMHi Pepe,
I tried it and it works, any explanation why it does?
I also removed the declare baud for both and it works.

I also tried Trastikata's one and that works but you cannot have a space in the text. I changed it to HELLO 1 / HELLO 2 and altered the length in the proc but only got HELLO

Changed to the original do / loop and that works without the proc and you can send text with spaces.

Bob

I have had problems before with the compiler automatically setting up the UART modules and made it habit to do he set-up manually. The procs in my code were to exclude Proteus as the reason for not receiving serial data. The missing spaces/numbers in my code are because the procedure argument sTx was declared as a string with size 6 only, again for simplicity and test purpose only.

The assembler code needs analysis to determine where's the bug in the compiler when setting the UART2 module.

Pepe

SPBRGH2 = 0
OSCCON=%01110111
it does not give the baud correctly


OSCCON=%01110111
SPBRGH2 = 0
works well

it is an error of the Proteus simulator, not of the compiler.

top204

#16
I've just tested USART2 with a PIC18F25K22 device and it is working as expeceted, both in the Proteus simulator and on a real device.

The simple test program below shows it working perfectly and echoing the characters typed into a serial terminal, and if left alone, timing out:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Test USART2 on a PIC18F25K22 device
' Written for the Positron BASIC compiler by Les Johnson
'
    Device = 18F25K22                       ' Tell the compiler what device to compile for
    Declare Xtal = 16                       ' Tell the compiler what frequency the device is operating at (in MHz)
'
' Setup USART2
'
    Declare Hserial2_Baud = 9600            ' Setup the Baud rate of USART2
    Declare HRSOut2_Pin   = PORTB.6         ' Tell the compiler what pin is used for USART2 TX
    Declare HRSIn2_Pin    = PORTB.7         ' Tell the compiler what pin is used for USART2 RX
   
    Dim MyByte As Byte
   
'--------------------------------------------------------------------------
' The main program starts here
'
Main:
    HRSOut2Ln "Type characters and they will be echoed"
   
    Do
Again:
        MyByte = HRSIn2, {5000, TimedOut}
        HRSOut2 MyByte
    Loop
   
TimedOut:
    HRSOut2Ln "\rTimed Out"
    GoTo Again

Setting values to SFRs will sometimes effect a peripheral's operation if it is not understood correctly, so always start tests with a simple program listing as above, then elaborate on it if required. A USARTx peripheral will always set its TX and RX pins to output and input when enabled, so there is no need to alter a TRIS bit. Pins are also set to digital mode by the compiler before a user's program starts and a few peripherals that effect a pin's mode are also altered.

For example, the assembler code setup for USART2 and the pins for the above program listing is shown below:

; UART2_ACTUALBAUD = 9615.38
; UART2_BAUDERROR = 0.16
    bsf BAUD2CON,PP_BRG16
    movlw 0x67
    movwf SP2BRG
    clrf SP2BRGH
    movlw 0x20
    movwf TX2STA
    movlw 0x90
    movwf RC2STA

;---------------------------------------------
; START OF THE USER'S PROGRAM CODE
F1_SOF equ $ ; TEST_18F25K22.BAS
    movlb 0x0F
    clrf ANSELA,1
    clrf ANSELB,1
    clrf ANSELC,1
    clrf CM1CON0,0
    clrf CM2CON0,0
    clrf SLRCON,0
Main

See how the ADCs and comparators have been disabled and the pin slew set for default TTL mode.

krizpin

#17
hello.
I just tested the code and it works fine.
In my code I have inserted the line SPBRGH2 = 0 and it works perfectly. Thank you very much to everyone for your help.

top204

#18
The Proteus simulator is not to be trusted. When using the internal oscillator and setting up the SFRs for its oscillation frequency, or depending on a config fuse setting, the isis simulator regularly fails to simulate correctly.

Whenever I use the Isis simulator, I always make a test of the frequency it is operating at, to make sure the device is simulating correctly before any further simulation tests take place. The compiler is producing code that is correct, but the device itself is not being simulated correctly.

I rarely answer questions based upon simulation only problems because of the rather poor performance of the Proteus simulation program, but when someone says "it is probably the compiler at fault", I will always try it out, because a third party program that is not written with much detail and testing (isis) is effecting the reputation of the Positron compiler!

I see the proteus program as a "very expensive toy". :-)

krizpin

Thank you very much for answering.
 Thank you once again to everyone for your collaboration.
I apologize for my audacity to blame the compiler. very sorry. I thought that proteus was totally reliable but now I see that it is not.
a hug