📄 calc.sdi
字号:
,,,;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
,,,;
,,,; CALC.ASM MPB Ver 1.0 28-8-05
,,,;
,,,; Simple calculator
,,,; Single digit input, two digit results
,,,; Integer handling only
,,,;
,,,;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
,,,
,,, PROCESSOR 16F877
,,,; Clock = XT 4MHz, standard fuse settings
00002007,3731,, __CONFIG 0x3731
,,,
,,,; LABEL EQUATES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
,,,
,,, INCLUDE "P16F877A.INC"
,,,
,,,
,,,Char EQU 30 ; Display character code
,,,Num1 EQU 31 ; First number input
,,,Num2 EQU 32 ; Second number input
,,,Result EQU 33 ; Calculated result
,,,Oper EQU 34 ; Operation code store
,,,Temp EQU 35 ; Temporary register for subtract
,,,Kcount EQU 36 ; Count of keys hit
,,,Kcode EQU 37 ; ASCII code for key
,,,Msd EQU 38 ; Most significant digit of result
,,,Lsd EQU 39 ; Least significant digit of result
,,,Kval EQU 40 ; Key numerical value
,,,
,,,RS EQU 1 ; Register select output bit
,,,E EQU 2 ; Display data strobe
,,,
,,,; Program begins ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
,,,
,,, ORG 0 ; Default start address
00000000,0000,, NOP ; required for ICD mode
,,,
00000001,1683 1303,, BANKSEL TRISC ; Select bank 1
00000003,30F0,, MOVLW B'11110000' ; Keypad direction code
00000004,0087,, MOVWF TRISC ;
00000005,0188,, CLRF TRISD ; Display port is output
,,,
00000006,1283 1303,, BANKSEL PORTC ; Select bank 0
00000008,30FF,, MOVLW 0FF ;
00000009,0087,, MOVWF PORTC ; Set keypad outputs high
0000000A,0188,, CLRF PORTD ; Clear display outputs
0000000B,280C,, GOTO start ; Jump to main program
,,,
,,,
,,,; MAIN LOOP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
,,,
0000000C,2050,start,start CALL inid ; Initialise the display
0000000D,3080,, MOVLW 0x80 ; position to home cursor
0000000E,10F4,, BCF Select,RS ; Select command mode
0000000F,2040,, CALL send ; and send code
,,,
00000010,0103,, CLRW Char ; ASCII = 0
00000011,0103,, CLRW Kval ; Key value = 0
00000012,0103,, CLRW DFlag ; Digit flags = 0
,,,
00000013,206B,scan,scan CALL keyin ; Scan keypad
00000014,08B0,, MOVF Char,1 ; test character code
00000015,1D03,, BTFSS STATUS,Z ; key pressed?
00000016,2818,, GOTO keyon ; yes - wait for release
00000017,2813,, GOTO scan ; no - scan again
,,,
00000018,0830,keyon,keyon MOVF Char,W ; Copy..
00000019,00B7,, MOVWF Kcode ; ..ASCIIcode
0000001A,3032,, MOVLW D'50' ; delay for..
0000001B,2036,, CALL xms ; ..50ms debounce
,,,
0000001C,206B,wait,wait CALL keyin ; scan keypad again
0000001D,08B0,, MOVF Char,1 ; test character code
0000001E,1D03,, BTFSS STATUS,Z ; key pressed?
0000001F,281C,, GOTO wait ; no - rescan
00000020,20E4,, CALL disout ; yes - show symbol
,,,
00000021,0AB6,, INCF Kcount ; inc count..
00000022,0836,, MOVF Kcount,W ; ..of keys pressed
00000023,0782,, ADDWF PCL ; jump into table
00000024,0000,, NOP
00000025,282A,, GOTO first ; process first key
00000026,2813,, GOTO scan ; get operation key
00000027,282D,, GOTO second ; process second symbol
00000028,28E8,, GOTO calc ; calculate result
00000029,2945,, GOTO clear ; clear display
,,,
0000002A,0840,first,first MOVF Kval,W ; store..
0000002B,00B1,, MOVWF Num1 ; first num
0000002C,2813,, GOTO scan ; and get op key
,,,
0000002D,0840,second,second MOVF Kval,W ; store..
0000002E,00B2,, MOVWF Num2 ; second number
0000002F,2813,, GOTO scan ; and get equals key
,,,
,,,
,,,; SUBROUTINES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
,,,
,,,; Include LCD driver routine
,,,
,,, INCLUDE "LCDIS.INC"
,,,;--------------------------------------------------------------
,,,; LCDIS.INC MPB 19-12-05
,,,;
,,,; Include file to operate 16x2 LCD display
,,,; Uses GPR 70 - 75
,,,;
,,,; Final version
,,,;--------------------------------------------------------------
,,,
,,,Timer1 EQU 70 ; 1ms count register
,,,TimerX EQU 71 ; Xms count register
,,,Var EQU 72 ; Output variable
,,,Point EQU 73 ; Program table pointer
,,,Select EQU 74 ; Used to set or clear RS bit
,,,OutCod EQU 75 ; Temp store for output code
,,,
,,,RS EQU 1 ; Register select output bit
,,,E EQU 2 ; Enable display input
,,,
,,,
,,,;--------------------------------------------------------------
,,,; 1ms delay with 1us cycle time (1000 cycles)
,,,;--------------------------------------------------------------
00000030,30F9,onems,onems MOVLW D'249' ; Count for 1ms delay
00000031,00F0,, MOVWF Timer1 ; Load count
00000032,0000,loop1,loop1 NOP ; Pad for 4 cycle loop
00000033,0BF0,, DECFSZ Timer1 ; Count
00000034,2832,, GOTO loop1 ; until Z
00000035,0008,, RETURN ; and finish
,,,
,,,;--------------------------------------------------------------
,,,; Delay Xms
,,,; Receives count in W, uses Onems
,,,;--------------------------------------------------------------
00000036,00F1,xms,xms MOVWF TimerX ; Count for X ms
00000037,2030,loopX,loopX CALL onems ; Delay 1ms
00000038,0BF1,, DECFSZ TimerX ; Repeat X times
00000039,2837,, GOTO loopX ; until Z
0000003A,0008,, RETURN ; and finish
,,,
,,,;--------------------------------------------------------------
,,,; Generate data/command clock siganl E
,,,;--------------------------------------------------------------
0000003B,1508,pulseE,pulseE BSF PORTD,E ; Set E high
0000003C,2030,, CALL onems ; Delay 1ms
0000003D,1108,, BCF PORTD,E ; Reset E low
0000003E,2030,, CALL onems ; Delay 1ms
0000003F,0008,, RETURN ; done
,,,
,,,;--------------------------------------------------------------
,,,; Send a command byte in two nibbles from RB4 - RB7
,,,; Receives command in W, uses PulseE and Onems
,,,;--------------------------------------------------------------
00000040,00F5,send,send MOVWF OutCod ; Store output code
00000041,39F0,, ANDLW 0F0 ; Clear low nybble
00000042,0088,, MOVWF PORTD ; Output high nybble
00000043,18F4,, BTFSC Select,RS ; Test RS bit
00000044,1488,, BSF PORTD,RS ; and set for data
00000045,203B,, CALL pulseE ; and clock display register
00000046,2030,, CALL onems ; wait 1ms for display
,,,
00000047,0EF5,, SWAPF OutCod ; Swap low and high nybbles
00000048,0875,, MOVF OutCod,W ; Retrieve output code
00000049,39F0,, ANDLW 0F0 ; Clear low nybble
0000004A,0088,, MOVWF PORTD ; Output low nybble
0000004B,18F4,, BTFSC Select,RS ; Test RS bit
0000004C,1488,, BSF PORTD,RS ; and set for data
0000004D,203B,, CALL pulseE ; and clock display register
0000004E,2030,, CALL onems ; wait 1ms for display
0000004F,0008,, RETURN ; done
,,,
,,,;--------------------------------------------------------------
,,,; Initialise the display
,,,; Uses Send
,,,;--------------------------------------------------------------
00000050,3064,inid,inid MOVLW D'100' ; Load count for 100ms delay
00000051,2036,, CALL xms ; and wait for display start
00000052,30F0,, MOVLW 0F0 ; Mask for select code
00000053,00F4,, MOVWF Select ; High nybble not masked
,,,
00000054,3030,, MOVLW 0x30 ; Load initial nibble
00000055,0088,, MOVWF PORTD ; and output it to display
00000056,203B,, CALL pulseE ; Latch initial code
00000057,3005,, MOVLW D'5' ; Set delay 5ms
00000058,2036,, CALL xms ; and wait
00000059,203B,, CALL pulseE ; Latch initial code again
0000005A,2030,, CALL onems ; Wait 1ms
0000005B,203B,, CALL pulseE ; Latch initial code again
0000005C,1208,, BCF PORTD,4 ; Set 4-bit mode
0000005D,203B,, CALL pulseE ; Latch it
,,,
0000005E,3028,, MOVLW 0x28 ; Set 4-bit mode, 2 lines
0000005F,2040,, CALL send ; and send code
00000060,3008,, MOVLW 0x08 ; Switch off display
00000061,2040,, CALL send ; and send code
00000062,3001,, MOVLW 0x01 ; Code to clear display
00000063,2040,, CALL send ; and send code
00000064,3006,, MOVLW 0x06 ; Enable cursor auto inc
00000065,2040,, CALL send ; and send code
00000066,3080,, MOVLW 0x80 ; Zero display address
00000067,2040,, CALL send ; and send code
00000068,300C,, MOVLW 0x0C ; Turn on display
00000069,2040,, CALL send ; and send code
,,,
0000006A,0008,, RETURN ; Done
,,,
,,,;--------------------------------------------------------------
,,,
,,,
,,,; Scan keypad .............................................
,,,
0000006B,300F,keyin,keyin MOVLW 00F ; deselect..
0000006C,0087,, MOVWF PORTC ; ..all rows
0000006D,1007,, BCF PORTC,0 ; select row A
0000006E,2030,, CALL onems ; wait output stable
,,,
0000006F,1A07,, BTFSC PORTC,4 ; button 7?
00000070,2876,, GOTO b8 ; no
00000071,3037,, MOVLW '7' ; yes
00000072,00B0,, MOVWF Char ; load key code
00000073,3007,, MOVLW 07 ; and
00000074,00C0,, MOVWF Kval ; key value
00000075,0008,, RETURN
,,,
00000076,1A87,b8,b8 BTFSC PORTC,5 ; button 8?
00000077,287D,, GOTO b9 ; no
00000078,3038,, MOVLW '8' ; yes
00000079,00B0,, MOVWF Char
0000007A,3008,, MOVLW 08
0000007B,00C0,, MOVWF Kval
0000007C,0008,, RETURN
,,,
0000007D,1B07,b9,b9 BTFSC PORTC,6 ; button 9?
0000007E,2884,, GOTO bd ; no
0000007F,3039,, MOVLW '9' ; yes
00000080,00B0,, MOVWF Char
00000081,3009,, MOVLW 09
00000082,00C0,, MOVWF Kval
00000083,0008,, RETURN
,,,
00000084,1B87,bd,bd BTFSC PORTC,7 ; button /?
00000085,288A,, GOTO rowb ; no
00000086,302F,, MOVLW '/' ; yes
00000087,00B0,, MOVWF Char ; store key code
00000088,00B4,, MOVWF Oper ; store operator symbol
00000089,0008,, RETURN
,,,
0000008A,1407,rowb,rowb BSF PORTC,0 ; select row B
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -