PART 1 : Basic Introduction
PART 2 : Interrupt
PART 3 : Timer
PART 4 : Calculation
PART 5 : LED Switching Project [END]
Time Calculation:Part-4
In this part we will connect a push button with microcontroller which will create an external interrupt and we will calculate the time duration of that interrupt .
Calculation :
Now , we need to know how to calculate duration time of an event .
![]() |
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-4 |
First, we know that clock frequency will be divided by 4 and secondly, it will go to prescalar. Finally, clock goes to timer register.
So, if we use 12MHz clock and 1:4 prescalar. First we will get system clock (12/4)=3MHz clock or (12000000/4)=3000000Hz clock. Secondly it goes to prescalar and divided by 4 again. So we get (3000000/4)=750000Hz clock.
We know that for each pulse TMR0L(Timer0) register is incremented by one. So when 750000 Hz clock successfully go to timer0 register and the TMR0L register increments for 750000 times. Is it clear? We have a problem here, our TMR0L register can only count from 0 to 255.
As we need to count 750000 or greater than 750000. We have to walk in another way. We will set an integer variable count which will be incremented for each overflow interrupt. That means, when TMR0L counts 255 and overflow interrupt occurs. So it gets reset and count is incremented by one. Actually when overflow interrupt occurs, it increases the value of count variable by one. That means count=count+1;
Now consider an External Interrupt situation on RB0 pin where count=3 and TMR0L(8bit Register)=123.
How many pulses has been passed on that External Interrupt situation ?
Total pulses will be [On that situation]
tp = (count * 255) + TMR0L;
tp = (count * 255) + TMR0L;
Think about that, We are using 12MHz clock and finally we get 750000Hz clock from 12MHz clock.
So , What is the meaning of this 750000 Hz clock frequency ?
It means, when 750000 pulses have been received, time is 1 second. We read the definition of frequency in physics. So the Time period t =(1/f); The time required for one pulse.Right Now, we can say that :
tp=(count * 255)+ TMR0L ;
750000 total pulses means = 1 second
750000 total pulses means = 1 second
so 1 ,, ,, ,, = (1/750000)second.
so tp ,, ,, ,, = (tp / 750000)second.
or [ (tp / 750000)*1000] miliseconds
or [ (tp / 750000)*1000000] microseconds
Using this method we can measure time duration of the interrupt.
MikroC Code of the Project :
sbit LCD_RS at RB7_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D7 at RB2_bit;
sbit LCD_RS_Direction at TRISB7_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
// End LCD module connections
unsigned int count=0,sgnal=0;
int chk=7,j=0,tmr=0,cnt=0;
float tp=0.00, tr=0.00;
char txt[]=" ";
char tmrl[]=" ";
void interrupt()
{
if(INTCON.TMR0IF==1){ // checking if overflow interrupt occurs
PORTC=0x00;
INTCON.TMR0IF=0; // clearing the overflow intrrupt flag bit
count=count+1;
if(count>=0xFF){
TMR0L=0; // clearing the timer register
count=0; // clearing count
}
}
if(INTCON.INT0IF==1){ // checking if external interrupt occur
INTCON.INT0IF=0; // clearing the external intrrupt flag bit
// so it will be wise , to reset timer before checking the interrupt flag
cnt=count;
tmr=TMR0L;
tp=0.00;
tr=0.00;
TMR0L=0; // clearing the timer register
count=0; // clearing count
tr=(cnt * 255) + tmr ;
tp= tr/750000;
tp=tp*1000; // sec to mili second
sgnal=tp;
}
}
void main()
{
ADCON1=0x0F;
CMCON=7;
TRISC=0x00;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
INTCON.GIE=1;
INTCON.PEIE=1;
INTCON.INT0IE=1;
INTCON.INT0IF=0;
INTCON2.RBPU=1; // DISABLE PULUP REGISTER
INTCON2.INTEDG0=0;
T0CON=0xC1; // setting up the timer0
INTCON.TMR0IE=1; // enabling timer overflow interrupt
while(1){
PORTC=0xFF;
inttostr(sgnal,txt);
Lcd_Out(1,1,"Time in milisec");
Lcd_Out(2,1,txt);
}
}
Proteus Circuit of the Project :
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-4 |
Note: Perhaps, we can measure the highest time: probably it is [{(255*255)+255}/750000]*1000=87.04 milliseconds. So we can measure the accurate time for 87 milliseconds probably. Actually Push button is not perfect for this. You may use another variable to increase measuring capacity.You may send pulses having time period less than 87 milliseconds on RB0 pin. I am suggesting you provide signal on RB0 pin from another microcontroller using PWM. It will be good for you! Thank You.
cannot download your projects... error (429)... plse help!
ReplyDeleteany alternative link ?
Link was updated. But you can download whole thing at the end of the tutorial series.
Deletethanks for the tips and information..i really appreciate it.. walky talky
ReplyDeleteYou are welcome.
DeletePlease let me know if you’re looking for a article writer for your site. You have some really great posts and I feel I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some material for your blog in exchange for a link back to mine. Please send me an email if interested. Thank you! infrared heating panels
ReplyDeleteThanks for this great post, i find it very interesting and very well thought out and put together. I look forward to reading your work in the future. heating tunnel
ReplyDeleteThanks for posting this fabulous post.
ReplyDeleteUsually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man,Keep it up. Meanwhile visit our website for amity project synopsis
ReplyDeleteThe call for the pilot licence holders is all over again developing more potent and the aspirants can now in fact see their dreams flying to fact. CEA Aviation, with its specialisation in aviation and considerable diploma in national and international first rate Commercial Pilot Training in India is amongst a particular few in India and is proving to be an outstanding launch pad for pilots with its International institutions for the tremendous feasible Pilot training.
In today's digital age, it is more important than ever to have a strong online presence. At our company, we offer digital marketing and e-commerce services to help businesses boost their online presence. Our e-commerce platform is a great way for businesses to sell their products online without having to pay any commission fees. We also offer a variety of other digital marketing services to help businesses grow their online reach. Contact us today to learn more about how we can help you boost your business online!
ReplyDeleteTo Know More Contact Us