News:

;) This forum is the property of Proton software developers

Main Menu

Transmit Data (Button) to PIC (receive) in Nextion

Started by Aries_Ryan, Sep 15, 2025, 10:49 AM

Previous topic - Next topic

Aries_Ryan

Hi,
I wish to transmit data from the Nextion Button to PIC.
I tried to do this way but still won't work, what is wrong with my code below:
'So far I still use the previous configuration as below

'----------------------------------- Device and Configuration -----------------------------------------------
Device = 16F628A                                                                      'Microprocessor Use   
Config INTRC_OSC_CLKOUT, MCLRE_OFF, LVP_OFF, WDT_OFF, PWRTE_OFF, CP_OFF, BODEN_OFF    'Configuration of bits
Declare Xtal = 4                                                                      'Use Crystal 4 Mhz
'------------------------------------- End Configuration -----------------------------------------------------

'--------------------------------------LCD Configuration -----------------------------------------------------
'--- Declare LCD Function here
'------------------------------------- End LCD Configuration --------------------------------------------------

Declare Hserial_Baud = 9600 ' Set to 9600 for communication Receiver and Transmitter baud rate
Declare Hserial_RCSTA = %10010000       ' Enable serial port and continuous receive
Declare Hserial_TXSTA = %00100100       ' Enable transmit and asynchronous mode
Declare HRSOut_Pin = PORTB.2            ' TX Pin 8 will connect to RX pin side to Nextion TFT Board
Declare HRSIn_Pin = PORTB.1             ' RX Pin 7 will connect to TX pin side to Nextion TFT Board
Declare Hserial_Clear = On ' Enable Error clearing on received characters

'------------------------------------- Prepare Comparator CMCON -----------------------------------------------
CMCON = 7                       'CMCON = 0b00100101
'--------------------------- Variable Addressing --------------------------------------------------------------
Dim Var1 As Byte
Dim Var2 As Byte

Dim nextion_byte As Byte
Dim page_id As Byte
Dim component_id As Byte

main:
DelayMS 10
'Var1 = HRSIn 1000 Time is Over "b0.val=1"
Var1 = HRSIn "b0.val=1"   'bo is Object Name of Nextion Editor
Var1 = HRSIn ("e")        'Send Button Command (serial byte e) Nextion to PIC, transmit and receive it
Var1 = HRSIn If PORTA.0 = 1 Then Setup_Counter_Time    'Get this button into PIC then
The scenario, once I press the touch button into Nextion TFT side, it will receive and toggle PORTA.0 = 1.
If anyone, have experiences with playing this Nextion instruction button, please help.

Thanks,
ARyan.



RGV250

Hi,
Is that all the code as it will just do the 4 lines after Main and stop?

Bob

Aries_Ryan

Hi Bob,
Firstly, thank you for your question.
That one is not a complete code, I wish to tested first to basic access transmit on Nextion Touch Button to received in PIC, once it working well then continue to next function.


Thanks,
ARyan.

RGV250

Hi,
What i am saying is with the code you have posted it will stop in milliseconds as you have no loop so probably before you press the button.

Bob

Stephen Moss

Quote from: Aries_Ryan on Sep 15, 2025, 11:51 AMThat one is not a complete code, I wish to tested first to basic access transmit on Nextion Touch Button to received in PIC, once it working well then continue to next function.
To clarify, he was wanting to know if what you posted is only part of a larger code where there is a Goto Main statement that you did not show in your example that will make the code you have provided loop repeatedly, because if the code you provided is all you have then it will only execute once and so the line
If PORTA.0 = 1 Then Setup_Counter_Time    'Get this button into PIC thenwill probably executes before you press the button and so will never detect the button press.

Also, I don't realy use serial so I may be wrong but to me you code...
Var1 = HRSIn "b0.val=1"   'bo is Object Name of Nextion Editor
Var1 = HRSIn ("e")        'Send Button Command (serial byte e) Nextion to PIC, transmit and receive it
look like you are trying to send data but are using the recieve data command instead of HRSout.
If the value after the HRSin command is a reminder of what you are expecting to recive then it should be after the comment character, additionally you have have defined Var1 as a byte and "b0.val=1" would be a string and cannot be stored in the single byte variable Var1.

Finally, If PORTA.0 = 1 Then Setup_Counter_Time    'Get this button into PIC then is not necessarily a good way of detecting the button press as there could be a large delay in detecting it if your final code is large and takes a long time to loop, and it only really works if the button state is latched (not changing until the button is pressed again), so consider using an input pin with IOC (Interrupt on Change) capabilities so that an interrupt is triggered when the button output changes state for a faster responce to the button press.