View previous topic :: View next topic |
Author |
Message |
gordon111
Joined: 05 Jul 2007 Posts: 18
|
TIMEOUT |
Posted: Thu Aug 30, 2007 12:52 am |
|
|
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
|
|
Posted: Thu Aug 30, 2007 1:07 am |
|
|
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
|
|
Posted: Thu Aug 30, 2007 1:16 pm |
|
|
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
|
|
Posted: Thu Aug 30, 2007 1:25 pm |
|
|
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
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Aug 30, 2007 2:48 pm |
|
|
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. |
|
|
|