Previously, Zheng Motion Technology shared with everyone the firmware upgrade of motion controllers, ZBasic program development, ZPLC program development, application of communication with touch screens and input/output IO, application of motion controller data and storage, use of motion controller ZCAN and EtherCAT bus, application of oscilloscopes, and the characteristics of multi-tasking operation.
Today, we will explain the application of motion controller interrupts (power-down interrupt, external interrupt, timer interrupt) in positive motion technology.
Video Tutorial: Application of Motion Controller Interrupts in Positive Motion Technology
1. Material preparation and controller wiring reference
1) Material preparation
2) Controller Wiring Reference
2. Interruption concept
An "interrupt" occurs when the CPU encounters an abnormality or sudden situation while executing a program, requiring emergency handling. The CPU must pause its current task to deal with the emergency, and then return to the point where the program was interrupted.
Interrupt handling consists of three stages:
1) Interruption response
Turn on the main interrupt switch and send an interrupt request.
2) Interruption handling
Interrupt handling consists of two parts: saving the breakpoint and the context, and executing the interrupt routine. After responding to the interrupt request, the system jumps to the entry point of the interrupt routine and executes the interrupt routine.
3) Interrupt return
Once the interrupt routine has finished processing all interrupts, it ends and waits for the next interrupt to be triggered.
3. Three types of interruptions
The controller interrupts are divided into three types: power failure interrupt, external interrupt, and timer interrupt.
Before using interrupts, the main interrupt switch must be turned on. To prevent the program from entering an interrupt before it has finished initializing, the interrupt switch is turned off by default when the controller is powered on.
All three types of interrupts are supported by both Basic programming and PLC programming methods.
1) Power failure interruption
It must be a global SUB function. The controller has only one power-down interrupt. The execution time of the power-down interrupt is very limited, and only a few statements can be written to store the data in the VR.
Related functions: INT_ENABLE, ONPOWEROFF.
2) External interrupt
It can be set to be triggered by either rising or falling edges. It must be a global SUB function, and currently only interrupt IN ports 0-31 can use it. It can only be used with firmware that supports PLC functions.
Related functions: Rising edge interrupt INT_ONn, Falling edge interrupt INT_OFFn.
3) Timer interrupt
The timer interrupt function is executed after the set time is reached. It must be a global SUB function. The number of timers depends on the controller model. You can check it by printing ?*max.
Related function: ONTIMERn, where n ranges from "0" to "maximum timer number minus 1".
4. Interrupt function
1) Basic interrupt-related functions
INT_ENABLE: Interrupt master switch, 1-on, 0-off
ONPOWEROFF: Power failure interruption
INT_ONn: External input interrupt, valid on rising edge, n - input port number
INT_OFFn: External input interrupt, valid on falling edge, n - input port number
ONTIMERn: Timer interrupt, n - timer number
GLOBAL SUB: Define the interrupt function
END SUB: The interrupt ends and returns to the main function.
2) PLC interrupt-related functions
EI: Enables interrupts, similar to BASIC commands; INT_ENABLE=1 enables it.
DI: Disable interrupts, same as BASIC commands, INT_ENABLE=0 disables interrupts.
ONPOWEROFF: Power failure interruption
INT_ONn: External input interrupt, valid on rising edge, n - input port number
INT_OFFn: External input interrupt, valid on falling edge, n - input port number
ONTIMERn: Timer interrupt, n - timer number
LBL: Defines the interrupt function
IRET: Returns to the main function after the interrupt ends.
5. Three types of interrupt usage (Basic)
When the three types of interrupts are running, the interrupt routine occupies a separate task number.
The following are usage routines for different types of interrupts:
1) Power failure interruption interruption usage
Only one is supported. Execution is triggered the instant power failure. The execution time is short, and only a few statements can be written.
Example:
'Main Program'
INT_ENABLE = 1
DPOS(0) = VR(0) 'Power on and read the saved values to restore the coordinates'
DPOS(1) = VR(1)
DPOS(2) = VR(2)
END 'Main program ended'
GLOBAL SUB ONPOWEROFF () 'Power-off interrupt'
VR(0) = DPOS(0) 'Save coordinates to VR
VR(1) = DPOS(1)
VR(2) = DPOS(2)
END SUB
2) External interrupt (rising edge/falling edge) interrupt usage
External interrupts can only be used by controllers that support PLC functionality, and must be global SUB procedures. INT_ONn() is triggered on the rising edge, and INT_OFFn() is triggered on the falling edge. n is the IN input number, which can only be selected from the controller's built-in numbers 0-31.
Example:
INT_ENABLE= 1 'Enable interrupts'
END 'Main program ended'
GLOBAL SUB INT_ON0 () 'External rising edge interrupt routine
PRINT "Input IN0 rising edge triggered"
END SUB
GLOBAL SUB INT_OFF0 () 'External falling edge interrupt routine
PRINT "Input IN0 Falling edge triggered"
END SUB
3) Timer interrupt usage
Multiple timer interrupts can be enabled simultaneously. The number of interrupts is determined by the number of timers and must be a global SUB procedure. In ONTIMERn(), n is the timer number.
Start the timer: TIMER_START(timer number, timeout in milliseconds)
Timer ends: TIMER_STOP (timer number)
Timer status: value = TIMER_IFEND (timer number), read-only parameter, return value: 0 - timing is in progress, 1 - timing is complete.
Example:
INT_ENABLE= 1 'Enable interrupts'
TIMER_START(0,100) 'Timer 0 is started, and it executes once every 100ms.'
END
GLOBAL SUB ONTIMER0() 'Interrupt procedure
PRINT "ontimer0 enter"
'TIMER_START(0,100) ' This option aims to interrupt the periodic execution and re-enable the timer in the SUB.
END SUB
6. Three types of interrupt usage (PLC)
In PLC programming, the LBL instruction is used to define the interrupt function to be called. The interrupt function is the same as that in Basic.
A simple example of the use of the three types of interrupts in PLC programming is shown in the ladder diagram program.
When using external interrupts, note that the X number is in octal, such as LBL @INT_ON8. The trigger condition for the external interrupt is the rising edge of X10. In actual wiring, use the IN8 input port.
When an interrupt routine runs, it occupies a task number, which is automatically assigned and does not need to be manually set.
The following figure shows the task status when the timer interrupt is running.
7. Precautions for interruption of use
1) Firmware that supports PLC functions must be used to use external interrupts.
2) The interrupt function is written as a global SUB procedure.
3) There is no priority between interrupts, interrupt nesting is supported, multiple interrupts can be executed at the same time, and the number of interrupt functions processed at the same time should not be too many.
The controller has only one task handling all interrupt signal responses, with a fixed interrupt task number. If there are too many interrupt handlers and their code is too long, it will slow down all interrupt responses or even cause interrupt blocking, affecting the execution of other interrupts.
Solution:
1) Minimize the number of interrupts; many applications can be handled by using cyclic scanning.
2) If an interrupt handler is particularly long, call a separate task to handle the complex tasks within the interrupt, so as not to block other interrupt responses.
The following is a routine for starting a task in an interrupt. The large block of code in Timer Interrupt 0 is run as Task 3. At this time, both Timer Interrupt 0 and Timer Interrupt 1 can be triggered normally, and there is no interrupt blocking.
Example:
INT_ENABLE= 1 'Enable interrupts
TIMER_START(0,1000) 'Start Timer 0'
TIMER_START(1,1100) 'Start Timer 1'
END
GLOBAL SUB ONTIMER0() 'Interrupt handling function
RUNTASK 3, MyIntHandler() 'Start the task and run
END SUB
GLOBAL SUB MyIntHandler()
DELAY 1000 'Assuming a large amount of blocking code'
WAIT UNTIL IN(0) <> 0
"The first interruption"
END SUB
GLOBAL SUB ONTIMER1() 'Interrupt handling function
"Second interruption"
END SUB
8. Interrupt Demonstration Example (Basic)
'Main Program'
BASE(0,1)
ATYPE=1,1 'Set as pulse axis type
UNITS = 100,100
DPOS = 0,0
SPEED= 100,100
ACCEL= 1000,1000
DECEL= 1000,1000
SRAMP = 100,100
INT_ENABLE= 1 'Enable the master interrupt switch
TIMER_START(2,1000) 'Starts timer 2 , executes once every 1000 ms.
DPOS( 0 ) = VR( 0 ) 'Read the saved values upon power-on and restore the coordinates.
DPOS(1) = VR(1)
PRINT "dpos(0),dpos(1)",dpos(0),dpos(1)
WHILE 1
IF SCAN_EVENT(IN(0))>0 THEN
TRIGGERBASE(0,1)
MOVEABS(0,0)
MOVECIRC2(100,100,200,0) 'Draw a semicircle with three points'
ENDIF
WEND 'Main program ended'
Interrupt routines are all global SUBs.
GLOBAL SUB ONTIMER2() 'Timer interrupt routine
PRINT "Timer Interrupt 2"
'TIMER_START( 2,1000 ) ' starts a timer in the sub function and executes it periodically via an interrupt.
END SUB
GLOBAL SUB INT_ON2 () 'External rising edge interrupt routine
PRINT "Input IN2 falling edge triggered..."
END SUB
GLOBAL SUB INT_OFF2 () 'External rising edge interrupt routine
PRINT "Input IN2, falling edge triggered..."
END SUB
GLOBAL SUB ONPOWEROFF () 'Power-off interrupt'
VR( 0 ) = DPOS( 0 ) 'Save coordinates to VR
VR(1) = DPOS(1)
END SUB
9. Interrupt Demonstration Program (PLC)
PLC statement table code:
//Main program X0 - Timer interrupt 0, X1 - Timer interrupt 1, X2 - Rising edge/falling edge interrupt, X3 - Disable all interrupts
LBL @MAIN
LD M8002
EI
EXE @ PRINT "VR(0)",VR(0)
LD X0
TMR T0 K2000
LD T0
OUT Y0
LD X1
ANI Y1
TMR T1 K3000
LD T1
OUT Y1
LD X3
DI
FEND
//External interrupt, controlled by the input port on the controller.
LBL @INT_ON2
LD M8000
EXE @ PRINT "External Interrupt 2 Rising Edge"
IRET
LBL @INT_OFF2
LD M8000
EXE @ PRINT "External Interrupt 2 Falling Edge"
IRET
//Timer 0 interrupt, interrupt enabled when timer reaches preset value.
LBL @ONTIMER0
LD M8000
EXE @ PRINT "Timer Interrupt 0"
IRET
//Timer 1 interrupt, interrupt enabled when timer reaches preset value.
LBL @ONTIMER1
LD M8000
EXE @ PRINT "Timer Interrupt 1"
IRET
//Power-down interrupt, this interrupt is executed when the controller is powered off.
LBL @ONPOWEROFF
LD M8000
MOV K12345 D0
EXE @ VR(0) = MODBUS_REG(0)
IRET
END
That concludes our discussion on the application of motion controller interrupts (power-down interrupt, external interrupt, timer interrupt) in positive motion technology. For more exciting content, please follow our official WeChat account.
This article was originally created by Zheng Motion Assistant. We welcome everyone to reprint it for mutual learning and to improve China's intelligent manufacturing capabilities. Copyright belongs to Zheng Motion Technology. Please indicate the source if you reprint this article.