RELOGIO DIGITAL DISPLAY 7 SEGMENTOS + TERMOMETRO + ALARME

……..

Funcoes:

  •  Ajuste de hora
  • Ajuste de alarme
  • Hora X Termometro
  • Alarme via buzzer
  • snooze 3x
  • opcoes de display

  • deve ter 11 saidas (7 segtos + 2 enablers + 2 pontos)
  • deve ter 1 entrada para o LM35
  • deve ter 2 entradas para o Clock RTC
  • 2 portas para botoes de ajuste
  • Total 16 portas

Conversor Hex para 7 segtos

Modelo de 7 segtos x 4 digitos utilizado.

</pre></pre></pre>

 //DS1307 RTC Interfacing with PIC16F877A
 sbit I2C1_Scl at RC3_bit;
 sbit I2C1_Sda at RC4_bit;
 sbit I2C1_Scl_Direction at TRISC3_bit;
 sbit I2C1_Sda_Direction at TRISC4_bit;

 //Letras para display no 7 segtos
 //#define h_hora1 0x74, 0x5C, 0x50, 0x5F       //"hora"
 //#define d_date1 0x5E, 0x5F, 0x78, 0x5F       //"data"
 #define enable_1 PORTA.F2
 #define enable_2 PORTA.F1
 #define enable_3 PORTA.F3
 #define enable_4 PORTC.F1
 #define TIME_FRAME 300 //tempo de exibicao do digito
 #define BUTTON_MODE (PORTC.F5 == 0) //mode
 #define BUTTON_UP (PORTC.F6 == 0) //mode
 #define ALARME_LED PORTA.F5 //led do alarme ligado
 #define BUTTON_SNOOZE (PORTC.F2 ==0)//led do alarme ligado

//Funcoes do relogio
 unsigned short read_ds1307(unsigned short address);
 void write_ds1307(unsigned short address,unsigned short w_data);
 void print_dia(unsigned short dia_x); //imprime dia da semana
 void buzzer_song();   //musica do alarme
 void acerta_alarme(); //acerta hora do alarme
 void acerta_relogio (unsigned short xtipo);
 void checa_alarme(); // checa alarme e toca sirene

 //funcoes do 7 segtos
 void display_7d(unsigned int numero, unsigned int numero2, int tempo); //numero = decimal a ser mostrado 2 dig. tempo = msegs a fica no display
 void displaychar_7d(unsigned int numero1, unsigned int numero2, unsigned int numero3, unsigned int numero4, int tempo); //numero1=dig1 numero2=digito2
 void efeito(unsigned int vezes);

//funcoes do termometro
void display_temperatura();

//variaveis de relogio
 unsigned short minute;
 unsigned short hour;
 unsigned short hour_b; //conversao
 unsigned short day;
 unsigned short date;
 unsigned short month;

//variaveis do alarme
  unsigned short a_hour;
  unsigned short a_min;
  unsigned short cnt2;
  unsigned short modo_exibicao; modo_snooze;//modo de exibicao

 //variaveis do 7 segtos
 unsigned char inversor; //inverte catodo para dar display
 unsigned char segto[]={0x3F, 0X06, 0X5B, 0X4F, 0X66, 0X6D, 0X7D, 0X07, 0x7f, 0X6F};
 unsigned char dia[7][3]={{0X5E,0X5C,0X54},{0X6D,0X79, 0X7D},{0X78,0X79,0X50},{0X67,0X3E, 0X77},{0X67,0X3E, 0X06},{0X6D,0X79, 0X6D},{0X6D,0X77,0X7C}};
 unsigned int xx, msd, lsd, cnx;//xx = contador
 int cnt; //contador
 unsigned short efe[7] ={0b11111110, 0b10111111, 0b11011111, 0b11101111, 0b11110111, 0b11111011, 0b11111101};

 //variaveis do termometro
  unsigned long temp_value;

void main()
 {

 TRISB = 0; // PORTB saida do display lcd
 ADCON1=0b10001110; // 1- opcao osc, 0- left justif, 00-void, 1110 - A0 - LM35 restante digital
 TRISA =0b00010001; //OUTPUTS PARA OS ENABLERS - A0 = INPUT LM35
 TRISC =0b01100100; // ATENCAO PARA INPUTS DO BOTAO = 1
CMCON = 0x07; // Disable Comparators

I2C1_Init(32768); //Inicia o DS1307 I2C e define a frequencia do xtal - xtal do tipo de relogio

//Set Initial Time - remover qdo ligar a bateria
 /*
 write_ds1307(0,0x80); //write sec stop oscilator
 write_ds1307(2,dec2bcd(22 & 0x3F)); //write hour
 write_ds1307(1,dec2bcd(55)); //write min
 write_ds1307(3,dec2bcd(7)); //write day of week 2:Monday
 write_ds1307(4,dec2bcd(17)); // write day
 write_ds1307(5,dec2bcd(9)); // write month
 write_ds1307(0,0x00); //SQWE output at 1 Hz pino 7 do ds1307
 write_ds1307(7,0x10); // pisca digitos
  */

 enable_1 = 0; //enableres
 enable_2 = 0;
 enable_3 = 0;
 enable_4 = 0;

 ALARME_LED = 0; //inicialmente alarme desligado

 //seta modo de exibicao inicial
 //********************************
 modo_exibicao =   EEPROM_Read(0XB4); //le modo de exibicao
 Delay_ms(10);
 if (modo_exibicao == 0xFF)     modo_exibicao = 0x00;

 //seta snooze inicial
 //********************************
 //modo_snooze =   EEPROM_Read(0XBE); //le modo de exibicao
 //Delay_ms(10);
 //if (modo_snooze == 0xFF)     modo_snooze = 0x00;

 Sound_Init(&PORTC, 7);  //inicializa placa de som

 efeito(4);
 displaychar_7d(0x76, 0x79, 0x38, 0x3F, TIME_FRAME*3); // "data"
 //displaychar_7d(0x76, 0x79, 0x38, 0x3F, TIME_FRAME); // "data"
 //displaychar_7d(0x76, 0x79, 0x38, 0x3F, TIME_FRAME); // "data"

 while(1)
 {

 delay_ms(100);
 minute=(read_ds1307(1) & 0x7F); // read minute
 hour=(read_ds1307(2) & 0x3F); // read hour
 hour_b = hour;
 day=(read_ds1307(3) & 0x7F); // read day
 date=(read_ds1307(4) & 0x3F); // read date
 month=(read_ds1307(5) & 0x1F); // read month

 if (modo_exibicao ==0)  // mostra todas as informacoes com efeito
 {
 efeito(1);
 displaychar_7d(0x76, 0x5C, 0x50, 0x77, TIME_FRAME); //"hora"
 efeito(1);
 display_7d(bcd2dec(hour_b),bcd2dec(minute), TIME_FRAME); //Hora
 efeito(1);
 displaychar_7d(0x5E, 0x77, 0x78, 0x77, TIME_FRAME); // "data"
 efeito(1);
 display_7d(bcd2dec(date), bcd2dec(month), TIME_FRAME); //Dia
 efeito(1);
 print_dia(day); //Dia da semana
 efeito(1);
 display_temperatura();
 efeito(1);
 }

   if (modo_exibicao ==1)  // mostra todas as informacoes sem efeito
 {
 displaychar_7d(0x76, 0x5C, 0x50, 0x77, TIME_FRAME); //"hora"
 display_7d(bcd2dec(hour_b),bcd2dec(minute), TIME_FRAME); //Hora
 displaychar_7d(0x5E, 0x77, 0x78, 0x77, TIME_FRAME); // "data"
 display_7d(bcd2dec(date), bcd2dec(month), TIME_FRAME); //Dia
 print_dia(day); //Dia da semana
 display_temperatura();
 }

 if (modo_exibicao ==2)  //hora somente
 {
 //displaychar_7d(0x76, 0x5C, 0x50, 0x77, TIME_FRAME); //"hora"
 display_7d(bcd2dec(hour_b),bcd2dec(minute), TIME_FRAME*4); //Hora
 //displaychar_7d(0x5E, 0x77, 0x78, 0x77, TIME_FRAME); // "data"
 //display_7d(bcd2dec(date), bcd2dec(month), TIME_FRAME); //Dia
 //print_dia(day); //Dia da semana
 //display_temperatura();
 }

  if (modo_exibicao ==3)  // hora e temperatura com efeito
 {
 //displaychar_7d(0x76, 0x5C, 0x50, 0x77, TIME_FRAME); //"hora"
 display_7d(bcd2dec(hour_b),bcd2dec(minute), TIME_FRAME); //Hora
 efeito(1);
 display_temperatura();
 efeito(1);
 }

 if (modo_exibicao ==4)  // hora e temperatura    sem efeito
 {
 //displaychar_7d(0x76, 0x5C, 0x50, 0x77, TIME_FRAME); //"hora"
 display_7d(bcd2dec(hour_b),bcd2dec(minute), TIME_FRAME); //Hora
 display_temperatura();
 }

 checa_alarme();

 if (BUTTON_MODE)//Acerto de hora
 {

 Sound_Play(659, 20);
 while(BUTTON_MODE)
 {
 write_ds1307(0,0x80); //write sec stop oscilator
 write_ds1307(7,0x00); //pisca digitos stop
 acerta_relogio(2); //hour
 acerta_relogio(1); //minute
 acerta_relogio(4); //date
 acerta_relogio(5); //month
 acerta_relogio(3); //day
 write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
 write_ds1307(7,0x10); // pisca digitos
 write_ds1307(7,0x10); // pisca digitos
 }
 }

 if (BUTTON_UP)//Acerto de alarme
 {
 Sound_Play(659, 20);
 while(BUTTON_UP)
 {
   acerta_alarme();
 }

 }
 }
 }

void write_ds1307(unsigned short address,unsigned short w_data)
 {
 I2C1_Start(); // issue I2C start signal
 I2C1_Wr(0xD0); // send byte via I2C (device address + W)
 I2C1_Wr(address); // send byte (address of DS1307 location)
 I2C1_Wr(w_data); // send data (data to be written)
 I2C1_Stop(); // issue I2C stop signal
 }

unsigned short read_ds1307(unsigned short address)
{
I2C1_Start();
I2C1_Wr(0xd0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --&gt; 0xD0
I2C1_Wr(address);
I2C1_Repeated_Start();
I2C1_Wr(0xd1);
I2C1_Rd(0);
I2C1_stop();
}

void display_7d(unsigned int numero, unsigned int numero2, int tempo) //mostra digito 7 segtos - se parametro = 0 apaga display
// variaveis
// numero = numero cheio de 2 digitos a ser exibido no 7 segtos
// tempo = tempo em msegs que o display será mostrado

{
 for ( cnt = 0 ; cnt < tempo; cnt++ ) //tempo de exibicao
 {
 msd = numero/10;
 inversor = segto[msd];
 inversor = ~inversor;
 PORTB = inversor;
 enable_2 =1;
 delay_ms(1);
 enable_2 =0;

 lsd = numero%10;
 inversor = segto[lsd];
 inversor = ~inversor;
 PORTB = inversor;
 enable_1 =1;
 delay_ms(1);
 enable_1 =0;

 msd = numero2/10;
 inversor = segto[msd];
 inversor = ~inversor;
 PORTB = inversor;
 enable_4 =1;
 delay_ms(1);
 enable_4 =0;

 lsd = numero2%10;
 inversor = segto[lsd];
 inversor = ~inversor;
 PORTB = inversor;
 enable_3 =1;
 delay_ms(1);
 enable_3 =0;

}
}
void displaychar_7d(unsigned int numero1, unsigned int numero2, unsigned int numero3, unsigned int numero4, int tempo) //numero1=dig1 numero2=digito2
{
 for ( cnt = 0 ; cnt < tempo; cnt++ ) //tempo de exibicao
 {
 inversor = numero1;
 inversor = ~inversor;
 PORTB = inversor;
 enable_2 =1;
 delay_ms(1);
 enable_2 =0;

 inversor = numero2;
 inversor = ~inversor;
 PORTB = inversor;
 enable_1 =1;
 delay_ms(1);
 enable_1 =0;

 inversor = numero3;
 inversor = ~inversor;
 PORTB = inversor;
 enable_4 =1;
 delay_ms(1);
 enable_4 =0;

 inversor = numero4;
 inversor = ~inversor;
 PORTB = inversor;
 enable_3 =1;
 delay_ms(1);
 enable_3 =0;
}
}

void print_dia(unsigned short dia_x) //Imprime o dia da semana
{
switch (dia_x) {
case 1 : displaychar_7d(dia[0][0], dia[0][1], dia[0][2], 0X54, TIME_FRAME); break; //seg
case 2 : displaychar_7d(dia[1][0], dia[1][1], dia[1][2], 0X00, TIME_FRAME); break;
case 3 : displaychar_7d(dia[2][0], dia[2][1], dia[2][2], 0X00, TIME_FRAME); break;
case 4 : displaychar_7d(dia[3][0], dia[3][1], dia[3][2], 0X00, TIME_FRAME); break;
case 5 : displaychar_7d(dia[4][0], dia[4][1], dia[4][2], 0X00, TIME_FRAME); break;
case 6 : displaychar_7d(dia[5][0], dia[5][1], dia[5][2], 0X00, TIME_FRAME); break; //sab
case 7 : displaychar_7d(dia[6][0], dia[6][1], dia[6][2], 0X00, TIME_FRAME); break; //dom
}
}

//*************************
void acerta_relogio (unsigned short xtipo)
{

unsigned short xlimite;
unsigned short xchart;

Delay_ms(1000) ; //debounce do botao mode

//xchart = 0;

if (xtipo==2) {xlimite=23;}
if (xtipo==1) {xlimite=59;}
if (xtipo==4) {xlimite=31;}
if (xtipo==5) {xlimite=12;}
if (xtipo==3) {xlimite=7;}

if (xtipo==2) {xchart= bcd2dec(hour);}
if (xtipo==1) {xchart= bcd2dec(minute);}
if (xtipo==4) {xchart= bcd2dec(date);}
if (xtipo==5) {xchart= bcd2dec(month);}
if (xtipo==3) {xchart= bcd2dec(day);}

for(;;)
{

display_7d(xchart,0,25);

if(BUTTON_UP) // advance hour
{
Sound_Play(784, 20);
//VDelay_ms(7);      //delay no incremento dos nrs
xchart++;
if (xchart>xlimite) xchart=0;
}

if(BUTTON_MODE) // valid
{
Sound_Play(659, 20);
enable_1 = 0;     //apaga dig
enable_2 = 0;     //apaga dig

VDelay_ms(150) ;

write_ds1307(xtipo,(dec2bcd(xchart)));

break ; // exit loop
}
}
write_ds1307(7,0x10); // pisca digitos
}

void display_temperatura()
{
  int cnn;
  int msdx, lsdx, inversorx;

  temp_value = ADC_Read(0);   // le o sensor numero 1
  delay_ms(100);
  temp_value = temp_value*4883;// DEVE SER AJUSTADO SGUNDO VREF se vref é vcc devese medir vcce ajusta
  temp_value = temp_value/10000;

  //DISPLAY A TEMPERATURA
  for ( cnn = 0 ; cnn < TIME_FRAME; cnn++ ) //tempo de exibicao
 {

 msdx = temp_value/10;
 inversorx = segto[msdx];
 inversorx = ~inversorx;
 PORTB = inversorx;
 enable_2 =1;
 delay_ms(1);
 enable_2 =0;

 lsdx = temp_value %10;
 inversorx = segto[lsdx];
 inversorx = ~inversorx;
 PORTB = inversorx;
 enable_1 =1;
 delay_ms(1);
 enable_1 =0;

 inversorx = 99;  //grau celcius
 inversorx = ~inversorx;
 PORTB = inversorx;
 enable_4 =1;
 delay_ms(1);
 enable_4 =0;

 inversorx = 0x39;    // "C"
 inversorx = ~inversorx;
 PORTB = inversorx;
 enable_3 =1;
 delay_ms(1);
 enable_3 =0;

}
  }

//***************************************************************
//Acerta Alarme
//*****************************************************************
void acerta_alarme()
{

     int cnt2;
     unsigned short z_hora, x_min, z_snooze;
     Delay_ms(1000) ; //debounce do botao mode

     z_hora=  EEPROM_Read(0xAA);     // posicoes de memoria para hora e minuto
     Delay_ms(10);
     x_min=   EEPROM_Read(0XAF);
     Delay_ms(10);
     z_snooze = EEPROM_Read(0XBE); //le modo de exibicao
     Delay_ms(10);
     modo_exibicao =   EEPROM_Read(0XB4); //le modo de exibicao
     Delay_ms(10);

     if (modo_exibicao == 0xFF)     modo_exibicao = 0x00;
     if (z_snooze == 0xFF)          z_snooze = 0x00;
     if (z_hora == 0xFF)            z_hora = 0; //se posicao de memoria vazia - zera variaveis
     if (x_min  == 0xFF)            x_min  = 0;

     for ( cnt2 = 0 ; cnt2 < 50; cnt2++ )
     {

            display_7d(z_hora,x_min,25);

            if(BUTTON_MODE) // advance hour
            {
                          cnt2=0;
                          Sound_Play(784, 2);
                          //VDelay_ms(7);      //delay no incremento dos nrs
                          z_hora++;
                          if (z_hora>23) z_hora=0;
                          EEPROM_Write(0xAA, z_hora);
                          Delay_ms(10);

            }

            if(BUTTON_UP) // advance hour
            {
                          cnt2=0;
                          Sound_Play(784, 2);
                          //VDelay_ms(7);      //delay no incremento dos nrs
                          x_min++;
                          if (x_min>59) x_min=0;
                          EEPROM_Write(0xAF, x_min);
                          Delay_ms(10);

            }

      }

//seta modo de exibicao
//***************************
  displaychar_7d(0x5E, 0x06, 0x6D, 0x73, TIME_FRAME); // "modo"
  for ( cnt2 = 0 ; cnt2 < 30; cnt2++ )
     {

            display_7d(modo_exibicao,0x00,25);

            if(BUTTON_UP) // advance hour
            {
                          cnt2=0;
                          Sound_Play(784, 5);
                          //VDelay_ms(7);      //delay no incremento dos nrs
                          modo_exibicao++;
                          if (modo_exibicao>4) modo_exibicao=0;
                          EEPROM_Write(0xB4, modo_exibicao);
                          Delay_ms(10);

            }
        }

//seta SNOOZE
//***************************
  displaychar_7d(0x6D, 0x54, 0x5C, 0x5C, TIME_FRAME); // "modo"
  for ( cnt2 = 0 ; cnt2 < 30; cnt2++ )
     {

            display_7d(z_snooze,0x00,25);

            if(BUTTON_UP) // advance hour
            {
                          cnt2=0;
                          Sound_Play(784, 5);
                          //VDelay_ms(7);      //delay no incremento dos nrs
                          z_snooze++;
                          if (z_snooze>3) z_snooze=0;
                          EEPROM_Write(0xBE, z_snooze);
                          Delay_ms(10);

            }
        }

//trata LED
if (z_snooze == 0)  {ALARME_LED =0;  displaychar_7d(0x3F, 0x71, 0x71, 0x71, TIME_FRAME);}
else {ALARME_LED = 1;}

}

//*****************************************************
//******************************************************
void buzzer_song()
{
  unsigned short bz_hora, bz_min, bz_snooze;

   bz_hora=  EEPROM_Read(0xAA);     // posicoes de memoria para hora e minuto
   Delay_ms(10);
   bz_min=   EEPROM_Read(0XAF);
   Delay_ms(10);
   bz_snooze=  EEPROM_Read(0XBE);
   Delay_ms(10);
   bz_snooze --;
   if(bz_snooze <0) bz_snooze =0;
   EEPROM_Write(0xBE, bz_snooze);
   Delay_ms(10);

  for ( xx = 0 ; xx < 120; xx++ )
   {
   Sound_Play(1659, 30);   //659
   displaychar_7d(0x77, 0x38, 0x77, 0x54, 10);
   Sound_Play(698, 30);
   displaychar_7d(0x77, 0x38, 0x77, 0x54, 10);
   Sound_Play(784, 30);
   displaychar_7d(0x77, 0x38, 0x77, 0x54, 10);
   Sound_Play( 880, 30);
   displaychar_7d(0x77, 0x38, 0x77, 0x54, 10);
   Sound_Play(1046, 30);
   displaychar_7d(0x77, 0x38, 0x77, 0x54, 10);
   Sound_Play(1318, 30);

   if (BUTTON_SNOOZE) break;

   }

   if(bz_snooze !=0)
   {
   bz_min = bz_min+1;
   if (bz_min > 59) {bz_min = 0; bz_hora++; if (bz_hora >23) {bz_hora =0; bz_min = bz_min+1;}};
   EEPROM_Write(0xAA, bz_hora); //reseta alarme
   Delay_ms(10);
   EEPROM_Write(0xAF, bz_min );
   Delay_ms(10);
   ALARME_LED =1;

   }

}

//***********************************************************
//*************************************************************
void  checa_alarme()
{
 unsigned short al_hora, al_min, x_hora, x_min, al_snooze;

 al_hora=  EEPROM_Read(0xAA);     // posicoes de memoria para hora e minuto
 Delay_ms(10);
 al_min=   EEPROM_Read(0XAF);
 Delay_ms(10);
 al_snooze=   EEPROM_Read(0XBE);
 Delay_ms(10);

 if (al_hora == 0xFF)     al_hora = 0; //se posicao de memoria vazia - zera variaveis
 if (al_min  == 0xFF)     al_min  = 0;
 if (al_snooze  == 0xFF)  al_snooze  = 0;

 x_hora = bcd2dec(hour); //hora atual
 x_min =  bcd2dec(minute);

 if (al_hora == x_hora && al_min == x_min && al_snooze > 0)    buzzer_song();

 //if (x_min == al_min)
 }

 void efeito(unsigned int vezes)
  {
  unsigned int seg, cne;

  write_ds1307(7,0x00); //pisca digitos stop

 for ( cne = 0 ; cne < vezes*2; cne++ ) //tempo de exibicao
 {

 for (seg =0; seg < 7; seg++)
 {
 PORTB = efe[seg];
 enable_2 =1;
 delay_ms(5);
 enable_2 =0;

 PORTB = efe[seg];
 enable_1 =1;
 delay_ms(5);
 enable_1 =0;

 PORTB = efe[seg];
 enable_4 =1;
 delay_ms(5);
 enable_4 =0;

 PORTB = efe[seg];
 enable_3 =1;
 delay_ms(5);
 enable_3 =0;

 }

 }

  write_ds1307(7,0x10); // pisca digitos
 }
About these ads
%d blogueiros gostam disto: