Motor RPM using IR and 8051
I am trying to make RPM Meter using 8051 Microcontroller.
code:
#include <REG51.h> #include <stdio.h> sbit LCD_en=P0^0; // LCD EN sbit LCD_rs=P0^1; // LCD RS sbit motor_1_signal=P1^5; unsigned int cnt=0; unsigned char buf[8]; void msdelay(unsigned int value) { unsigned int i,j; for(i=0;i<value;i++) for(j=0;j<100;j++); } void delay() { unsigned int i; for(i=0;i<1500;i++) {TMOD = 0x01; // Timer 0 Mode 1TH0= 0xF8; //initial value for 2msTL0 = 0xCC;TR0 = 1; // timer startwhile (TF0 == 0) // check overflow condition{ if(motor_1_signal==1) { cnt=cnt+1; while(motor_1_signal==1); }}TR0 = 0; // Stop TimerTF0 = 0; // Clear flag } } void main() { unsigned int rpm,i; msdelay(20); LCD_init(); lcdclear(); buz=0; lcdrow1(); LCD_puts("MOTOR-1 RPM"); while(1) { cnt=0; delay(); rpm=0; rpm=cnt*20; for(i=0;i<8;i++) buf[i]='\0'; lcdrow2(); sprintf(buf, "%d", rpm); LCD_puts(buf); LCD_puts(" "); } }
as i am rotating the DC motor at the speed of 80 RPM(i am sure about the speed of Motor because at 80 RPM, the RPM can be easily visualize and counted manually), but the it shows 280 to 300 on LCD.
what i have done in the code is:
created a delay function for 3 seconds and incremented the cnt variable by 1, and again after delay function i multiple the cnt valve by 20 to get the value for 1 minute and store it in rpm and then print it on LCD.
can anybody please let me know what the issue is
Here is the image of IR and Motor.
Crystal value is 11.0592
Hi,
What if during this: while(motor_1_signal==1);
a timer overflow happens?
--> you will miss the timer overflow and thus your timing is not correct.
Did you check wheteher the "3s" are correct.
Why don′t you use an interrupt? Less programming effort, best precision, less then 1% processing time.
Klaus
You are right Klaust.
i am having the wrong timing. the timing goes around 4.25 to 4.9 seconds instead of 3
will try using an interrupt
yes... bit confused.. can you help me for the logic.
how can i implement this using timer interrupt.
should i increment cnt value by 1 in interrupt and if so for how many seconds.
how can i create a delay for specific time
Hi,
Use one timer to generate the "window" timing.
User another counter, to count the edges.
****
You may use one interrpt, but then you need to give us the timing of the IR signal. At least min HIGH time and min LOW time. Absoulte values.
Klaus
sorry did not get your point.
how can i give the min HIGH time and LOW time of IR signal... it depends upon the RPM of motor, which is going to vary.
Hi,
true:
* LOW time depends on RPM
* HIGH time depends on RPM
But MINIMUM timing is your personal decision.
We can′t know what′s your maximum RPM and what is the expectable duty cycle. Only you know.
Klaus