BASCOM是basic語言的開發(fā)工具,比C容易入門,雖然很多功能受限,但常用庫已經(jīng)集成在內(nèi),很適合初學者。本例中有些測試用代碼未刪除,但不影響功能。8051在1602液晶和串口輸出時間,到秒為0時讀取18B20溫度,顯示在液晶屏,并通過串口打印18B20的寄存器值,同時將分鐘和溫度值寫入24C04。仿真時如暫停可用鼠標右鍵查看24C04內(nèi)存儲器的值,一小時循環(huán)一次,覆蓋上一小時的記錄。本想加上讀取24C04和數(shù)碼管顯示的語句,但還是覺得留一點給有興趣的人自己做吧。
仿真原理圖如下(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, 下載次數(shù): 10)
2020-4-23 05:51 上傳
點擊文件名下載附件
|