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

Need Help in Timer Interrupts, CCS C and PIC16F628A

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



Joined: 18 Oct 2004
Posts: 21

View user's profile Send private message

Need Help in Timer Interrupts, CCS C and PIC16F628A
PostPosted: Sat Nov 04, 2006 8:57 am     Reply with quote

I am making a speed measurement device.

Speed ranges from 250 fps to 800 fps, so timing is really critical.

I tried using this kind of loop but seems creates some kind of delay, attached is my original code.

Code:
include "C:\Documents and Settings\Glenjoy\My Documents\Chronograph 2\Chronograph.h"
#include <LCD>

long micro_S;
long milli_S;

long speed;

void main()
{



   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   lcd_init();
   setup_oscillator(False);

   speed = 0;

   set_tris_a(0x03);
 //  set_tris_b(0x00);

   output_b(0x00);

   lcd_putc("\f");
   lcd_putc("S: ");

   for(;;)
   {



      while(input(PIN_A0)==0);

      lcd_putc("\f");
      lcd_putc("S: ");


        do

         {
         delay_us(1);
         micro_S++;
         }while(!input(PIN_A1));

      speed = 328080/micro_S;

      milli_S = micro_S;

      micro_S = 0;


      printf(LCD_PUTC, "%Lu",speed);

   }
}


I need your help in editing this code by using interrupts, the code creates a delay in display of lcd.

Thanks.

Glenjoy
Ttelmah
Guest







PostPosted: Sat Nov 04, 2006 11:18 am     Reply with quote

Of course the loop creates a delay. _Everything_ you do in a microprocessor takes time. The act of incrementing the counter, the act of testing the input, and the act of looping back.
I don't understand what you mean about 'creates a delay in display of LCD'. The code will only wait for the high pulse input.
Interrupts may well not be a good choice, becaus of the delays associated with these. Consider something like:
Code:

   setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
   while (true) {
      while(!input(PIN_A0));
      set_timer1(0);
      lcd_putc("\f");
      lcd_putc("S: ");
      while(!input(PIN_A1));
      micro_S=get_timer1();
      speed = 328080/(int32)micro_S;
      printf(LCD_PUTC, "%Lu",speed);
   }

Are you sure about your inputs?. You are waiting for A0 to rise, and then timing till A1 rises. If these pulses are from a normal tacho signal, I'd have expected them to be on the same pin.

Best Wishes
dbotkin



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Nov 04, 2006 2:05 pm     Reply with quote

Just a question. You have a CCP on that chip, why not use it? The LCD driver doesn't even use the CCP pin (RB3).

At the very least let a timer do the work for you. Clear the timer at the beginning of the event, read it at the end. Trying to time stuff with a software loop is a terrible waste of the hardware you have at hand. Make the chip work for you. You've already paid for it!
glenjoy



Joined: 18 Oct 2004
Posts: 21

View user's profile Send private message

PostPosted: Sat Nov 04, 2006 4:28 pm     Reply with quote

I do not know how to use CCP.
glenjoy



Joined: 18 Oct 2004
Posts: 21

View user's profile Send private message

PostPosted: Sun Nov 05, 2006 2:14 am     Reply with quote

This is not a tachometer, this is a chronometer, it measures the speed of a BB bullet.

Thanks.

Glenjoy
Ttelmah
Guest







PostPosted: Sun Nov 05, 2006 3:44 am     Reply with quote

Fair enough. A pair of optical sensors, with differentiators on them, so they return a pulse on the fast 'edge' as the pellet goes over. You might ned to have a pulse 'stretcher' (monostable multivibrator), on each signal, to ensure that the pulses fed to the chip are long enough to be reliably seen. The code as I posted it, should be close to useable.
The alternative though, is to use a little more hardware, and drive the two signals into a flip-flop. Have the first reset this, and the second trigger it. This then gives you a single pulse, corresponding to the period between the two input pulses. This can then be used to drive the CCP. You set this to 'capture mode', reset the timer, when you see the pulse go high, and then when the CCP triggers, the count stored is the length of the pulse.

Best Wishes
dbotkin



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Nov 08, 2006 8:48 pm     Reply with quote

glenjoy wrote:
I do not know how to use CCP.


What a perfect excuse to learn something new, then! I didn't know how to use it either, until I needed to use it for PWM -- then I learned to use it for PWM. Some time later I neede to use it for a a tachometer, and learned to use the capture mode. The PIC Midrange manual is pretty good for learning how to use the various peripheral devices.

Seriously... using the timers makes life much easier (and gets projects working), and learning the more complex devices like the CCP makes life even easier. And you only have to learn it once.
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