'DS1620.TIG

'This program illustrates how to make measurmements using the DS1620. 
'
'The DS1620 is first configured and then 100 measurments are made and 
'displayed on an LCD panel.

'Coyright, Krystal Donald, Morgan State University, Oct 21, '97

'DATA = 80  PORT 8 PIN 0
'CLK  = 81
'RST  = 82

   WORD I_9_BIT                         'GLOBAL VARIBLES
   BYTE I_9_BIT2
   WORD I_9_BIT1
   BYTE OLD          
   BYTE VARI
   BYTE CLK_HI
   
Task Main				'STARTS MAIN    
   VARI = 00000100B                     'SETS THE RESET PIN HI
   INSTALL DEVICE #1, "LCD1.TDD"        'INITIALIZES THE LCD
   RUN_TASK config_1620                 'CALL TASK
   WAIT_DURATION 200                    'DELAY
   RUN_TASK start_convert               'CALL TASK
   WAIT_DURATION 1000                   'DELAY
   for times = 1 to 100                 'FOR LOOP
      RUN_TASK meas                     'CALL TASK
      T_C = i_9_bit/2                   'CALC CELSIUS TEMP
        PRINT #1, "TC=", T_C            'PRINTS TEMP
   WAIT_DURATION 9000                   'DELAY
   NEXT
   RUN_TASK stop_convert                'CALL TASK
END                                     'END FUNCTION

TASK config_1620
	DIR_PORT 8,0                    'SETS PORT 8 TO OUTPUT
        LL_IPORT_out 8,00000000b        'SETS EVERYTHING LOW
        LL_IPORT_out 8,00000010b        'CLK HIGH
        LL_IPORT_out 8,00000110b        'RST AND CLK GIGH
        
        OLD = 00001100b                 'COMMAND FOR 1620   
        call out_B(OLD)                 'CALL TO OUPUT ROUTINE
        OLD = 00000010b  
        call out_B(OLD)
        LL_IPORT_out 8, 00000000b       'SETS EVERYTHING LOW
        wait_duration 200               'DELAY
END                                     'END FUNCTION

SUB OUT_B(VAR BYTE OLD)                 'BEGIN ROUTINE FOR OUTPUT
	BYTE OLD2                       'DECLARES VARIBLES FOR FUNCTION
        BYTE OLD1
        BYTE CLK_HI
        DIR_PORT 8,0                    'MAKES PORT 8 OUPUT
        OLD2 = OLD                      'SETS OLD2 EQAUL TO OLD
        FOR TIMES = 1 TO 8              'LOOP 8 TIMES
           OLD1 =OLD BITAND 1           'THE FOLLOWING LINES ARE USED TO
           OLD = OLD1 BITOR VARI        'SINGLE OUT EACH BIT FOR OUTPUT
           CLK_HI= OLD BITOR 00000010B  'EACH BIT IS THEN CLOCKED OUT 
           LL_IPORT_OUT 8,OLD
           LL_IPORT_OUT 8,OLD
           LL_IPORT_OUT 8,CLK_HI
           OLD =  OLD2 SHR TIMES       
        NEXT                            
END                                     'END ROUTINE
                     
TASK start_convert                      'START CONVERSION ROUTINE
	DIR_PORT 8,0                    'SETS PORT 8 TO AN OUTPUT
        LL_IPORT_OUT 8, 00000010b       'SETS CLK HIGH
        LL_IPORT_out 8, 00000110b       'SETS RST AND CLK HIGH
        OLD = 11101110b                 'COMMAND FOR CHIP
        call out_B(OLD)                 'CALL TO FUNCTION OUT
        LL_IPORT_out 8, 00000000b       'SETS EVERYTHING TO 0
END                                     'END FUNCTION

TASK meas     				'STARTS FUNCTION
	LL_IPORT_out 8, 00000010b       'CLK HIGH
        LL_IPORT_out 8, 00000110b       'CLK AND RST HIGH
        OLD = 10101010b                 'COMMAND FOR CHIP
        call out_B(OLD)                 'CALL OUTPUT FUNCTION
        call GET_9_bits()               'CALL GET 9 FUNCTION
        LL_IPORT_out 8,00000010         'SETS CLK HIGH RESET LOW
END                                     'END FUNCTION

SUB GET_9_BITS                            'START FUNCTION
	DIR_PIN 8,0,1                     'PIN 0 INPUT
        DIR_PIN 8,1,0                     'PIN 1 OUTPUT
        DIR_PIN 8,2,0                     'PIN 2 OUTPUT
        I_9_BIT =0                                  
        FOR TIMES = 0 TO 8                'STARTS LOOP
           OUT 8, 00000010B,2             'CLK HI
           OUT 8, 00000100B,4             'RST HI
           OUT 8, 00000010B,0             'CLK LO
           LL_IPORT_IN 8, I_9_BIT1, 00000001B    'INPORT AND STORE IN
I_9_BIT
           OUT 8,00000010B,2                     'CLK HI
              IF TIMES > 0 THEN                  'IF GREATER THAN O
                 I_9_BIT2 = I_9_BIT1 SHL TIMES   ' SHIFT TIMES PLACES
                 I_9_BIT1 = I_9_BIT2             'THEN SET TO I_9_BIT1
             ENDIF                               'END IF STATEMENT
           I_9_BIT = I_9_BIT BITOR I_9_BIT1      'OR AND SET EQUAL TO
I_9_BIT

        NEXT 
                                                 'END FUNCTION
END             
TASK stop_convert                                'START CONVERT FUNCTION
	LL_IPORT_out 8, 00000010b                'SET CLK HI
        LL_IPORT_out 8, 00000110b                'SET CLK AND RST HI
        OLD = 00100010b                          'COMMAND FOR CHIP
        call out_B(OLD)                          'CALL OUT FUNCTION
        LL_IPORT_out 8,00000000b                 'SET EVERYTHING LOW
end