View previous topic :: View next topic |
Author |
Message |
ad
Joined: 24 Mar 2015 Posts: 14
|
speedometer |
Posted: Wed Apr 22, 2015 4:23 am |
|
|
Hi all, I probably did not explain what I wanted to do and not intended that you should do me the program, sorry if it seemed that.
I have connected to the pin 6 (RA4 / T0CK1) of PIC18F4550 inductive sensor. What I intend to do is the following. It is through a small wheel about 8 cm in diameter, it will give impetus by hand, so that it takes speed to simulate what would in the wheel of a motorcycle on a small scale. I'm trying to make the program through the TIMER0 count pulses (applying the formula for speed), but can not get it right and I'm rolling, if you could advise me. Thank you.
Code: |
#include <18F4550.h>
#fuses NOWDT,XT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#include "C:\Users\xx\Desktop\VELOCIDAD\flex_lcd420.c"
int16 counter;
float Velocidad=0;
#int_TIMER0
void TIMER0_isr(){
counter=get_timer0(); //lee contador TMR0
Velocidad= Counter * 25.13 * 0.036;
printf(lcd_putc,"%61u vel", Velocidad);
lcd_gotoxy(1,1);
counter=0;
Velocidad=0;
set_timer0(0); //Reinicia cuenta
}
void main(){
lcd_init();
setup_timer_0(rtcc_ext_l_to_h|RTCC_DIV_2); //ConfiguraciĆ³n TMR0
set_timer0(0); //Borrado contador
enable_interrupts(global);
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Wed Apr 22, 2015 5:05 am |
|
|
hmm..
inductive sensor ...
Have you got an oscilloscope to see the output of the sensor ? The PIC needs a good ,clean squarewave NOT a lazy, AC waveform as the input.
Most inductive sensors I've used need 'signal conditioning' to get a proper,clean input to the PIC.
You should post a 'link' of the inductive sensor datasheet.
Also, it's a good idea to use a second PIC as a 'frequency generator' to use as a known signal source. AN 8 pin PIC can easily give you low, mid, high 'speed signals' for testing and calibrations.
Jay |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Apr 22, 2015 9:12 am |
|
|
Code: |
Velocidad= Counter * 25.13 * 0.036;
printf(lcd_putc,"%61u vel", Velocidad);
lcd_gotoxy(1,1); |
GET THIS OUT OF THE ISR !!!!!!!!!!
and when you do - reduce this to a single operation.
25.13 * 0.036; |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 22, 2015 9:50 am |
|
|
Quote: | and when you do - reduce this to a single operation.
25.13 * 0.036; |
If you put the two numbers in parentheses like this,
Code: | Velocidad= Counter * (25.13 * 0.036); |
the compiler will do the math at compile-time, instead of
creating code to do it at run-time. In other words, it
will pre-calculate the result of 0.905 so the PIC doesn't
have to do it. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Apr 22, 2015 10:31 am |
|
|
You also need to look at the sensor output over the full range of expected speeds.
Mike |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 22, 2015 10:52 am |
|
|
Put a while(TRUE) statement at the end of main(). The compiler puts
a hidden Sleep instruction there, and you don't want to execute that
instruction. A while(TRUE) statement will prevent this.
Quote: | void main()
{
lcd_init();
setup_timer_0(rtcc_ext_l_to_h|RTCC_DIV_2); //ConfiguraciĆ³n TMR0
set_timer0(0); //Borrado contador
enable_interrupts(global);
while(TRUE);
}
|
And then of course, this:
Quote: | #include <18F4550.h>
#fuses NOWDT,XT,NOPROTECT,NOLVP
#use delay(clock=20000000) |
Which means you're running this in Proteus. |
|
|
|