Wednesday, January 06, 2016

IR (Infrared) Communication Between Two Microcontrollers -Step By Step Tutorial : Part-4

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
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;

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

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  
  INTCON.TMR0IF=0;  // clearing the overflow intrrupt flag bit  
 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  
 TMR0L=0; // clearing the timer register  
 count=0; // clearing count  
 tr=(cnt * 255) + tmr ;  
 tp= tr/750000;  
 tp=tp*1000; // sec to mili second  
 void main()  
  Lcd_Cmd(_LCD_CLEAR);        // Clear display  
  T0CON=0xC1;  // setting up the timer0  
  INTCON.TMR0IE=1; // enabling timer overflow interrupt  
 Lcd_Out(1,1,"Time in milisec");  

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.

Watch the Video :



  1. cannot download your projects... error (429)... plse help!
    any alternative link ?

    1. Link was updated. But you can download whole thing at the end of the tutorial series.


Share and Bookmark this Blog

Developing Industrial Internet of Things

An Introduction to Programming the Internet of Things (IOT) (Online Course)

Industrial IoT Markets and Security (Online Course)


: (1) 18F2550 (1) 36KHz (3) and (1) Based (1) battery (1) Bipolar (1) Blinking (1) blinks (1) Bluetooth (1) bluetooth device interfacing (1) bluetooth module (1) button (1) circuit (1) clock (1) control (1) crystal oscillator (3) Db9 (1) DC Motor (2) digital (2) Digital Voting Machine (1) digital voting machine using pic (1) display (2) DS1307 (1) electronic (1) embedded c programming tutorial (11) embedded c tutorial (11) experiment kit (4) external interrupt (4) flash (1) flashing (1) Gas Leakage detector (1) HC-06 (1) home (1) how (1) How to (10) i2c tutorial (1) in (1) indicator (1) infrared Connection (3) interface (8) interfacing (3) Interrupt (3) Introduction (1) IR Connection (3) IR Receiver (4) IR Transmitter (4) key pad (1) keypad (1) lavel (1) Lcd 16x2 (2) lcd 2x16 (2) led (1) lm35 (2) LPG (1) machine (1) make (1) Make bootloader (1) making (1) matrix (1) max232 (1) membrane keyboard (1) meter (2) Micocontroller (1) microchip (4) microchip pic (2) microchips (3) microcontroller (9) microcontroller based (3) microcontroller programming (3) Microcontroller Project (4) Microcontroller Projects (1) microcontroller_project (2) microcontrollers (4) Microprocessor (2) mikroC (5) mikroc code to start and stopstart and stop dc motor (1) mikroc pro for pic (2) Motion detector (1) MQ-9 Gas Sensor (1) musical (1) NEC Protocol (4) pcb (5) PIC (3) pic controller (11) pic microcontroller (11) pic microcontroller tutorial (11) pic programming (1) pic programming in c (12) pic proteus (1) Pic Tutorial (12) pic18 (2) pic18f2550 (11) picmicrocontroller (4) picRFモジュール (1) PIR Motion Sensor (1) printed circuit board (1) proteus (6) pulse width modulation (1) push (1) push button (1) PWM (1) real (1) rf transmitter (3) Rs 232 (1) Rs232 (1) scroll (1) scrolling (1) Serial communication (1) Serial Connection (1) Serial Port (1) serial port rs232 (1) Servo Motembedded c programming tutorial (1) simulation (2) Soil Moisture Meter (1) speed control (1) step by step (7) step bystep (1) Stepper Motor (2) text (2) Thief Detector (1) time (1) timer (4) timer0 (4) tone (1) TSOP38236 Receiver (4) tutorial (2) Unipolar (1) USART Connection (1) USB (1) usb 1.0 (1) USB bootloadere (1) USB HID (1) using (9) voltmeter (1) voting (1) water level indicator (3) with (2) work (1)

Live Traffic Feed

Live Traffic Feed
Visitor Tracking

Comment Option isn't working properly

Comment option isn't working properly. If you have anything to say, you are requested to use the "Contact Form" below here.

Leave Your Message Here


Email *

Message *

Like on Facebook