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

Timer 1 problem. How to make this work..

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







Timer 1 problem. How to make this work..
PostPosted: Fri May 02, 2003 3:12 am     Reply with quote

Hello.
I have a problm with timer1. I can't get this to work. So If anyone pleace could help me!

Here is some of my code:

#include "16F877.h"
#use delay(clock=11000000)

main()
{

float time,starttime,time1;
int i=0;
long value;
byte verdi;

setup_adc_ports(A_ANALOG);
setup_adc(ADC_CLOCK_DIV_32);

setup_timer_1(T1_external);
set_timer1(0);
while(TRUE){

minir=1024;
maxir=0;
for(i=0;i<=100;i++){

SET_ADC_CHANNEL(0);
output_low(PIN_C0);
delay_us(15000);
value = Read_ADC();
if(value < minir)
minir=value;
if(value > maxir){
maxir=value;
time=get_timer1();
printf("Time \%f",time); if(time>125)
printf("Time is over 125");
MY QUESTION IS: What am I doing wrong with the timer. I was told to keep the timer running and just use the get_timer function to find the time for every new max value maesured. I need to get the time for every max value that is over 125ms.
How can I do this?

Thank you so much for the help!
___________________________
This message was ported from CCS's old forum
Original Post ID: 14137
R.J.Hamlett
Guest







Re: Timer 1 problem. How to make this work..
PostPosted: Fri May 02, 2003 5:06 am     Reply with quote

:=Hello.
:=I have a problm with timer1. I can't get this to work. So If anyone pleace could help me!
:=
:=Here is some of my code:
:=
:=#include "16F877.h"
:=#use delay(clock=11000000)
:=
:=main()
:={
:=
:=float time,starttime,time1;
:=int i=0;
:=long value;
:=byte verdi;
:=
:=setup_adc_ports(A_ANALOG);
:=setup_adc(ADC_CLOCK_DIV_32);
:=
:=setup_timer_1(T1_external);
Start here. What is connected to the Timer1 input?. You are telling the system to use this input to increment the timer, so the results will depend on what is being fed into this pin. To work and read in mSec (as you seem to expect further on), the timer pin will need a 1KHz signal connected.

:=set_timer1(0);
:=while(TRUE){
:=
:= minir=1024;
:= maxir=0;
:= for(i=0;i<=100;i++){
:=
:= SET_ADC_CHANNEL(0);
:= output_low(PIN_C0);
:= delay_us(15000);
:= value = Read_ADC();
:= if(value < minir)
:= minir=value;
:= if(value > maxir){
:= maxir=value;
:= time=get_timer1();
get_timer, is a long integer, not a float. The compiler _should_ convert here, but one general problem with the CCS compiler, is that automatic type 'casts', can be unreliable. Realistically, the arithmetic will be faster, if you retain the value as an int16.

:= printf("Time \%f",time);
This should work, but you are again creating a lot of processor overhead, by outputing a float (which involves a lot more work than an integer)....

:= if(time>125)
Again here, you are making extra work. 'time' at this point is a float, and you are comparing it with an integer. The compiler will have to cast the integer into a float, to do the comparison.

:= printf("Time is over 125");
:=MY QUESTION IS: What am I doing wrong with the timer. I was told to keep the timer running and just use the get_timer function to find the time for every new max value maesured. I need to get the time for every max value that is over 125ms.
:=How can I do this?
:=
:=Thank you so much for the help!

If you have not got an external 1KHz signal, then use something like this instead:

int16 time;

#int_timer2
void tick_handler(void) {
static tick=11;
if (--tick==0) {
++time;
tick=11;
}
}

In main, remove the timer1 references, and add:
setup_timer_2(T2_DIV_BY_1,49,5);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);

Then when you want to start the timer, simply set 'time=0';

To display the value you can just access 'time' as a variable, which counts in mSec.

What is happening with this code, is that timer2, counts on the master clock/4, from 0 to 49, then restarts, giving a timer period of 18.18uSec. This happens five times, then an interrupt is generated every 90.9uSec. In this interrupt, 'tick' is decremented, and if it gets to zero, 'time' is incremented. 'tick' counts through eleven values, so 'time' increments every mSec.
Hence you can just read 'time' whenever wanted and use this as a value.
Now your 'timer_1' code, will also work, but unless you have the external 1KHz clock, you have to use the internal oscillator. Unfortunately at the clock rate given (11MHz), you can't make this timer count in mSec intervals, so you would have to 'scale' the resulting number. The advantage of using this timer, is that the processor doesn't have to handle the code in the interrupt. However having a timer running at mSec intervals, would allow you to make the sampling itself 'synchronous', with code like:

int16 wait_till;

wait_till=time+15;
while (time
This will wait till the 'time' has advanced by 15mSec, and whereas with a 'delay_us', the chip is doing nothing else, with this type of code, you could be doing other checking/processing int the while loop.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 14140
MOLLY
Guest







Re: Something with the counter.
PostPosted: Sun May 04, 2003 7:18 pm     Reply with quote

Thank you for your reply. I have tested your code, but the timer just counts to 29000 and then goes down again with - values. Why is this hapening? And one more thing I couldn't use int16, even when I used TYPE int16. So I used long instead . I 'm just wondering can I use time just like an variable or do i have to use get_timer2? Do I have to write any other code?

Thank you som much for your help!!
___________________________
This message was ported from CCS's old forum
Original Post ID: 14174
R.J.Hamlett
Guest







Re: Something with the counter.
PostPosted: Mon May 05, 2003 7:02 am     Reply with quote

:=Thank you for your reply. I have tested your code, but the timer just counts to 29000 and then goes down again with - values. Why is this hapening? And one more thing I couldn't use int16, even when I used TYPE int16. So I used long instead . I 'm just wondering can I use time just like an variable or do i have to use get_timer2? Do I have to write any other code?
:=
:=Thank you som much for your help!!
int16, depends on the age of your compiler. The version3 compilers, define this type (together with int8, and int32), to make it more 'obvious' how long a particular integer is.
You can generate an int16, by either a simple:
#define int16 long int

or by using typedef (not 'type').
This suggests, that you have a very old compiler.

Now which 'timer' just counts to 29000?. As I said in my original post, your solution will depend on how your hardware is actually set up. The solution given, is for a crystal at 11MHz, and does nothing except maintain the 'time' counter.

#include "16F877.h"
#use delay(clock=11000000)
#device ADC=10

long time;

#int_timer2
void tick_handler(void) {
static tick=11;
if (--tick==0) {
++time;
tick=11;
}
}

main()
{

int i;
long value;
byte verdi;

setup_adc_ports(A_ANALOG);
setup_adc(ADC_CLOCK_DIV_32);

setup_timer_2(T2_DIV_BY_1,49,5);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
time=0;

minir=1024;
maxir=0;
SET_ADC_CHANNEL(0);

for(i=0;i<=100;i++){
output_low(PIN_C0);
while ((time \% 15) != 14) ;
value = Read_ADC();
if(value < minir)
minir=value;
if(value > maxir){
maxir=value;
printf("Time \%lu",time); if(time>125l)
printf("Time is over 125");

Now this excludes the rest of the code, but is based on your original posted example.
Remember that the 'printout' section as shown, will never be reached if the incoming value is not greater the 'maxir'.
___________________________
This message was ported from CCS's old forum
Original Post ID: 14181
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