View previous topic :: View next topic |
Author |
Message |
mourad
Joined: 02 Apr 2014 Posts: 7
|
timer in c |
Posted: Mon May 05, 2014 10:29 am |
|
|
Hello,
I am just wondering if there are any timers in c standard library.
I just need a simple timer that will start and stop.
example. Enter the function and start the timer. After a certain condition
stop the timer.
Many thanks, |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19477
|
|
Posted: Mon May 05, 2014 11:11 am |
|
|
The PIC (depending on the one involved), has hardware 'timers'. These are just counters, which increment from a source (either an external clock or a division from the master clock), that can be 'read' or set at will. So you clear one, and then at any time can read it, and know how many counts have passed from when it was cleared.
You can trigger an event at a particular count, with the 'CTC' module.
Then there is the #USE TIMER ability.
This uses one of the hardware timers, to give a system 'tick', which can be used for simple reading, or to schedule events.
Then some PIC's have an RTC module, giving times in a 'clock' form, rather than just a count. |
|
|
mourad
Joined: 02 Apr 2014 Posts: 7
|
|
Posted: Mon May 05, 2014 11:19 am |
|
|
i'm actually using a pic18f4520 and my objective is to start a function when the timer starts and i just can't figure a code to solve the problem |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19477
|
|
Posted: Mon May 05, 2014 11:28 am |
|
|
The timer starts when you start it....
I suspect you mean when it times out?. If so then it'd depend on the sort of timescale involved. For short timescales, the CTC. This can interrupt when a particular count is reached. For longer timescales, then just generate a 'tick' interrupt, and have it set a flag when the required number of counts has passed. Call your routine when the flag is set. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon May 05, 2014 12:36 pm |
|
|
can you post the code that you have written so far? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9215 Location: Greensville,Ontario
|
|
Posted: Mon May 05, 2014 1:36 pm |
|
|
there is an example of a 'stopwatch' in the examples folder that CCS supplies, that might be useful ?
I think it's called ex_swtc.ex or something close......
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19477
|
|
Posted: Mon May 05, 2014 1:49 pm |
|
|
Also, the #USE TIMER manual entry, shows how to use this to toggle a pin at 1 second intervals. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 05, 2014 3:59 pm |
|
|
Quote: |
Enter the function and start the timer. After a certain condition
stop the timer. |
Do you want to measure the time required to complete a function, or
do you want to do a timeout for it ?
Instead of asking in general terms, tell us specifically what you want.
Explain the whole problem, such as "The getc() function blocks. My
program can't accept this. How can I do a timeout for getc() ?".
There will likely be example code in the forum archives that we can
point you to, or we accurately suggest a solution. |
|
|
mourad
Joined: 02 Apr 2014 Posts: 7
|
|
Posted: Tue May 06, 2014 3:01 pm |
|
|
my project is to create a RJ45 cable tester ( straight through or cross over)
and i'm using 2 microchips ( master-slave)
the thing is i'm testing pair per pair of wires.
the concept that i thought of is simple :
lets say we're testing the 1st pair
pinD0 of the master micro sends '1', if pinD0 from the slave micro receives '1' it sends a '1' from pinD1 (slave) to pinD1(master).
i'm a beginner in programming microchips and that's the timer i thought of:
the Master micro:
Code: |
int1 TestSTPair(int8 p) // Test traight-through pair p
{
int address;
address = p<<1;
bit_set(PORTD,address); // test High
for(int i=0;i<100;i++)
{
if (!bit_test(PORTD,address+1))
return 0;
delay_ms(1);
}
bit_clear(PORTD,address); // test Low
for(int j=0;j<100;j++)
{
if (bit_test(PORTD,address+1))
return 0;
delay_ms(1);
}
return 1;
}
|
the Slave micro:
Code: |
void main()
{
int address=0;
while(address<7)
{
if(bit_test(PORTD,address))
bit_set(PORTD,address+1);
if(!bit_test(PORTD,address))
bit_clear(PORTD,address+1);
address++;
}
} |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue May 06, 2014 4:40 pm |
|
|
I'm no wiser as to how your 'simple' method will work.
This is at least your third go at an RJ45 cable tester.
PCM programmer pointed you towards a solution previously.
Why use two chips, which will then have to talk to each other?
What is wrong with the KISS principle using one chip only?
Mike |
|
|
mourad
Joined: 02 Apr 2014 Posts: 7
|
|
Posted: Wed May 07, 2014 12:13 pm |
|
|
if the cable was long and already installed and can not be removed from it place, we can use 2 devices to test the cable if it's working or not.
we put one in an edge and the other in the other edge. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19477
|
|
Posted: Wed May 07, 2014 12:38 pm |
|
|
That is why most testers have a loopback connector you put at the other end. |
|
|
mourad
Joined: 02 Apr 2014 Posts: 7
|
|
Posted: Wed May 07, 2014 12:57 pm |
|
|
you've just make my work easier :D
thx man :P |
|
|
|