CHƯƠNG III: MÔ PHỎNG HỆ THỐNG III.1. Thiết kế mạch nguyên lí
III.3. Chạy mô phỏng chương trình bằng phần mềm Proteus
III.3.1 Chương trình hợp ngữ
#include <p18f4520.inc>
CONFIG WDT=OFF; watch dog timer off
CONFIG BOREN=OFF; reset when power supply lower than level required CONFIG OSC=HS
CONFIG LVP=OFF CONFIG MCLRE=ON
CONFIG PWRT = OFF ;Power-up Timer Enable bit (PWRT disabled) CBLOCK 0x20
DVDL, DVDH, DVSL, DVSH, QUL, QUH, REL, REH, ; valuable for division
ARG1H, ARG1L, ARG2, RES2, RES1, RES0, ;valuable for multiply start at 0X28
TDENL, TDENH, RNDL, RNDH, ;valuable for handling numerator, start at 0x2E
RESULTL, RESULTH, DEC, ; Final value,
start at 0x32
COUNT, COUNT1, CHAR_COUNT ; DELAY , start at 0x35 HUND, TEN, UNIT ; LCD display, start at 0x38
LIML, LIMH ; Limitation, start at 0x3B ENDC
#DEFINE RS PORTC,0
#DEFINE RW PORTC,1
#DEFINE E PORTC,2 ORG 0X000
MAIN
CLRF TRISC CLRF TRISD CLRF TRISB
CALL INIT_ADC CALL START_ADC CALL PRE_LINE
CALL XU_LI_PT ; TINH TOAN CALL XU_LI_KQ ; TACH SO
CALL HIEN_THI_LCD ; XUAT KQ SANG LCD
CALL KT_NGUONG ; KIEM TRA NGUONG BAT TAT DEN CALL LED_STATUS; HIEN THI TRANG THAI DEN
GOTO MAIN INIT_ADC
CLRF ADRESL CLRF ADRESH MOVLW 0XFF MOVWF TRISA MOVLW 0X01
MOVWF ADCON0; AD On and use channel 0 MOVLW 0X0E
MOVWF ADCON1 MOVLW 0XA1 MOVWF ADCON2 RETURN
START_ADC BSF ADCON0,1 WAIT_CON
BTFSC ADCON0,1
BRA WAIT_CON RETURN
INIT_LCD MOVLW 0X38 CALL SEND_CMD MOVLW 0X0C CALL SEND_CMD RETURN
SEND_CMD BCF RS
MOVWF PORTD CALL ALLOW CALL DELAY RETURN ALLOW
BSF E NOP BCF E RETURN
SEND_DATA BSF RS
CALL ALLOW CALL DELAY RETURN
PRE_LINE MOVLW 0X80 CALL SEND_CMD MOVLW 'L'
CALL SEND_DATA MOVLW 'U'
CALL SEND_DATA MOVLW 'X'
CALL SEND_DATA MOVLW ':'
CALL SEND_DATA MOVLW 0XC0 CALL SEND_CMD MOVLW 'L'
CALL SEND_DATA MOVLW 'I'
CALL SEND_DATA MOVLW 'G'
CALL SEND_DATA MOVLW 'H'
CALL SEND_DATA
MOVLW 'T'
CALL SEND_DATA MOVLW ':'
CALL SEND_DATA RETURN
DIV ;==CHUONG TRINH CHIA 2 SO 16
BIT====;
MOVLW 0XFE CPFSGT QUL GOTO SUBL1 INCF QUH,F SUBL1
INCF QUL MOVF DVSL,W SUBWF DVDL, F BTFSS STATUS,C DECF DVDH,F SUBH2
MOVF DVSH,W SUBWF DVDH,F BNN DIV
DECF QUL,F MOVLW D'1'
REMAINDER MOVF DVDL,W ADDWF DVSL,W MOVWF REL BTFSC STATUS,C INCF DVDH,F MOVF DVSH,W ADDWF DVDH,W MOVWF REH RETURN
;===CHUONG TRINH NHAN SO 16 BIT VOI 10 ===;
MUL_10
MOVF ARG1L,W
MULWF ARG2 ;ARG1L*ARG2
MOVFF PRODH, RES1 MOVFF PRODL, RES0 MOVF ARG1H,W
MULWF ARG2 ;ARG1H*ARG2
MOVF PRODL,W ADDWF RES1,F CLRF WREG
ADDWFC PRODH, F MOVFF PRODH, RES2 RETURN
;=========XU LI PHUONG TRINH=======;
;=========XU LI MAU==========;
MAU_SO ;5115/Vadc-5
CALL CLEAR_DIV
;TRUYEN GIA TRI CHO BIEN MOVLW 0X13
MOVWF DVDH MOVLW 0XFB MOVWF DVDL MOVF ADRESL,W MOVWF DVSL MOVF ADRESH,W MOVWF DVSH
; BAT DAU CHIA VÀ LUU KET QUA CALL DIV
MOVFF QUL,TDENL MOVFF QUH,TDENH MOVFF REH, RNDH MOVFF REL, RNDL
; LAM TRON KET QUA CUA 5115/VADC CALL CLEAR_MUL
MOVFF RNDH, ARG1H MOVFF RNDL, ARG1L MOVLW 0X0A
MOVWF ARG2
CALL CLEAR_DIV MOVFF RES1,DVDH MOVFF RES0,DVDL MOVF ADRESL,W MOVWF DVSL MOVF ADRESH,W MOVWF DVSH CALL DIV
MOVLW 0X05 ; NEU PHAN THAP PHAN LON HON HOAC BANG 5 THÌ TANG KET QUA LEN 1, SAU DO LAY 5115/Vadc -5
CPFSLT QUL CALL TANG1 MOVLW 0X05 SUBWF TDENL,F CLRF WREG
SUBWFB TDENH,F RETURN
;==== LAY 500/ (5115/Vadc-5) ===;
XU_LI_PT
CALL MAU_SO CALL CLEAR_DIV MOVLW 0X01 MOVWF DVDH MOVLW 0XF4 MOVWF DVDL
MOVFF TDENL,DVSL
MOVFF TDENH, DVSH CALL DIV
MOVFF QUL, RESULTL MOVFF QUH, RESULTH
CALL CLEAR_MUL ; LAY PHAN THAP PHAN MOVFF REH, ARG1H
MOVFF REL, ARG1L MOVLW 0X0A
MOVWF ARG2
CALL MUL_10 ; SO DU NHAN 10
CALL CLEAR_DIV ; CHIA CHO MAU SO MOVFF RES1, DVDH
MOVFF RES0, DVDL MOVFF TDENL, DVSL MOVFF TDENH, DVSH CALL DIV
MOVFF QUL, DEC ; LAY PHAN THAP PHAN RETURN
XU_LI_KQ
CALL CLEAR_DIV ; TACH SO HANG TRAM MOVFF RESULTL, DVDL
MOVFF RESULTH, DVDH MOVLW 0X00
MOVLW 0X64 MOVWF DVSL CALL DIV
MOVFF QUL, HUND
MOVFF REH, DVDH ; TACH SO HANG CHUC MOVFF REL, DVDL
MOVLW 0X00 MOVWF DVSH MOVLW 0X0A MOVWF DVSL CALL CLEAR_DIV CALL DIV
MOVFF QUL, TEN
MOVFF REL, UNIT ; TACH HANG DON VI RETURN
HIEN_THI_LCD MOVLW 0X30 ADDWF HUND,F ADDWF TEN,F ADDWF UNIT,F ADDWF DEC,F MOVLW 0X85 CALL SEND_CMD MOVF HUND,W
CALL SEND_DATA MOVF TEN,W CALL SEND_DATA MOVF UNIT,W CALL SEND_DATA MOVLW '.'
CALL SEND_DATA MOVF DEC,W CALL SEND_DATA RETURN
KT_NGUONG ; GIA SU NGUONG LA 100 LUX MOVLW 0X00
MOVWF LIMH MOVLW 0X64 MOVWF LIML
SUBWF RESULTL,W MOVF LIMH,W
SUBWFB RESULTH,W BC TAT
BAT BSF PORTB,7 GOTO OUT TAT BCF PORTB,7 OUT RETURN
; HIEN THI TRANG THAI DEN TREN LCD MOVLW 0XC7
CALL SEND_CMD BTFSS PORTB,7 GOTO OFF ON MOVLW 'O'
CALL SEND_DATA MOVLW 'N'
CALL SEND_DATA MOVLW ' '
CALL SEND_DATA GOTO RET
OFF MOVLW 'O' CALL SEND_DATA MOVLW 'F'
CALL SEND_DATA MOVLW 'F'
CALL SEND_DATA RET
RETURN
TANG1
INCF TDENL,F CLRF WREG
ADDWFC TDENH,F
RETURN
CLEAR_DIV; XOA KET QUA PHEP CHIA CLRF QUL
CLRF QUH CLRF REL CLRF REH RETURN
CLEAR_MUL ; XOA KQ PHEP NHAN CLRF ARG2
CLRF ARG1L CLRF ARG1H CLRF RES2 CLRF RES1 CLRF RES0 RETURN DELAY
MOVLW 0XFF MOVWF COUNT DEL
MOVLW 0XFF MOVWF COUNT1 DECFSZ COUNT1,1 GOTO $-2
DECFSZ COUNT,1
RETURN END