找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1740|回复: 0
收起左侧

BASCOM-8051写的1602显示18B20温度和时间程序+仿真

[复制链接]
ID:732506 发表于 2020-4-23 05:53 | 显示全部楼层 |阅读模式
BASCOM是basic语言的开发工具,比C容易入门,虽然很多功能受限,但常用库已经集成在内,很适合初学者。本例中有些测试用代码未删除,但不影响功能。8051在1602液晶和串口输出时间,到秒为0时读取18B20温度,显示在液晶屏,并通过串口打印18B20的寄存器值,同时将分钟和温度值写入24C04。仿真时如暂停可用鼠标右键查看24C04内存储器的值,一小时循环一次,覆盖上一小时的记录。本想加上读取24C04和数码管显示的语句,但还是觉得留一点給有兴趣的人自己做吧。

仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
89c52Lcd.JPG

单片机源程序如下:
  1. ' Program Led1.bas

  2. $crystal = 11059000
  3. $baud = 9600

  4. '$crystal = 12000000
  5. '$baud = 4800


  6. Config 1wire = P1.7                                           'use this pin
  7. Dim Ar(8) As Byte , A As Byte , I As Byte




  8. Declare Sub Write_eeprom(adres As Byte , Value As Byte)
  9. Declare Sub Read_eeprom(adres As Byte , Value As Byte)
  10. Config I2cdelay = 1                                           'default so not needed
  11. 'declare constants
  12. Const Addressw = 160
  13. Const Addressr = 161



  14. Dim B1 As Byte , Adres As Byte , Value As Byte                'dim byte


  15. Dim T As Byte
  16. Dim T1 As Byte
  17. Dim T2 As Byte
  18. Dim Ct As Byte
  19. Dim H As Byte
  20. Dim H1 As Byte
  21. Dim H2 As Byte
  22. Dim M As Byte
  23. Dim M1 As Byte
  24. Dim M2 As Byte
  25. Dim S As Byte
  26. Dim S1 As Byte
  27. Dim S2 As Byte
  28. Dim Led As Bit                                                'LED is a Bit variable since it will turn on and
  29. Dim Led1 As Bit

  30. Config Lcd = 16 * 2
  31. 'P1.0 = 0
  32. Initlcd
  33. Cls



  34. Config Timer0 = Timer , Gate = Internal , Mode = 1
  35. 'Timer0 = counter : timer0 operates as a counter
  36. 'Gate = Internal  : no external gate control
  37. 'Mode = 2         : 8-bit auto reload (default)
  38. 'Mode = 1         : 16-bit reload
  39. On Timer0 Timer_0_int
  40. Load Timer0 , 0                                               'when the timer reaches 100 an interrupt will occur
  41. Enable Interrupts                                             'enable the use of interrupts
  42. Enable Timer0                                                 'enable the timer


  43. Rem Setting Of Priority
  44. Priority Set Timer0                                           'highest priority
  45. Start Timer0                                                  'start the timer

  46. Call Write_eeprom(0 , 48)
  47. Call Write_eeprom(1 , 49)                                     'write value of three to address 1 of EEPROM

  48. Print Value
  49. Call Read_eeprom(0 , Value) : Print Value                     'read it back
  50. Call Read_eeprom(5 , Value) : Print Value                     'again for address 5

  51. Led1 = 1                                                      'initial value
  52. Led = 0                                                       'Do is a start of a Do-Loop loop

  53. Do



  54. Loop                                                          'end of Do-Loop loop

  55. End

  56. Rem The Interrupt Handler For The Timer0 Interrupt
  57. Timer_0_int:
  58. If Ct = 13 Then
  59.      Ct = 0

  60.      P0.1 = Led1
  61.      P0.0 = Led
  62.      Led = Not Led
  63.      Led1 = Not Led1                                          'invert LED value
  64.      H1 = H / 10
  65.      H2 = H Mod 10
  66.      M1 = M / 10
  67.      M2 = M Mod 10
  68.      S1 = S / 10
  69.      S2 = S Mod 10
  70.      Print "Time is:" ; H1 ; H2 ; ":" ; M1 ; M2 ; ":" ; S1 ; S2       '; (Second / 10) ; (Second%10)       ' test serial com


  71.      Lowerline
  72.      Lcd "Time is:" ; H1 ; H2 ; ":" ; M1 ; M2 ; ":" ; S1 ; S2

  73.      S = S + 1
  74.      If S = 60 Then
  75.            S = 0
  76.            M = M + 1

  77.            1wreset                                            'reset the device
  78.            Print Err                                          'print error 1 if error

  79.            1wwrite &HCC
  80.            1wwrite &H44                                       'read ROM command

  81.             Waitms 255
  82.             Waitms 255
  83.             Waitms 240
  84.            1wreset
  85.            1wwrite &HCC
  86.            1wwrite &HBE

  87.             For I = 1 To 8
  88.               Ar(i) = 1wread()                                'place into array
  89.             Next
  90.             For I = 1 To 8
  91.                Printhex Ar(i);                                'print output
  92.             Next
  93.             Print
  94.             T1 = Ar(1) / 16                                 'linefeed
  95.             T2 = Ar(2) * 16
  96.             T = T1 + T2

  97.             Upperline
  98.             Lcd "Temp is:" ; T

  99.             Call Write_eeprom(m , T)


  100.            If M = 60 Then
  101.               M = 0
  102.               H = H + 1
  103.               If H = 24 Then
  104.                  H = 0
  105.               End If
  106.            End If
  107.      End If
  108. Else
  109.    Incr Ct
  110. End If
  111. Return

  112.   'sample of writing a byte to EEPROM AT2404
  113. Sub Write_eeprom(adres As Byte , Value As Byte)
  114.     I2cstart                                                  'start condition
  115.     I2cwbyte Addressw                                         'slave address
  116.     I2cwbyte Adres                                            'asdress of EEPROM
  117.     I2cwbyte Value                                            'value to write
  118.     I2cstop                                                   'stop condition
  119.     Waitms 10                                                 'wait for 10 milliseconds
  120. End Sub


  121. 'sample of reading a byte from EEPROM AT2404
  122. Sub Read_eeprom(adres As Byte , Value As Byte)
  123.    I2cstart                                                   'generate start
  124.    I2cwbyte Addressw                                          'slave adsress
  125.    I2cwbyte Adres                                             'address of EEPROM
  126.     Waitms 10
  127.    I2cstart                                                   'repeated start
  128.    I2cwbyte Addressr                                          'slave address (read)
  129.    I2crbyte Value , 9                                         'read byte
  130.    I2cstop                                                    'generate stop
  131. End Sub
复制代码
51hei.png
所有资料51hei提供下载:
89C52LCD DS1820 24c04.zip (26.37 KB, 下载次数: 10)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表