* Project name:
RTC5 click (Reading/Writing time data to MCP79510 through SPI)
* Copyright:
(c) Mikroelektronika, 2015.
* Revision History:
20151012:
- initial release (FJ);
* Description:
This is a sample program which demonstrates the use of the Microchip's
MCP79510 Real-Time Clock Calendar. The time is inputted to the MCP79510
via SPI, then read and displayed on the Lcd
* Test configuration:
MCU:
PIC18F45K22
http://ww1.microchip.com/downloads/en/DeviceDoc/41412F.pdf
Dev.Board:
EasyPIC7
http://www.mikroe.com/easypic/
Oscillator:
* NOTES:
- Place RTC5 click board at the mikroBUS socket 1 on the EasyPIC7 board.
- Put power supply jumper (J5) on the EasyPIC7 board in 3.3V position
- Put Lcd display and turn on backlight.
*/
// RTC Commands
#define EEREAD 0x03
#define EEWRITE 0x02
#define EEWRDI 0x04
#define EEWREN 0x06
#define SSREAD 0x05
#define SRWRITE 0x01
#define READ 0x13
#define WRITE 0x12
#define UNLOCK 0x14
#define IDWRITE 0x32
#define IDREAD 0x33
#define CLRRAM 0x54
#define MILISECONDS_REGISTERS
0x00
#define SECONDS_REGISTER
0x01
#define MINUTES_REGISTER
0x02
#define HOURS_REGISTER
0x03
#define DAY_REGISTER
0x04
#define DATE_REGISTER
0x05
#define MONTH_REGISTER
#define YEAR_REGISTER
0x06
0x07
#define STATUS_REGISTER
#define CALIBRATION
0x08
0x09
//alarm 0 Registers
#define ALARM0_SECONDS_REGISTER
0x0C
#define ALARM0_MINUTES_REGISTER
0x0D
#define ALARM0_HOURS_REGISTER
0x0E
#define ALARM0_DAY_REGISTER
0x0F
#define ALARM0_DATE_REGISTER
0x10
#define ALARM0_MONTH_REGISTER
0x11
//alarm 1 Registers
#define ALARM1_SECONDS_REGISTER
0x12
#define ALARM1_MINUTES_REGISTER
0x13
#define ALARM1_HOURS_REGISTER
0x14
#define ALARM1_DAY_REGISTER
0x15
#define ALARM1_DATE_REGISTER
0x16
#define ALARM1_MONTH_REGISTER
0x17
}
// Set RTC time
void SetTime(int seconds, int minutes, int hours) {
Chip_Select = 1;
delay_us(1);
}
void Start_RTC(void)
{
//buffer seconds register
unsigned char temp;
Chip_Select = 0;
SPI1_Write(READ);
SPI1_Write(SECONDS_REGISTER);
temp = SPI1_Read(0);
Chip_Select = 1;
delay_us(1);
//set ST bit
Chip_Select = 0;
SPI1_Write(WRITE);
SPI1_Write(SECONDS_REGISTER);
SPI1_Write(temp|0x80);
Chip_Select = 1;
delay_us(1);
temp = 0;
//check if oscillator is running
while(temp&0x20 == 0)
{
Chip_Select = 0;
SPI1_Write(READ);
SPI1_Write(DAY_REGISTER);
temp = SPI1_Read(0);
Chip_Select = 1;
delay_us(1);
}
OSCON = 1; //light up OSCON diode
// Read seconds
read_array[1] = SPI1_Read(0);
// Read minutes
read_array[2] = SPI1_Read(0);
// Read hours
Chip_Select = 1;
delay_us(1);
}
// Format time
void FormatTime() {
// Format seconds, minutes and hours
read_array[0] = Bcd2Dec(read_array[0] & 0x7F);
read_array[1] = Bcd2Dec(read_array[1] & 0x7F);
read_array[2] = Bcd2Dec(read_array[2] & 0x1F);
// Display time
void DisplayTime() {
Lcd_Out(2, 5, txtHour);
// Display hours
Lcd_Chr(2, 7, ':');
Lcd_Out(2, 8, txtMin);
// Display minutes
// Display seconds
void main () {
// Set all pins as digital
ANSELA = 0;
ANSELB = 0;
ANSELC = 0;
ANSELD = 0;
ANSELE = 0;
SLRCON = 0;
Chip_Select_Direction = 0;
Chip_Select = 1;
delay_us(1);
OSCON_Direction = 0;
OSCON = 0;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
// Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
// Cursor off
// Write text
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
// Set time
Clear();
SetTime(0x00,0x10,0x10);
Start_RTC();
Lcd_Out(1, 6, "Time:");
// Write text
while(1){
ReadTime();
FormatTime();
// Read time
// Format time
DisplayTime();
Delay_ms(100);
}
}
// Display time