PIC16F877 con DS1307 RTC

1

Estoy intentando detener el reloj RTC configurando el bit CH en su segundo registro. Leí el segundo registro, enmascaré el séptimo bit a 1, reescribí esto al segundo registro. Pero por alguna razón, este enfoque no está funcionando.

Quiero detener / iniciar el RTC de acuerdo con mis necesidades. ¿Me puede guiar en este problema?

El esquema es:

Micódigoes:

//LCDmoduleconnectionssbitLCD_RSatRB2_bit;sbitLCD_ENatRB3_bit;sbitLCD_D4atRB4_bit;sbitLCD_D5atRB5_bit;sbitLCD_D6atRB6_bit;sbitLCD_D7atRB7_bit;sbitLCD_RS_DirectionatTRISB2_bit;sbitLCD_EN_DirectionatTRISB3_bit;sbitLCD_D4_DirectionatTRISB4_bit;sbitLCD_D5_DirectionatTRISB5_bit;sbitLCD_D6_DirectionatTRISB6_bit;sbitLCD_D7_DirectionatTRISB7_bit;//EndLCDmoduleconnectionsunsignedshortread_ds1307(unsignedshortaddress){unsignedshortr_data;I2C1_Start();I2C1_Wr(0xD0);//address0x68followedbydirectionbit(0forwrite,1forread)0x68followedby0-->0xD0I2C1_Wr(address);I2C1_Repeated_Start();I2C1_Wr(0xD1);//0x68followedby1-->0xD1r_data=I2C1_Rd(0);I2C1_Stop();return(r_data);}voidwrite_ds1307(unsignedshortaddress,unsignedshortw_data){I2C1_Start();//issueI2Cstartsignal//address0x68followedbydirectionbit(0forwrite,1forread)0x68followedby0-->0xD0I2C1_Wr(0xD0);//sendbyteviaI2C(deviceaddress+W)I2C1_Wr(address);//sendbyte(addressofDS1307location)I2C1_Wr(w_data);//senddata(datatobewritten)I2C1_Stop();//issueI2Cstopsignal}unsignedcharBCD2UpperCh(unsignedcharbcd){return((bcd>>4)+'0');}unsignedcharBCD2LowerCh(unsignedcharbcd){return((bcd&0x0F)+'0');}intBinary2BCD(inta){intt1,t2;t1=a%10;t1=t1&0x0F;a=a/10;t2=a%10;t2=0x0F&t2;t2=t2<<4;t2=0xF0&t2;t1=t1|t2;returnt1;}intBCD2Binary(inta){intr,t;t=a&0x0F;r=t;a=0xF0&a;t=a>>4;t=0x0F&t;r=t*10+r;returnr;}intsecond;intminute;inthour;inthr;intday;intdday;intmonth;intyear;intap;unsignedshortset_count=0;shortset;chartime[]="0000:00:00 P";
char date[] = "00-00-00";

char toggle = 'P';
char str[10];

void main()
{
   I2C1_Init(100000);                 //DS1307 I2C is running at 100KHz

   CMCON = 0x07;                      // To turn off comparators
   ADCON1 = 0x06;                     // To turn off analog to digital converters

   TRISA = 0x07;
   PORTA = 0x00;

   Lcd_Init();                        // Initialize LCD
   Lcd_Cmd(_LCD_CLEAR);               // Clear display
   Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

   second = read_ds1307(0);           // To stop the ds1307 oscillator
   second &= ~(1 << 7);               // Setting CH bit
   write_ds1307(0, second);

   Lcd_out(1,1,"Time:");
   Lcd_out(2,1,"Date:");

   do
   {
      second = read_ds1307(0);
      minute = read_ds1307(1);
      hour = read_ds1307(2);
      hr = hour & 0b00011111;
      ap = hour & 0b00100000;
      dday = read_ds1307(3);
      day = read_ds1307(4);
      month = read_ds1307(5);
      year = read_ds1307(6);

      time[2] = BCD2UpperCh(hr);
      time[3] = BCD2LowerCh(hr);
      time[5] = BCD2UpperCh(minute);
      time[6] = BCD2LowerCh(minute);
      time[8] = BCD2UpperCh(second);
      time[9] = BCD2LowerCh(second);

      date[0] = BCD2UpperCh(day);
      date[1] = BCD2LowerCh(day);
      date[3] = BCD2UpperCh(month);
      date[4] = BCD2LowerCh(month);
      date[6] = BCD2UpperCh(year);
      date[7] = BCD2LowerCh(year);

      if(ap)
      {
         time[10] = 'P';
      }
      else
      {
         time[10] = 'A';
      }

      Lcd_out(1, 6, time);
      Lcd_out(2, 6, date);
      Delay_ms(100);


   }while(1);
}
    
pregunta Mohsin

0 respuestas

Lea otras preguntas en las etiquetas