DS1388

General discussion on mikroBasic.
Post Reply
Author
Message
4ali
Posts: 10
Joined: 23 Feb 2009 16:20

DS1388

#1 Post by 4ali » 07 Apr 2011 19:47

Hi guys
I need a working example for RTC based on DS1388 (with tenths and hundreds of second)
Try already to modify a working example based on DS1307 with NO success.
I believe i do something wrong with the addresses, but i don't know what.
Please help!!!!
Thanks

California
Posts: 361
Joined: 14 Dec 2005 20:56
Location: Slovenia

Re: DS1388

#2 Post by California » 08 Apr 2011 09:50

Code?
Vlado

4ali
Posts: 10
Joined: 23 Feb 2009 16:20

Re: DS1388

#3 Post by 4ali » 09 Apr 2011 08:04

Hi Vlado

the initial code for DS1307 is the following


program ds1307clock

dim Text as string[3]
dim OldState as byte
dim ShowDateTime as byte
dim Aux as byte
dim g_hours, g_minutes, g_seconds as byte
dim g_year, g_month, g_day, g_date as byte

'dim xTSec as byte
'dim Time_calc as byte[3] ' Use 7 digits array xx:xx:xx.x for time

dim xMin, xHr as word
dim Time_calc as byte[8] ' Use 7 digits array xx:xx:xx.x for time
dim markon, markoff, sethron, setminon as byte

sub procedure marktime()
if Button(PortD, 7, 1, 1) then
markon = 1
end if

if Button(PortD, 6, 1, 1) then
markoff = 1
end if
end sub

sub procedure settime()
if Button(PortD, 5, 1, 1) then
sethron = 1
end if

if Button(PortD, 4, 1, 1) then
setminon = 1
end if

end sub

'//----------------------------------------------------------------------------/
'//
'//
'//----------------------------------------------------------------------------/
sub procedure DS1307_Init()

Dim Seconds as byte

Seconds = 0

Soft_I2C_Start()
Soft_I2C_Write(0xD0) '// WR to RTC
Soft_I2C_Write(0x00) '// REG 0

Soft_I2C_Start()
Soft_I2C_Write(0xD1) '// RD from RTC
seconds = Bcd2Dec(Soft_I2C_Read(0)) '// Read current "seconds" in DS1307
Soft_I2C_Stop()
seconds = (seconds and 0x7F)

Delay_ms(50)

Soft_I2C_Start()
Soft_I2C_Write(0xD0) '// WR to RTC
Soft_I2C_Write(0x00) '// REG 0
Soft_I2C_Write(Dec2Bcd(seconds)) '// Start oscillator with current "seconds value
Soft_I2C_Start()
Soft_I2C_Write(0xD0) '// WR to RTC
Soft_I2C_Write(0x07) '// Control Register
Soft_I2C_Write(0x80) '// Disable squarewave output pin
Soft_I2C_Stop()
end sub

'//----------------------------------------------------------------------------/
'//
'//
'//----------------------------------------------------------------------------/
sub procedure DS1307_DataSet(dim xSeconds as byte,
dim xMinutes as byte,
dim xHours as byte,
dim xDay as byte,
dim xDate as byte,
dim xMonth as byte,
dim xYear as byte)

Soft_I2C_start ' I2C start communication
Soft_I2C_Write($D0) ' writing command to DS1307
Soft_I2C_Write($00) ' write to $00 address
Soft_I2C_Start ' I2C start communication
Soft_I2C_Write($D0) ' writing command to DS1307
Soft_I2C_Write($00) ' write to starting address $00

Soft_I2C_Write(Dec2Bcd(xSeconds)) 'Seconds, Bit 7 (CH) = 0 -> start oscilador
Soft_I2C_Write(Dec2Bcd(xMinutes)) 'Minutes
Soft_I2C_Write(Dec2Bcd(xHours)) 'Hours
Soft_I2C_Write(Dec2Bcd(xDay)) 'Day of week
Soft_I2C_Write(Dec2Bcd(xDate)) 'Day
Soft_I2C_Write(Dec2Bcd(xMonth)) 'Month
Soft_I2C_Write(Dec2Bcd(xYear)) 'Year

Soft_I2C_Write(0x80) ' Disable squarewave output pin 32.768kHz
Soft_I2C_stop ' I2C stop communication

end sub

'//----------------------------------------------------------------------------/
'//
'//
'//----------------------------------------------------------------------------/
sub procedure DS1307_GetTime()

Soft_I2C_start
Soft_I2C_Write($D0)
Soft_I2C_Write(0)
Soft_I2C_Start
Soft_I2C_Write($D1)
g_seconds = Bcd2Dec(Soft_I2C_Read(1))
g_minutes = Bcd2Dec(Soft_I2C_Read(1))
g_hours = Bcd2Dec(Soft_I2C_Read(1))
g_day = Bcd2Dec(Soft_I2C_Read(1))
g_date = Bcd2Dec(Soft_I2C_Read(1))
g_month = Bcd2Dec(Soft_I2C_Read(1))
g_year = Bcd2Dec(Soft_I2C_Read(0))
Soft_I2C_stop

end sub

'//----------------------------------------------------------------------------/
'//
'//
'//----------------------------------------------------------------------------/
sub procedure ByteToString(dim data as byte, dim byref text as char[2])

dim strData as char[3]
ByteToStr(data, strData)
text[0] = strData[1]
text[1] = strData[2]
text[2] = 0
if text[0] = 32 then text[0] = "0" end if

end sub

'//----------------------------------------------------------------------------/
'//
'//
'//----------------------------------------------------------------------------/
sub procedure DisplayData(dim row as byte, dim col as byte, dim data as byte)

dim text as char[2]
ByteToString(data, text)
Lcd_out(row, col, text)

end sub

'//----------------------------------------------------------------------------/
'//
'//
'//----------------------------------------------------------------------------/
sub procedure DisplayDateTime(Dim Status as byte)

if Status then
if Aux <> 1 then
Lcd_Cmd(LCD_TURN_ON)
Aux = 1
end if
' Time
DisplayData(1, 1, g_hours)
DisplayData(1,4, g_minutes)
DisplayData(1,7, g_seconds)

'Day
if (g_day)=1 then
Lcd_Out (2,1,"Mon")
else
if (g_day)=2 then
Lcd_Out (2,1,"Tue")
else
if (g_day)=3 then
Lcd_Out (2,1,"Wed")
else
if (g_day)=4 then
Lcd_Out (2,1,"Thu")
else
if (g_day)=5 then
Lcd_Out (2,1,"Fri")
else
if (g_day)=6 then
Lcd_Out (2,1,"Sat")
else
if (g_day)=7 then
Lcd_Out (2,1,"Sun")
end if
end if
end if
end if
end if
end if
end if

' Date
DisplayData(2, 5, g_date)
DisplayData(2, 8, g_month)
DisplayData(2, 13, g_year)
else
Lcd_Cmd(LCD_TURN_OFF)
Aux = 0
end if

end sub

'//----------------------------------------------------------------------------/
'//
'//
'//----------------------------------------------------------------------------/
main:
xHr = 0
xMin = 0

TRISb = 0 ' PORTC is output
PORTb = 0


TRISC = 0 ' PORTC is output
PORTC = 0

TRISD = %11111111
PORTD = 0

Lcd_Init(PORTB) ' Initialize LCD connected to PORTD
Lcd_Cmd(LCD_CLEAR) ' Send command to LCD "clear display"
Lcd_Cmd(LCD_CURSOR_OFF) ' Send command cursor off
Lcd_out(1, 1, " DS1307 Clock ")
Lcd_out(2, 5, "Test")

Delay_ms(3000)
Lcd_Cmd(LCD_CLEAR) ' Send command to LCD "clear display"
Lcd_Cmd(LCD_CURSOR_OFF) ' Send command cursor off
Lcd_out(1, 3, ":")
Lcd_Out(1, 6, ":")
Lcd_Out(1, 9, ".")
Lcd_out(2, 7, "/")
Lcd_Out(2, 10,"/20")

' Initialize full master mode
' SDA --> RC4
' SCL --> RC2
PORTC.4 = 1
PORTC.2 = 1
Soft_I2C_Config(PORTC, 4, 2)

DS1307_Init

ShowDateTime = true

while true

settime

marktime

if (markon = 1) and Button(PortD, 7, 1, 0) then ' mark on while button release

LCD_Out(3,1,"Mark>") ' show mark on time
LCD_chr(3,8,":") ' :
LCD_chr(3,11,":") ' :
LCD_chr(3,14,".") ' .


DisplayData(3, 6, g_hours)
DisplayData(3,9, g_minutes)
DisplayData(3,12, g_seconds)

markon = 0 ' clear mark on for next time mark

end if

if (markoff = 1) and Button(PortD, 6, 1, 0) then ' mark off while button release

LCD_Out(3,1," ") ' clear mark time
markoff = 0 ' clear mark off

end if

DS1307_GetTime

DisplayDateTime(ShowDateTime)

'-----------------------------------------------------------
'/ Set a defined date and time
'/ Seconds,minutes,hours,dayofweek,day,month,year
'/------------------------------------------------
timeset:

settime
' set time
if (sethron = 1) and Button(PortD, 5, 1, 0) then ' mark on while button release
xHr = xHr + 1

if xHr <> 24 then
Time_calc[1] = xhr mod 10
Time_calc[0] = integer(xhr div 10) mod 10
else
xHr = 0
Time_calc[1] = xhr
Time_calc[0] = xhr
end if

LCD_Out(3,1,"Set:")
LCD_Out(3,5,"Hr") ' show set hr
LCD_chr(3,8,Time_calc[0]+48) ' h
LCD_chr(3,9,Time_calc[1]+48) ' h


sethron = 0
end if

' set minute
if (setminon = 1) and Button(PortD, 4, 1, 0) then ' mark on while button release
xmin = xmin + 1
if xMin <> 60 then
Time_calc[3] = xmin mod 10
Time_calc[2] = integer(xmin div 10) mod 10
else
Time_calc[3] = xmin
Time_calc[2] = xmin
end if

LCD_Out(3,1,"Set:")
LCD_Out(3,11,"Min") ' show set min
LCD_chr(3,15,Time_calc[2]+48) ' m
LCD_chr(3,16,Time_calc[3]+48) ' m

setminon = 0
end if

'/ Set a defined date and time
'/ Seconds,minutes,hours,dayofweek,day,month,year
'/------------------------------------------------

if Button(PORTD, 2,1,1) then

DS1307_DataSet(00,xMin,xHr,1,01,01,01)

LCD_Out(3,1," ") ' clear set time
end if
'-----------------------------------------------------------
wend
end.


***Above code is working perfectly o.k. with DS1307,
then, when i change to DS1388, i add lines for the tenths of second
(unfortunately i'm out of my house today so i dont have with me the modified code),
and lcd show 65:65:65.65 on the time row
and 65/65/65 on the date row
clock is not running.

I will post the changes i made on Monday if you need it

Thanks / brgds
Kostas

4ali
Posts: 10
Joined: 23 Feb 2009 16:20

Re: DS1388

#4 Post by 4ali » 13 Apr 2011 16:45

Days are passing by and no help received

Please HHHHHEEEEELLLLLPPPPPP

srnet
Posts: 163
Joined: 28 Mar 2009 17:14

Re: DS1388

#5 Post by srnet » 13 Apr 2011 20:34

Seems to be an echo in here.

orpheedulogis
Posts: 240
Joined: 16 Jan 2010 22:26

Re: DS1388

#6 Post by orpheedulogis » 17 May 2011 22:03

In french, for DS1338 :

Code: Select all

[color=#000080]'------------------------------------------------------------

sub function Convert4bits(dim Valeur as byte) as byte
'sépare les diziemes des unités sur 4 bits (horloge)
dim Vhigh,Vlow,VTemp as byte

VHigh=valeur/10
VTemp=VHigh<<4
VLow=Valeur-(Vhigh*10)
result=Vtemp or Vlow

end sub

'------------------------------------------------------------

sub procedure WriteDateTime()

   I2C1_start
   I2C1_Wr(AdresseDS1338)
   I2C1_Wr($00)

   I2C1_Wr($80)
   I2C1_Wr(Convert4bits(HMinute))
   I2C1_Wr(Convert4bits(HHeure))
   I2C1_Wr(HJourDeLaSemaine)
   I2C1_Wr(Convert4bits(HDate))
   I2C1_Wr(Convert4bits(HMois))
   I2C1_Wr(Convert4bits(HAnnee))

   I2C1_stop
   I2C1_start
   I2C1_Wr(AdresseDS1338)
   I2C1_Wr($00)
   I2C1_Wr($00) 
   I2C1_stop
end sub


'------------------------------------------------------------

sub procedure ReadDateTime()

   I2C1_start()
   I2C1_Wr(AdresseDS1338)
   I2C1_Wr(0) 
   I2C1_repeated_Start
   I2C1_Wr(AdresseDS1338 or %00000001) 
   Hseconde = Bcd2Dec(I2C1_Rd (1))
   Hminute = Bcd2Dec(I2C1_Rd (1))
   Hheure  = Bcd2Dec(I2C1_Rd (1))
   HJourDelaSemaine = Bcd2Dec(I2C1_Rd (1))
   Hdate    = Bcd2Dec(I2C1_Rd (1))
   HMois   = Bcd2Dec(I2C1_Rd (1))
   HAnnee    = Bcd2Dec(I2C1_Rd (0))
   I2C1_stop()

end sub
'------------------------------------------------------------
[/color]

Post Reply

Return to “mikroBasic General”