You are on page 1of 7

PCF8583

O PCF8583 UM RTC (REAL TIME CLOCK)


DESENVOLVIDO PELA PHILIPS
SEMICONDUCTOR.
COMO CARACTERSTICAS PRINCIPAIS SO:
INTERFACE I2C
FUNO DE CLOCK E CALENDRIO
REGISTRADORES ENDEREAVEIS (HH,MM...)
240 BYTES DE RAM, ALM DOS CITADOS
ACIMA
CRISTAL DE 32768Hz
FORMATO DE HORA: 24 OU 12
INCREMENTO DE ENDEREO AUTOMTICO
ALARME PROGRAMVEL, COM
INTERRUPO
ENDEREO DO ESCREVO (LEITURA) A1
ENDEREO DO ESCRAVO (ESCRITA) A0









DIAGRAMA DE BLOCOS
PINAGEM



CONTROL/STATUS REGISTER









REGISTROS


FORMATO DA HORA

OS DADOS SO ARMAZENADOS EM FORMATO
BCD

4+4 NIBLES = EXEMPLO

64 SEGUNDOS = 01100100
SETANDO HORA

unsigned char sec, min1, hr, day, mn, year;
void Read_Time(char *sec, char *min, char *hr, char *day, char
*mn, char *year) {
I2C_Start();
I2C_Wr(0xA0);
I2C_Wr(2);
I2C_Repeated_Start();
I2C_Wr(0xA1);
*sec =I2C_Rd(1);
//while (I2C_Is_Idle() == 0) ;
*min =I2C_Rd(1);
//while (I2C_Is_Idle() == 0) ;
*hr =I2C_Rd(1);
//while (I2C_Is_Idle() == 0) ;
*day =I2C_Rd(1);
//while (I2C_Is_Idle() == 0) ;
*mn =I2C_Rd(0);
//while (I2C_Is_Idle() == 0) ;
I2C_Stop();
}//~

void Transform_Time(char *sec, char *min, char *hr, char *day, char
*mn, char *year) {
*sec = ((*sec & 0xF0) >> 4)*10 + (*sec & 0x0F);
*min = ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
*hr = ((*hr & 0xF0) >> 4)*10 + (*hr & 0x0F);
*year = (*day & 0xC0) >> 6;
*day = ((*day & 0x30) >> 4)*10 + (*day & 0x0F);
*mn = ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
}//~

void main()
{
Soft_I2C_Config(&PORTD, 4,3); // Initialize full master mode
Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xA0); // Address PCF8583
Soft_I2C_Write(0); // Start from word at address 0 (config word)
Soft_I2C_Write(0x80); // Write 0x80 to config. (pause counter...)
Soft_I2C_Write(0); // Write 0 to cents word
Soft_I2C_Write(0); // Write 0 to seconds word
Soft_I2C_Write(0x30); // Write 0x30 to minutes word
Soft_I2C_Write(0x11); // Write 0x11 to hours word
Soft_I2C_Write(0x30); // Write 0x24 to year/date word
Soft_I2C_Write(0x08); // Write 0x08 to weekday/month
Soft_I2C_Stop(); // Issue stop signal
Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xA0); // Address PCF8530
Soft_I2C_Write(0); // Start from word at address 0
Soft_I2C_Write(0); // Write 0 to config word (enable counting)
Soft_I2C_Stop(); // Issue stop signal

While(1)
{
Read_Time(&sec,&min1,&hr,&day,&mn,&year);
Transform_Time(&sec,&min1,&hr,&day,&mn,&year);

}

}//~!
Extras

//Relogio DS1307
//Acerta hora

unsigned char txt[6]; //utilizada para conversao de binario para txt
para ser impresso

short hora_atual;
short minuto_atual;
short segundo_atual;

void Escreve_Memoria(unsigned int endereco, unsigned char dado);
unsigned char Le_Memoria(unsigned int endereco);

//Passa como parametro as hora em binario (decimal) e entao converte
para BCD
//para acertar a hora...
void acerta_hora(short hora, short minuto, short segundo)
{
short hora_bcd;
short minuto_bcd;
short segundo_bcd;

hora_bcd = (hora / 10) * 16 + (hora % 10);
minuto_bcd = (minuto / 10) * 16 + (minuto % 10);
segundo_bcd = (segundo / 10) * 16 + (segundo % 10);

Escreve_Memoria(0,segundo_bcd); //11 segundos
Escreve_Memoria(1,minuto_bcd); //11 minutos
Escreve_Memoria(2,hora_bcd); //11 horas
}

//Passa como parametro tres variaveis que receberao a hora
//A hora lida em bcd e deve novamente ser transformada em Binario
para o usurio
void le_hora(short *hora, short *minuto, short *segundo)
{
short hora_binario;
short minuto_binario;
short segundo_binario;

segundo_binario = Le_Memoria(0);
minuto_binario = Le_Memoria(1);
hora_binario = Le_Memoria(2);

*hora = ((hora_binario & 0xF0) / 16) * 10 + (hora_binario &
0x0F);
*minuto = ((minuto_binario & 0xF0) / 16) * 10 +
(minuto_binario & 0x0F);
*segundo = ((segundo_binario & 0xF0) / 16) * 10 +
(segundo_binario & 0x0F);
}

void Escreve_Memoria(unsigned int endereco, unsigned char dado){
I2C1_Start(); // issue I2C start signal
I2C1_Wr(0xD0); // send byte via I2C (device address + W)
I2C1_Wr(endereco); // send byte (address of EEPROM
location)
I2C1_Wr(dado); // send data (data to be written)
I2C1_Stop();
delay_ms(16);
}

unsigned char Le_Memoria(unsigned int endereco){
unsigned char dado_lido;

I2C1_Start(); // issue I2C start signal
I2C1_Wr(0xD0); // send byte via I2C (device address + W)
I2C1_Wr(endereco); // send byte (address of EEPROM
location)
I2C1_Repeated_Start(); // issue I2C signal repeated start
I2C1_Wr(0xD1); // send byte (device address + R)
dado_lido = I2C1_Rd(0u); // Read the data (NO acknowledge)
I2C1_Stop();
return(dado_lido);
}

void main(){
I2C1_Init(100000); // initialize I2C communication
Delay_ms(100); // Wait for UART module to stabilize
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize

UART1_Write_Text("Start");
//Acerta hora para 11:11:11

hora_atual = 11;
minuto_atual = 12;
segundo_atual = 17;

acerta_hora(hora_atual,minuto_atual,segundo_atual);

UART1_Write_Text("CELESC INFORMA...");

while(1)
{

le_hora(&hora_atual,&minuto_atual,&segundo_atual);


UART1_Write(13);
UART1_Write(10);

//converte inteiro para string
inttostr(hora_atual,txt);
UART1_Write_Text(txt);
UART1_Write_Text(":");

//converte inteiro para string
inttostr(minuto_atual,txt);
UART1_Write_Text(txt);
UART1_Write_Text(":");

//converte inteiro para string
inttostr(segundo_atual,txt);
UART1_Write_Text(txt);

delay_ms(1000);
}
}

You might also like