|
|
View previous topic :: View next topic |
Author |
Message |
farah0103
Joined: 20 Apr 2010 Posts: 6
|
Time interval using C language |
Posted: Tue Apr 20, 2010 7:38 pm |
|
|
Hi,
I would like help with the source code, where I need to relate this source code with the time interval. I need it so the count begins together with the timer starts. Anyhow I managed only to increase the count. How do I do it?
Code: |
#include <16f628.h>
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1)
int count = 0;
#int_ext
void isr_ext()
{
for(count=0;count<=20;count++)
{
SET_TRIS_B(0b00000001);
printf("Count Increase = %d\r\n",count);
if(count==20)
{
printf("Sense \r");
}
}
}
void main()
{
SET_TRIS_B(0b00000001);
count = 0;
enable_interrupts(int_ext);
enable_interrupts(global);
ext_int_edge(L_TO_H);
while(true){
}
}
|
|
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Apr 21, 2010 6:34 am |
|
|
Could you better describe what you want the code to do? What you have seems to count interrupts.
If you use the default STANDARD_IO the compiler takes care of the TRIS registers for you. You don't need to set them. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
farah0103
Joined: 20 Apr 2010 Posts: 6
|
|
Posted: Thu Apr 22, 2010 7:10 pm |
|
|
From the source code, the count increases at the rising edge.
What I want to actually do is the time starts together with the count (rising edge detected) starts. Time interval is 10sec and count is 20.
If the count reaches 20 before the time reaches 10sec, there is certain output. The problem is that I don't know how to create the time interval. I try to use the timer interrupt but I don't know how to implement it. Hope you can give me the idea.
Code: |
#include <16f628.h>
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1)
int count = 0;
#int_ext
void isr_ext()
{
for(count=0;count<=20;count++)
{
SET_TRIS_B(0b00000001); \\port B0 is set as input
printf("Count Increase = %d\r\n",count);
if(count==20) \\ condition fulfillment
{
printf("Sense \r");
}
}
}
void main()
{
SET_TRIS_B(0b00000001); \\port B0 is set as input
count = 0;
enable_interrupts(int_ext); \\ enable external interrupt
enable_interrupts(global); \\ enable interrupt global
ext_int_edge(L_TO_H); \\ interrupt at rising edge
while(true){
}
}
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Apr 24, 2010 2:27 am |
|
|
Not tested for compilation errors but try something like this: Code: | #include <16f628.h>
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, ERRORS)
#define SOME_DELAY 1000 // delay time between tests [ms]
#define TEN_SECONDS (10 * (1000 / SOME_DELAY))
int8 count;
#int_ext
void isr_ext()
{
count++;
}
void main()
{
SET_TRIS_B(0b00000001); // port B0 is set as input
enable_interrupts(INT_EXT); // enable external interrupt
ext_int_edge(L_TO_H); // interrupt at rising edge
clear_interrupt(INT_EXT) // Clear interrupt
enable_interrupts(GLOBAL); // enable interrupt global
while(TRUE)
{
// Wait for first pulse
count = 0;
while(count == 0) {}
// Wait for 20 pulses or 10 seconds to pass, whatever comes first
while ((count < 20) && (TimePassed < TEN_SECONDS))
{
delay_ms(SOME_DELAY);
}
// Test reason for loop exit
if(count>=20) // condition fulfillment
{
printf("Sense \r");
}
}
} |
For faster response time you can change SOME_DELAY to a smaller value.
If the external interrupt is triggered by a switch you could add a delay of 10ms inside the interrupt routine for switch debouncing. |
|
|
farah0103
Joined: 20 Apr 2010 Posts: 6
|
|
Posted: Thu May 06, 2010 9:48 am |
|
|
Thanks a lot...I really appreciate it...It really helps |
|
|
|
|
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
|