$TITLE(INIT.A51) ; Module INIT.A51 Rev 1.0 $XREF ; By Frank Fritz -- Copyright 1997 (C) Frank Fritz $DEBUG ; $NOLIST ; switch off generation of the listing file $NOMOD51 ; switch off 8051 controller defaults and use... $INCLUDE (c:\fsi\inc\reg51.inc) ; Include the 8051 register definitions/declarations. $LIST ; switch on listing. STACK SEGMENT IDATA RSEG STACK DS 32H ; 50 Byte Stack ; ----- data_seg SEGMENT DATA ; Segment for DATA RAM. RSEG data_seg temperature: DS 1 ; Temperature data byte from DS1620. intr_count: DS 1 ; Interrupt Service counter byte. disp_val: DS 1 ; LED display value. ee_address: DS 1 ; Write-to, Read-from address of the EEPROM. (0 - 3FH : 0 - 63D) ee_lsb: DS 1 ; EEPROM LSB data byte. ee_msb: DS 1 ; EEPROM MSB data byte. ee_cntrl: DS 1 ; EEPROM Control byte (01=READ 02=WRITE 04=EWEN 08=EWDS). pwm_cntr: DS 1 ; The PWM cycle-time counter. error: DS 1 ; The PID SERVO error value. cv: DS 1 ; The Control Variable. duty_cycle: DS 1 ; PWM duty-cycle re-load value. i_old_h: DS 1 ; Old integrator high byte. i_old_l: DS 1 ; Old integrator low byte. active_pb: DS 1 ; The ACTIVE Port Bit (Only one port bit can be controlled at any given time). limit_cntr_lo: DS 1 ; Integrator timeout limit counter (low byte). limit_cntr_hi: DS 1 ; Integrator timeout limit counter (high byte). old_temp: DS 1 ; The previous temperature (old) value. test_pb: DS 1 ; Test Port-Bit variable for the AI algorithm. ai_old_temp: DS 1 ; Old temperature value retained in MAIN. h_list_dptr: DS 1 ; Heat-List data pointer (offset). c_list_dptr: DS 1 ; Cool-List data pointer (offset). ai_cntr_hi: DS 1 ; Timeout counter hi byte for AI stability test. ai_cntr_lo: DS 1 ; Timeout counter lo byte for AI stability test. ; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- xdata_seg SEGMENT XDATA ; Segment for XDATA (RAM). RSEG XDATA_SEG ORG 00H h_list: DS 8 c_list: DS 8 ; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- bit_seg SEGMENT BIT ; Segment for BIT RAM. RSEG bit_seg on_targ_flag: DBIT 1 ; PID on-target (deadband) flag. pid_error_flag: DBIT 1 ; PID Integrator time-out error flag. limit_flag: DBIT 1 ; Integrator upper/lower limit flag. DS1620_FLAG: DBIT 1 ; Function control flag for Dallas DS1620 sensor. eesv_flag: DBIT 1 ; Service the EEPROM (1=service the device/in service, 0=device ready). neg_flag: DBIT 1 ; Negative math operation flag (1=operation negative) while_flag1: DBIT 1 ; Timeout while flag 1. mode: DBIT 1 ; Control mode of operation (0=heat, 1=cool). while_dtt_flag: DBIT 1 ; Flag to indicate when valid temperature has been acquired by DTT. ai_tl_flag: DBIT 1 ; Flag to indicate AI Training / Learning in progress. (1=in progress, 0=normal). error_flag: DBIT 1 ; Flag to signify an AI error. bit_defined_flag: DBIT 1 ; Flag to control flow of AI search algorithm if a port bit already exists. test_bit: DBIT 1 ; Debug test bit. ; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- TL0_val SET 0AFH ; Interrupt timer 0 reload values. (3CAFH = 15535D. 65535 - 15535 = 50000) TH0_val SET 03CH ; - (50000 / 1,000,000 = 0.05s interrupt) TL1_val SET 0C7H ; Interrupt timer 1 reload values. (FEC7H = 65223D. 65535 -65223 = 312) TH1_val SET 0FEH ; - (312 / 1,000,000 = 0.000312s interrupt = 25Hz) intr_service SET 10D ; Interrupt device service count (10D. 10 * 0.05s = 0.5 sec.) read_ee SET 01H ; Define EEPROM function control codes. write_ee SET 02H ewen SET 04H ewds SET 08H ; PID Control variables. setpoint SET 04EH ; Setpoint value. gain_p SET 076H ; Proportional Gain value. gain_i SET 032H ; Integral Gain value. deadband_val SET 001H ; Deadband range. limit_lo_val SET 0C0H ; Integrator timeout limit low byte value. Time = 960* 0.5 = 480s (8 min) limit_hi_val SET 03H ; Integrator timeout limit high byte value. ai_cntr_lo_val SET 0B0H ; AI stability timeout-reload value hi byte. Time = 1200*0.5 = 600s (10 min). ai_cntr_hi_val SET 04H ; AI stability timeout-reload value lo byte. ; ------------------------------------------------------------------- PUBLIC Declarations ------------------------------------------------------------------------------- PUBLIC temperature, intr_count, disp_val, ee_address, ee_lsb, ee_msb, ee_cntrl, pwm_cntr, error, cv, duty_cycle, i_old_h, i_old_l PUBLIC active_pb, limit_cntr_lo, limit_cntr_hi, old_temp, h_list_dptr, c_list_dptr, ai_cntr_hi, ai_cntr_lo, error_flag, bit_defined_flag PUBLIC test_pb, ai_old_temp PUBLIC h_list, c_list PUBLIC on_targ_flag, pid_error_flag, limit_flag, DS1620_FLAG, eesv_flag, neg_flag, while_flag1, mode, while_dtt_flag, ai_tl_flag PUBLIC error_flag, bit_defined_flag, test_bit PUBLIC TL0_val, TH0_val, TL1_val, TH1_val, intr_service, read_ee, write_ee, ewen, ewds, setpoint, gain_p, gain_i, deadband_val PUBLIC limit_lo_val, limit_hi_val, ai_cntr_hi_val, ai_cntr_lo_val PUBLIC while_ee, init EXTRN CODE (main, timer0int, timer1int) ; ---------------------------------------- Declare Interrept and Reset Code Vectors ------------------------------------------------------------------------------- $EJECT CSEG AT 00BH ; Set up TIMER 0 interrupt vector (0BH). LJMP timer0int ; Service TIMER 0 Interrupt on overflow. CSEG AT 01BH ; Set up TIMER 1 interrupt vector (1BH). LJMP timer1int ; Service TIMER 1 Interrupt on overflow. CSEG AT 0 ; absolute Code Segment at address 0 LJMP init ; install RESET-INITIALIZE location (jump to start) ; ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ; Module: INIT.A51 ; Revision: 1.0 ; Date: August 8, 1997 ; By: Frank Fritz ; About: The INIT module is the Initialization module that is executed after a power-on reset at the microprocessor. ; All variables are initialized, and the hardware configurations (internal interrupt timers) are established, the EEPROM ; is read, start-up conditions are determined, and the working heat and cool lists are updated in RAM. ; ; Program Flow ; Called From: Power-on reset (hardware) ; Input: Nothing ; Output: Configures interrupt timers, initializes all variables. ; Modifies: Nothing ; Calls: MAIN ; ; History: Date Comments ; ------------ -------------------------------------------------------------------------------------------------------------------------------- ; 08/08/97 Developed Rev 1.0 ; ; ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- INITIALIZE SEGMENT CODE RSEG INITIALIZE ; Switch to Initialization code segment USING 0 ; Use register set 0 Init: MOV SP,#STACK-1 ; Start here... Initialize the program, hardware, and system. CLR EA ; Disable interrupts. MOV TMOD,#11H ; Setup Timers 0 and 1 for 16 bit interrupt timer. MOV TL0,#TL0_val ; Timer 0 Low value. MOV TH0,#TH0_val ; Timer 0 High value. MOV TL1,#TL1_val ; Timer 1 Low value. MOV TH1,#TH1_val ; Timer 1 High value. SETB PT1 ; Set timer 1 interrupt to higher priority (over timer 0). MOV intr_count,#intr_service ; Set up the interrupt interval service timer byte. SETB ET0 ; Enable Timer 0 interrupt. SETB ET1 ; Enable Timer 1 interrupt. SETB EA ; Enable interrupts. ; Setup devices. CLR P3.1 ; Disable display. CLR P3.3 ; Disable Dallas DS1620. CLR P3.5 ; Disable the 93C46 EEPROM. CLR P3.0 ; Initialize Clock line. ; Initialize Port Bits. CLR P1.0 ; Set P1.0 to 0. (Bulb A) CLR P1.1 ; Set P1.1 to 0. (Fan A) CLR P1.2 ; Set P1.2 to 0. (Fan B) CLR P1.3 ; Set P1.3 to 0. (LED) CLR P1.4 ; Set P1.4 to 0. (Bulb B) SETB DS1620_FLAG ; Configure DS1620 function bit (1=convert, 0=read). CLR on_targ_flag ; Clear the on-target flag bit. CLR limit_flag ; Clear the Integrator wind-up limit flag. CLR eesv_flag ; Initialize EEPROM service bit to "device ready". CLR pid_error_flag ; Initialize error flag. CLR mode ; Initialize mode to 0 (heat). CLR ai_tl_flag ; Set AI_TL_Flag to normal operation. CLR error_flag ; Reset error flag (0 = no error). MOV limit_cntr_lo,#limit_lo_val ; Initialize limit timeout counter. MOV limit_cntr_hi,#limit_hi_val ; - MOV i_old_l,#00H ; Initialize Integrator count. MOV i_old_h,#00H ; - MOV pwm_cntr,#07FH ; Initialize cycle count. MOV active_pb,#00H ; Initialize active port bit to 0.. SETB TR0 ; Turn on (run) Timer 0. (Begin the interrupt cycle for peripherals). ; Get the AI instruction lists from the EEPROM. MOV R7,#04H ; R7 is loop counter. MOV ee_cntrl,#read_ee ; READ the EEPROM. MOV ee_address,#00H ; Set address. MOV DPTR,#h_list ; Initialize data pointer to HEAT List. read_ee_1: LCALL get_ee_data ; Read the EEPROM data. DJNZ R7,read_ee_1 ; If not done, loop back. MOV R7,#04H ; R7 is loop counter. MOV DPTR,#c_list ; Set DPTR to COOL List. read_ee_2: LCALL get_ee_data ; Read the EEPROM data. DJNZ R7, read_ee_2 ; Get all data. LCALL while_dtt ; Determine start-up conditions (heat or cool). MOV old_temp,temperature ; Update the old temperature value. CLR C ; - MOV A,#setpoint ; Operation: Setpoint - PV = Error. Sign of error indicates heat (0) or cool (1). SUBB A,temperature ; - MOV mode,C ; Mode bit now set to heat or cool. MOV h_list_dptr, #01H ; Initialize h_list data pointer. MOV c_list_dptr, #01H ; Initialize c_list data pointer. JC set_cool ; Initialize active port bit. set_heat: MOV DPTR,#h_list ; Get active bit from heat list. MOV A,h_list_dptr ; - SJMP set_ok ; Resume. set_cool: MOV DPTR,#c_list ; Get active bit from cool list. MOV A,c_list_dptr ; - set_ok: ADD A,DPL ; - MOV DPL,A ; Set address. MOVX A,@DPTR ; Read list byte. MOV active_pb,A ; Active bit now set from proper list (H or C). SETB TR1 ; Turn on (run) Timer 1. (Begin the interrupt cycle for the PWM function). LJMP main ; Program flow to MAINLINE routine forever. ; --------------------------------------------------------------------------- Subroutines ------------------------------------------------------------------------------------ get_ee_data: LCALL while_ee ; Wait until EE serviced. MOV A,ee_msb ; Put EEPROM MSB byte in Acc. MOVX @DPTR,A ; Write EEPROM data to RAM list. INC DPTR ; Point to next RAM Location. MOV A,ee_lsb ; Put EEPROM LSB in Acc. MOVX @DPTR,A ; Write EEPROM data to RAM list. INC DPTR ; Next byte. INC ee_address ; Point to next EEPROM address. RET ; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ while_dtt: SETB while_dtt_flag ; Set busy-bit. while_dtt0: JB while_dtt_flag, while_dtt0 RET ; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ while_wf: SETB while_flag1 ; Set busy-bit. while_wf0: JB while_flag1, while_wf0 RET ; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ while_ee: SETB eesv_flag ; Set busy-bit. while_ee0: JB eesv_flag,while_ee0 RET ; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ END ; End of module.