51单片机基础篇 对于有一定基础的人看看 包含书中所以的源代码

源代码在线查看: ch16_b13-6.asm

软件大小: 5099 K
上传用户: ydyzd_2008
关键词: 51单片机 源代码
下载地址: 免注册下载 普通下载 VIP

相关代码

				;PROGRAM NAME B13-6.ASM
				BPS9600 EQU     FDH
				OUT_CNT EQU     8
				MS10    EQU     9217    ;APPROXIMATELY 10ms INTERRUPR
				;
				INTR_CNT EQU    30H     ;CNT STORIAGE
				BUFFER  EQU     31H     ;DISPLAY BUFFER
				BINARY  EQU     32H     ;BINARY DATA STORIAGE
				ASCBUF  EQU     33H     ;ASCII TEMPERATURE DATA BUFFER(8 BYTES)
				TX_CNT  EQU     3BH     ;TRANSMIT BYTE COUNT
				;
				CNTH_L  EQU     3CH
				CNTH_H  EQU     3DH     ;COUNTER FOR DUTY HI
				CNTT_L  EQU     3EH
				CNTT_H  EQU     3FH     ;COUNTER FOR DUTY HI+LO
				;
				EE_ID   EQU     2FH     ;ID READ FROM 24LC16
				BEEP    EQU     2EH     ;BUZZER ON TIME
				;
				SCLK    REG     P1.7    ;SERIAL CLOCK
				SDA     REG     P1.6    ;SERIAL DATA
				SMT160  REG     P3.2    ;TEMP SENSOR
				SCOPE   REG     P3.3    ;CHECK BY SCOPE
				BUZZER  REG     P3.4
				CHKPUL  REG     P3.5    ;CHECK BY SCOPE
				LED     REG     P3.7
				KEY     REG     P3.7
				;
				SAMPLING REG    20H.0
				TX_START REG    20H.1
				;
				        ORG     0000H
				        JMP     START
				        ORG     0003H   ;[DISABLE]  EXTERNAL INT0 INTERRUPT
				        RETI
				        ORG     000BH   ;[ENABLE]  TIMER0 OVERFLOW INTERRUPT
				        JMP     T0_ISR
				;
				        ORG     0013H
				        RETI            ;[DISABLE] EXTERNAL INT1 INTERRUPT
				        ORG     001BH
				        RETI            ;[DISABLE] TIMER1 OVERFLOW INTERRUPT
				        ORG     0023H
				        JMP     SIO_ISR ;[ENABLE] SERIAL INTERRUPT
				;
				        ORG     30H
				START   MOV     R0,#00H
				        DJNZ    R0,$    ;DELAY A WHILE
				        MOV     SP,#60H
				        MOV     P1,#FFH
				        MOV     P3,#FFH
				        MOV     INTR_CNT,#00H
				        CLR     TX_START
				        MOV     TX_CNT,#00H
				        CLR     BUZZER  ;P3.4=0,BUZZER OFF
				        CLR     LED     ;P3.7=0,LED OFF
				        CALL    SET_TMR0
				        MOV     BUFFER,#FFH ;BLANK DISPLAY
				        CLR     SAMPLING
				        CALL    CHECK_EE
				        CALL    READ_ID
				;
				$WAIT   JNB     SAMPLING,$WAIT
				        SETB    LED
				        CALL    READ_TEMP
				        CLR     LED
				        CLR     SAMPLING
				        SJMP    $WAIT
				;
				
				CHECK_EE
				        MOV     R0,#00H ;PAGE BLOCK (3 bits)
				        MOV     R1,#FFH ;8 bits
				        CALL    IIC_READ ;RESULT IN A
				        ADD     A,#01H
				        MOV     B,A
				        PUSH    A
				;
				        MOV     R0,#00H
				        MOV     R1,#FFH
				        CALL    IIC_WRITE ;SAVE AGAIN
				        CALL    DELAY
				        MOV     R0,#00H ;PAGE BLOCK (3 bits)
				        MOV     R1,#FFH ;8 bits
				        CALL    IIC_READ ;RESULT IN A
				        MOV     B,A     ;NEW DATA
				        POP     A
				        CJNE    A,B,$ERR
				        SJMP    $END
				$ERR    MOV     BEEP,#10        ;BUZZER ON 0.1S
				        MOV     A,#99H
				        MOV     BUFFER,A        ;DISPLAY 99
				        CALL    DELAY
				        CALL    DELAY
				        MOV     A,#FFH
				        MOV     BUFFER,A
				        CALL    DELAY
				        CALL    DELAY
				        SJMP    $ERR
				$END    RET
				;
				READ_ID MOV     R0,#00H ;PAGE BLOCK (3 bits)
				        MOV     R1,#00H ;8 bits
				        CALL    IIC_READ
				        MOV     BINARY,A
				        JZ      $SET
				        CJNE    A,#10,$CHK
				$CHK    JC      $CONT
				$SET    MOV     A,#01H  ;ID=1
				        MOV     BINARY,A
				        MOV     R0,#00H
				        MOV     R1,#00H
				        MOV     B,A
				        CALL    IIC_WRITE ;SAVE AGAIN
				$CONT   MOV     BEEP,#100
				;KEY CHECK
				        SETB    KEY
				        JB      KEY,$NOKEY
				$0      MOV     A,BINARY
				        ADD     A,#01H
				        MOV     BINARY,A
				        CJNE    A,#10,$1
				$1      JC      $2
				        MOV     A,#01H
				        MOV     BINARY,A
				$2      MOV     A,BINARY
				        CALL    CONV
				        MOV     A,BINARY
				        MOV     B,A
				        MOV     R0,#00H
				        MOV     R1,#00H
				        CALL    IIC_WRITE ;SAVE AGAIN
				        CALL    DELAY
				        CALL    DELAY
				        CALL    DELAY
				        JMP     $CONT
				;
				$NOKEY  MOV     A,BINARY
				        MOV     EE_ID,A
				        MOV     A,BINARY
				        CALL    CONV    ;DISPLAY IT
				$LOOP   MOV     A,BEEP
				        JNZ     $LOOP   ;LOOP UNTIL BEEP=0
				        RET
				;
				;MEASURE PULSE
				READ_TEMP
				        CLR     EA
				$HI     JB      SMT160,$HI
				$LO     JNB     SMT160,$LO
				;LOW TO HIGH TRANSITION
				H_ONLY  MOV     DPTR,#0000H
				        MOV     R0,#10  ;READ COUNT
				        SETB    SCOPE
				$0      INC     DPTR
				        JB      SMT160,$0
				$1      JNB     SMT160,$1       ;SKIP LO
				        DJNZ    R0,$0
				        MOV     R4,DPL
				        MOV     R5,DPH
				        CLR     SCOPE
				;
				H_L     MOV     DPTR,#0000H
				        MOV     R0,#10
				$HI     JB      SMT160,$HI
				$LO     JNB     SMT160,$LO
				        SETB    SCOPE
				$0      INC     DPTR
				        JB      SMT160,$0
				$1      INC     DPTR
				        JNB     SMT160,$1
				        DJNZ    R0,$0
				        MOV     R6,DPL
				        MOV     R7,DPH
				        SETB    EA
				        CLR     SCOPE
				;
				        MOV     CNTH_L,R4
				        MOV     CNTH_H,R5
				        MOV     CNTT_L,R6
				        MOV     CNTT_H,R7
				;
				        MOV     R4,CNTH_L
				        MOV     R5,CNTH_H
				        MOV     R7,#00H         ;(07H 06H 05H 04H)=DUTY HI
				        MOV     R6,#00H
				;
				        MOV     0FH,#00H        ;(0FH 0EH 0DH 0CH)=212 CONSTANT
				        MOV     0EH,#00H
				        MOV     0DH,#00H
				        MOV     0CH,#212
				        CALL    MUL_4BYTE       ;RESULT IN (07H 06H 05H 04H)
				;
				        MOV     0FH,#00H
				        MOV     0EH,#00H
				        MOV     0CH,CNTT_L
				        MOV     0DH,CNTT_H
				        CALL    DIV_4BYTE       ;(7)(6)(5)(4)/=(F)(E)(D)(C)
				        MOV     A,04H           ;GET LOWER BYTE ONLY
				        SUBB    A,#68           ;A=(212*HI)/(HI+LO)-68
				        MOV     BINARY,A
				TEST    CALL    CONV            ;DATA IN BUFFER
				        CALL    ASC_CONV
				;       SETB    TX_START        ;TX_START=1
				;       SETB    TI              ;SIO INTERRUPT
				        MOV     TX_CNT,#00H
				        CLR     SCOPE
				        RET
				;
				;
				ASC_CONV
				        MOV     ASCBUF  ,#'T'
				        MOV     A,EE_ID
				        ADD     A,#'0'          ;ID=ID+'0'
				        MOV     ASCBUF+1,A
				        MOV     ASCBUF+2,#'='
				        MOV     A,BUFFER
				        SWAP    A
				        ANL     A,#0FH
				        ADD     A,#'0'
				        MOV     ASCBUF+3,A ;SAVE DIGIT1
				        MOV     A,BUFFER
				        ANL     A,#0FH
				        ADD     A,#'0'
				        MOV     ASCBUF+4,A ;SAVE DIGIT2
				        MOV     ASCBUF+5,#'C'
				        MOV     ASCBUF+6,#0DH   ;CARRIAGE RETURN
				        MOV     ASCBUF+7,#0AH   ;LINE FEED
				        RET
				;
				CONV    MOV     A,BINARY
				        MOV     B,#10
				        DIV     AB      ;A=A/10,B=A MOD 10
				        ANL     A,#0FH  ;A=A AND 0FH
				        JNZ     $1
				        MOV     A,#0FH  ;IF A=00H THEN A=0FH LEADING BLANK
				$1      SWAP    A
				        ORL     A,B     ;A AS BCD FORM
				        MOV     BUFFER,A ;SAVE AT BUFFER
				        RET
				;
				DELAY   MOV     R7,#00H
				$1      MOV     R6,#00H
				        DJNZ    R6,$
				        DJNZ    R7,$1
				        RET
				;
				SIO_ISR
				        PUSH    A
				        PUSH    PSW
				        PUSH    00H
				        JNB     RI,$CHK_TI
				        CLR     RI
				        MOV     A,SBUF
				        CJNE    A,EE_ID,$CHK_TI
				        SETB    TX_START
				        SETB    TI
				        NOP
				        NOP
				$CHK_TI JNB     TI,$NO_TX
				        JNB     TX_START,$NO_TX
				        CLR     TI
				        MOV     A,TX_CNT
				        MOV     R0,#ASCBUF
				        ADD     A,R0
				        MOV     R0,A
				        MOV     A,@R0
				        MOV     SBUF,A
				        INC     TX_CNT  ;TX_CNT=TX_CNT+1
				        MOV     A,TX_CNT
				        CJNE    A,#OUT_CNT,$ISRE
				        CLR     TX_START        ;STOP TX
				        MOV     TX_CNT,#00H
				$ISRE
				$NO_TX  POP     00H
				        POP     PSW
				        POP     A
				        RETI
				;
				T0_ISR
				        MOV     TH0,#(65536-MS10) / 256 ;RELOAD TH0
				        MOV     TL0,#(65536-MS10) .MOD. 256
				        PUSH    A
				        PUSH    PSW
				        PUSH    00H
				;
				        MOV     A,BUFFER
				        ANL     A,#0FH  ;GET LOW NIBBLE
				        SETB    ACC.5   ;BIT5=1
				        MOV     P1,A    ;SHOW 1 DIGIT
				        MOV     R0,#30H
				        DJNZ    R0,$    ;DELAY
				;inter-blanking
				        CLR     ACC.4
				        CLR     ACC.5
				        MOV     P1,A    ;ALL DIGIT BLANK
				        MOV     R0,#04H
				        DJNZ    R0,$    ;DELAY
				;
				        MOV     A,BUFFER
				        ANL     A,#F0H  ;GET HIGH NIBBLE
				        SWAP    A       ;SWAP A
				        SETB    ACC.4   ;BIT4=1
				        MOV     P1,A    ;SHOW ANOTHER DIGIT
				        MOV     R0,#30H
				        DJNZ    R0,$    ;DELAY
				;
				        CLR     ACC.4
				        CLR     ACC.5
				        MOV     P1,A    ;ALL DIGIT BLANK
				;
				;INTR_CNT+1
				        MOV     A,INTR_CNT
				        JNZ     $NEXT
				        SETB    SAMPLING        ;IF A=0 OR A=50 THEN SAMPLING=1
				        SJMP    $INC
				$NEXT   CJNE    A,#50,$INC
				        SETB    SAMPLING
				$INC    INC     INTR_CNT
				        MOV     A,INTR_CNT
				        CJNE    A,#100,$T0_END
				        MOV     INTR_CNT,#00H
				;CHECK BUZZER
				$T0_END MOV     A,BEEP
				        JZ      $NOBEEP
				        DEC     BEEP
				        SETB    BUZZER
				        SJMP    $END
				$NOBEEP CLR     BUZZER
				$END    POP     00H
				        POP     PSW
				        POP     A
				        RETI
				;
				SET_TMR0
				        MOV     A,#21H  ;TIMER1 MODE2,TIMER0 MODE1
				        MOV     TMOD,A
				        MOV     TH0,#(65536-MS10) / 256 ;RELOAD TH0
				        MOV     TL0,#(65536-MS10) .MOD. 256
				        CLR     TF0     ;CLEAR TIMER0 FLAG
				        SETB    TR0     ;TIMER0 START COUNT
				        MOV     SCON,#50H    ;8BIT DATA OUTPUT
				        MOV     TH1,#BPS9600 ;BAUD RATE 9600b/s
				        SETB    TR1     ;TIMER1 START COUNT
				        SETB    ET0     ;ENABLE TIMER0 INTERRUPT
				        SETB    ES      ;ENABLE SERIAL INTERRUPT
				        SETB    EA      ;ENABLE SYSTEM INTERRUPT
				        RET
				;
				;ROUTINES FOR AT2051 KIT
				;
				;FUNCTION NAME : CLEAR_INTERNAL_DATA_MEMORY
				;FUNCTION : CLEAR 4 BYTES DATA MEMORY 08H,09H,0AH,0BH
				;
				CLEAR_INTERNAL_DATA_MEMORY
				        MOV     R0,#08H ;DATA MEMORY ADDR
				        MOV     R2,#04H ;COUNT
				$1      CLR     A
				        MOV     @R0,A
				        INC     R0
				        DJNZ    R2,$1
				        RET
				;
				;FUNCTION NAME : DATA_ADD_4BYTE
				;FUNCTION : ADD 4 BYTES IN DATA MEMORY
				;(R0+3)(R0+2)(R0+1)(R0)=(R0+3)(R0+2)(R0+1)(R0)+(R1+3)(R1+2)(R1+1)(R1)
				;
				DATA_ADD_4BYTE
				        MOV     R2,#04H
				        CLR     C
				$1      MOV     A,@R0
				        ADDC    A,@R1
				        MOV     @R0,A
				        INC     R0
				        INC     R1
				        DJNZ    R2,$1
				        RET
				;
				;
				;FUNCTION NAME : DATA_SUBTRACT
				;FUNCTION : SUBTRACT 4 BYTES,RESULT IN DATA MEMORY
				;(R0+3)(R0+2)(R0+1)(R0)=(R0+3)(R0+2)(R0+1)(R0)-(R1+3)(R1+2)(R1+1)(R1)
				;
				DATA_SUBTRACT
				        MOV     R2,#04H
				        CLR     C
				$1      MOV     A,@R0
				        SUBB    A,@R1
				        MOV     @R0,A
				        INC     R0
				        INC     R1
				        DJNZ    R2,$1
				        RET
				;
				;FUNCTION NAME : DATA_COMPLEMENT
				;FUNCTION : COMPLEMENT 4 BYTES IN (07)(06)(05)(04)
				;
				DATA_COMPLEMENT
				        MOV     R0,#04
				        MOV     R2,#04H         ;COUNT
				        SETB    C               ;CY=1
				$1      MOV     A,@R0
				        CPL     A
				        ADDC    A,#00H          ;A=A+CY
				        MOV     @R0,A
				        INC     R0
				        DJNZ    R2,$1
				        RET
				;
				;
				;FUNCTION NAME : INTERNAL_DATA_COMPARE
				;FUNCTION : COMPARE 2 BLOCKS INTERNAL DATA MEMORY (R2) TIMES
				;
				INTERNAL_DATA_COMPARE
				$1      MOV     A,@R0
				        MOV     B,@R1
				        CJNE    A,B,$2
				        DEC     R0
				        DEC     R1
				        DJNZ    R2,$1
				$2      RET
				;
				;FUNCTION NAME : ACC_COMPARE
				;FUNCTION : COMPARE ACC WITH (R0) IN DATA MEMORY (R2) TIMES
				;
				ACC_COMPARE
				$1      MOV     B,@R0
				        CJNE    A,B,$2
				        INC     R0
				        DJNZ    R2,$1
				        CLR     A
				        SJMP    $3              ;IF FOUND THEN A=0
				$2      MOV     A,#01H          ;ELSE          A=1
				$3      RET
				;
				;FUNCTION NAME :MUL_4BYTE
				;FUNCTION : UNSIGNED 4 BYTE MULTIPLY
				;(07H)(06H)(05H)(04H)=(07H)(06H)(05H)(04H) X (0FH)(0EH)(0DH)(0CH)
				;WORKING AREA DATA MEMORY : (0BH),(0AH),(09H),(08H)
				;
				MUL_4BYTE
				        LCALL   CLEAR_INTERNAL_DATA_MEMORY
				        MOV     R3,#33          ;SHIFT COUNT
				        CLR     C
				$1      MOV     R1,#0BH
				        MOV     R2,#08H
				$2      MOV     A,@R1
				        RRC     A
				        MOV     @R1,A
				        DEC     R1
				        DJNZ    R2,$2
				        JNC     $3
				        CLR     C
				        MOV     R0,#08H
				        MOV     R1,#0CH
				        LCALL   DATA_ADD_4BYTE
				$3      DJNZ    R3,$1
				        RET
				;
				;FUNCTION NAME :DIV_4BYTE
				;FUNCTION : UNSIGNED 4 BYTE DIVIDE
				;(07H)(06H)(05H)(04H)=(07H)(06H)(05H)(04H) / (0FH)(0EH)(0DH)(0CH)
				;WORKING AREA DATA MEMORY :(0BH),(0AH),(09H),(08H)
				;
				DIV_4BYTE
				        LCALL   CLEAR_INTERNAL_DATA_MEMORY
				        MOV     R3,#32          ;SHIFT COUNT
				        CLR     C
				$1      MOV     R1,#04H
				        MOV     R2,#08H
				$2      MOV     A,@R1
				        RLC     A
				        MOV     @R1,A
				        INC     R1
				        DJNZ    R2,$2
				        MOV     R0,#0BH
				        MOV     R1,#0FH
				        MOV     R2,#04H
				        LCALL   INTERNAL_DATA_COMPARE
				        JC      $3
				        MOV     R0,#08H
				        MOV     R1,#0CH
				        LCALL   DATA_SUBTRACT
				$3      CPL     C
				        DJNZ    R3,$1
				        MOV     R1,#04H
				        MOV     R2,#04H
				$4      MOV     A,@R1
				        RLC     A
				        MOV     @R1,A
				        INC     R1
				        DJNZ    R2,$4
				        RET
				;
				;MEM BLOCK IN R0
				;ADDRESS   IN R1
				;DATA      IN B
				IIC_WRITE
				        CALL    IIC_START
				        MOV     A,R0
				        ANL     A,#07H  ;MASK BIT7-BIT3
				        RL      A
				        ORL     A,#A0H  ;WR=TRUE(0) AS BIT0=1
				        CALL    IIC_OUT ;COMMAND OUT
				        CALL    IIC_ACK ;9 CLK
				;
				        MOV     A,R1
				        CALL    IIC_OUT ;ADDR OUT
				        CALL    IIC_ACK
				;
				        MOV     A,B
				        CALL    IIC_OUT ;DATA OUT
				        CALL    IIC_ACK
				;
				        CALL    IIC_STOP
				        RET
				;
				IIC_OUT CLR     SCLK
				        CALL    DLY
				        MOV     R7,#8
				$RL     RLC     A
				        JC      $O1
				$O0     CLR     SDA
				        JMP     $NEXT
				$O1     SETB    SDA
				$NEXT
				        CALL    DLY
				        SETB    SCLK
				        CALL    DLY
				        CLR     SCLK
				        CLR     SDA
				        CALL    DLY
				        DJNZ    R7,$RL
				        RET
				;
				;MEM BLOCK IN R0
				;ADDRESS   IN R1
				;DATA RETURN AT ACC
				;
				IIC_READ
				        CALL    IIC_START
				        MOV     A,R0
				        ANL     A,#07H  ;MASK BIT7-BIT3
				        RL      A
				        ORL     A,#A0H  ;RD/!WR=0 AS BIT0=1
				        CALL    IIC_OUT ;COMMAND OUT(WR)
				        CALL    IIC_ACK
				;
				        MOV     A,R1
				        CALL    IIC_OUT ;ADDR OUT
				        CALL    IIC_ACK
				;
				        CALL    IIC_START
				        MOV     A,R0
				        ANL     A,#07H  ;MASK BIT7-BIT3
				        RL      A
				        ORL     A,#A1H  ;RD=1 AS BIT0=1
				        CALL    IIC_OUT ;COMMAND OUT(RD)
				        CALL    IIC_ACK
				;
				        CALL    IIC_IN ;DATA IN
				        CALL    IIC_ACK
				;
				        CALL    IIC_STOP
				        RET
				;
				IIC_ACK
				        SETB    SCLK
				        NOP
				        CLR     SCLK
				        NOP
				        RET
				;
				IIC_IN  SETB    SDA
				        CLR     SCLK
				        MOV     R2,#8
				        MOV     R0,#00H
				$SI     SETB    SCLK
				        JB      SDA,$SI1
				$SI0    CLR     C
				        JMP     $SNX
				$SI1    SETB    C
				$SNX    NOP
				        MOV     A,R0    ;LOW BYTE
				        RLC     A
				        MOV     R0,A
				        CLR     SCLK
				        DJNZ    R2,$SI
				        MOV     A,R0
				        RET
				;
				IIC_INIT
				        SETB    SCLK
				        SETB    SDA
				        RET
				;
				IIC_START
				        SETB    SCLK
				        SETB    SDA
				;
				        CLR     SDA
				        NOP
				        CLR     SCLK
				        NOP
				        RET
				;
				IIC_STOP
				        SETB    SCLK
				        SETB    SDA
				        RET
				;
				DLY     NOP
				        NOP
				        NOP
				        RET
							

相关资源