PCF8583 O PCF8583 É UM RTC (REAL TIME CLOCK) DESENVOLVIDO PELA PHILIPS SEMICONDUCTOR. COMO CARACTERÍSTICAS PRINCIPAIS SÃO: INTERFACE I2C FUNÇÃO DE CLOCK E CALENDÁRIO REGISTRADORES ENDEREÇAVEIS (HH,MM...) 240 BYTES DE RAM, ALÉM DOS CITADOS ACIMA CRISTAL DE 32768Hz FORMATO DE HORA: 24 OU 12 INCREMENTO DE ENDEREÇO AUTOMÁTICO ALARME PROGRAMÁVEL, COM INTERRUPÇÃO ENDEREÇO DO ESCREVO (LEITURA) A1 ENDEREÇO DO ESCRAVO (ESCRITA) A0
DIAGRAMA DE BLOCOS PINAGEM
CONTROL/STATUS REGISTER
REGISTROS FORMATO DA HORA OS DADOS SÃO 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); *min =I2C_Rd(1); *hr =I2C_Rd(1); *day =I2C_Rd(1); *mn =I2C_Rd(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 usuário 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); Delay_ms(100); UART1_Init(9600); Delay_ms(100); // initialize I2C communication // Wait for UART module to stabilize // Initialize UART module at 9600 bps // 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);