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 CCS Technical Support

DS1307 interface
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Tue Apr 07, 2015 1:13 am     Reply with quote

Hi All,

I am testing to update new alarm in DS1307. The main program is here:
Code:
#include <program.h>
#include <MAX517.C>
#include <ds1307.C>



#define LCD_RS_PIN      PIN_A3
#define LCD_RW_PIN      PIN_A2
#define LCD_ENABLE_PIN  PIN_A1
#define LCD_DATA4       PIN_D4
#define LCD_DATA5       PIN_D5
#define LCD_DATA6       PIN_D6
#define LCD_DATA7       PIN_D7   

#include <lcd.c>

//GENERAL VARIABLES

int count_tc = 0;
byte alarm_x = 0;
byte alarm_y = 0;

int8 check_alarm(int8 hrs, int8 min, int8 sec);

int8 alarm_list [3] [3] = {
                         {07, 38, 00},
                         {07, 40, 45},
                         {07, 45, 12}
};
                         

//timeclock variables

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

//timeclock Alarm variables
// alarm at 17:23:00

int8 sec_alarm;
int8 min_alarm;
int8 hrs_alarm;
int8 day_alarm;
int8 month_alarm;
int8 yr_alarm;
int8 dow_alarm;



   
load_alarm()
{
     
     
      hrs_alarm = alarm_list[alarm_y] [alarm_x];
      min_alarm = alarm_list[alarm_y] [alarm_x++];
      sec_alarm = alarm_list[alarm_y] [alarm_x++];
      alarm_x--;

     
     
}




#INT_TIMER1
void  TIMER1_isr(void)
{
   count_tc++;
   if (count_tc = 10)
   {
      if(check_alarm(hrs, min, sec))
      {
         printf("Alarm Activated! \%02d:\%02d:\%02d\n", hrs,min,sec);
         
         output_toggle(PIN_D1);
         load_alarm();
         
      }
         
   }
}





void main()
{
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);      //104 ms overflow
   EXT_INT_EDGE(L_TO_H);
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
ds1307_init();
delay_ms(400);
lcd_init();
delay_ms(200);
   


   
   load_alarm();
   output_high(PIN_D1);
   
   printf("Alarm at \%02d:\%02d:\%02d", hrs_alarm,min_alarm,sec_alarm);
   
   while(true)
   {
   
   
    ds1307_get_date(day,month,yr,dow);
    ds1307_get_time(hrs,min,sec);
    lcd_gotoxy(0,0); 
    printf(lcd_putc, "Date: \%02d/\%02d/\%02d\r\n",day,month,yr);
    lcd_gotoxy(0,1);
    printf(lcd_putc, "Time: \%02d:\%02d:\%02d", hrs,min,sec);
   
   
   }
   
}

   int8 check_alarm(int8 hrs, int8 min, int8 sec)
   {
      if((hrs == hrs_alarm) &&
         (min == min_alarm) &&
         (sec == sec_alarm))
         return(TRUE);
         else
         return(FALSE);
   
   }


the three alarms are:

Code:
 {07, 38, 00},
                         {07, 40, 45},
                         {07, 45, 12}


I am trying to use 2d array to update the alarm variables and I am testing this update through RS232 communication. for some reason, the first alarm, instead of 07:38:00 it is coming as 07:07:38

Can someone guide me what is the problem? I can't see the issue? I dont know if I am using the complicated method for this!
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Apr 07, 2015 1:28 am     Reply with quote

Try changing ALL the values in your alarm array to different numbers.
Then you will be able to tell where your alarm settings are coming from.

As it stands you can't tell which of the three 7's you are reading.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 20061

View user's profile Send private message

PostPosted: Tue Apr 07, 2015 1:33 am     Reply with quote

Look at what you are loading:

Code:

load_alarm()
{
      hrs_alarm = alarm_list[alarm_y] [alarm_x];
      min_alarm = alarm_list[alarm_y] [alarm_x++];
      //Increments alarm_x _after_ loading the value.......

//You need:
load_alarm()
{
      hrs_alarm = alarm_list[alarm_y] [alarm_x];
      min_alarm = alarm_list[alarm_y] [++alarm_x];
      sec_alarm = alarm_list[alarm_y] [++alarm_x];
      alarm_x=0; 
}


As another comment, I have to say since the 'hrs', 'mins' & 'secs' are already declared as global, let 'check_alarms' use these directly.

Currently the code has to copy the global variables into temporary values to pass to the check_alarms function, which then uses these. Since this is inside an interrupt handler, 'extra data movement' that is not wanted/needed....
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Tue Apr 07, 2015 8:11 am     Reply with quote

thanks Ttelmah for your help it worled perfectly. Continue a more advance process and since I have multiple alarms, I need to load the new alarms once teh current one expires. The code I am using is:

Code:
load_alarm()
{
     
      hrs_alarm = alarm_list[alarm_y] [alarm_x];
      min_alarm = alarm_list[alarm_y] [++alarm_x];
      sec_alarm = alarm_list[alarm_y] [++alarm_x];
      printf("Alarm at \%02d:\%02d:\%02d", hrs_alarm,min_alarm,sec_alarm);
      alarm_x=0;
      alarm_y++;     
}


the alarms are:
Code:
int8 alarm_list [3] [3] = {
                         {7, 38, 00},
                         {7, 39, 00},
                         {7, 40, 00}
};


it goes through 7:38:00 to 7:39:00 and then to 7:40:00 but after 7:40 expires, it does 00:40:07! and not from the beginning. I tried to use if..else statement but the pic hangs.
alan



Joined: 12 Nov 2012
Posts: 358
Location: South Africa

View user's profile Send private message

PostPosted: Tue Apr 07, 2015 8:45 am     Reply with quote

You do reset alarm_y = 0 after your 3rd alarm, don't see it in the code.

Regards
Ttelmah



Joined: 11 Mar 2010
Posts: 20061

View user's profile Send private message

PostPosted: Tue Apr 07, 2015 8:46 am     Reply with quote

Code:

      alarm_y++;   
      if (alarm_y==3)
         alarm_y=0;
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Tue Apr 07, 2015 8:47 am     Reply with quote

dear all, yes as explained, I made that code and the PIC is hanging!
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Tue Apr 07, 2015 8:51 am     Reply with quote

forgive me! Instead of "==" I made just one "="

Getting tired after long hours working.....

sorry for annoying you. The code worked perfectly! thanks
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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