Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- { =====================================================================
- Main MICROBALANCE Application Load file
- Copyright (c) 2000, LucidTek
- This file initializes the ports for the Microbalance Application
- PORT D is used to drive and sense the opto-interupter
- the PWM output PD5(OC1A) is used to drive the galvometer.
- The galvo is mirror driven by PD5 with ext. current limiting.
- PD0 RXD \ UART Receive data
- PD1 TXD \ UART Transmit data
- PD2 SENSOR E \ Sensor phototransistor Emitter lead
- PD3 SENSOR C \ Sensor phototransistor Collector lead
- PD4 Coil-
- PD5 Coil+
- PD6 EMITTER K
- PD7 EMITTER A
- ===================================================================== }
- INCLUDE EEMAP \ EEPROM data
- INCLUDE LCDdrv \ LCD drivers
- INCLUDE DISPLAY \ MODIFIED LCD DRIVER SETUP
- { =================================================================== }
- \ Configure STK200 board for GALVO based Microbalance 4/29/00
- : INIT-PORTS ( -- )
- $FF DDRA C! \ PORTA Output to drive LCD
- $CC DDRB C! \ PORTB KEYPAD Scan/Read bits, SPI
- $F7 DDRC C! \ PORTC Ext brd select, LCD-RS,EN,SPKR
- $FA DDRD C!
- $00 PORTA C! \ Set LCF data to 0's
- $FF PORTB C! \ PULL UP PB5..PB0 MOSI, MISO, Keypad
- $00 PORTC C! \ LCD, SPKR, Camera brds
- $37 PORTD C!
- $A3 TCCR1A C!
- $01 TCCR1B C!
- ;
- $08 EQU TARE-SW
- 500 EQU T-GALVO \ Tare Switch bit position
- VARIABLE GALVO
- VARIABLE SPEED
- VARIABLE TARE \ must be followed by ERROR for proper init
- VARIABLE ERROR
- CVARIABLE EXIT-CNT
- INCLUDE SYS-CONFIG \ SYSTEM CONFIGURATION SETUP
- : PRINT-GALVO ( -- )
- 0 0 AT-XY GALVO @ . \ "." Displays result fetched from GALVO at LCD position 0,0
- 6 0 AT-XY ." micrograms" \ position LCD and show message
- ;
- : ?TARE ( -- f ) \ TRUE if TARE button (PD3) down, momentarily reconfig PORT
- PORTD C@ DDRD C@ OVER OVER $F7 AND DDRD C! \ OPEN OPTO Sensor Emitter
- TARE-SW OR PORTD C! \ TURN ON PULL UP RESISTOR AT INPUT
- PIND C@ TARE-SW AND NOT \ IF FALSE ELSE TRUE THEN
- SWAP DDRD C! SWAP PORTD C! \ Put the PORT connections back where they were
- ;
- : TARE-GALVO ( -- )
- ?TARE IF
- DUP GALVO @ TARE ! ( SAR ) THEN \ Test Tare Switch, Update& SAR Tare IF ?
- GALVO @ TARE @ - \ Prepare Tared Difference
- 0 1 AT-XY 6 SPACES 0 1 AT-XY . \ CLEAN UP DISPLAY AND PRINT TARE Value
- 6 1 AT-XY ." Tare --PD3"
- ;
- : SERIAL-DATA-OUT
- SERIAL-IO \ select the serial IO for printing etc.
- GALVO @ . \ Print the GALVO result to the serial IO
- <CR> (S-EMIT) \ with <CR> to end data value for EXCEL column data entry
- ?TARE IF \ CHECK if TARE down repeately, means send <ESC> to stop data acq
- 1 EXIT-CNT C+! \ TARE is down this time
- EXIT-CNT C@ 2 > IF
- 0 EXIT-CNT C! <ESC> (S-EMIT) \ it was down enought
- THEN
- ELSE
- 0 EXIT-CNT C! THEN
- LCD-TERMINAL \ back to using the LCD for printing data & messages
- ;
- : >PWM ( n -- ) \ n is the value to store in PWM register
- 1023 MIN 0 MAX \ Hard Limit VALUE before PWM Update
- DUP $FF AND SWAP 8 RSHIFT \ position n's bits for PWM Hi Lo
- OCR1AH C! \ transfer data to PWM registers
- OCR1AL C!
- ;
- : ?BEAM ( -- f ) \ Test if light beam can pass TRUE means beam on
- PORTD C@ $3F AND $80 OR PORTD C! 75 MS \ Turn ON OPTO LED only and wait
- PIND C@ 4 AND NOT \ then test SENSOR bit, it's neg logic
- ;
- : SHOW-BEAM ( -- ) \ SHOW THE RESULT OF ?BEAM TEST
- ?BEAM IF
- PORTB C@ $7F AND PORTB C! ELSE \ TURN LED OFF
- PORTB C@ $80 OR PORTB C! THEN \ TURN ON LED=BEAM, DO NOT DISTURB OTHERS
- ;
- : SAR ( -- ) \ 10 BIT Sucessive approximation using stack recursion algo
- 0 GALVO ! \ Initialize to all bits OFF
- 10 0 DO GALVO @ \ Using the current approximation, turn on
- 1 9 I - LSHIFT OR DUP \ sucessively lower bit positions, starting at MSB
- >PWM 50 MS \ and try the new value on the galvo motor, see if lifts the needle
- ?BEAM IF \ If the beam is ON, still too high, drop this trial value
- DROP ELSE \ othewise beam is blocked, save this trial value
- GALVO ! THEN \ continue this way using smaller and smaller trails
- LOOP \ Iterate for each bit position
- ;
- : UPDATE-GALVO \ simple PID loop update ... I only for example
- ?BEAM IF
- 1 ERROR +! -1 ELSE
- -1 ERROR +! 1 THEN
- GALVO @ 20 * 10 + ERROR @ - 20 / +
- DUP 0 MAX 1023 MIN GALVO ! >PWM
- ;
- |U| |S| |R| BACKGROUND electroBAL
- \ This is the microbalance background task
- : /BALANCE \ Bring up the electro balance application
- INIT-SYS \ Initialize the system and variables
- electroBAL BUILD \ Build and Activate this task
- electroBAL ACTIVATE \ which runs forever in the background
- INIT-TASK \ final initializations
- PAGE \ Clear LCD page
- ." Power-On Test OK" \ If you can read this Power-up Message, system is OK
- 1000 MS \ pause so the message can be read
- PAGE \ clear the LCD page ( 2 lines in this case)
- 0 MODE C! \ clear the engineering mode flag
- SAR GALVO @ >PWM \ Quickly acquire using sucessive approximation
- BEGIN \ This is the BEGINing of the Infinite measurement loop
- MODE C@ NOT IF \ IF engr mode non-zero, disable the Loop for debug, otherwise
- UPDATE-GALVO \ update using your favorite algorithm, results in GALVO
- PRINT-GALVO \ Print the value in GALVO at the right place, with "~units"
- TARE-GALVO
- SERIAL-DATA-OUT
- THEN
- SHOW-BEAM \ Show ?BEAM test results on the LEDs at PORTB.7
- SPEED @ MS \ idle here so speed of loop is not too fast
- AGAIN \ where the infinite loop returns to the BEGINing
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement