Ir ao conteúdo
  • Cadastre-se

Alguem sabe como funciona esta biblioteca- CCS COMPILER


diego.maga

Posts recomendados

Alo galera, estou com duvida de entender esta biblioteca. será que tem alguem que saiba interpreta-lá.

Obrigado

////////////////////////////////////////////////////////////////////////////////

/// DS1307.C ///

/// Driver for Real Time Clock ///

/// ///

/// ds1307_init() - Enable oscillator without clearing the seconds register -///

/// used when PIC loses power and DS1307 run from 3V BAT ///

/// - Disable squarewave output ///

/// ///

/// ds1307_set_date_time(day,mth,year,dow,hour,mim,sec) Set the date/time ///

/// ///

/// ds1307_get_date(day,mth,year,dow) Get the date ///

/// ///

/// ds1307_get_time(hr,mim,sec) Get the time ///

/// ///

////////////////////////////////////////////////////////////////////////////////

#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)

BYTE bin2bcd(BYTE binary_value);

BYTE bcd2bin(BYTE bcd_value);

void ds1307_init(void)

{

BYTE seconds = 0;

i2c_start();

i2c_write(0xD0); // WR to RTC

i2c_write(0x00); // REG 0

i2c_start();

i2c_write(0xD1); // RD from RTC

seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1307

i2c_stop();

seconds &= 0x7F;

delay_us(3);

i2c_start();

i2c_write(0xD0); // WR to RTC

i2c_write(0x00); // REG 0

i2c_write(bin2bcd(seconds)); // Start oscillator with current "seconds value

i2c_start();

i2c_write(0xD0); // WR to RTC

i2c_write(0x07); // Control Register 0777777777

i2c_write(0x80); // Disable squarewave output pin

i2c_stop();

}

void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE mim, BYTE sec)

{

sec &= 0x7F;

hr &= 0x3F;

i2c_start();

i2c_write(0xD0); // I2C write address

i2c_write(0x00); // Start at REG 0 - Seconds

i2c_write(bin2bcd(sec)); // REG 0

i2c_write(bin2bcd(mim)); // REG 1

i2c_write(bin2bcd(hr)); // REG 2

i2c_write(bin2bcd(dow)); // REG 3

i2c_write(bin2bcd(day)); // REG 4

i2c_write(bin2bcd(mth)); // REG 5

i2c_write(bin2bcd(year)); // REG 6

i2c_write(0x80); // REG 7 - Disable squarewave output pin

i2c_stop();

}

void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)

{

i2c_start();

i2c_write(0xD0);

i2c_write(0x03); // Start at REG 3 - Day of week

i2c_start();

i2c_write(0xD1);

dow = bcd2bin(i2c_read() & 0x7f); // REG 3

day = bcd2bin(i2c_read() & 0x3f); // REG 4

mth = bcd2bin(i2c_read() & 0x1f); // REG 5

year = bcd2bin(i2c_read(0)); // REG 6

i2c_stop();

}

void ds1307_get_time(BYTE &hr, BYTE &mim, BYTE &sec)

{

i2c_start();

i2c_write(0xD0);

i2c_write(0x00); // Start at REG 0 - Seconds

i2c_start();

i2c_write(0xD1);

sec = bcd2bin(i2c_read() & 0x7f);

mim = bcd2bin(i2c_read() & 0x7f);

hr = bcd2bin(i2c_read(0) & 0x3f);

i2c_stop();

}

BYTE bin2bcd(BYTE binary_value)

{

BYTE temp;

BYTE retval;

temp = binary_value;

retval = 0;

while(1)

{

// Get the tens digit by doing multiple subtraction

// of 10 from the binary value.

if(temp >= 10)

{

temp -= 10;

retval += 0x10;

}

else // Get the ones digit by adding the remainder.

{

retval += temp;

break;

}

}

return(retval);

}

// Input range - 00 to 99.

BYTE bcd2bin(BYTE bcd_value)

{

BYTE temp;

temp = bcd_value;

// Shifting upper digit right by 1 is same as multiplying by 8.

temp >>= 1;

// Isolate the bits for the upper digit.

temp &= 0x78;

// Now return: (Tens * 8) + (Tens * 2) + Ones

return(temp + (temp >> 2) + (bcd_value & 0x0f));

}

Link para o comentário
Compartilhar em outros sites

Consegui fazer funcionar aqui:

você cola esse código aí num projeto novo do CCS e salva na pasta drivers.

Usa esse programa teste aqui:

#include <16F877A.h>

#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //Clock <=4Mhz
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection

#use delay(clock=4000000)
#include <ds1307.c>
#include <lcd.c>

void main()
{
BYTE sec;
BYTE min;
BYTE hrs;
BYTE day;
BYTE month;
BYTE yr;
BYTE dow;

ds1307_init();
lcd_init();

// Set date for -> 15 June 2005 Tuesday
// Set time for -> 15:20:55
ds1307_set_date_time(6,12,10,2,20,19,55);

while(1)
{
delay_ms(1000);

ds1307_get_date(day,month,yr,dow);
ds1307_get_time(hrs,min,sec);

printf(lcd_putc,"\f\%02d/\%02d/\%02d\r\n",day,month,yr);
printf(lcd_putc,"\%02d:\%02d:\%02d", hrs,min,sec);
}
}

SS:

semttulofq.png

Conclusão:

você não precisa fazer a lógica de tempo no PIC, o DS1307 faz tudo pra você. Nesse caso, nem precisa usar um 16F877A que tem muitos pinos e é grande. Um 16F628A faz tranquilo.

Falou

Link para o comentário
Compartilhar em outros sites

Caro MatheusLPS obrigado por responder, mas o problema não é esse, eu já fiz esse código que você postou, porém eu estou desenvolvendo um relogio digital, tinha até um topico aqui no forum "RELOGIO DE TEMPO COM PIC E DS1307" o topico foi até encerrado,porém estava eu aperfeiçoando o codigo, porém o mesmo ficou extenso de mais, caso soubesse o funcionamento da biblioteca talvez o codigo do programa ficaria mais enxuto.Assim que terminar o codigo eu posto ele ai.Beleza

Link para o comentário
Compartilhar em outros sites

Fiz p você:

Faça o down aqui do projeto completo: RTC com DS1307

Código:



#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //Clock <=4Mhz
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection

#use delay(clock=4000000)

#include <ds1307.c>
#include <lcd.c>

#define botao_incremento PIN_B0
#define botao_decremento PIN_B1

BYTE sec;
BYTE min;
BYTE hrs;
BYTE day;
BYTE month;
BYTE yr;
BYTE dow;
int8 modo;

char dia_da_semana[7][8]=
{
"DOMINGO",
"SEGUNDA",
"TERCA",
"QUARTA",
"QUINTA",
"SEXTA",
"sábado",
};

#int_ccp2
void trata_ccp_2()
{
modo++;
if (modo > 7 )
{
modo = 0;
}
}

void main()
{
setup_ccp2(CCP_CAPTURE_RE);
enable_interrupts (int_ccp2);
enable_interrupts (global);

ds1307_init();
lcd_init();
modo = 0;

while(1)
{
switch (modo)
{
case 1:
{
if (input(BOTAO_INCREMENTO))
{
delay_ms (75);
hrs++;
}

if (input(BOTAO_DECREMENTO))
{
delay_ms (75);
hrs--;
}

if (hrs > 23 )
{
hrs = 0;
}

/*if (hrs == 255 )
{
hrs = 23;
}*/

printf(lcd_putc,"\fAjustar Hora:\n%u",hrs);
delay_ms (100);
break;
}

case 2:
{
if (input(BOTAO_INCREMENTO))
{
delay_ms (75);
min++;
}

if (input(BOTAO_DECREMENTO))
{
delay_ms (75);
min--;
}

if (min > 59 )
{
min = 0;
}

/*if (min == 255 )
{
min = 59;
}*/

printf(lcd_putc,"\fAjustar Minutos:\n%u",min);
delay_ms (100);
break;
}

case 3:
{
if (input(BOTAO_INCREMENTO))
{
delay_ms (75);
sec++;
}

if (input(BOTAO_DECREMENTO))
{
delay_ms (75);
sec--;
}

if (sec > 59 )
{
sec = 0;
}

/*if (sec == 255 )
{
sec = 59;
}*/

printf(lcd_putc,"\fAjustar Segundos:\n%u",sec);
delay_ms (100);
break;
}

case 4:
{
if (input(BOTAO_INCREMENTO))
{
delay_ms (75);
day++;
}

if (input(BOTAO_DECREMENTO))
{
delay_ms (75);
day--;
}

if (day > 31 )
{
day = 1;
}

if (day == 0 )
{
day = 31;
}

printf(lcd_putc,"\fDia do Mes:\n%u",day);
delay_ms (100);
break;
}

case 5:
{
if (input(BOTAO_INCREMENTO))
{
delay_ms (75);
month++;
}

if (input(BOTAO_DECREMENTO))
{
delay_ms (75);
month--;
}

if (month > 12 )
{
month = 1;
}

if (month == 0 )
{
month = 12;
}

printf(lcd_putc,"\fAjustar Mes:\n%u",month);
delay_ms (100);
break;
}

case 6:
{
if (input(BOTAO_INCREMENTO))
{
delay_ms (75);
yr++;
}

if (input(BOTAO_DECREMENTO))
{
delay_ms (75);
yr--;
}

if (yr > 99 )
{
yr = 1;
}

if (yr == 0 )
{
yr = 99;
}

printf(lcd_putc,"\fAjustar Ano:\n%u",yr);
delay_ms (100);
break;
}

case 7:
{
if (input(BOTAO_INCREMENTO))
{
delay_ms (75);
dow++;
}

if (input(BOTAO_DECREMENTO))
{
delay_ms (75);
dow--;
}

if (dow > 7 )
{
dow = 1;
}

if (dow == 0)
{
dow = 7;
}

printf(lcd_putc,"\fDia da Semana:\n%s",dia_da_semana[dow-1]);
delay_ms (100);
ds1307_set_date_time(day,month,yr,dow,hrs,min,sec);
break;
}

default:
{
delay_ms(1000);

ds1307_get_date(day,month,yr,dow);
ds1307_get_time(hrs,min,sec);

printf(lcd_putc,"\f%s %02d:\%02d:\%02d\n%02d/\%02d/\%02d DIEGO",dia_da_semana[dow-1],hrs,min,sec,day,month,yr);

}
}
}
}
#include <16F877A.h>

Circuito:

semttuloukb.png

Falou

Link para o comentário
Compartilhar em outros sites

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

 

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!