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 CCS Technical Support

PIC18F452 PULSE Counter
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rgraham8



Joined: 28 Oct 2010
Posts: 16

View user's profile Send private message

PIC18F452 PULSE Counter
PostPosted: Thu Oct 28, 2010 2:45 pm     Reply with quote

I am using the PIC18F452 to count pulses. I am using Timer0 to calculate elapsed time. I am trying to use Timer1 to count pulses from a Hall Sensor. However, the Timer1 counter never increments. I tried to use the portB interrupt pins, but they never seemed to detect changes in PULSE. I have posted my code below. Any help would be great!
Code:

#include <18F452.H>
#use delay(clock=20000000) // Frequency set at 20Mhz
#include <flex_lcd.c>
#fuses HS,NOWDT,NOBROWNOUT,NOPROTECT,PUT
#use standard_io(b)

// # of Overflows in 1 sec Calculation //
// ----------------------------------------
// 1/((1/(10000000/4)) *256 * 256) = 38.14 overflows in 1 sec
// FCPU = 10Mhz/4

// Time Period = 1/(10Mhz/4)
// Prescaler Period = Timer Period * 256
// Overflow Period = Prescaler Period * 256

// 1/Overflow Period = # overflows in 1 sec
//-----------------------------------------

// SENSOR -> PIN_C0
#define LED1 PIN_a3
int count=0;
int timer_value;
#INT_TIMER0
void timer_irg(){
   count++;
   timer_value = get_timer1();
   //printf(lcd_putc, "\fCount: %d ", timer_value);
   if (count == 38){ // 38 overflows per second
      count = 0; // reset counter
      set_timer1(0); // reset timer1 counter
      output_toggle(LED1); // turn on/off LED
      printf(lcd_putc, "\f%d /sec", timer_value);
   }
}
      

void main(){
   int counter;
   lcd_init();
   setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1); // Set up timer 1 (counter);
   set_timer1(0); // Reset timer1 counter
   setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256 | RTCC_8_BIT); // Set up timer0 to use internal clock, 8bit mode, and prescale of 256;
   set_timer0(0); // Reset timer0
   enable_interrupts(INT_TIMER0); // interrupt on every timer0 overflow
   enable_interrupts(GLOBAL);
   for(;;){
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 3:19 pm     Reply with quote

Start by getting rid of all Timer0 code and related code. Just run your
signal into the T1 external clock input, and periodically read Timer1 in
a while() loop in main, and display the results in a terminal window.
This very simple program will allow you to trouble-shoot the Timer1 as
external counter problem.
rgraham8



Joined: 28 Oct 2010
Posts: 16

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 3:22 pm     Reply with quote

I did this, and it was not receiving any values. I am confused. Do the RC1 and RC0 pins need to be tied together? I saw this somewhere recently. I know Timer1 works as I tried it as the main timer.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 4:06 pm     Reply with quote

What are the Timer1 input signal voltage levels ? On a 5v PIC, they
need to be from 0v for the low level, to at least 4.0 volts for the high
level. This is a requirement for the external clock signal for Timer1.

Also, are you confusing using an external clock for Timer1, with using
the CCP1 or CCP2 module in capture mode ? These are different.
rgraham8



Joined: 28 Oct 2010
Posts: 16

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 4:09 pm     Reply with quote

The Hall Sensor I am using utilizes 12V of external input power. The signal wire is then connected to the Timer input pin (RC0). I connected the signal pin to RA4 (Timer0 input) and it successfully incremented the counter. I just don't know what the problem is when using the Timer1 external input pin.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 4:16 pm     Reply with quote

What's the manufacturer and part number of the Hall sensor ?
rgraham8



Joined: 28 Oct 2010
Posts: 16

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 4:20 pm     Reply with quote

The datasheet for the sensor is here --> http://www.google.com/url?sa=D&q=http://www.electro-sensors.com/documents/906_907_ES116_RevI.pdf&usg=AFQjCNHikRDIIraYcyjtwq73r8uzjDCdxw
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 4:29 pm     Reply with quote

http://www.electro-sensors.com/documents/906_907_ES116_RevI.pdf
The data sheet says this at the bottom of the page:
Quote:
Output Type ............... NPN Open Collector

That means a pull-up resistor is required on the output signal to get a
high level. The output driver can pull down, but it can't pull up without
that resistor. Do you have have a pull-up on the output ? What is the
value of the resistor ? What voltage is it connected to ? (Hopefully not
+12v).

Do you also have a ground connection between the Hall Effect sensor
and the PIC's ground (Vss) ?
rgraham8



Joined: 28 Oct 2010
Posts: 16

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 4:32 pm     Reply with quote

I had 12V from a voltage generator supplied to it. As far as a pull up resistor, what kind of resister should I use?
rgraham8



Joined: 28 Oct 2010
Posts: 16

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 4:34 pm     Reply with quote

And I do have the Sensor connected to ground as well
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 4:40 pm     Reply with quote

I would start with a 1K ohm pull-up, connected to +5v. This is for the
output signal of the sensor. This signal (with the pullup) then goes to
the Timer1 external clock input pin on the PIC.

The power pin of the sensor is different than the output signal pin.
I am only talking about the output signal pin, in the paragraph above.
rgraham8



Joined: 28 Oct 2010
Posts: 16

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 4:43 pm     Reply with quote

Ok, so I will connect the power pin of the sensor to the power supply, and the signal pin to a 1K ohm resistor, and the resistor to the timer1 input. is this correct? (along with the ground connections)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 4:54 pm     Reply with quote

The connections between the sensor and the PIC should look like this:
Code:

                  +5v
                   |
                   <
                   > 1K ohm       
                   <         
                   |                     
From Sensor --------------- To PIC T1CKI pin (RC0)
output pin                         


From Sensor --------------- To PIC Vss pin (or the board ground)
Ground pin
rgraham8



Joined: 28 Oct 2010
Posts: 16

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 4:56 pm     Reply with quote

sounds great. Thanks. Is it still ok to power the sensor by +12V?
rgraham8



Joined: 28 Oct 2010
Posts: 16

View user's profile Send private message

PostPosted: Thu Oct 28, 2010 5:00 pm     Reply with quote

Also, when I connect the sensor the the oscilloscope, does this mean the high signal will effectively be closer to 5V?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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