Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

help with simultaneous alarm events w/o clock interruptions

Status
Not open for further replies.

elizabeth ann

Junior Member level 2
Junior Member level 2
Joined
Feb 16, 2008
Messages
21
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,468
hello everyone...i have a problem with this simple alarm program. i am using CCS C and Proteus VSM and i would like to implement this program in actual user interface. i've used the DS1307.c driver found in this forum and made some modifications according to my program.

here's what i want to do:
1) set the alarm for 10 bins independently. that is, i can immediately set the alarm for another bin when i'm done setting the time for Bin #1, for example, WITHOUT affecting the alarm event that i have just configured.
2) the alarm should repeat if intake>1 and will execute until it is equal to "intake", and should afterwards go back to the part where the user will be asked to choose another bin if he still wants to set an alarm. (---thankfully, i've done this already)
3) clock should continue to run in the background all throughout the program, even when the alarm event for that certain bin is over.


however, what's happening with this program is this:

1) i can set the alarm for the other bins ONLY WHEN the ongoing alarm has finished.
2) Time stops and restarts to original time i've set when the alarm event is finished.


that's it for now. but i still have so many questions to ask...
i'm running out of ideas...can somebody help please...

Code:
#include "D:\All SP\all programs\white\scarlet.h"
#include "C:\Documents and Settings\SP2008\Desktop\rtc programs\rtcdriver.c"
#include <stdlib.h>
#include <LCD.C>
#include <KBD.C>
void startko();void another();void showtime();
void showtime2();void alarm_over();void loop_alarm();
void intake();void rt();void rtc1();void second();

char k[5];
int dig2,termi,dura,a,secnow;
int x;
  BYTE sec;
  BYTE min; 
  BYTE hrs; 
  BYTE day; 
  BYTE month; 
  BYTE yr; 
  BYTE dow; 

  BYTE aHr=0;
  BYTE aMin=0;
  BYTE aSec=0;
  BYTE isAlarm = 0;

                     void second()
                     {
                     lcd_putc("\finterval: 0=exit");
                     lcd_putc("\n4,6,8,12,24");
                     delay_ms(400);
                      for(;;)
                      {
                        k[2]=kbd_getc();
                        if(k[2]!=0)
                           {if (k[2]=='4' || k[2]=='6' || k[2]=='8')
                              {  printf(lcd_putc,"\fevery %c hrs",k[2]);
                                 dig2=0;}
                              else
                                 {if (k[2]=='1' || k[2]=='2')
                                    {if (k[2]=='1')
                                       {lcd_putc("\fevery 12 hrs");
                                          dig2=1;}
                                     else
                                      {lcd_putc("\fevery 24 hrs");
                                       dig2=2;}
                                    }
                                 }
                              delay_ms(400);
                              
   /////////////////////SET NUMBER OF INTAKE////////////////////                           
                              
                              lcd_putc("\f# of intake:");
                              for(;;)
                              {
                                 k[3]=kbd_getc();
                                 if (k[3]!=0)
                                 if (k[3]=='*' || k[3]=='#')
                                 { lcd_putc("\fINVALID\nenter intake:"); }
                                 else
                                 { printf(lcd_putc,"\fintake: %c",k[3]);
                                   rtc1(); //GO TO TIMER PROPER
                                   
                                   
                                   lcd_putc("\fback");
                                   termi=k[3]-'0';
                                   dura=termi;
                                   
                                   for (termi=1;termi<dura;termi++)
                                   {
                                   isAlarm=0;
                                   ds1307_get_time(hrs,min,sec);
                                   secnow=sec;
                                   aSec=secnow+x;
                                   
                                     while(1)
                                     {
                                        showtime();
                                        lcd_gotoxy(1,1);
                                        if (( aHr==hrs ) && ( aMin==min ) && ( aSec==sec ))
                                        { isAlarm=1;
                                        }
                                        if (isAlarm )
                                        { output_toggle(PIN_C0);
                                          delay_ms(100);
                                          if (!input(PIN_A1))
                                             { output_low(PIN_C0);
                                                break;
                                                
                                             } 
                                        }
                                     }
                                    }lcd_putc("\fAlarm over\nEnter bin #");
                                    return;
                               }
                              }
                           }
                      }
                     }
                     
                                  
                                 
void showtime()
{
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); 
delay_ms(500);
}

void rtc1()
{ 
  
  ds1307_init();
   ds1307_set_date_time(19,3,87,2,0,0,0); 
  set_tris_a(0x02);
  if (k[2]=='1' || k[2]=='2')
  {
   if(k[2]=='1') 
      k[2]='2';
  else
      {k[2]='4';}
        x=(dig2*10+(k[2]-'0'));
  }

  else
  x=(k[2]-'0');

  aSec=x;
  a=x;
  while(1) 
  { 
    showtime();
   lcd_gotoxy(1,1);
    
    
    if (( aHr==hrs ) && ( aMin==min ) && ( aSec==sec ))
    { isAlarm=1;
      i2c_start();
      i2c_write(0x36);
      i2c_write(0x06);
      i2c_write(0x00);
      i2c_write(0x00);
      i2c_stop();
      i2c_start();
      i2c_write(0x36);
      i2c_write(0x02);
      i2c_write(0x00);
      i2c_write(0x01);
      i2c_stop();
    
    }
    if (isAlarm )
    { 
      output_toggle(PIN_C0);
      if (!input(PIN_A1))
         {
         output_low(PIN_C0);
         return;
         }  
    }
    
  } 
}

void main()
{
   port_b_pullups(TRUE);
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   lcd_init();
   kbd_init();
   init_io();

   while(TRUE)
   {
               
                ds1307_get_time(hrs,min,sec);
                lcd_putc("\fbin #:\n#-bin 10"); //set bin number
                for(;;)
                {
                  k[1]=kbd_getc();
                  if(k[1]!=0)
                  if (k[1]=='0' || k[1]=='*')
                  { lcd_putc("\fINVALID\nenter #");}
                  else
                  {
                     printf(lcd_putc,"\fbin %c",k[1]);
                     delay_ms(200);
                     second();
                  }
                }
   }
}
                       




///////////////////////this is the rtcdriver.c//////////////////////

#use i2c(master, 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 // Write command
i2c_write(0x00); // REG 0  // Write to address
i2c_write(0x00); // data
i2c_stop();

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 
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 min, 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(min)); // 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 &min, BYTE &sec) 
{ 
i2c_start(); 
i2c_write(0xD0); // 1i2c address @ write mode
i2c_write(0x00); // Start at REG 0 - Seconds // reg address

i2c_start();
i2c_write(0xD1); 
sec = bcd2bin(i2c_read() & 0x7f); 
min = bcd2bin(i2c_read() & 0x7f); 
hr = bcd2bin(i2c_read(0) & 0x7f); 
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)); 
} 

void init_io()
{
   i2c_start();
   i2c_write(0x36);
   i2c_write(0x06);
   i2c_write(0x00);
   i2c_write(0x00);
   i2c_stop();
   i2c_start();
   i2c_write(0x36);
   i2c_write(0x02);
   i2c_write(0x00);
   i2c_write(0x02);
   i2c_stop();
}
 

once you start the RTC it will keep running unless stopped, all you need to do is read the time and compare to your set alarms
 

Re: help with simultaneous alarm events w/o clock interrupti

yeah, i'm sorry, i didn't realize that earlier. that's because my program constantly sets and resets time each time i access it (another bug, that is:cry:)

i have another question, how do i access ALL the contents of an array like the "scanning" way?
 

to read array contents you can use pointer
 

Re: help with simultaneous alarm events w/o clock interrupti

Instead of the DS1307, the DS1337 is identical (and less expensive) and also has a 2 alarm output capability, which can simplify the process to some extent:

**broken link removed**
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top