TFT Display Page Access Through Button (Nextion)

Started by Aries_Ryan, Jul 18, 2026, 08:14 AM

Previous topic - Next topic

Aries_Ryan

I am encountering an issue with page navigation and need some assistance. I have two pages, page1 and page2.
There is a button on page1 intended to redirect to page2, but it fails to do so. Below is my code:
Device = 18F4620
Declare Xtal = 4
Declare Reminders Off

'--- PIC18F4620 Configuration Fuses ---
Config_Start
  OSC = XT       
  FCMEN = OFF   
  IESO = OFF     
  PWRT = On     
  BOREN = SBORDIS
  WDT = OFF     
  MCLRE = On     
  LVP = OFF     
  PBADEN = OFF   
  XINST = OFF   
  Cp0 = OFF     
Config_End

Declare Reminders On


Declare Hserial_Baud = 9600
Declare Hserial_RCSTA = %10010000
Declare Hserial_TXSTA = %00100100  'BRGH=1 (High Speed Baud)
Declare Hserial_Clear = On 

ADCON1 = %00000110 
ADCON0 = %00000001
ADCON2 = %10000111 

TRISA = %11111111               
TRISB = %01000100               
TRISC = %10000000               
TRISD = %00000000               
TRISE = %00000111               

CMCON = 7

Dim Active_Page As Byte

Tft_Cmd = 0
   
    ' Clear Hardware Overrun Error if buffer crashed
    If RCSTA.1 = 1 Then
        RCSTA.4 = 0
        RCSTA.4 = 1
    EndIf
   
    ' Pull ALL bytes from the hardware FIFO buffer
    While PIR1.5 = 1
        Tft_Cmd = RCREG
    Wend
   
    ' Page1 command button, if I press Button "Touch Press Event(1), for prints "M",0
      Then page1 dissapears then go to page2
    If Active_Page = 1 Then
        If Tft_Cmd = "M" Then
            Active_Page = 2
            HSerOut ["page page2", 255, 255, 255]
        EndIf
'-------------------------------'

End
 

Could someone please help me identify the root cause of this problem?

Thanks.
A_Ryan

 

RGV250

Hi,
As you only have 2 pages I cannot see why you would need to test for which page it is on.
Also, I have not used Nextion but Industrial screens I used have a change screen function which does not need comms. Just wondering if Nextion screens have this.

Bob

Pepe

Test this
Device = 18F4620
Declare Xtal = 4
Declare Reminders Off

'--- PIC18F4620 Configuration Fuses ---
Config_Start
    OSC = XT
    FCMEN = OFF
    IESO = OFF
    PWRT = ON
    BOREN = SBORDIS
    WDT = OFF
    MCLRE = ON
    LVP = OFF
    PBADEN = OFF
    XINST = OFF
    CP0 = OFF
Config_End

Declare Reminders On

Declare Hserial_Baud = 9600
Declare Hserial_RCSTA = %10010000
Declare Hserial_TXSTA = %00100100
Declare Hserial_Clear = On

ADCON1 = %00000110
ADCON0 = %00000001
ADCON2 = %10000111

TRISA = %11111111
TRISB = %01000100
TRISC = %10000000
TRISD = %00000000
TRISE = %00000111

CMCON = 7

Dim Active_Page As Byte
Dim Tft_Cmd As Byte

DelayMS 500

' Comenzar en la página 1
HSerOut ["page page1",255,255,255]
Active_Page = 1

Do

    ' Limpiar Overrun
    If RCSTA.1 = 1 Then
        RCSTA.4 = 0
        RCSTA.4 = 1
    EndIf

    ' Procesar todos los bytes recibidos
    While PIR1.5 = 1

        Tft_Cmd = RCREG

        Select Active_Page

            Case 1
                If Tft_Cmd = "M" Then
                    HSerOut ["page page2",255,255,255]
                    Active_Page = 2
                EndIf

            Case 2
                If Tft_Cmd = "N" Then
                    HSerOut ["page page1",255,255,255]
                    Active_Page = 1
                EndIf

        EndSelect

    Wend

Loop

Frizie

#3
Try:  HSEROUT1 ["dp=2", 0xFF, 0xFF, 0xFF]



It is better to create a procedure for this, for example:

;Procedure om Nextion HMI naar opgegeven pagina te gaan of de huidige pagina te verversen
PROC HMIpag(HMI_Pagina AS BYTE)                  ;Laat de HMI naar de opgegeven pagina gaan of ververs de huidige pagina als de HMI zich al op de betreffende pagina bevindt
  DIM  PagString AS STRING * 2                    ;Declareer een stringvariabele voor 2 karakters (max. "99" HMI pagina's)
  STRN PagString = STR$(DEC HMI_Pagina)          ;Zet BYTE variabele 'HMI_Pagina' om naar stringvariabele 'PagString' voor HMI
  HSEROUT1 ["dp=", PagString, 0xFF, 0xFF, 0xFF]  ;Verzend via UART1 de opdracht naar de HMI  (Voorbeeld: dp=1 = pgHome)
ENDPROC


Declare the page names and page numbers exactly the same as in your Nextion screen, e.g.:
SYMBOL pgHome      = 1
SYMBOL pgInfo      = 2
SYMBOL pgInOut     = 3
SYMBOL pgMenu      = 4
SYMBOL pgMotors    = 5


Call it with:
HMIpag(pgHome) ;Verzend via UART1 de opdracht naar de HMI  (Voorbeeld: dp=1 = pgHome)
Ohm sweet Ohm | www.picbasic.nl

ChrisRowe

Hi,
Not sure why you need to involve the PIC in order to swap pages on nextion display using a button on the display.
Just set the button TouchPress event to page x. I always use a "sendme" when page opens to send page info to PIC. It will send 0x66 page number 0xFF,0xFF,0xFF.

Hope this helps ....
regards Chris

Frizie

That's right, I always do that too.
But that wasn't the questioner's question.

In some cases, you want the PIC itself to direct the Nextion to a page.
For example, suppose you want to jump to a page if the touchscreen hasn't been touched for a while (such as an image or screensaver); then the PIC will need to control that.
It just depends on what the questioner is building.
Ohm sweet Ohm | www.picbasic.nl

RGV250

QuoteBut that wasn't the questioner's question.
There is a button on page1 intended to redirect to page2,
That was the original question, no mention of the PIC needing to control it.
The fact there was code was the way the poster thought it needed to be done.

Regards,
Bob

Frizie

You're right.
Apparently I'm not paying attention either and also need to open my eyes better  ::)
Ohm sweet Ohm | www.picbasic.nl

Aries_Ryan

Thank you, Pepe and Frizie. I integrated your code into my project, and it was a great solution that significantly improved my work. The project is now complete and working very well, and the button functions smoothly. Thank you so much again!
Declare Xtal = 16
Declare Reminders Off


Config_Start
    FOSC = INTIO67
    PLLCFG = OFF
    PRICLKEN = On
    FCMEN = OFF
    IESO = OFF
    PWRTEN = On
    BOREN = SBORDIS
    WDTEN = OFF
    LVP = OFF
Config_End

Declare Reminders On
Declare Bus_SCL On

All_Digital = true

Declare Float_Rounding = On

Declare Adin_Res = 10
Declare Adin_Tad = FRC
Declare Adin_Delay = 50

'TFT Configuration
Declare Hserial_Baud = 9600
Declare Hserial_RCSTA = %10010000
Declare Hserial_TXSTA = %00100100  ' BRGH=1 (High Speed Baud)
Declare Hserial_Clear = On


' AN0 to AN8 as Analog Inputs (PORTA 0-5, PORTE 0-2)
ANSELA = %00101111
ANSELB = %00000000
ANSELC = %00000000
ANSELD = %00000000
ANSELE = %00000011

ADCON1 = 0
ADCON2 = %10100110

'Pin Directions
TRISA = %11111111
'Button To switch ON TRISB so PORTB.5 is now an OUTPUT for the Relay
TRISB = %11000000
TRISC = %10000000  ' RC7(RX) Input, RC6(TX) Output
TRISD = %00000000
TRISE = %00000011


'Nextion Page There Are 3 Pages into Nextion Editor
Symbol pgMain           = 0  'page0
Symbol pgMenu           = 1  'page1
Symbol pgController     = 2  'page2


HSerOut ["dp=", Dec pgMain, 255, 255, 255]
DelayMS 100
'Here your code
HSerOut ["dp=", Dec pgMain, 255, 255, 255]
Active_Page = pgMenu

 If RCSTA.1 = 1 Then
        RCSTA.4 = 0
        RCSTA.4 = 1
    EndIf

    While PIR1.5 = 1
        Nextion_Cmd = RCREG

        Select Active_Page

            Case pgMain
                If Nextion_Cmd = "M" Then   'Button is press, then go to Menu Page
                    Active_Page = pgMenu    'Activated to Menu Page
                    HSerOut ["dp=", Dec pgMenu, 255, 255, 255]
                EndIf
                If Nextion_Cmd = "R" Then   'Button is press, then go to activted Relay
                    Toggle ToggleLED        'PORTB.5 Led is On (Relay)
                EndIf