Thông báo

Collapse
No announcement yet.

giúp về code đo tần số với ạ

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • giúp về code đo tần số với ạ

    tôi muốn sủa lại code này để thay cai lcd bang led 7 đoạn ạ. có anh em nào ghé ngang giúp với. vìkhong o rành cai vụ này lắm ạ
    Click image for larger version

Name:	New Bitmap Image.PNG
Views:	1
Size:	32.0 KB
ID:	1418189

    Code:
    ;D4 - P1.0
    ;D5 - P1.1
    ;D6 - P1.2
    ;D7 - P1.3
    ;EN - P1.7
    ;RW - P1.6
    ;RS - P1.5
    
    	lcd_port equ P1         ;LCD connected to Port3
    	en equ P1.7             ;Enable connected to P1.7
    
    	var1 equ r2
    	temp equ r3
    	temp1 equ r4
    	delay equ r5
    	tick equ r6
    	Freq equ r7
    
    	;4Byte hex 2 Dec conv variables
    	XX0	equ	30H
    	XX1	equ	31H
    	XX2	equ	32H
    	XX3 equ 33H
    	YY0	equ	34H
    	YY1	equ	35H
    	YY2	equ	36H
    	YY3	equ	37H
    	YY4	equ	38H
    	ZZ0	equ	39H
    	ZZ1	equ	3AH
    	ZZ2	equ	3BH
    	ZZ3	equ	3CH
    	ZZ4	equ	3DH
    	BITS equ 3EH
    
    	UpdateLCD equ 0H
    	
    	org 0H
    	sjmp main
    	org 0BH	;TMR0 ISR
    	ajmp TMR0_ISR
    	org 1BH	;TMR1 ISR
    	ajmp TMR1_ISR
    
    main:
    	mov SP,#50H
    	mov tick, #20
    	mov tmod, #51H
    	mov Freq, #00
    	setb ET0
    	setb ET1
    	setb EA
    	clr UpdateLCD
    	acall lcd_init
    	mov a,#'F'
    	acall lcd_dat
    	mov a,#'r'
    	acall lcd_dat
    	mov a,#'e'
    	acall lcd_dat
    	mov a,#'q'
    	acall lcd_dat
    	mov a,#':'
    	acall lcd_dat
    	mov a,#' '
    	acall lcd_dat
    again:
    	mov a,#86H
    	acall lcd_cmd
    
    	mov TH0, #3CH
    	mov TL0, #0BAH
    	mov tick, #20
    
    	mov Freq, #0
    	mov TH1, #0
    	mov TL1, #0
    
    	setb TR0
    	setb TR1
    
    	jnb UpdateLCD,$
    	clr UpdateLCD
    
    	mov XX2,Freq
    	mov XX1,TH1
    	mov XX0,TL1
    	acall X2D
    
    	mov a,ZZ3
    	acall disp_num
    	mov a,ZZ2
    	acall disp_num
    	mov a,ZZ1
    	acall disp_num
    	mov a,ZZ0
    	acall disp_num
    	mov a,#'H'
    	acall lcd_dat
    	mov a,#'z'
    	acall lcd_dat
    
    	sjmp again
    
    TMR0_ISR:
    	djnz tick, TMR0_ISR_GO
    	clr TR1
    	clr TF1
    	clr TR0
    	clr TF0
    	setb UpdateLCD
    	reti
    TMR0_ISR_GO:
    	mov TH0, #3CH
    	mov TL0, #0BAH
    	reti
    
    TMR1_ISR:
    	inc Freq
    	reti
    
    disp_num:
    	mov DPTR,#hex_table
    	mov temp1,a
    	anl a,#0F0H
    	swap a
    	movc a,@a+DPTR
    	acall lcd_dat
    	
    	mov a,temp1
    	anl a,#0FH
    	movc a,@a+dptr
    	acall lcd_dat
    	ret
    
    lcd_reset:                  ;LCD reset sequence
    	mov lcd_port, #0FFH
    	mov delay,#20           ;20mS delay
    	acall delayms
    	mov lcd_port, #83H      ;Data = 30H, EN = 1, First Init
    	mov lcd_port, #03H      ;Data = 30H, EN = 0
    	mov delay,#15           ;Delay 15mS
    	acall delayms
    	mov lcd_port, #83H      ;Second Init, Data = 30H, EN = 1
    	mov lcd_port, #03H      ;Data = 30H, EN = 0
    	mov delay,#5            ;Delay 5mS
    	acall delayms
    	mov lcd_port, #83H      ;Third Init
    	mov lcd_port, #03H
    	mov delay,#5            ;Delay 5mS
    	acall delayms
    	mov lcd_port, #82H      ;Select Data width (20H for 4bit)
    	mov lcd_port, #02H      ;Data = 20H, EN = 0
    	mov delay,#5            ;Delay 5mS
    	acall delayms
    	ret
    
    lcd_init:
    	acall lcd_reset         ;Call LCD Reset sequence
    	mov a,#28H              ;4-bit, 2 line, 5x7 dots
    	acall lcd_cmd           ;Call LCD command
    	mov a,#0CH              ;Display ON cursor OFF
    	acall lcd_cmd           ;Call LCD command
    	mov a,#06H              ;Set entry mode (Auto increment)
    	acall lcd_cmd           ;Call LCD command
    	mov a,#80H              ;Bring cursor to line 1
    	acall lcd_cmd           ;Call LCD command
    	ret
    
    lcd_wait:
    	mov a,#0CFH
    	mov temp,00H
    
    	mov lcd_port, a
    	mov temp, lcd_port
    	clr en
    
    	setb en
    	mov lcd_port, a
    	clr en
    
    	mov a,temp
    	jb acc.3, lcd_wait
    	ret
    
    lcd_cmd:                  ;LCD command Routine
    	mov temp,a            ;Save a copy of command to temp
    	swap a                ;Swap to use higher nibble
    	anl a,#0FH            ;Mask the first four bits
    	add a,#80H            ;Enable = 1, RS = 0, RW = 0
    	mov lcd_port,a        ;Move it to lcd port
    	anl a,#0FH            ;Enable = 0, RS = 0, RW = 0
    	mov lcd_port,a        ;Move to lcd port
    
    	mov a,temp            ;Reload the command from temp
    	anl a,#0FH            ;Mask first four bits
    	add a,#80H            ;Enable = 1
    	mov lcd_port,a        ;Move to port
    	anl a,#0FH            ;Enable = 0
    	mov lcd_port,a        ;Move to lcd port
    
    	acall lcd_wait
    	ret
    
    lcd_dat:                  ;LCD data Routine
    	mov temp,a            ;Keep copy of data in temp
    	swap a                ;We need higher nibble
    	anl a,#0FH            ;Mask first four bits
    	add a,#0A0H           ;Enable = 1, RS = 1, RW = 0
    	mov lcd_port,a        ;Move to lcd port
    	nop
    	clr en                ;Enable = 0
    
    	mov a,temp            ;Reload the data from temp
    	anl a,#0FH            ;we need lower nibble now
    	add a,#0A0H           ;Enable = 1, RS = 1, RW = 0
    	mov lcd_port,a        ;Move to lcd port
    	nop
    	clr en                ;Enable = 0
    
    	acall lcd_wait
    	ret
    
    delayms:
    	mov var1,#230
    d:
    	nop
    	nop
    	djnz var1,d
    	djnz delay,delayms
    	ret
    
    X2D:         CALL  CLEARYZ             ;;Clear All YY and ZZ bytes
                 MOV   YY0,#1              ;;DECIMAL ADDER = 1
                 ;                         ;;
                 MOV   R0,#XX3             ;;LOCATE HOW MANY BYTES WITH DATA
                 MOV   B,#4                ;;POSSIBLE 8 BYTES W/DATA ON XX
    BITS1:       MOV   A,@R0               ;;GET BYTE FROM INPUT REGISTER
                 CJNE  A,#0,BITS2          ;;JUMP IF FOUND THE FIRST NON ZERO
                 DEC   R0                  ;;GO TO LOWER BYTE
                 DJNZ  B,BITS1             ;;ONE BYTE DONE, GO AGAIN
                                           ;;
    BITS2:       MOV   A,#8                ;;8 BITS PER BYTE, B CONTAINS BYTE #
                 MUL   AB                  ;;A = QUANTITY OF BITS W/DATA
                 MOV   BITS,A              ;;SAVE
                 CJNE  A,#0,X2DMAIN2       ;;B = NUMBER OF DIGITS W/DATA
                 RET                       ;;RETURN IF ONLY ZEROS AT XX
                                           ;;
    X2DMAIN1:    CALL  X2DSHIFTD           ;;SHIFT DECIMAL RESULT
    X2DMAIN2:    CALL  X2DSHIFTH           ;;SHIFT HEXA
                 JNC   X2DMAIN3            ;;IF NOT CARRY, JUST SKIP IT
                 CALL  X2DADD              ;;ADD NEW RESULT
    X2DMAIN3:    DJNZ  BITS,X2DMAIN1       ;;ONE BIT DONE, GO AGAIN
                 RET                       ;;GENERAL EXIT FROM THIS ROUTINE
                                           ;;ZZ0 - ZZ7 CONTAINS DECIMAL RESULT
                                           ;;
    X2DSHIFTD:   MOV   R0,#YY0             ;;YY * 2  (DECIMAL)
                 MOV   B,#4                ;;NUMBER OF BYTES
                 CLR   C                   ;;NEED CARRY ZERO
    X2DSHIFTD1:  MOV   A,@R0               ;;IGNORE LAST CARRY
                 ADDC  A,@R0               ;;ADD BYTE TO ITSELF
                 DA    A                   ;;DECIMAL ADJUST
                 MOV   @R0,A               ;;PUT IT BACK
                 INC   R0                  ;;GO TO UPPER BYTE
                 DJNZ  B,X2DSHIFTD1        ;;DO IT 8 BYTES
                 RET                       ;;RETURN
                                           ;;
    X2DSHIFTH:   MOV   R0,#XX3             ;;SHIFT XX7 --> XX0 RIGHT 1 BIT
                 MOV   B,#4                ;;NUMBER OF BYTES
    SHIFTR0B:    CLR   C                   ;;NEED CARRY ZERO
    SHIFTR0B1:   MOV   A,@R0               ;;GET BYTE
                 RRC   A                   ;;ROTATE RIGHT THROUGH CARRY BIT
                 MOV   @R0,A               ;;SAVE IT BACK
                 DEC   R0                  ;;GO TO LOWER BYTE
                 DJNZ  B,SHIFTR0B1         ;;DO IT AGAIN "B" TIMES
                 RET                       ;;RETURN
                                           ;;
    X2DADD:      MOV   R0,#ZZ0             ;; GET RESULT ZZ 8 BYTES REGISTER
                 MOV   B,#4                ;; 8 BYTES
                 MOV   R1,#YY0             ;; GET YY OPERATOR
                 CLR   C                   ;; NEED CARRY OFF
    X2DADD1:     MOV   A,@R0               ;; ZZ = ZZ + YY (8BYTES) W/DAA
                 ADDC  A,@R1               ;; ADD BYTE TO BYTE 8 TIMES
                 DA    A                   ;; DECIMAL ADJUST
                 MOV   @R0,A               ;; PUT IT BACK
                 INC   R0                  ;; BUMP POINTER NEXT BYTE
                 INC   R1                  ;; BUMP POINTER NEXT BYTE
                 DJNZ  B,X2DADD1           ;; ONE BYTE DONE, GO AGAIN
                 MOV  A,ZZ3                ;; LAST CARRY TO 9TH BYTE OF ZZ
                 ADDC A,#0                 ;; JUST CARRY TO ZZ8
                 MOV  ZZ3,A                ;;
                 RET                       ;; RETURN
    
    CLEARYZ:
    			mov r1,#9
    			clr A
    			mov r0,#YY0
    CLEARYZ_1:
    			mov @r0,A
    			inc r0
    			djnz r1, CLEARYZ_1
    			ret
    
    hex_table:
    db '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
    	end
    Khi mê tiền chỉ là tiền. Ngộ ra mới biết trong tiền có tâm

Về tác giả

Collapse

ngohieu3186 Tìm hiểu thêm về ngohieu3186

Bài viết mới nhất

Collapse

Đang tải...
X