mavey80

Untitled

Sep 8th, 2011
123
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. { =====================================================================
  2. Main MICROBALANCE Application Load file
  3.  
  4. Copyright (c) 2000, LucidTek
  5.  
  6. This file initializes the ports for the Microbalance Application
  7. PORT D is used to drive and sense the opto-interupter
  8. the PWM output PD5(OC1A) is used to drive the galvometer.
  9. The galvo is mirror driven by PD5 with ext. current limiting.
  10. PD0 RXD \ UART Receive data
  11. PD1 TXD \ UART Transmit data
  12. PD2 SENSOR E \ Sensor phototransistor Emitter lead
  13. PD3 SENSOR C \ Sensor phototransistor Collector lead
  14. PD4 Coil-
  15. PD5 Coil+
  16. PD6 EMITTER K
  17. PD7 EMITTER A
  18. ===================================================================== }
  19.  
  20. INCLUDE EEMAP \ EEPROM data
  21. INCLUDE LCDdrv \ LCD drivers
  22. INCLUDE DISPLAY \ MODIFIED LCD DRIVER SETUP
  23.  
  24. { =================================================================== }
  25. \ Configure STK200 board for GALVO based Microbalance 4/29/00
  26. : INIT-PORTS ( -- )
  27. $FF DDRA C! \ PORTA Output to drive LCD
  28. $CC DDRB C! \ PORTB KEYPAD Scan/Read bits, SPI
  29. $F7 DDRC C! \ PORTC Ext brd select, LCD-RS,EN,SPKR
  30. $FA DDRD C!
  31.  
  32. $00 PORTA C! \ Set LCF data to 0's
  33. $FF PORTB C! \ PULL UP PB5..PB0 MOSI, MISO, Keypad
  34. $00 PORTC C! \ LCD, SPKR, Camera brds
  35. $37 PORTD C!
  36.  
  37. $A3 TCCR1A C!
  38. $01 TCCR1B C!
  39. ;
  40.  
  41. $08 EQU TARE-SW
  42. 500 EQU T-GALVO \ Tare Switch bit position
  43.  
  44. VARIABLE GALVO
  45. VARIABLE SPEED
  46. VARIABLE TARE \ must be followed by ERROR for proper init
  47. VARIABLE ERROR
  48. CVARIABLE EXIT-CNT
  49.  
  50. INCLUDE SYS-CONFIG \ SYSTEM CONFIGURATION SETUP
  51.  
  52. : PRINT-GALVO ( -- )
  53. 0 0 AT-XY GALVO @ . \ "." Displays result fetched from GALVO at LCD position 0,0
  54. 6 0 AT-XY ." micrograms" \ position LCD and show message
  55. ;
  56.  
  57. : ?TARE ( -- f ) \ TRUE if TARE button (PD3) down, momentarily reconfig PORT
  58. PORTD C@ DDRD C@ OVER OVER $F7 AND DDRD C! \ OPEN OPTO Sensor Emitter
  59. TARE-SW OR PORTD C! \ TURN ON PULL UP RESISTOR AT INPUT
  60. PIND C@ TARE-SW AND NOT \ IF FALSE ELSE TRUE THEN
  61. SWAP DDRD C! SWAP PORTD C! \ Put the PORT connections back where they were
  62. ;
  63.  
  64. : TARE-GALVO ( -- )
  65. ?TARE IF
  66. DUP GALVO @ TARE ! ( SAR ) THEN \ Test Tare Switch, Update& SAR Tare IF ?
  67. GALVO @ TARE @ - \ Prepare Tared Difference
  68. 0 1 AT-XY 6 SPACES 0 1 AT-XY . \ CLEAN UP DISPLAY AND PRINT TARE Value
  69. 6 1 AT-XY ." Tare --PD3"
  70. ;
  71.  
  72. : SERIAL-DATA-OUT
  73. SERIAL-IO \ select the serial IO for printing etc.
  74. GALVO @ . \ Print the GALVO result to the serial IO
  75. <CR> (S-EMIT) \ with <CR> to end data value for EXCEL column data entry
  76. ?TARE IF \ CHECK if TARE down repeately, means send <ESC> to stop data acq
  77. 1 EXIT-CNT C+! \ TARE is down this time
  78. EXIT-CNT C@ 2 > IF
  79. 0 EXIT-CNT C! <ESC> (S-EMIT) \ it was down enought
  80. THEN
  81. ELSE
  82. 0 EXIT-CNT C! THEN
  83. LCD-TERMINAL \ back to using the LCD for printing data & messages
  84. ;
  85.  
  86. : >PWM ( n -- ) \ n is the value to store in PWM register
  87. 1023 MIN 0 MAX \ Hard Limit VALUE before PWM Update
  88. DUP $FF AND SWAP 8 RSHIFT \ position n's bits for PWM Hi Lo
  89. OCR1AH C! \ transfer data to PWM registers
  90. OCR1AL C!
  91. ;
  92.  
  93. : ?BEAM ( -- f ) \ Test if light beam can pass TRUE means beam on
  94. PORTD C@ $3F AND $80 OR PORTD C! 75 MS \ Turn ON OPTO LED only and wait
  95. PIND C@ 4 AND NOT \ then test SENSOR bit, it's neg logic
  96. ;
  97.  
  98. : SHOW-BEAM ( -- ) \ SHOW THE RESULT OF ?BEAM TEST
  99. ?BEAM IF
  100. PORTB C@ $7F AND PORTB C! ELSE \ TURN LED OFF
  101. PORTB C@ $80 OR PORTB C! THEN \ TURN ON LED=BEAM, DO NOT DISTURB OTHERS
  102. ;
  103.  
  104. : SAR ( -- ) \ 10 BIT Sucessive approximation using stack recursion algo
  105. 0 GALVO ! \ Initialize to all bits OFF
  106. 10 0 DO GALVO @ \ Using the current approximation, turn on
  107. 1 9 I - LSHIFT OR DUP \ sucessively lower bit positions, starting at MSB
  108. >PWM 50 MS \ and try the new value on the galvo motor, see if lifts the needle
  109. ?BEAM IF \ If the beam is ON, still too high, drop this trial value
  110. DROP ELSE \ othewise beam is blocked, save this trial value
  111. GALVO ! THEN \ continue this way using smaller and smaller trails
  112. LOOP \ Iterate for each bit position
  113. ;
  114.  
  115. : UPDATE-GALVO \ simple PID loop update ... I only for example
  116. ?BEAM IF
  117. 1 ERROR +! -1 ELSE
  118. -1 ERROR +! 1 THEN
  119. GALVO @ 20 * 10 + ERROR @ - 20 / +
  120. DUP 0 MAX 1023 MIN GALVO ! >PWM
  121. ;
  122.  
  123. |U| |S| |R| BACKGROUND electroBAL
  124.  
  125. \ This is the microbalance background task
  126.  
  127. : /BALANCE \ Bring up the electro balance application
  128. INIT-SYS \ Initialize the system and variables
  129. electroBAL BUILD \ Build and Activate this task
  130. electroBAL ACTIVATE \ which runs forever in the background
  131. INIT-TASK \ final initializations
  132.  
  133. PAGE \ Clear LCD page
  134. ." Power-On Test OK" \ If you can read this Power-up Message, system is OK
  135. 1000 MS \ pause so the message can be read
  136.  
  137. PAGE \ clear the LCD page ( 2 lines in this case)
  138. 0 MODE C! \ clear the engineering mode flag
  139.  
  140. SAR GALVO @ >PWM \ Quickly acquire using sucessive approximation
  141.  
  142. BEGIN \ This is the BEGINing of the Infinite measurement loop
  143. MODE C@ NOT IF \ IF engr mode non-zero, disable the Loop for debug, otherwise
  144. UPDATE-GALVO \ update using your favorite algorithm, results in GALVO
  145. PRINT-GALVO \ Print the value in GALVO at the right place, with "~units"
  146. TARE-GALVO
  147. SERIAL-DATA-OUT
  148. THEN
  149. SHOW-BEAM \ Show ?BEAM test results on the LEDs at PORTB.7
  150. SPEED @ MS \ idle here so speed of loop is not too fast
  151. AGAIN \ where the infinite loop returns to the BEGINing
  152. ;
RAW Paste Data