BASCOM是basic语言的开发工具,比C容易入门,虽然很多功能受限,但常用库已经集成在内,很适合初学者。本例中有些测试用代码未删除,但不影响功能。8051在1602液晶和串口输出时间,到秒为0时读取18B20温度,显示在液晶屏,并通过串口打印18B20的寄存器值,同时将分钟和温度值写入24C04。仿真时如暂停可用鼠标右键查看24C04内存储器的值,一小时循环一次,覆盖上一小时的记录。本想加上读取24C04和数码管显示的语句,但还是觉得留一点給有兴趣的人自己做吧。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- ' Program Led1.bas
- $crystal = 11059000
- $baud = 9600
- '$crystal = 12000000
- '$baud = 4800
- Config 1wire = P1.7 'use this pin
- Dim Ar(8) As Byte , A As Byte , I As Byte
- Declare Sub Write_eeprom(adres As Byte , Value As Byte)
- Declare Sub Read_eeprom(adres As Byte , Value As Byte)
- Config I2cdelay = 1 'default so not needed
- 'declare constants
- Const Addressw = 160
- Const Addressr = 161
- Dim B1 As Byte , Adres As Byte , Value As Byte 'dim byte
- Dim T As Byte
- Dim T1 As Byte
- Dim T2 As Byte
- Dim Ct As Byte
- Dim H As Byte
- Dim H1 As Byte
- Dim H2 As Byte
- Dim M As Byte
- Dim M1 As Byte
- Dim M2 As Byte
- Dim S As Byte
- Dim S1 As Byte
- Dim S2 As Byte
- Dim Led As Bit 'LED is a Bit variable since it will turn on and
- Dim Led1 As Bit
- Config Lcd = 16 * 2
- 'P1.0 = 0
- Initlcd
- Cls
- Config Timer0 = Timer , Gate = Internal , Mode = 1
- 'Timer0 = counter : timer0 operates as a counter
- 'Gate = Internal : no external gate control
- 'Mode = 2 : 8-bit auto reload (default)
- 'Mode = 1 : 16-bit reload
- On Timer0 Timer_0_int
- Load Timer0 , 0 'when the timer reaches 100 an interrupt will occur
- Enable Interrupts 'enable the use of interrupts
- Enable Timer0 'enable the timer
- Rem Setting Of Priority
- Priority Set Timer0 'highest priority
- Start Timer0 'start the timer
- Call Write_eeprom(0 , 48)
- Call Write_eeprom(1 , 49) 'write value of three to address 1 of EEPROM
- Print Value
- Call Read_eeprom(0 , Value) : Print Value 'read it back
- Call Read_eeprom(5 , Value) : Print Value 'again for address 5
- Led1 = 1 'initial value
- Led = 0 'Do is a start of a Do-Loop loop
- Do
- Loop 'end of Do-Loop loop
- End
- Rem The Interrupt Handler For The Timer0 Interrupt
- Timer_0_int:
- If Ct = 13 Then
- Ct = 0
- P0.1 = Led1
- P0.0 = Led
- Led = Not Led
- Led1 = Not Led1 'invert LED value
- H1 = H / 10
- H2 = H Mod 10
- M1 = M / 10
- M2 = M Mod 10
- S1 = S / 10
- S2 = S Mod 10
- Print "Time is:" ; H1 ; H2 ; ":" ; M1 ; M2 ; ":" ; S1 ; S2 '; (Second / 10) ; (Second%10) ' test serial com
- Lowerline
- Lcd "Time is:" ; H1 ; H2 ; ":" ; M1 ; M2 ; ":" ; S1 ; S2
- S = S + 1
- If S = 60 Then
- S = 0
- M = M + 1
- 1wreset 'reset the device
- Print Err 'print error 1 if error
- 1wwrite &HCC
- 1wwrite &H44 'read ROM command
- Waitms 255
- Waitms 255
- Waitms 240
- 1wreset
- 1wwrite &HCC
- 1wwrite &HBE
- For I = 1 To 8
- Ar(i) = 1wread() 'place into array
- Next
- For I = 1 To 8
- Printhex Ar(i); 'print output
- Next
- Print
- T1 = Ar(1) / 16 'linefeed
- T2 = Ar(2) * 16
- T = T1 + T2
- Upperline
- Lcd "Temp is:" ; T
- Call Write_eeprom(m , T)
- If M = 60 Then
- M = 0
- H = H + 1
- If H = 24 Then
- H = 0
- End If
- End If
- End If
- Else
- Incr Ct
- End If
- Return
- 'sample of writing a byte to EEPROM AT2404
- Sub Write_eeprom(adres As Byte , Value As Byte)
- I2cstart 'start condition
- I2cwbyte Addressw 'slave address
- I2cwbyte Adres 'asdress of EEPROM
- I2cwbyte Value 'value to write
- I2cstop 'stop condition
- Waitms 10 'wait for 10 milliseconds
- End Sub
- 'sample of reading a byte from EEPROM AT2404
- Sub Read_eeprom(adres As Byte , Value As Byte)
- I2cstart 'generate start
- I2cwbyte Addressw 'slave adsress
- I2cwbyte Adres 'address of EEPROM
- Waitms 10
- I2cstart 'repeated start
- I2cwbyte Addressr 'slave address (read)
- I2crbyte Value , 9 'read byte
- I2cstop 'generate stop
- End Sub
复制代码
所有资料51hei提供下载:
89C52LCD DS1820 24c04.zip
(26.37 KB, 下载次数: 10)
|