Dit keer geen vraag maar misschien wel leuk om te tonen:
(misschien elders posten?)
Een programmaatje mbv MikroBasic, dat gebruik maakt van twee I2C-modulen van de PIC16F1847.
Daarbij zijn 2 RTC klokjes aangesloten; een DS3231 en een DS1307.
Beide zijn bijna gelijk, zij het dat bij de 1307 een CH-bit ("Halt") bij de seconden zit. Deze moet je er bij het lezen dus afhalen met
.
Er is een LCD diplay 16*2 aangesloten met een HD44780 chip, waarbij dus gebruik gemaakt wordt van een 4 data lijnen, een EN en RS.
Het programma toont van beide RTC-en de datum en de tijd, waarbij je dus kan zien hoe groot het verschil na verloop van tijd gaat worden.
pic basic code:
program Tijd1847
dim LCD_RS as sbit at LATB6_bit
LCD_EN as sbit at LATB7_bit
LCD_D4 as sbit at LATA0_bit
LCD_D5 as sbit at LATA1_bit
LCD_D6 as sbit at LATA2_bit
LCD_D7 as sbit at LATA3_bit
dim LCD_RS_Direction as sbit at TRISB6_bit
LCD_EN_Direction as sbit at TRISB7_bit
LCD_D4_Direction as sbit at TRISA0_bit
LCD_D5_Direction as sbit at TRISA1_bit
LCD_D6_Direction as sbit at TRISA2_bit
LCD_D7_Direction as sbit at TRISA3_bit
dim Time As Byte[7]
Str As String[3]
Dim Txt as string[2]
Sub procedure Write_ds3231_1()
i2c1_Start()
i2c1_Wr(0xD0)
i2c1_Wr(0x00)
i2c1_Repeated_Start()
i2c1_Wr(0xD0)
i2c1_Wr(0x00)
i2c1_Wr(0x01)
i2c1_Wr(0x20)
i2c1_Wr(0x23)
i2c1_Wr(0x01)
i2c1_Wr(0x01)
i2c1_Wr(0x12)
i2c1_Wr(0x19)
i2c1_Stop()
End Sub
Sub procedure Write_ds3231_2()
i2c2_Start()
i2c2_Wr(0xD0)
i2c2_Wr(0x00)
i2c2_Repeated_Start()
i2c2_Wr(0xD0)
i2c2_Wr(0x00)
i2c2_Wr(0x01)
i2c2_Wr(0x20)
i2c2_Wr(0x23)
i2c2_Wr(0x01)
i2c2_Wr(0x01)
i2c2_Wr(0x12)
i2c2_Wr(0x19)
i2c2_Stop()
End Sub
Sub procedure Read_DS3231_1()
i2c1_Start()
i2c1_Wr(0xD0)
i2c1_Wr(0x00)
i2c1_Repeated_Start()
i2c1_Wr(0xD1)
Time[0] = Bcd2Dec(i2c1_Rd(1) And 0x7F)
Time[1] = Bcd2Dec(i2c1_Rd(1))
Time[2] = Bcd2Dec(i2c1_Rd(1) And 0x3F)
Time[3] = Bcd2Dec(i2c1_Rd(1))
Time[4] = Bcd2Dec(i2c1_Rd(1))
Time[5] = Bcd2Dec(i2c1_Rd(1) And 0x7F)
Time[6] = Bcd2Dec(i2c1_Rd(0))
i2c1_Stop()
End Sub
Sub procedure Read_DS3231_2()
i2c2_Start()
i2c2_Wr(0xD0)
i2c2_Wr(0x00)
i2c2_Repeated_Start()
i2c2_Wr(0xD1)
Time[0] = Bcd2Dec(i2c2_Rd(1) And 0x7F)
Time[1] = Bcd2Dec(i2c2_Rd(1))
Time[2] = Bcd2Dec(i2c2_Rd(1) And 0x3F)
Time[3] = Bcd2Dec(i2c2_Rd(1))
Time[4] = Bcd2Dec(i2c2_Rd(1))
Time[5] = Bcd2Dec(i2c2_Rd(1) And 0x7F)
Time[6] = Bcd2Dec(i2c2_Rd(0))
i2c2_Stop()
End Sub
main:
ANSELB = 0
ANSELA = 0
TRISA = 0
TRISB = 0
OSCCON = %11101011
i2c1_init(100000)
i2c2_init(100000)
Lcd_Init()
Lcd_Cmd(_LCD_CLEAR)
Lcd_Cmd(_LCD_CURSOR_OFF)
While True
Read_DS3231_1()
Delay_ms(5)
ByteToStrWithZeros(Time[0], Txt)
LCD_Out(1,14, txt)
ByteToStrWithZeros(Time[1], Txt)
LCD_Out(1,11, Txt + ":")
ByteToStr(Time[2], Str)
LCD_Out(1,8, Str + ":")
ByteTostr (Time[4], Str)
ltrim(str)
LCD_Out(1,1, Str)
ByteTostr (Time[5], Str)
ltrim(str)
LCD_Out(1,3, Str)
ByteTostr (Time[6], Str)
ltrim(str)
LCD_Out(1,5, Str)
delay_ms(5)
Read_DS3231_2()
Delay_ms(5)
ByteToStrWithZeros(Time[0], Txt)
LCD_Out(2,14, txt)
ByteToStrWithZeros(Time[1], Txt)
LCD_Out(2,11, Txt + ":")
ByteToStr(Time[2], Str)
LCD_Out(2,8, Str + ":")
ByteTostr (Time[4], Str)
ltrim(str)
LCD_Out(2,1, Str)
ByteTostr (Time[5], Str)
ltrim(str)
LCD_Out(2,3, Str)
ByteTostr (Time[6], Str)
ltrim(str)
LCD_Out(2,5, Str)
delay_ms(10)
Wend
end.