Mikroelektronika MIKROE-724 データシート

ページ / 726
mikroBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
673
Library Example
Example demonstrates function cross calling using setjmp and longjmp functions. When called, Setjmp() saves its 
calling environment in its 
buf argument for later use by the Longjmp(). Longjmp(), on the other hand, restores the 
environment saved by the most recent invocation of the Setjmp() with the corresponding
 buf argument.
Copy Code To Clipboard 
program Setjmp
dim buf as word[4]             ‘ Note: Program flow diagrams are indexed according
                               ‘       to the sequence of execution
sub procedure func33()         ‘  2<------------|
  Delay_ms(1000)               ‘                |
                               ‘                |
  nop                          ‘                |
  longjmp(buf, 2)              ‘  3---------------->|
  nop                          ‘                |   |
                               ‘                |   |
end sub                        ‘                |   |
                               ‘                |   |
sub procedure func()           ‘  1<--------|   |   |
  PORTB = 3                    ‘            |   |   |
  if (setjmp(buf) = 2) then    ‘  3<----------------|
    PORTB = 1                  ‘  4-->|     |   |
  else                         ‘      |     |   |
    func33()                   ‘  2------------>|
  end if                       ‘      |     |
                               ‘  4<--|     |
end sub                        ‘  5----->|  |
                               ‘         |  |
main:                          ‘         |  |
  ADPCFG = 0xFFFF              ‘         |  |
                               ‘         |  |
  PORTB = 0                    ‘         |  |
  TRISB = 0                    ‘         |  |
                               ‘         |  |
  nop                          ‘         |  |
                               ‘         |  |
  func()                       ‘  1-------->|
                               ‘         |
  nop                          ‘  5<-----|
  Delay_ms(1000)
  PORTB = 0xFFFF
end.