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

TIMEOUT

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



Joined: 05 Jul 2007
Posts: 18

View user's profile Send private message

TIMEOUT
PostPosted: Thu Aug 30, 2007 12:52 am     Reply with quote

I am trying to create a timeout. When the lights is switched on it must stay on for 30 minutes and then go off, but it doesnt seem to work kan anybody pleas help me my code is as follows

Code:


// Time Structures
struct time
{
   unsigned int ms;                  // Milliseconds
   unsigned char ss;                  // Seconds
   unsigned char mm;                  // Minutes
   unsigned char hh;                  // Hours
};


void light_on(int8 motionsensor)
{

   
         if(motionsensor > 0)
            {
            printf(" The motion sensor is high!");
             output_high(PIN_B5);
             output_high(PIN_B6);
            }

         if((motionsensor == 0) & (now.mm ==1))
           {

            printf("The motion sensor is low!");
            printf("now == 1");
             output_low(PIN_B5);
             output_low(PIN_B6);
         
             
         }
   

}

// The interrupt used

#INT_TIMER2
void isr_TIMER2()      //interrupt routine when overflow occurs in TIMER2. ====> VIR TIMEOUT
{

  static unsigned char ticks = 0;

   if(++ticks == 3)         // 1 millisecond
   {
     
      // Software Clock
      if(++now.ms == 1000)   // One second elapsed
      {
          now.ms = 0;

         if(++now.ss == 60)   // One minute elapsed
         {
           now.ss = 0;
         if( ++now.mm == 2)
            {
               now.mm = 0;
            }

         }
      }

      ticks = 0;            // Reset ticks
   }

}





Thanks any help will be appreciated
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Aug 30, 2007 1:07 am     Reply with quote

Code:
unsigned int ms;                  // Milliseconds
Change to an int16.

In C the size of an integer is compiler dependent and often defaults to the size of the processor's data bus, i.e. 8 bits in the PIC. Read the chapter 'Basic and Special types' in the manual and you will see that in the CCS compiler an int equals an int8.
It is possible to change the default sizes using the #TYPE qualifier but I wouldn't recommend this. For new programs it is good practice to use type-specifiers like int8 and int16 which are much tighter defined and easier to port to other compilers.


Just curious, what is your clock frequency?
An interrupt every 250us just for updating a timer is very often considering the relative large overhead interrupts have (about 100 instructions, depending on PIC model).
Assuming you have a 4MHz clock:
- 4MHz = 1 million instructions per second.
- interrupt every 250us -> 4000 interrupts per second
- 100 instructions interrupt overhead
---> 40% time spent in the interrupt routine !!!
Guest








PostPosted: Thu Aug 30, 2007 1:16 pm     Reply with quote

Thanx for the reply, but still it doen't work because now == 1 for a minute and then the light switches rapidly on and of when movement is detected
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Aug 30, 2007 1:25 pm     Reply with quote

Code:
         if((motionsensor == 0) & (now.mm ==1))
Change & to &&.

Your minute counter is only counting up to 2 minutes, not the 30 you say you want.

Still curious, what is your clock frequency?
kevcon



Joined: 21 Feb 2007
Posts: 142
Location: Michigan, USA

View user's profile Send private message

PostPosted: Thu Aug 30, 2007 1:30 pm     Reply with quote

ckielstra I'm surprised you didn't recommend your own post about an accurate RTC, I’ve used this several time and it works great.


http://www.ccsinfo.com/forum/viewtopic.php?t=26177
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Aug 30, 2007 2:48 pm     Reply with quote

Hi kevcon, thanks for providing the link. Always nice to hear other people are appreciating my work although in this case credits are for Neutone's original code.
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