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

📄 avr单片机_计算器程序.txt

📁 proteus的例子-计算器
💻 TXT
字号:
'项目:4*4键盘1602液晶显示的计算器
'结果:在proteus仿真运行成功
'时间:2007年8月22日 修正bug
'开发软件: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                                       '运算结果,单精度数。
Dim Error As Bit : Error = 0                                '出错标志位
Declare Sub Correct()                                       '键盘值校正子程序
Declare Sub Account()
Declare Sub Keynum()

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
         Keynum
      Case 3
         If B <> 0 Then Account
         Operator = 4                                       '除运算
      Case Is < 7
         Keynum
      Case 7
         If B <> 0 Then Account
         Operator = 3                                       '乘运算
      Case Is < 11
         Keynum
      Case 11
         If B <> 0 Then Account
         Operator = 2                                       '减运算
      Case 12
         Cls
         Key = 0
         A = 0
         B = 0
         Operator = 0
         Resault = 0
      Case 13
         Keynum
      Case 14
         Account
         Operator = 0
         Resault = 0
      Case 15
         If B <> 0 Then Account
         Operator = 1                                       '加运算
   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
   Case Else
      Keyvalue = Key
End Select
End Sub

Sub Account()
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 !"
         Set Error                                          '置出错标志位
      Else
         Resault = A / B
      End If
End Select
If Error = 0 Then
   Cls
   Lcd Resault
   A = Resault
   B = 0
Else
   Wait 1
   Cls
   A = 0
   B = 0
   Operator = 0
   Resault = 0
   Reset Error                                              '清出错标志位
End If
End Sub

Sub Keynum()
Cls
If Operator = 0 Then
   A = A * 10
   A = A + Keyvalue
   Lcd A
Else
   B = B * 10
   B = B + Keyvalue
   Lcd B
End If
End Sub

⌨️ 快捷键说明

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