CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

set RTC on keypad problem!!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
labpic
Guest







set RTC on keypad problem!!
PostPosted: Sun Oct 18, 2009 9:56 pm     Reply with quote

Hi everybody
I cannot to set real time clock on keypad!!
....please to solve my code...
thank you very much.


Code:
#include <16F887.h>

#include<LCD.c>
#include<input.c>
#use i2c(master,sda=PIN_C4,scl=PIN_C3)
#define ADDR_DS1307  0xD0

unsigned char scankey(void)
{
   unsigned char m = 0;
   unsigned char key;
   
   //--------------------------------------------- col3-row1-4
   output_high(pin_c0);
   output_low(pin_b7);
   output_high(pin_b6);
   output_high(pin_b5);

   if (input(pin_b1)==0)
   {
      delay_ms(m);
      key='1';
   }
   if (input(pin_b2)==0)
   {
      delay_ms(m);
      key='4';
   }
   if (input(pin_b3)==0)
   {
      delay_ms(m);
      key='7';
   }
   if (input(pin_b4)==0)
   {
      delay_ms(m);
      key='*';
   }
   
////////////////////////////////////////////////col2-row1-4
   output_high(pin_c0);
   output_high(pin_b7);
   output_low(pin_b6);
   output_high(pin_b5);

   if (input(pin_b1)==0)
   {
      delay_ms(m);
      key='2';
   }
   if (input(pin_b2)==0)
   {
      delay_ms(m);
      key='5';
   }
   if (input(pin_b3)==0)
   {
     
      delay_ms(m);
      key='8';
   }
   if (input(pin_b4)==0)
   {
       delay_ms(m);
      key='0';
   }
   
///////////////////////////////////////////col1-row1-4
   output_high(pin_c0);
   output_high(pin_b7);
   output_high(pin_b6);
   output_low(pin_b5);

   if (input(pin_b1)==0)
   {
       delay_ms(m);
      key='3';
   }
   if (input(pin_b2)==0)
   {
       delay_ms(m);
      key='6';
   }
   if (input(pin_b3)==0)
   {
       delay_ms(m);
      key='9';
   }
   if (input(pin_b4)==0)
   {
       delay_ms(m);
      key='#';
   }
   output_low(pin_c0);
   output_high(pin_b7);
   output_high(pin_b6);
   output_high(pin_b5);
   if (input(pin_b1)==0)
   {
       delay_ms(m);
      key='+';
   }
   if (input(pin_b2)==0)
   {
       delay_ms(m);
      key='-';
   }
   if (input(pin_b3)==0)
   {
       delay_ms(m);
      key='/';
   }
   if (input(pin_b4)==0)
   {
       delay_ms(m);
      key='%';
   //printf(lcd_putc,"\f #");
   }
 return(key);
}

typedef struct
   {
      BYTE sec;
      BYTE min;
      BYTE hr;
      BYTE day;
      BYTE date;
      BYTE month;
      BYTE year;
   }
   DS1307_RTC;
   DS1307_RTC RTC;
  /****************************Prototypes*/
  void DS1307_Write(unsigned char ctl,unsigned char dat);
 BYTE DS1307_Read(unsigned char ctl);
  void DS1307_WriteDate(void);
  void DS1307_WriteTime(void);
  void DS1307_ReadDate(void);
  void DS1307_ReadTime(void);
  /***************DS1307 Write***********************/
  void DS1307_Write(unsigned char ctl,unsigned char dat)
  {
      i2c_start();
      i2c_write(ADDR_DS1307);
      i2c_write(ctl);
      i2c_write(dat);
      i2c_stop();
  }
  /********************DS1307 Read***********************/
  BYTE DS1307_Read(unsigned char ctl)
  {
      BYTE dat;
      i2c_start();
      i2c_write(ADDR_DS1307);
      i2c_write(ctl);
      i2c_start();
      i2c_write(ADDR_DS1307+1);
      dat=i2c_read(0);
      i2c_stop();
      return(dat);
  }
  /***********************DS1307 Write*************************/
  void DS1307_WriteDate(void)
  {
      DS1307_Write(0x04,RTC.date);
      DS1307_Write(0x05,RTC.month);
      DS1307_Write(0x06,RTC.year);
  }
/////////////////*********DS1307 Write TIME*************/
void DS1307_WriteTime(void)
   {
      DS1307_Write(0x00,RTC.sec);
      DS1307_Write(0x01,RTC.min);
      DS1307_Write(0x02,RTC.hr);
   }
/////////////////*******DS1307 Read Date*//////////////
void DS1307_ReadDate(void)
   {
      RTC.date=DS1307_Read(0x04);
      RTC.month=DS1307_Read(0x05);
      RTC.year=DS1307_Read(0x06);
   }
/////////////***********DS1307 Read Time**********/
void DS1307_ReadTime(void)
   {
      RTC.sec=DS1307_Read(0x00);
      RTC.min=DS1307_Read(0x01);
      RTC.hr=DS1307_Read(0x02);
   }
void DS1307_SetDate(void)
   {
     
      printf(lcd_putc,"\fDate:");
      RTC.date=scankey();
     
      printf(lcd_putc,"\fMonth:");
      RTC.month=scankey();
     
      printf(lcd_putc,"\fYear:");
      RTC.year=scankey();
   }
void DS1307_SetTime(void)
   {
     
      printf(lcd_putc,"\fHour:");
      RTC.hr=scankey();
      printf(lcd_putc,"\fMin:");
      RTC.min=scankey();
      printf(lcd_putc,"\fSec:");
      RTC.sec=scankey();
   }
 ////////////*********Main Function***************///////
 void main()
   {     char ch;
         lcd_init();         
         ch=scankey();
         printf(lcd_putc,"\fSet RTC:#");
         printf(lcd_putc,"\nAuto time:*");
         
         switch(ch)
         {
         case '#':
            {
               DS1307_SetDate();
               DS1307_WriteDate();
               DS1307_SetTime();
               DS1307_WriteTime();
            }
            break;
         case '*':
         
         
          {
               DS1307_ReadDate();
               
               DS1307_ReadTime();
             
               printf(lcd_putc,"\fDate:%2X/%2X/%2X",RTC.date,RTC.month,RTC.year);
               
               printf(lcd_putc,"\nTime:%2X:%2X:%2X",RTC.hr,RTC.min,RTC.sec);
               
               delay_ms(1000);
          }
          break;
         }
}


Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group