RTC DS3231 Read error

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
NAGARSU5
Posts: 12
Joined: 19 Nov 2016 05:06

RTC DS3231 Read error

#1 Post by NAGARSU5 » 30 May 2022 01:54

Dear helpers,
Im doing first RTC project based on DS3231 module. It works but sometimes the reading getting erratic and the values all goes to zero out of sudden. So im wondering is there any particular bits to turn on or off before the reading or is the DS3231 address to read is really correct. Kindly advise. TQ.

So here's the code.

program Tester
' Declarations section
dim L1 as sbit at PORTB.6'
dim L2 as sbit at PORTB.7'
dim SW1 as sbit at PORTB.0'
dim SW2 as sbit at PORTB.1'
dim T2 as byte'
hour1,mins1,secs1,day1,date1,mon1,yr1 as byte
hours,minutes,seconds,wkday,_date,_month,year as byte
hour,mins,secs,day,date,mon,yr as word
SECDATA,MINDATA,HOURDATA,DAYDATA,DATEDATA,MONDATA,YEARDATA as byte
time_str as string[9]
date_str as string[9]
msg1 as string[3]
'
'
'program RTC_Read
'Software I2C connections
dim Soft_I2C_Scl as sbit at RC3_bit
Soft_I2C_Sda as sbit at RC4_bit
Soft_I2C_Scl_Direction as sbit at TRISC3_bit
Soft_I2C_Sda_Direction as sbit at TRISC4_bit
'End Software I2C connections
'
'LCD Drive bits (4-Bit Mode)
dim LCD_EN as sbit at RB5_bit
dim LCD_RS as sbit at RB4_bit
dim LCD_D4 as sbit at RD4_bit
dim LCD_D5 as sbit at RD5_bit
dim LCD_D6 as sbit at RD6_bit
dim LCD_D7 as sbit at RD7_bit

LCD_EN_Direction as sbit at TRISB5_bit
LCD_RS_Direction as sbit at TRISB4_bit
LCD_D4_Direction as sbit at TRISD4_bit
LCD_D5_Direction as sbit at TRISD5_bit
LCD_D6_Direction as sbit at TRISD6_bit
LCD_D7_Direction as sbit at TRISD7_bit

'******************************************************************



'
' DEVICE 18F4520 20mhz
' FUNCTION: RTC (Real Time Clock DS3231) Read, Write and show on LCD

sub procedure interrupt ' Timer1 interrupt(every 0.5 sec.oscillating)
if(TMR1IF_bit)then ' if Overflow bit HIGH **************
T2=T2+1'
end if
if(T2>1)then ' 1 for 0.5 sec *
L2=not L2
T2=0 ' *
end if ' *
TMR1IF_bit= 0 ' Reset Overflow bit *
exit
end sub '***********************************************************]
'
'Reads time and date information from RTC (DS3231)
sub procedure Read_Time()
Soft_I2C_Start()
Soft_I2C_Write(0xD0)
Soft_I2C_Write(0)
Soft_I2C_Start()
Soft_I2C_Write(0xD1)
seconds = Soft_I2C_Read(1)
minutes = Soft_I2C_Read(1)
hours = soft_I2C_Read(1)
wkday = Soft_I2C_Read(1)
_date = Soft_I2C_Read(1)
_month = Soft_I2C_Read(1)
year = Soft_I2C_Read(0)
Soft_I2C_Stop()
exit
end sub
'*******************************************************************
sub procedure Transform_Time()
seconds = ((seconds and 0x70) >> 4)* 10 + (seconds and 0x0F)
minutes = ((minutes and 0xF0) >> 4)* 10 + (minutes and 0x0F)
hours = ((hours and 0x30) >> 4)*10 + (hours and 0x0F)
_date = ((_date and 0xF0) >> 4)*10 + (_date and 0x0F)
_month = ((_month and 0x10) >>4)*10 +(_month and 0x0F)
year = ((year and 0xF0) >> 4)*10 + (year and 0x0F)
time_str[0] = (hours div 10) + 48
time_str[1] = (hours mod 10) + 48
time_str[2] = ":"
time_str[3] = (minutes div 10) + 48
time_str[4] = (minutes mod 10) + 48
time_str[5] = ":"
time_str[6] = (seconds div 10) + 48
time_str[7] = (seconds mod 10) + 48
time_str[8] = 0
date_str[0] = (_date div 10) + 48
date_str[1] = (_date mod 10) + 48
date_str[2] = "/"
date_str[3] = (_month div 10) + 48
date_str[4] = (_month mod 10) + 48
date_str[5] = "/"
date_str[6] = (year div 10) + 48
date_str[7] = (year mod 10) + 48
date_str[8] = 0
exit
end sub
'********************************************************************
sub procedure Display_RTC
Lcd_Out(1,1,time_str)
Lcd_Out(2,1,date_str)
'
'Day of the week display
bytetostr(DAYDATA,msg1)
Lcd_Out(3,1,"DAY>"+msg1)
exit
end sub
'********************************************************************


main:
ADCON1=0x0f 'PORTA inputs all are digital
ADCON0=0x00 'Disable analog functions
CMCON=0x07 'Turn off Comparators
CCP1CON=0x00'Turns off Capture/ Compare/PWM
CCP2CON=0x00'
CCPR1L=0x00'
CCPR1H=0x00'
CCPR2L=0x00
CCPR2H=0x00'
TRISA=0xff '00111111
PORTA=0XFF '
TRISD=0x00 '00000000
PORTD=0X00 '
TRISC=0X39 '0X39 11111001
PORTC=0X39 '
TRISB=0x3F '00111111
PORTB=0X3F '
TRISE.2=0' '
PORTE.2=0 '
T1CON= 0x031 ' Timer 1(16-bit) usage control bits **********
TMR1IF_bit= 0 '// // // *
TMR1H= 0x0B ' 0B *
TMR1L= 0xDC ' DC *
TMR1IE_bit= 1 ' 1111111 *
INTCON =0xC0 '********************************************
L1=0 '
L2=0 '
Lcd_Init() '
Lcd_Cmd(_LCD_CLEAR) ' Clear display
Lcd_Cmd(_LCD_CURSOR_OFF) ' Cursor off
Soft_I2C_Init() ' Initialize Soft I2C communication
' Delay_ms(5000)
SECDATA=0 '
mins=54 'minutes 'Section to write RTC value to the unit**********
hour=21 'hour (24 hours format)
day=05 'day of the week (1=SUN 2=MON 3=TUE 4=WED 5=THU 6=FRI 7=SAT)
date=31 'date
mon=03 'month
yr=22 'year the last two digits
mins1=Dec2Bcd(mins)'Convert decimal to BCD to write into RTC
hour1=Dec2Bcd(hour)
day1=Dec2Bcd(day)
date1=Dec2Bcd(date)
mon1=Dec2Bcd(mon)
yr1=Dec2Bcd(yr)
if not SW1 then 'the SW1 once pressed then will update RTC value
Soft_I2C_Init()
Soft_I2C_Start()
Soft_I2C_Write(0xD0)
Soft_I2C_Write(0)
Soft_I2C_Write(0x80)
Soft_I2C_Write(mins1)
Soft_I2C_Write(hour1)
Soft_I2C_Write(day1)
Soft_I2C_Write(date1)
Soft_I2C_Write(mon1)
Soft_I2C_Write(yr1)
Soft_I2C_Stop()
Soft_I2C_Start()
Soft_I2C_Write(0xD0)
Soft_I2C_Write(0)
Soft_I2C_Write(0)
Soft_I2C_Stop()
end if


'**********************************************************
while (true) ' Start of the program
'**********************************************************
Display_RTC() ' Prepare and display on Lcd
Transform_Time() ' Format date and time
Read_Time() ' Read time from RTC(DS3231)
'
'RTC Data conversion into bytes value for comparison & switching
SECDATA=Bcd2Dec(seconds)
MINDATA=Bcd2Dec(minutes)
HOURDATA=Bcd2Dec(hours)
DAYDATA=Bcd2Dec(wkday)
DATEDATA=Bcd2Dec(_date)
MONDATA=Bcd2Dec(_month)
YEARDATA=Bcd2Dec(year)
if(SECDATA>50)then
L1=1
else
L1=0
end if
wend
end.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: RTC DS3231 Read error

#2 Post by filip » 02 Jun 2022 11:33

Hi,

Have you tried any of the DS3231 examples from the LibStock, there are many of them there ?

Also, have a look at our implementation (in C) :
https://github.com/MikroElektronika/mik ... icks/rtc10

Regards,
Filip.

NAGARSU5
Posts: 12
Joined: 19 Nov 2016 05:06

Re: RTC DS3231 Read error

#3 Post by NAGARSU5 » 04 Jun 2022 11:16

Hi filip,

The reading looks stable after i replace soft_I2C with I2C1_Rd commands. Also thanks for the link sharing. :)

Post Reply

Return to “mikroBasic PRO for PIC General”