⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 计算器.bas

📁 4*4键盘控制1602液晶显示
💻 BAS
字号:
'项目:4*4键盘1602液晶显示的计算器
'结果:在proteus仿真运行成功
'时间:2007年8月20日
'开发软件:bascom-avr 11.8.5
'作者:あ邂逅记忆あ
'宜昌AVR单片机 http://ycavr.cn
$regfile = "m16def.dat"
$crystal = 8000000
Config Lcdpin = Pin , Db4 = Porta.4 , Db5 = Porta.5 , Db6 = Porta.6 , Db7 = Porta.7 , E = Porta.3 , Rs = Porta.2
Config Lcd = 16 * 2
Config Kbd = Portc
Cursor Off
Cls

Dim Key As Byte
Dim Oldkey As Byte
Dim Keyvalue As Byte                                        '键盘值校正
Dim A As Long                                               '第一个运算数据
Dim B As Long                                               '第二个运算数据
Dim Operator As Byte : Operator = 0                         '运算符标识 1为加2为减3为乘4为除
Dim Resault As Single                                       '运算结果,单精度数。
Declare Sub Correct()                                       '键盘值校正子程序

Do
   Do
      Oldkey = Getkbd()
      Waitms 20
      Do
         Key = Getkbd()
         Waitms 20
      Loop Until Key <> 16
   Loop Until Key <> Oldkey                                 '取键盘值,直到键松开为止,防止按住不松,连续出现多个。
   Correct                                                  '键盘值校正
   Select Case Key                                          '据校正后的键盘值作出相应动作
      Case Is < 3
         Cls
         If Operator = 0 Then
            A = A * 10
            A = A + Keyvalue
            Lcd A
         Else
            B = B * 10
            B = B + Keyvalue
            Lcd B
         End If
      Case 3
         Operator = 4                                       '除运算
         B = 0
      Case Is < 7
         Cls
         If Operator = 0 Then
            A = A * 10
            A = A + Key
            Lcd A
         Else
            B = B * 10
            B = B + Key
            Lcd B
         End If
      Case 7
         Operator = 3                                       '乘运算
         B = 0
      Case Is < 11
         Cls
         If Operator = 0 Then
            A = A * 10
            A = A + Keyvalue
            Lcd A
         Else
            B = B * 10
            B = B + Keyvalue
            Lcd B
         End If
      Case 11
         Operator = 2                                       '减运算
         B = 0
      Case 12
         Cls
         Key = 0
         A = 0
         B = 0
         Operator = 0
         Resault = 0
      Case 13
         Cls
         If Operator = 0 Then
            A = A * 10
            Lcd A
         Else
            B = B * 10
            Lcd B
         End If
      Case 14
         Select Case Operator
            Case 1
               Resault = A + B
            Case 2
               Resault = A - B
            Case 3
               Resault = A * B
            Case 4
               If B = 0 Then                                '除数为零,显示错误!
                  Cls
                  Lcd "B=0"
                  Locate 2 , 4
                  Lcd "Error !"
                  Waitms 500
               Else
                  Resault = A / B
               End If
         End Select
         Cls
         Lcd Resault
         A = Resault
         B = 0
         Operator = 0
         Resault = 0
      Case 15
         Operator = 1                                       '加运算
         B = 0
   End Select
Loop
End

Sub Correct()
Select Case Key
   Case 0
      Keyvalue = 7
   Case 1
      Keyvalue = 8
   Case 2
      Keyvalue = 9
   Case 8
      Keyvalue = 1
   Case 9
      Keyvalue = 2
   Case 10
      Keyvalue = 3
   Case 13
      Keyvalue = 0
End Select
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -