Kód: Egész kijelölése
Print Bcd(ora) ; ":" ; Bcd(perc) ; ":" ; Bcd(masodperc)Kód: Egész kijelölése
Print Bcd(ora) ; ":" ; Bcd(perc) ; ":" ; Bcd(masodperc)Kód: Egész kijelölése
Writebyte = &B00100000Kód: Egész kijelölése
X=32
..
..
..
..
..
Writebyte = Bin(x)Kód: Egész kijelölése
Sub 1302_írás
Config Portd.5 = Output
Serialclock = 1
Serialin_ou = 0
Ds1302 = 1
Reset Serialclock
Reset Ds1302
Waitus 5
Set Ds1302
Waitus 5
Shiftout Serialin_ou , Serialclock , Writecommand , 3
Shiftout Serialin_ou , Serialclock , Writebyte , 3
Reset Serialclock
Waitus 5
Reset Ds1302
Waitus 5
End SubKód: Egész kijelölése
'================================================
'= DS1302 SERIAL CLOCK BASCOM PÉLDA =
'= =
'= AZ ALAPJÁT EGY JAPÁN OLDALON TALÁLTAM, =
'= (http://blog.daum.net/leewonil/8075398) =
'= ÁTÍRTAM TINY2313-AS TAVIR TINYBOARDRA =
'= =
'= FELHASZNÁLÁS ELŐNYEI: =
'= - ÁR KB BRUTTÓ 480 FT =
'= - 2-5.5v-IG MŰKÖDIK (DS1307 CSAK 5-RŐL) =
'= =
'= FELHASZNÁLÁS HÁTRÁNYAI: =
'= - SPI BUSZ MIATT 3 ADATLÁB KELL NEKI =
'= I2C BUSZOS DS1307-HEZ CSAK KETTŐ =
'= =
'= 2010.11.20 - DOGMAN =
'================================================
'
'- Real-Time Clock Counts Seconds, Minutes,
' Hours, Date of the Month, Month, Day of the
' Week, and Year with Leap-Year
' Compensation Valid Up to 2100
'- 31 x 8 RAM for Scratchpad Data Storage
'- Serial I/O for Minimum Pin Count
'- 2.0V to 5.5V Full Operation
'- Uses Less than 300nA at 2.0V
'- Single-Byte or Multiple-Byte (Burst Mode)
' Data Transfer for Read or Write of Clock or
' RAM Data
'- 8-Pin DIP or Optional 8-Pin SO for Surface
' Mount
'- Simple 3-Wire Interface
'- TTL-Compatible (VCC = 5V)
'- Optional Industrial Temperature Range:
' -40°C to +85°C
'- DS1202 Compatible
'- Underwriters Laboratory (UL) Recognized
' 32.768 kHz
' | |
' +--------------+
' | X1 X2 |
' | |
' Portd.3 --| CE VCC2 | -- 2-5.5V
' | |
' Pind.4 --| IO |
' | |
' Portd.5 --| SCLK VCC1 | -- 3.3V BATT.
' | |
' | GND |
' +--------------+
' |
' -
'================================================
$regfile = "ATtiny2313.dat"
$crystal = 7372800
$baud = 9600
'LCD konfig
Config Pinb.3 = Output
Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7 , E = Portb.1 , Rs = Portb.2
'LCD háttérvilágítás be
Portb.3 = 1
Dim Wr_command As Byte
Dim Wr_byte As Byte
Dim Rd_command As Byte
Dim Rd_byte As Byte
Dim T1 As Byte
Config Portd.5 = Output
Config Portd.3 = Output
Rtc_clock Alias Portd.5 'DS1302 SCLK
Rtc_out Alias Pind.4 'DS1302 IO '
Ds1302 Alias Portd.3 'DS1302 CE (régebbi adatlapokon még RST-nek jelölve)
Declare Sub Ds1302_read
Declare Sub Ds1302_write
Declare Sub Write_data
Declare Sub Read_data
'================================================================
Main:
Locate 1 , 1
Lcd "Start..."
' idő és dátum beállítása
' ezt az első futtatás után ki lehet REM-elni,
' hogy később az óra ne mindig innen induljon
Write_data
Cls
Do
Locate 2 , 10
Waitms 100
Read_data
Loop
End 'end program
'*****************************************************************************
Sub Write_data
Wr_command = &H8E
Wr_byte = &B00000000
Ds1302_write
Waitus 5
Wr_command = &H8C '// YY
Wr_byte = Makebcd(10) '2010
Ds1302_write
Wr_command = &H8A '// Day
Wr_byte = &B00000100
Ds1302_write
Wr_command = &H88 '//mon
Wr_byte = Makebcd(11) '//04
Ds1302_write
Wr_command = &H86 '//dd
Wr_byte = 6 '//06
Wr_byte = Makebcd(21)
Ds1302_write
Wr_command = &H84 '//HH
Wr_byte = Makebcd(02)
Ds1302_write
Wr_command = &H82 '//MM
Wr_byte = Makebcd(58)
Ds1302_write
Wr_command = &H80 '//SS
Wr_byte = Makebcd(00)
Ds1302_write
End Sub
'*****************************************************************************
Sub Read_data
Rd_command = &H85
Ds1302_read
Locate 1 , 1
Lcd Makedec(rd_byte)
Rd_command = &H83
Ds1302_read
Lcd ":" ; Makedec(rd_byte)
Rd_command = &H81
Ds1302_read
Lcd ":" ; Makedec(rd_byte) ; " "
Rd_command = &H8D
Ds1302_read
Locate 2 , 1
Lcd "20" ; Makedec(rd_byte)
Rd_command = &H89
Ds1302_read
Lcd "-" ; Makedec(rd_byte)
Rd_command = &H87
Ds1302_read
Lcd "-" ; Makedec(rd_byte)
'
End Sub
'*****************************************************************************
Sub Ds1302_read
Config Portd.4 = Output
Rtc_o Alias Portd.4
Rtc_clock = 1
Rtc_o = 1
Ds1302 = 1
Reset Rtc_clock
Reset Ds1302
Waitus 5
Set Ds1302
Waitus 5
Shiftout Rtc_o , Rtc_clock , Rd_command , 3
Config Portd.4 = Input
Serialin Alias Pind.4
Rtc_clock = 1
Rtc_o = 0
Ds1302 = 1
Shiftin Serialin , Rtc_clock , Rd_byte , 2
Reset Ds1302
Rotate Rd_byte , Left , 1
Waitus 5
End Sub
'*****************************************************************************
Sub Ds1302_write
Config Portd.4 = Output
Rtc_clock = 1
Rtc_o = 0
Ds1302 = 1
Reset Rtc_clock
Reset Ds1302
Waitus 5
Set Ds1302
Waitus 5
Shiftout Rtc_o , Rtc_clock , Wr_command , 3
Shiftout Rtc_o , Rtc_clock , Wr_byte , 3
Reset Rtc_clock
Waitus 5
Reset Ds1302
Waitus 5
End Sub
'*****************************************************************************Kód: Egész kijelölése
Print "{027}[2JSettime"
Input "Year (0..99) " , Year
Input "Month (1..12)" , Month
Input "Day (1..31) " , D
Input "Day (of week)" , Day
Input "Hour (0..23) " , H
Input "Minute (0..59)" , M
Input "Second (0..59)" , S
S = Makebcd(s) 'seconds
M = Makebcd(m) 'minutes
H = Makebcd(h) 'hours
D = Makebcd(d) 'days
Month = Makebcd(month) 'months
I2cstart 'generate start
I2cwbyte &B11011110 'write address
I2cwbyte 0 'address of RTC: 0
I2cwbyte 0 'STOP RTC (and sec=0)
I2cwbyte M 'Min
I2cwbyte H 'Hour
D.3 = 1 'VBATEN=1
I2cwbyte D 'Day & VBATEN=1
I2cwbyte Day 'Date
I2cwbyte Month 'month
I2cwbyte Year 'Year
I2cstop
I2cstart
I2cwbyte &B11011110
I2cwbyte 0 '0. CIM
S.7 = 1 'Start timer
I2cwbyte S 'Write second & Set start
I2cstopKód: Egész kijelölése
Print "{027}[2JGettime"
Do
I2cstart 'generate start
I2cwbyte &B11011110 'write addres of MCP79410
I2cwbyte 0 'select sec register
I2cstart 'generate repeated start
I2cwbyte &B11011111 'read address of MCP79410
I2crbyte S , Ack
S.7 = 0 'mask start bit
I2crbyte M , Ack
I2crbyte H , Ack
I2crbyte D , Ack 'day of week, NC
I2crbyte Day , Ack
I2crbyte Month , Ack
Month.5 = 0 'skip LeapYear bit
I2crbyte Year , Nack
I2cstop 'generate stop
Print Chr(27) ; "[2;2f"; ' VT100 emulation set pos to 2,2
Print "Time "
Print Bcd(h) ; ":" ; Bcd(m) ; ":" ; Bcd(s)
Print "Day : " ; Bcd(day) ; " Month : " ; Bcd(month) ; " Err:" ; Err
Waitms 50
Loop Until Inkey() = 27Kód: Egész kijelölése
Input "Write-test (0-skip)?" , Temp1b
If Temp1b = 0 Then Goto Prog42olvas
Print "Start MCP-EEPWrite (32 byte)"
Temp1b = 0 ' Generate test data (=0<256).
Do
I2cinit
I2cstart
I2cwbyte &B10101110 'MCP EEP address (I2Caddress)
I2cwbyte Temp1b 'address of data
I2cwbyte Temp1b 'data for write
I2cstop
Incr Temp1b ' Generate test data (=0<256).
Loop Until Temp1b = 32
Print "All written." ' Tell what is going on.
Prog42olvas:
Print
Print "Read back...."
Temp1b = 0
Do
I2cinit
I2cstart
I2cwbyte &B10101110 'MCP EEP i2caddress for write
I2cwbyte Temp1b 'addres for read
I2cstart
I2cwbyte &B10101111 'MCP EEP i2caddress for read
I2crbyte Temp2b , Nack
I2cstop
If Err = 0 Then ' Check status of operation.
Print "OK :" ; Temp1b ; " " ; Temp2b ' Report success.
Else ' or
Print "Error:" ; Temp1b ' Report failure.0
End If
Incr Temp1b ' Increment address counter.
Waitms 100
Loop Until Temp1b = 32
Print "All read OK." ' Tell what is going on.
Wait 5Kód: Egész kijelölése
I2cstart ' Generate start code
I2cwbyte Rtc_w ' send address
I2cwbyte 0 ' start address in 1307
I2cstart ' Generate start code
I2cwbyte Rtc_r ' send address
I2crbyte Rtc_sec , Ack
I2crbyte Rtc_min , Ack ' MINUTES
I2crbyte Rtc_hour , Ack ' Hours
I2crbyte Rtc_dow , Ack ' Day of Week
I2crbyte Rtc_day , Ack ' Day of Month
I2crbyte Rtc_month , Ack ' Month of Year
I2crbyte Rtc_year , Nack ' Year
I2cstop
If Err = 1 Then
Reset Global_error.1
End If
If Rtc_sec.7 = 1 Then ' Oscillator halted
Rtc_sec.7 = 0
I2cstart ' Generate start code
I2cwbyte Rtc_w ' send address
I2cwbyte 0 ' start address in 1307
I2cwbyte Rtc_sec ' Send Data to SECONDS
I2cstop
End If