A comunicação do 1307 com o PIC é feita através do uso de comunicação I2C. Como não entendo muito sobre a programação com esse recurso, copiei o código desse site https://microcontroladores-c.blogspot.com/2010/11/usando-rtc-ds1307.html?m=0 com algumas adaptações na função main, responsáveis por transformar a leitura da hora do 1307 para os displays e verificar se o botão de ajuste de hora é pressionado:
void main()
{
int config=0;
unsigned int a,b,c,d; //just variables
unsigned int seg[]={0b00111111, //Hex value to display the number 0
0b00000110, //Hex value to display the number 1
0b01011011, //Hex value to display the number 2
0b01001111, //Hex value to display the number 3
0b01100110, //Hex value to display the number 4
0b01101101, //Hex value to display the number 5
0b01111101, //Hex value to display the number 6
0b00000111, //Hex value to display the number 7
0b01111111, //Hex value to display the number 8
0b01101111 //Hex value to display the number 9
}; //End of Array for displaying numbers from 0 to 9
set_tris_b(ALL_OUT);
set_tris_d(ALL_OUT);
set_tris_a(ALL_IN);
output_b(0b00000000);
output_d(0b00000000);
DS1307_ler_datas();
while(1)
{
while(config == 0)
{
DS1307_ler_datas();
a = segundos%10; //6th digit is saved here
b = segundos/10;
c = b%10; //5rd digit is saved here
d = b/10;
output_d(seg[a]);output_high(s1);
delay_ms(1);output_low(s1);
output_d(seg[c]);output_high(s2);
delay_ms(1);output_low(s2);
a = minutos%10; //4nd digit is saved here
b = minutos/10;
c = b%10; //3st digit is saved here
d = b/10;
output_d(seg[a]);output_high(s3);
delay_ms(1);output_low(s3);
output_d(seg[c]);output_high(s4);
delay_ms(1);output_low(s4);
a = horas%10; //2st digit is saved here
b = horas/10;
c = b%10; //1st digit is saved here
d = b/10;
output_d(seg[a]);output_high(s5);
delay_ms(1);output_low(s5);
output_d(seg[c]);output_high(s6);
delay_ms(1);output_low(s6);
if(input(ADJ_SALVAR) == 1) // AJUSTAR HORA
{
delay_ms(100);
config = 1;
}
}
a = segundos%10; //6th digit is saved here
b = segundos/10;
c = b%10; //5rd digit is saved here
d = b/10;
output_d(seg[a]);output_high(s1);
delay_ms(5);output_low(s1);
output_d(seg[c]);output_high(s2);
delay_ms(5);output_low(s2);
a = minutos%10; //4nd digit is saved here
b = minutos/10;
c = b%10; //3st digit is saved here
d = b/10;
output_d(seg[a]);output_high(s3);
delay_ms(5);output_low(s3);
output_d(seg[c]);output_high(s4);
delay_ms(5);output_low(s4);
a = horas%10; //2st digit is saved here
b = horas/10;
c = b%10; //1st digit is saved here
d = b/10;
output_d(seg[a]);output_high(s5);
delay_ms(5);output_low(s5);
output_d(seg[c]);output_high(s6);
delay_ms(5);output_low(s6);
if(input(ADJ_SEGUNDOS) == 1) // MINUTOS AJUSTE
{
if(segundos > 58)
{
segundos = 0;
delay_ms(100);
}
else
{
segundos = segundos + 1;
delay_ms(100);
}
}
if(input(ADJ_MINUTOS) == 1) // MINUTOS AJUSTE
{
if(minutos > 58)
{
minutos = 0;
delay_ms(100);
}
else
{
minutos = minutos + 1;
delay_ms(100);
}
}
if(input(ADJ_HORAS) == 1) // HORAS AJUSTE
{
if(horas > 22)
{
horas = 0;
delay_ms(100);
}
else
{
horas = horas + 1;
delay_ms(100);
}
}
if(input(ADJ_SALVAR) == 1) // SALVAR
{
DS1307_programar_tempo();
delay_ms(100);
config = 0;
}
}
}
Positivos:
- Contagem de hora independente da alimentação
- Consumo baixo de energia
Negativos:
- Conector USB esquerdo com mal contato
- Ás vezes a placa o PIC não recebe energia ao conectar
Comentários
Postar um comentário