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.
RTC DS3231 Read error
General discussion on mikroBasic PRO for PIC.
Author
Message
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.
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.
Return to “mikroBasic PRO for PIC General”
Jump to
- NECTO Studio
- ↳ IDE
- ↳ IDE General
- ↳ IDE Beta Testing
- ↳ IDE Wish List
- ↳ ARM AI Compilers
- ↳ mikroC AI for ARM
- ↳ mikroC AI for ARM General
- ↳ mikroC AI for ARM Beta Testing
- ↳ mikroC AI for ARM Wish List
- ↳ PIC AI compilers
- ↳ mikroC AI for PIC
- ↳ mikroC AI for PIC General
- ↳ mikroC AI for PIC Beta Testing
- ↳ mikroC AI for PIC Wish List
- ↳ PIC32 AI Compilers
- ↳ mikroC AI for PIC32
- ↳ mikroC AI for PIC32 General
- ↳ mikroC AI for PIC32 Beta Testing
- ↳ mikroC AI for PIC32 Wish List
- ↳ dsPIC AI Compilers
- ↳ mikroC AI for dsPIC
- ↳ mikroC AI for dsPIC General
- ↳ mikroC AI for dsPIC Beta Testing
- ↳ mikroC AI for dsPIC Wish List
- ↳ AVR AI Compilers
- ↳ mikroC AI for AVR
- ↳ mikroC AI for AVR General
- ↳ mikroC AI for AVR Beta Testing
- ↳ mikroC AI for AVR Wish List
- ↳ Additional Software
- ↳ Visual TFT AI
- ↳ Visual TFT AI General
- ↳ Visual TFT AI Beta Testing
- ↳ Visual TFT AI Wish List
- PRO Compilers
- ↳ PIC PRO Compilers
- ↳ PIC Compilers General
- ↳ mikroC PRO for PIC
- ↳ mikroC PRO for PIC General
- ↳ mikroC PRO for PIC Beta Testing
- ↳ mikroC PRO for PIC Wish List
- ↳ mikroBasic PRO compiler for PIC
- ↳ mikroBasic PRO for PIC General
- ↳ mikroBasic PRO for PIC Beta Testing
- ↳ mikroBasic PRO for PIC Wish List
- ↳ mikroPascal PRO for PIC
- ↳ mikroPascal PRO for PIC General
- ↳ mikroPascal PRO for PIC Beta Testing
- ↳ mikroPascal PRO for PIC Wish List
- ↳ dsPIC PRO Compilers
- ↳ dsPIC Compilers General
- ↳ mikroC PRO for dsPIC30/33 and PIC24
- ↳ mikroC PRO for dsPIC30/33 and PIC24 General
- ↳ mikroC PRO for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroC PRO for dsPIC30/33 and PIC24 Wish List
- ↳ mikroBasic PRO for dsPIC30/33 and PIC24
- ↳ mikroBasic PRO for dsPIC30/33 and PIC24 General
- ↳ mikroBasic PRO for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroBasic PRO for dsPIC30/33 and PIC24 Wish List
- ↳ mikroPascal PRO for dsPIC30/33 and PIC24
- ↳ mikroPascal PRO for dsPIC30/33 and PIC24 General
- ↳ mikroPascal PRO for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroPascal PRO for dsPIC30/33 and PIC24 Wish List
- ↳ PIC32 PRO Compilers
- ↳ PIC32 Compilers General
- ↳ mikroC PRO for PIC32
- ↳ mikroC PRO for PIC32 General
- ↳ mikroC PRO for PIC32 Beta Testing
- ↳ mikroC PRO for PIC32 Wish List
- ↳ mikroBasic PRO for PIC32
- ↳ mikroBasic PRO for PIC32 General
- ↳ mikroBasic PRO for PIC32 Beta Testing
- ↳ mikroBasic PRO for PIC32 Wish List
- ↳ mikroPascal PRO for PIC32
- ↳ mikroPascal PRO for PIC32 General
- ↳ mikroPascal PRO for PIC32 Beta Testing
- ↳ mikroPascal PRO for PIC32 Wish List
- ↳ AVR PRO Compilers
- ↳ AVR Compilers General
- ↳ mikroC PRO for AVR
- ↳ mikroC PRO for AVR General
- ↳ mikroC PRO for AVR Beta Testing
- ↳ mikroC PRO for AVR Wish List
- ↳ mikroBasic PRO compiler for AVR
- ↳ mikroBasic PRO for AVR General
- ↳ mikroBasic PRO for AVR Beta Testing
- ↳ mikroBasic PRO for AVR Wish List
- ↳ mikroPascal PRO for AVR
- ↳ mikroPascal PRO for AVR General
- ↳ mikroPascal PRO for AVR Beta Testing
- ↳ mikroPascal PRO for AVR Wish List
- ↳ FT90x PRO Compilers
- ↳ FT90x Compilers General
- ↳ mikroC PRO for FT90x
- ↳ mikroC PRO for FT90x General
- ↳ mikroC PRO for FT90x Beta Testing
- ↳ mikroC PRO for FT90x Wish List
- ↳ mikroBasic PRO compiler for FT90x
- ↳ mikroBasic PRO for FT90x General
- ↳ mikroBasic PRO for FT90x Beta Testing
- ↳ mikroBasic PRO for FT90x Wish List
- ↳ mikroPascal PRO for FT90x
- ↳ mikroPascal PRO for FT90x General
- ↳ mikroPascal PRO for FT90x Beta Testing
- ↳ mikroPascal PRO for FT90x Wish List
- ↳ 8051 PRO Compilers
- ↳ 8051 Compilers General
- ↳ mikroC PRO for 8051
- ↳ mikroC PRO for 8051 General
- ↳ mikroC PRO for 8051 Beta Testing
- ↳ mikroC PRO for 8051 Wish List
- ↳ mikroBasic PRO compiler for 8051
- ↳ mikroBasic PRO for 8051 General
- ↳ mikroBasic PRO for 8051 Beta Testing
- ↳ mikroBasic PRO for 8051 Wish List
- ↳ mikroPascal PRO for 8051
- ↳ mikroPascal PRO for 8051 General
- ↳ mikroPascal PRO for 8051 Beta Testing
- ↳ mikroPascal PRO for 8051 Wish List
- ↳ ARM PRO Compilers
- ↳ ARM Compilers General
- ↳ mikroC PRO for ARM
- ↳ mikroC PRO for ARM General
- ↳ mikroC PRO for ARM Beta Testing
- ↳ mikroC PRO for ARM Wish List
- ↳ mikroBasic PRO for ARM
- ↳ mikroBasic PRO for ARM General
- ↳ mikroBasic PRO for ARM Beta Testing
- ↳ mikroBasic PRO for ARM Wish List
- ↳ mikroPascal PRO for ARM
- ↳ mikroPascal PRO for ARM General
- ↳ mikroPascal PRO for ARM Beta Testing
- ↳ mikroPascal PRO for ARM Wish List
- ↳ Older versions of Compilers
- ↳ Older versions of mikroC Compilers
- ↳ mikroC for PIC
- ↳ mikroC General
- ↳ mikroC Beta Testing
- ↳ mikroC Wish List
- ↳ mikroC for dsPIC30/33 and PIC24
- ↳ mikroC for dsPIC30/33 and PIC24 General
- ↳ mikroC for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroC for dsPIC30/33 and PIC24 Wish List
- ↳ RSC-4x mikroC
- ↳ RSC-4x mikroC General
- ↳ RSC-4x mikroC Wish List
- ↳ mikroC Beta testing
- ↳ Older versions of mikroBasic Compilers
- ↳ mikroBasic compiler for PIC
- ↳ mikroBasic General
- ↳ mikroBasic Beta Testing
- ↳ mikroBasic Wish List
- ↳ mikroBasic compiler for dsPIC30/33 and PIC24
- ↳ mikroBasic for dsPIC30/33 and PIC24 General
- ↳ mikroBasic for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroBasic for dsPIC30/33 and PIC24 Wish List
- ↳ mikroBasic for dsPIC30/33 and PIC24 Bug List
- ↳ Obsolete Project: mikroBasic compiler for AVR
- ↳ mikroBasic for AVR General
- ↳ mikroBasic for AVR Wish List
- ↳ mikroBasic Beta testing
- ↳ Older versions of mikroPascal Compilers
- ↳ mikroPascal compiler for PIC
- ↳ mikroPascal FAQ
- ↳ mikroPascal General
- ↳ mikroPascal Beta Testing
- ↳ mikroPascal Wish List
- ↳ mikroPascal Bug List
- ↳ mikroPascal compiler for dsPIC30/33 and PIC24
- ↳ mikroPascal for dsPIC30/33 and PIC24 General
- ↳ mikroPascal for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroPascal for dsPIC30/33 and PIC24 Wish List
- ↳ mikroPascal for dsPIC30/33 and PIC24 Bug List
- ↳ Obsolete Project: mikroPascal compiler for AVR
- ↳ mikroPascal for AVR General
- ↳ mikroPascal for AVR Wish List
- ↳ mikroPascal Beta testing
- ↳ Library Development Discussion
- ↳ Code Requests
- Additional Software
- ↳ Visual TFT Software
- ↳ Visual TFT General
- ↳ Visual TFT Beta Testing
- ↳ Visual TFT Wish List
- ↳ Visual GLCD Software
- ↳ Visual GLCD General
- ↳ Visual GLCD Beta Testing
- ↳ Visual GLCD Wish List
- ↳ Package Manager Software
- ↳ Package Manager General
- ↳ Package Manager Beta Testing
- ↳ Package Manager Wishlist
- ↳ GLCD Font Creator Software
- ↳ SpeakUp Software
- ↳ Timer Calculator
- ↳ CAN Calculator
- ↳ mikroBootloader
- Hardware
- ↳ Development Boards
- ↳ CODEGRIP programmer and debugger
- ↳ Mikromedia
- ↳ mikroProg™ programmer and debugger
- ↳ Development Boards Wishlist
- ↳ Review
- ↳ Click Boards release
- ↳ Hexiwear
- Website & Forums
- ↳ Libstock Discussion
- ↳ Product Announcements
- ↳ Website & Forums General Discussion
- ↳ Website & Forums Wishlist
- ↳ User Projects