Anda di halaman 1dari 12

/*

* 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:

HS-PLL 32.0000 MHz, 8.0000 MHz Crystal

ext. modules: rtc5 click 2x16 Lcd character display - ac:Lcd


SW:

mikroC PRO for PIC


http://www.mikroe.com/mikroc/pic/

* 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

//time and configuration registers

#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

//test diodes connections


sbit OSCON at LATA0_bit;
sbit OSCON_Direction at TRISA0_bit;

0x17

// Lcd module connections


sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;


sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

// RTC5 click module connections


sbit Chip_Select at LATE0_bit;
sbit Chip_Select_Direction at TRISE0_bit;
// RTC5 click module connections

unsigned char read_array[16];


unsigned char txtSec[12];
unsigned char txtMin[12];
unsigned char txtHour[12];
unsigned char hours;

unsigned char minutes;


unsigned char seconds;

//clear RTCC and SRAM memory


void Clear(void)
{
//Clear RTCC memory
int i;
for(i=0;i<0x20;i++)
{
Chip_Select = 0;
SPI1_Write(WRITE);
SPI1_Write(i);
SPI1_Write(0x00);
Chip_Select = 1;
delay_us(1);
}
//Clear SRAM memory
Chip_Select = 0;
SPI1_Write(CLRRAM);
Chip_Select = 1;
delay_us(1);

}
// Set RTC time
void SetTime(int seconds, int minutes, int hours) {

// Write seconds into RTC


Chip_Select = 0;
SPI1_Write(WRITE);
SPI1_Write(SECONDS_REGISTER);
SPI1_Write(seconds);
Chip_Select = 1;
delay_us(1);

// Write minutes into RTC


Chip_Select = 0;
SPI1_Write(WRITE);
SPI1_Write(MINUTES_REGISTER);
SPI1_Write(minutes);
Chip_Select = 1;
delay_us(1);

// Write hours into RTC


Chip_Select = 0;
SPI1_Write(WRITE);
SPI1_Write(HOURS_REGISTER);
SPI1_Write(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

//set VBATEN bit


Chip_Select = 0;
SPI1_Write(WRITE);
SPI1_Write(DAY_REGISTER);
SPI1_Write(temp|0x08);
Chip_Select = 1;
delay_us(1);
}

// Read time from the RTC


void ReadTime() {
Chip_Select = 0;
SPI1_Write(READ);
SPI1_Write(SECONDS_REGISTER);
read_array[0] = SPI1_Read(0);

// 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);

// Convert seconds, minutes and hours to string


ByteToStr(read_array[0], txtSec);
ByteToStr(read_array[1], txtMin);
ByteToStr(read_array[2], txtHour);

// Trim leading spaces


Ltrim(txtSec);
Ltrim(txtMin);
Ltrim(txtHour);
}

// Display time

void DisplayTime() {
Lcd_Out(2, 5, txtHour);

// Display hours

Lcd_Chr(2, 7, ':');
Lcd_Out(2, 8, txtMin);

// Display minutes

Lcd_Chr(2, 10, ':');


if(read_array[0] <= 9){
Lcd_Out(2, 11, "0");

// Display seconds

Lcd_Out(2, 12, txtSec);


}
else
Lcd_Out(2, 11, txtSec);
}

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);

// Set chip select pin direction

OSCON_Direction = 0;
OSCON = 0;

Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);

// Clear display

Lcd_Cmd(_LCD_CURSOR_OFF);

// Cursor off

Lcd_Out(1,1,"mikroElektronika"); // Write text


Lcd_Out(2,4,"RTC5 click");

// Write text

Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);

// Initalize SPI module


SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV16, _SPI_DATA_SAMPLE_MIDDLE,
_SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

// 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

Anda mungkin juga menyukai