Saturday, November 4, 2017

DS3231 RTC Clock mikro C Demo Code - 16F648A

Views:


LCD Clock
LCD Clock

Introduction:

The DS323x is a low-cost, extremely accurate I²C real-time clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal. The device incorporates a battery input, and maintains accurate timekeeping when main power to the device is interrupted. The integration of the crystal resonator enhances the long-term accuracy of the device as well as reduces the piece-part count in a manufacturing line. The DS323x is available in commercial and industrial temperature ranges, and is offered in a 16-pin, 300-mil SO package.

DS3231 Block Diagram
DS3231 Block Diagram
The RTC maintains seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with an active-low AM/PM indicator. Two programmable time-of-day alarms and a programmable square-wave output are provided. Address and data are transferred serially through an I²C bidirectional bus.

Most RTCs use an external 32kHz timing crystal that is used to keep time with low current draw. And that’s all well and good, but those crystals have slight drift, particularly when the temperature changes (the temperature changes the oscillation frequency very very very slightly but it does add up!) This RTC is in a beefy package because the crystal is inside the chip! And right next to the integrated crystal is a temperature sensor. That sensor compensates for the frequency changes by adding or removing clock ticks so that the timekeeping stays on schedule.

DS3231 module
DS3231 module

This is the finest RTC you can get, and now it in a compact, breadboard-friendly breakout. With a coin cell plugged into the back, you can get years of precision timekeeping, even when main power is lost. Great for data-logging and clocks, or anything where you need to really know the time.

The Circuit:

The circuit is very simple. It used PIC16F648A Pic micro, 16x2 LCD and DS3231 RTC module. The PIC used it's internal oscillator and run at 4MHz. Proteus and Hex file can download from the bottom of the page.

DS323x RTC Clock Circuit
DS323x RTC Clock Circuit


MikroC PRO Source Code:

/**************************************************************************

DS323x RTC Clock Demo
Copyright (C) 2017 Scorpionz

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.

>> Email: scorpionzblog@gmail.com
>> Blog : scopionz.blogspot.com

***************************************************************************/


// Software I2C connections
sbit Soft_I2C_Scl at RB2_bit;
sbit Soft_I2C_Sda at RB1_bit;
sbit Soft_I2C_Scl_Direction at TRISB2_bit;
sbit Soft_I2C_Sda_Direction at TRISB1_bit;
// End Software I2C connections

// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

char seconds, minutes, hours, day, date, month, year, tem_l, tem_h; // Global variables

//--------------------- Reads time and date information from RTC (DS3231)
void Read_Time()
{
Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xD0); // Address DS3231, see DS3231 datasheet
Soft_I2C_Write(0); // Start from address 0
Soft_I2C_Start(); // Issue repeated start signal
Soft_I2C_Write(0xD1); // Address DS3231 for reading R/W=1

seconds = Bcd2Dec(Soft_I2C_Read(1)); // Read seconds byte
minutes = Bcd2Dec(Soft_I2C_Read(1)); // Read minutes byte
hours = Bcd2Dec(Soft_I2C_Read(1)); // Read hours byte
day = Bcd2Dec(Soft_I2C_Read(1)); // Read day byte
date = Bcd2Dec(Soft_I2C_Read(1)); // Read date byte
month = Bcd2Dec(Soft_I2C_Read(1)); // Read month byte
year = Bcd2Dec(Soft_I2C_Read(1)); // Read year byte

Bcd2Dec(Soft_I2C_Read(1)); // Alarm
Bcd2Dec(Soft_I2C_Read(1));
Bcd2Dec(Soft_I2C_Read(1));
Bcd2Dec(Soft_I2C_Read(1));
Bcd2Dec(Soft_I2C_Read(1));
Bcd2Dec(Soft_I2C_Read(1));
Bcd2Dec(Soft_I2C_Read(1));

Bcd2Dec(Soft_I2C_Read(1)); // Data
Bcd2Dec(Soft_I2C_Read(1));

Bcd2Dec(Soft_I2C_Read(1));

tem_h = Bcd2Dec(Soft_I2C_Read(1)); // Temp
tem_l = Bcd2Dec(Soft_I2C_Read(0));

Soft_I2C_Stop(); // Issue stop signal
}

void write_data(char address, char w_data)
{
Soft_I2C_Start(); // issue I2C start signal
Soft_I2C_Write(0xD0); // send byte via I2C (device address + W)
Soft_I2C_Write(address); // send byte (address of DS3231 location)
Soft_I2C_Write(w_data); // send data (data to be written)
Soft_I2C_Stop(); // issue I2C stop signal
delay_ms(50);
}

//-------------------- Output values to LCD
void Display_Time()
{
Lcd_Chr(2, 7, (hours / 10) + 48);
Lcd_Chr(2, 8, (hours % 10) + 48);
Lcd_Chr(2,10, (minutes / 10) + 48);
Lcd_Chr(2,11, (minutes % 10) + 48);
Lcd_Chr(2,13, (seconds / 10) + 48);
Lcd_Chr(2,14, (seconds % 10) + 48);

Lcd_Chr(2,16, (day % 10) + 48); // day
}

void Display_Date()
{
Lcd_Out(1,1,"Date: "); // Prepare and output static text on LCD
Lcd_Chr(1,9,'-');
Lcd_Chr(1,12,'-');
Lcd_Out(1,13,"20"); // start from year 2000

Lcd_Chr(1, 7, (date / 10) + 48);
Lcd_Chr(1, 8, (date % 10) + 48);
Lcd_Chr(1,10, (month / 10) + 48);
Lcd_Chr(1,11, (month % 10) + 48);
Lcd_Chr(1,15, (year / 10) + 48);
Lcd_Chr(1,16, (year % 10) + 48);
}
void Display_Temp()
{
Lcd_Out(1, 1,"Temp: +");
Lcd_Out(1,13,"ßC ");
Lcd_Chr(1,10,'.');

tem_l=tem_l*25;

Lcd_Chr(1, 8, ((tem_h & 127) / 10) + 48);
Lcd_Chr(1, 9, ((tem_h & 127) % 10) + 48);
Lcd_Chr(1, 11, (tem_l / 10) + 48);
Lcd_Chr(1, 12, (tem_l % 10) + 48);

if(tem_h & 128) Lcd_Chr(1, 7, '-');
}

//------------------ Performs project
void Init_Main()
{
PCON.OSCF = 1; //4MHz
CMCON |= 0x07; // Disable Comparators CMCON |= 7;
OPTION_REG = 0;

Soft_I2C_Init(); // Initialize Soft I2C communication
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD display
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off

Lcd_Out(1,3,".:Scorpionz:.");
Delay_ms(500);
Lcd_Out(2,1,"Ds3231 RTC Clock");
Delay_ms(1500);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(2,1,"Time: ");
Lcd_Chr(2,9,':');
Lcd_Chr(2,12,':');

write_data(14,0x40); //SQWE output at 1 Hz
}

//----------------- Main procedure
void main() {
Delay_ms(500);

Init_Main(); // Perform initialization

while (1) { // Endless loop
Read_Time(); // Read time from RTC(DS3231)
Display_Time(); // Prepare and display on LCD
Display_Date(); Delay_ms(450);
Display_Temp();
Delay_ms(450);
}
}

 

on line

Labels

Recent comment

Visitors

Free counters!
Copyright © 2012 - Scorpionz™.,All rights reserved | Powered by Blogger