View previous topic :: View next topic |
Author |
Message |
rgraham8
Joined: 28 Oct 2010 Posts: 16
|
PIC18F452 PULSE Counter |
Posted: Thu Oct 28, 2010 2:45 pm |
|
|
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(;;){
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 28, 2010 3:19 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
rgraham8
Joined: 28 Oct 2010 Posts: 16
|
|
Posted: Thu Oct 28, 2010 3:22 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 28, 2010 4:06 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
rgraham8
Joined: 28 Oct 2010 Posts: 16
|
|
Posted: Thu Oct 28, 2010 4:09 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 28, 2010 4:16 pm |
|
|
What's the manufacturer and part number of the Hall sensor ? |
|
![](templates/subSilver/images/spacer.gif) |
rgraham8
Joined: 28 Oct 2010 Posts: 16
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 28, 2010 4:29 pm |
|
|
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) ? |
|
![](templates/subSilver/images/spacer.gif) |
rgraham8
Joined: 28 Oct 2010 Posts: 16
|
|
Posted: Thu Oct 28, 2010 4:32 pm |
|
|
I had 12V from a voltage generator supplied to it. As far as a pull up resistor, what kind of resister should I use? |
|
![](templates/subSilver/images/spacer.gif) |
rgraham8
Joined: 28 Oct 2010 Posts: 16
|
|
Posted: Thu Oct 28, 2010 4:34 pm |
|
|
And I do have the Sensor connected to ground as well |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 28, 2010 4:40 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
rgraham8
Joined: 28 Oct 2010 Posts: 16
|
|
Posted: Thu Oct 28, 2010 4:43 pm |
|
|
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) |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 28, 2010 4:54 pm |
|
|
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
|
|
|
![](templates/subSilver/images/spacer.gif) |
rgraham8
Joined: 28 Oct 2010 Posts: 16
|
|
Posted: Thu Oct 28, 2010 4:56 pm |
|
|
sounds great. Thanks. Is it still ok to power the sensor by +12V? |
|
![](templates/subSilver/images/spacer.gif) |
rgraham8
Joined: 28 Oct 2010 Posts: 16
|
|
Posted: Thu Oct 28, 2010 5:00 pm |
|
|
Also, when I connect the sensor the the oscilloscope, does this mean the high signal will effectively be closer to 5V? |
|
![](templates/subSilver/images/spacer.gif) |
|