|
|
View previous topic :: View next topic |
Author |
Message |
Acetoel Guest
|
Playing with Timer1 (16-bit) and I/O Pin |
Posted: Wed Mar 26, 2003 6:38 pm |
|
|
Hello, I would like to starts the Timer1 when RC.7 (on a 16F76 running at 16MHz) goes from High to Low, and stops when goes from Low to High. The result of the Timer1 has to be in a Unsigned Int16 variable.
How can I do this? I wrote a piece of code, that doesn't work, why? Is this okey? Can I improve this?
while (RXPin);
If (!RXPin) {
Set_Timer1(0);
While (!RXpin);
Pulse = get_timer1();
SPEN = 1; //USART Enable
}
Thanks very much,
Ezequiel
___________________________
This message was ported from CCS's old forum
Original Post ID: 13114 |
|
|
Robert Holmer Guest
|
Re: Playing with Timer1 (16-bit) and I/O Pin |
Posted: Thu Mar 27, 2003 2:25 am |
|
|
:=Hello, I would like to starts the Timer1 when RC.7 (on a 16F76 running at 16MHz) goes from High to Low, and stops when goes from Low to High. The result of the Timer1 has to be in a Unsigned Int16 variable.
:=How can I do this? I wrote a piece of code, that doesn't work, why? Is this okey? Can I improve this?
:=
:= while (RXPin);
:= If (!RXPin) {
:= Set_Timer1(0);
:= While (!RXpin);
:= Pulse = get_timer1();
:= SPEN = 1; //USART Enable
:= }
:=
:=Thanks very much,
:=Ezequiel
Set_Timer1(0);
while (!RXPin); //If RXPin already is low you must wait until it goes high
while (RXPin); //Wait until RXPin goes from high to low
TMR1ON = 1; // Start timer
While (!RXpin);
TMR1ON = 0;
Pulse = get_timer1();
SPEN = 1; //USART Enable
Sure it is possible to simplify. The timer must be set up before and the counted value should not exceed 0xFFFF with this code...
/Rob
___________________________
This message was ported from CCS's old forum
Original Post ID: 13118 |
|
|
Robert Holmer Guest
|
Re: Playing with Timer1 (16-bit) and I/O Pin |
Posted: Thu Mar 27, 2003 2:36 am |
|
|
I forgot, where RXPin is read it should be input(RXPin), for example "while(!input(RXPin))..."
/Rob
___________________________
This message was ported from CCS's old forum
Original Post ID: 13120 |
|
|
Acetoel Guest
|
Re: Playing with Timer1 (16-bit) and I/O Pin |
Posted: Thu Mar 27, 2003 6:39 pm |
|
|
Hi,
So the final piece of code, should look like this. Is there any way I can make this code more efficient, more fast to execute. I tested using a 104uSec pulse, and the result that the timer gives in Pulse us 99uSec. I would like to have a little more of accuracy. Or I can add 5uSec to the Pulse variable whatever result is that variable ?!?!?
Thanks
Ezequiel
If (!Input(RXPin)) {
while (!input(RXPin));
Set_Timer1(0); // Reset Timer
while (input(RXPin));
TMR1ON = 1; // Start Timer
While (!input(RXpin));
TMR1ON = 0; // Stop Timer
Pulse = get_timer1();
SPEN = 1; // Enable UART
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 13163 |
|
|
Robert Holmer Guest
|
Re: Playing with Timer1 (16-bit) and I/O Pin |
Posted: Fri Mar 28, 2003 2:26 am |
|
|
:=Hi,
:= So the final piece of code, should look like this. Is there any way I can make this code more efficient, more fast to execute. I tested using a 104uSec pulse, and the result that the timer gives in Pulse us 99uSec. I would like to have a little more of accuracy. Or I can add 5uSec to the Pulse variable whatever result is that variable ?!?!?
:=Thanks
:=Ezequiel
:= If (!Input(RXPin)) {
:= while (!input(RXPin));
:= Set_Timer1(0); // Reset Timer
:= while (input(RXPin));
:= TMR1ON = 1; // Start Timer
:= While (!input(RXpin));
:= TMR1ON = 0; // Stop Timer
:= Pulse = get_timer1();
:= SPEN = 1; // Enable UART
:= }
If you check in the LST-file you could see what code is generated:
while (input(RXPin));
BTFSS xx.xx // If true: Two cycles 500 ns
goto xxx // If false: One cycle for BTFS and two for goto = 750 ns
TMR1ON = 1;
BSF xxx // One cycle 250 ns
So the maximal delay (worst case), before the timer starts must be 1.25 us (if the edge comes exactly after the BTFSS-instruction: 500 ns for Goto, 500 ns for a new BTFSS and finally 250ns for BSF). If the stop timer part is perfect it takes anyway 0.75 us. So the accuracy should be much better than what you have got so far. How is the timer setup? With the prescaler set to 1:1 the timer counts every 250ns. If I am not wrong this means a accuracy around 1 us.
/Rob
___________________________
This message was ported from CCS's old forum
Original Post ID: 13178 |
|
|
Acetoel Guest
|
Re: Playing with Timer1 (16-bit) and I/O Pin |
Posted: Fri Mar 28, 2003 11:03 am |
|
|
Hello,
This is the conf. for Timer1
Using 16MHz Clock
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
setup_timer_2(T2_DISABLED,0,1);
:=:=Hi,
:=:= So the final piece of code, should look like this. Is there any way I can make this code more efficient, more fast to execute. I tested using a 104uSec pulse, and the result that the timer gives in Pulse us 99uSec. I would like to have a little more of accuracy. Or I can add 5uSec to the Pulse variable whatever result is that variable ?!?!?
:=:=Thanks
:=:=Ezequiel
:=:= If (!Input(RXPin)) {
:=:= while (!input(RXPin));
:=:= Set_Timer1(0); // Reset Timer
:=:= while (input(RXPin));
:=:= TMR1ON = 1; // Start Timer
:=:= While (!input(RXpin));
:=:= TMR1ON = 0; // Stop Timer
:=:= Pulse = get_timer1();
:=:= SPEN = 1; // Enable UART
:=:= }
:=
:=If you check in the LST-file you could see what code is generated:
:=
:=while (input(RXPin));
:=BTFSS xx.xx // If true: Two cycles 500 ns
:=goto xxx // If false: One cycle for BTFS and two for goto = 750 ns
:=TMR1ON = 1;
:=BSF xxx // One cycle 250 ns
:=
:=So the maximal delay (worst case), before the timer starts must be 1.25 us (if the edge comes exactly after the BTFSS-instruction: 500 ns for Goto, 500 ns for a new BTFSS and finally 250ns for BSF). If the stop timer part is perfect it takes anyway 0.75 us. So the accuracy should be much better than what you have got so far. How is the timer setup? With the prescaler set to 1:1 the timer counts every 250ns. If I am not wrong this means a accuracy around 1 us.
:=
:=/Rob
___________________________
This message was ported from CCS's old forum
Original Post ID: 13206 |
|
|
Robert Holmer Guest
|
Re: Playing with Timer1 (16-bit) and I/O Pin |
Posted: Mon Mar 31, 2003 2:10 am |
|
|
:=Hello,
:=
:=This is the conf. for Timer1
:=Using 16MHz Clock
:=setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
:=setup_timer_2(T2_DISABLED,0,1);
:=
Try to change to the following(prescaler 1:1, timer stopped = (TMR1ON=0), internal clock):
setup_timer_1(0x00);
With this setup and code the maximum time for the pulse is approx 16 ms. You should obtain an accuracy around 1 us. If still not, can it be that you get a glitch or something like that on the actual input which stops the counting to early?
setup_timer_1(0x00);
If (!Input(RXPin)) {
Set_Timer1(0); // Reset Timer
while (!input(RXPin)); // Wait as long as RXPin is low
while (input(RXPin)); // Wait as long as RXPin is high
TMR1ON = 1; // Start Timer
While (!input(RXpin)); // Wait as long as RXPin is low
TMR1ON = 0; // Stop Timer
Pulse = get_timer1();
SPEN = 1; // Enable UART
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 13245 |
|
|
|
|
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
|