Wednesday, January 6, 2016

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : LAST PART ( END )

PART 1 : Basic Introduction 

PART 2 : Interrupt 

PART 3 : Timer

PART 4 : Calculation

PART 5 : LED Switching Project [END]

 

IR Controlled LED Switching : 5th & LAST PART



I have to make it quick and i think we have got all required information . I ain't going to describe source code . You have to help your self on that case . It already has become too long !!.
I can honestly say that i tried from my best !!!. Today I am going to complete this series tutorial and it's the last part!!.

So first of all ,take a look on a little calculation part.

Final Calculation :

 

To identify each bit uniquely , we will use another calculation method. You may use as you want . I ain't going to use misisecond .you can use milisecond or microsecond .
If you want to use milisecond to measure pulse , then for logical 0 you get (562us_on+562us_off)=1.124ms. and for logical 1 you get (562us_on+1687us_off)=2.249ms.
 
You know , for each task  microcontroller needs time . That's why reading will be being varied from calculation . So if you use integer number , you will get 1ms for logical and 2ms for 0 . By applying this you may successfully able to make it. Alright!


Now i am going to show about my method that was used on my project:

we know that , 1secon means=750000 pulses OK!.
So, 1000ms means=750000 pulses and 1000000us means=750000 pulses.

So, 1000000us means=750000 pulse
 and   1us     ,,  = (750000/1000000)=0.75 pulses
 and  562us   ,,   =(0.75*562)=421.5=421 pulses

again,

So, 1000000us means=750000 pulse
 and   1us     ,,  = (750000/1000000)=0.75 pulses
 and  1687us   ,,   =(0.75*1687)=1265.25=1265 pulses


So, for logical 1 :

(562+1687) or (421.5+1265.75)=1686 pulses

Now , we know 255 pulses means 1 interrupt or Count=1 .OK!
 So ,          1    ,,     ,,  (1/255)
 so ,        1686   ,,     ,,   (1686/255)=6.62

So, we can say 6.62 interrupts means logical 1 . In coding we i will use 5,6 and 7 to identify logical 1 in proteus.

Note : In real life we will get  3 or 4 instead of 6.62.

So, for logical 0 :

 

(562+562) or (421.5+421.5)=843 pulses

Now , we know 255 pulses means 1 interrupt or Count=1 .OK!
 So ,          1    ,,     ,,  (1/255) or count=(1/255)
 so ,        843   ,,     ,,   (843/255)=3.31 or count=3

So, we can say 3.31 interrupts means logical 0 . In coding we i will use 2,3 and 4 to identify logical 0 in proteus.

Note : In real life we will get  1 or 2 instead of 3.31.

      

By using this technique , from count variable we can identify logical 1 or logical 0.


Remember this , In proteus simulation all are fine and all calculation works accurately .But  in real life , reading will be changed . We will get count=1 or count=2 for Logical 0 . For Logical 1 we will get count=3 or count=4 .  

***Transmitter Section(Important):

 In source code of transmitter , 4 commands are used .
  
  #define command_2 0x81 or 10000001
  #define command_3 0x42 or 01000010
  #define command_4 0xC3 or 11000011
  #define command_5 0x24 or 00100100 

Here blue color represents 4bit command and red color represents reverse order of the main 4bit command (blue colored).To avoid noise and to achieve clear signal we need this.

Consider that we have main 4bit command 0001.So the reverse command should be 1000 command .Now we have to attach these and that will be 10000001. Finally we get 8bit command in which a main command and a reverse of that command are saved .

We need some knowledge about PWM(Pulse Width Modulation)

We will use PWM to generate 36KHz pulse for transmitter and that's why we will connect positive(+) pin of Transmitter with MCU's CPP1 pin . Because using that pin MCU will generate 36KHz pulse .

PWM1_Init(36000); will set up the CPP1 pin for PWM .

PWM1_Set_Duty(127); 

set_duty(127) used to provide equal for both ON and OFF .That means (T/2) time it will ON and (T/2) time it will OFF .If we use set_duty(255) , then total Time period (T) will be remain ON. If we use set_duty(0) , then total Time period (T) will be remain OFF.    
Look at this photo.
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : LAST PART ( END )
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : LAST PART ( END )

 

Again look at another picture given below

  Here in if statement , an AND operation performed between Command and 0x01 . We are using this to identify each bit of data command.
Suppose , our command is 10000001 and 0x01=00000001.So ,when an AND operation performs the result will be like this.

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : LAST PART ( END )
Shifting Operation in Programming

Shifting Operation in Programming
Shifting Operation in Programming

 

So, if result is 1 ,then the related bit is 1 and delay should be 1687us.

So, if result is 0 ,then the related bit is 0 and delay should be 562us.

 

***Receiver Section(Important):

Here this part of code is comparing that Data signal commands received in receiver unit are correct .



   if(val[7]==val[0]){  
    txt[7]=val[7];  
   }  
   if(val[6]==val[1]){  
    txt[6]=val[6];  
   }  
   if(val[5]==val[2]){  
    txt[5]=val[5];  
   }  
   if(val[4]==val[3]){  
    txt[4]=val[4];  
  }  

   if(txt[4]=='0' && txt[5]=='0' && txt[6]=='0' && txt[7]=='1') {  
     PORTB.F6=1;  
     PORTB.F5=0;  
     PORTB.F4=0;  
     PORTB.F3=0;  
   }  

 When microcontroller understand about the 4bit command "0001" , consequently it turin's ON RB6 pin or First LED .  
 

# IR-Controlled LED Switching in Proteus 8.

Source Code :

Transmitter:

 



 

 #define address  0xff  
  #define command_1 0xFF  
  #define command_2 0x81  
  #define command_3 0x42  
  #define command_4 0xC3  
  #define command_5 0x24  
 unsigned int i=0,j=0;  
 void repeat(){  
 PWM1_Start();  
 delay_us(8000);  
 PWM1_Stop();  
 delay_us(8000);  
 }  
  send_data_byte(unsigned char dattt){  
   PWM1_init(36000);  
 PWM1_Set_Duty(127);  
  for(j=0;j<8;j++){  
 PWM1_Start();  
 delay_us(562);  
 PWM1_Stop();  
  if(dattt & 0x01){  
  delay_us(1687);  
  dattt=dattt>>1;  
  }  
 else  
  {  
  delay_us(562);  
   dattt=dattt>>1;  
  }  
  }  
 PWM1_Stop();  
  }  
  void send_data_command(unsigned char dat){  
  PWM1_init(36000);  
 PWM1_Set_Duty(127);  
 PWM1_Start();  
 delay_us(9000);  
 PWM1_Stop();  
 delay_us(4500);  
  send_data_byte(dat);  
  repeat();  
  }  
 void main() {  
 ADCON1=0x0F;  // disable ADC converter  
 CMCON=7; // to disable comparator  
 TRISA.F0=1;  
 TRISA.F1=1;  
 TRISA.F2=1;  
 TRISA.F3=1;  
 TRISC.F2=0;  
  /*  Start S  
   */  
  PORTC.F2=0;  
 while(1){  
  if(PORTA.F0==0){  
 send_data_command(command_2);  
 while(PORTA.F0==0){  
 repeat();  
 }  
  }  
  if(PORTA.F1==0){  
 send_data_command(command_3);  
 while(PORTA.F1==0){  
 repeat();  
 }  
  }  
   if(PORTA.F2==0){  
 send_data_command(command_4);  
 while(PORTA.F2==0){  
 repeat();  
 }  
  }  
    if(PORTA.F3==0){  
 send_data_command(command_5);  
 while(PORTA.F3==0){  
 repeat();  
 }  
  }  
 }  
 }  

 

Receiver

 



 
////////  
 unsigned int count=0,sgnal=0;  
 int i=7,j=0,xr=0;  
 char txt[]="........";  
 char val[]="........";  
 void timer_routin(){  
 count++;  
  if(count>0xFF){  
   count=0;  
   TMR0L=0;  
  }  
 }  
 void external_routin(){  
 }  
 void interrupt()  
 {  
 if(INTCON.TMR0IF){  
  // delay_us(100);  
  timer_routin();  
  INTCON.TMR0IF=0;  
 }  
 if(INTCON.INT0IF){  
  PORTC=0xFF;  
  xr=count;  
  count=0;  
  TMR0L=0;  
   if(i<0){ i=7; }  
  if(xr==3||xr==4){  
   val[i]='0';  
   i--;  
   if(i<0){ i=7; }  
  }  
  else if(xr==5||xr==6){  
   val[i]='1';  
  i--;  
   if(i<0){ i=7; }  
  }  
  else{  
         /// For start signal or Repeat signal , i=0 and start to count  
   TMR0L=0;  
   i=7;  
   count=0;  
  }  
  INTCON.INT0IF=0;  
 }  
 }  
 void main()  
 {  
  ADCON1=0x0F;  
  CMCON=7;  
   TRISB=0x01;  
   PORTB.F6=0;  
    PORTB.F5=0;  
    PORTB.F4=0;  
    PORTB.F3=0;  
    T0CON=0xC1;  
  TMR0L=0;  
  INTCON.GIE=1;  
  INTCON.PEIE=1;  
  INTCON.TMR0IE=1;  
  INTCON.INT0IE=1;  
  INTCON.TMR0IF=0;  
  INTCON.INT0IF=0;  
  INTCON2.RBPU=1; // DISABLE PULUP REGISTER  
  INTCON2.INTEDG0=0;  
  while(1){  
  PORTC=0x00;  
  if(val[7]==val[0]){  
   txt[7]=val[7];  
  }  
  if(val[6]==val[1]){  
   txt[6]=val[6];  
  }  
  if(val[5]==val[2]){  
   txt[5]=val[5];  
  }  
  if(val[4]==val[3]){  
   txt[4]=val[4];  
  }  
 if(txt[4]=='0' && txt[5]=='0' && txt[6]=='0' && txt[7]=='1') {  
    PORTB.F6=1;  
    PORTB.F5=0;  
    PORTB.F4=0;  
    PORTB.F3=0;  
  }  
 if(txt[4]=='0' && txt[5]=='0' && txt[6]=='1' && txt[7]=='0') {  
    PORTB.F6=0;  
    PORTB.F5=1;  
    PORTB.F4=0;  
    PORTB.F3=0;  
  }  
 if(txt[4]=='0' && txt[5]=='0' && txt[6]=='1' && txt[7]=='1') {  
    PORTB.F6=0;  
    PORTB.F5=0;  
    PORTB.F4=1;  
    PORTB.F3=0;  
  }  
 if(txt[4]=='0' && txt[5]=='1' && txt[6]=='0' && txt[7]=='0') {  
    PORTB.F6=0;  
    PORTB.F5=0;  
    PORTB.F4=0;  
    PORTB.F3=1;  
  }  
  }  
 }  


Proteus Circuit :

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : LAST PART ( END )



Watch Video for Proteus 8

 

 

Download All of this Project




IR (Infrared) Remote Controlled Communication Between Two Microcontroller -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

 We already know that firstly clock frequency will be divided by 4 and secondly it goes to prescaler to prescale .Finally clock goes to timer register . 
So, if we use 12MHz clock and 1:4 prescaler . First we will get system clock (12/4)=3MHz clock or (12000000/4)=3000000Hz clock. Secondly it goes to prescaler and divided by 4 again . So we get (3000000/4)=750000Hz clock .Alright!

We know that for each pulse TMR0L(Timer0) register increments by one . So when 750000 Hz clock successfully enters on timer0 register and the TMR0L register increments 750000 times .Is it clear!! . We have a restriction , our TMR0L register can count from 0 to 255.
As we need to count 750000 or greater that 750000.We have to walk in another way. We will set an int(8bit) variable count which will be incremented for each overflow interrupt . That means, TMR0L counts 255 and overflow interrupt occurs . So it's get reset and count variable increments by one . Actually when overflow interrupt occur , it increase the value of count variable by one . That means count=count+1;

Now consider an External Interrupt situation on RB0 pin when : 
count=3 and TMR0L(8bit Register)=123.

So How much 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 has 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 occur  
  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;  
 }  
 }  
 // <<<<<<<<<--ads_space-->>>>>>>>>>>> 



 
 // <<<<<<<<<--ads_space-->>>>>>>>>>>>  
 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: One thing to remember , Here we can measure highiest time : [{(255*255)+255}/750000]*1000=87.04 miliseconds .So we can measure accurate time for 87 milisecond . Actually Push button is not perfect for this . You may use other variable to increase measuring capacity .You may send pulses having time period  less than 87 miliseconds on RB0 pin.  I am suggesting you to provide signal on RB0 pin from another microcontroller using PWM. It will be good for you !. Thank You!!

Watch the Video :



Download This Project

>>Continue to the 5th Part<<

Saturday, January 2, 2016

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3



PART 1 : Basic Introduction 

PART 2 : Interrupt 

PART 3 : Timer

PART 4 : Calculation

PART 5 : LED Switching Project [END

 

Timer :Part-3

 

 

 

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3

Today i will discuss about Timer. It's very important part of microcontroller . Basically Timer is used to measure time .It can be called as  Counter , when we don't know the frequency . Microcontroller performs time related operations using timer . As we need to measure the time of each falling edge of IR-Signal .In this tutorial we will use timer0 of pic18f2550 and we gonna use 8bit mode. You may use 16bit mode , but first time skip complexity . Both TMROL and TMROH registers works in 16bit mode.

PIC 18f2550 microcontroller has 4 timer modules. Timer0,Timer1,Timer2 and Timer3 . I will only describe Timer0 and i will try to make it easy so that you can make it by yourself . 
I don't know , i am just trying .

Generally Timer0 is controlled by T0CON register of pic18f2550 and TMR0L is Timer0 register low byte .In 16bit mode both TMR0L(8bit) and TMR0H(8bit) works.But in 8bit mode we will use only TMR0L . This timer0 register will be incremented for each pulse comes to it and can count from 0 to 255 (0xFF).

Now we need to take a look about Timer0's control register and some necessary information . After that we will see how timer works and that will be good to us . 

Read all information carefully provided on these images!!



IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3

Just keep in your mind , all those given in previous steps.We will need those in source code .

Now we will see how timer works 

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3


Generally We connect crystal clock with microconteroller so that it can work with clock . Consider that we are using 12MHz clock . So , that means 12000000 pulses are passing to the microcontroller . OK! Is that clear ?

Prescaler:

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3

Prescaler is used to scale frequency .Look at the picture named scaling Ratio . When (fosc/4) or (12/4=3MHz) frequency pulse comes to the the microcontroller we need make this smaller to make calculation easy . So at this stage we need prescaler . Alright! There is another term "Postscaler" . Postscaler works after signal has come to the microcontroller .

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3

On that picture clock cycle  represents (Clock/4) or (12/4=3MHz) frequency . If we use 1:2 to scale , then 2 pulses will be considered as 1 pulse . In 1:4 , 4 pulses will be considered as one pulse . For 1:8 , eight pulses will be considered as one pulse.

Working of Timer0



Note:To Understand Clearly about Timer0 . You have to keep this picture in your memory . All we see in that part are the details of this Picture and save the Picture .

For Clock frequency we could use T0CKL pin (External Source) but Crystal frequency (12 MHz Crystal Clock) will be good for us .

When we connect a crystal (we are using 12MHz Crystal Clock) with our microcontroller , microcotnroller receives pulse continuously. At this case we know the frequency and we can calculate the time of each pulse from (T=1/f). So if we know how many pulse are received , we can calculate time by multiplying .We will see this later .

Generally , Crystal clock frequency divided by 4 . That means first time clock frequency prescaled by 4 . Look at the picture and you can see that "clock/4" or "System Clock"

After that stage signal comes to prescaler . In this tutorial we will use 1:4 prescaler . That means we are dividing the system clock by 4 . So , it means when 4 system clock pulse comes to prescaler , it sends out 1 pulse to Timer0 register .

Basically , when one pulse enters into Timer0 register , TMR0L  register will be incremented by 1 . The capacity of this register is 0 to 255 . So when TMR0L already count 255, there will be an  overflow interrupt. In the interrupt function routine we will reset TMR0L register and we will increase (int count variable) by 1. We need this variable to remember that how many times interrupt happened . When we perform [count * TMR0L] ,we get total number of pulses . From this information we can measure time of "1" and "0"


We will create a project  in Calculation(4th Part)


Setup Timer0 Register :

Bit 0 to 2 are used to select prescaler. As we are using 1:4 prescaler , we need to set 001

Now set 3rd bit to 0 . So bit comes 0001.

Now set 4th bit to 0 and  we get   00001.

Now set 5th bit to 0 and we get   000001

Now set 6th bit to 1 , because we are using 8 bit mode . You may use 16bit mode. and  we get      1000001

Now set 7th bit to  1, because we need to start timer . Finally we get : 11000001 or 0xc1 (hexadecimal).


IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3

 Setup Timer0 overflow flag :

When overflow happens timer overflow interrupt register will be 1. When Overflow Register becomes 1 and microcontroller understand that interrupt happens . So microcontroller goes to perform timer interrupt routine . When flag remains 0 , microcontroller understand that everything is OK and No interrupt happens .

To enable overflow interrupt we need to set that enable . Unless overflow interrupt will not work.


So first set INTCON.TMR0IE=1;



Note: you can find INTCON register on Interrupt section in Datasheet or in Interrupt tutorial i had described about INTCON register.


After that set INTCON.TMR0IF bit to 0 , that will set Flag as 0 .

First we need to enter on the INTCON register and then set TMR0IF=0;
 

 >>Continue to 4th PART<<

Sunday, December 20, 2015

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-2

PART 1 : Basic Introduction 

PART 2 : Interrupt

PART 3 : Timer

PART 4 : Calculation

PART 5 : LED Switching Project [END

 

 

Part-2 : Interrupt


In this part i will describe about microcontroller's interrupt . You need to learn about this , it's very important !!.

Interrupt means , make a restriction to something. In microcontroller when interrupt happens , the whole process will be restricted and microcontroller will execute only the instructions which are provided into the interrupt function .
I think it's hard to clearly understand about interrupt.

Take it easy . Consider that we are driving a car and we are moving right now. The road is clear and clean . So we are doing what we are supposed to do .Compare this with microcontroller or microprocessor,during running state MCU performs it's general routine like our car. Alright ! . 

Again consider ,Suddenly three Dogs have come to the middle of the road . So we need to stop and get the dogs out of there . After finishing that we can move . When we saw dog , our journey was interrupted and we needed to do some certain process. Compare that with microprocessor . When microcontroller get notification about interrupt , microcontroller forget about everything and perform only what are told in interrupt function . After doing that MCU get back to it's general routine . That's the way the microcontroller or microprocessor's interrupt work .
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-2
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-2

There are two types of Interrupt

1. Internal Interrupt    2.External Interrupt

 

***Internal Interrupt :

Internal interrupts occurs during the time of internal events . Timer overflow interrupt can be considered like one kind of internal interrupt . When timer counts and timer register becomes full with data , on that situation it's required to reset the register so that it can be able to count again . On that situation Timer overflow interrupt occurs.Interrupt function clear the register and finally mcu goes to the regular routine .                 

In this Part , i will describe only about External Interrupt


***External Interrupt :

External interrupt occurs during the time of external events . When any external signal comes to the external interrupt pin , external interrupt happens . To understand completely , we will interface a push button with microcontroller and that will provide an interrupt event to the MCU .

Now it's time to take a look on pic 18f25550 microcontroller's properties: 

IR (Infrared) Remote Control Communication Between Two Microcontroller -Step By Step Tutorial : Part-2
INTCON Register of PIC 18f2550


Note : INT pins indicates the pins for External Interrupt

PIC 18f2550 microcontroller has three external interrupt pins , which are RB0(INT0) , RB1(INT1) and RB2(INT2) . To control these pins we have INTCON , INTCON2 and INTCON3 registers. In this tutorial we will use RB0(INT0) as an external interrupt and we will see an example of this .
IR (Infrared) Remote Control Communication Between Two Microcontroller -Step By Step Tutorial : Part-2
Pin Out of PIC 18f2550

Now take a look on INTCON register

IR (Infrared) Remote Control Communication Between Two Microcontroller -Step By Step Tutorial : Part-2
INTCON Register of PIC 18f2550
IR (Infrared) Remote Control Communication Between Two Microcontroller -Step By Step Tutorial : Part-2
INTCON Register

Required Configuration to Setup RB0(INT0) as Interrupt:

1. First set Global interrupt [INTCON.GIE=1] to 1.
2. Set Peripheral Interrupt to 1 .[INTCON.PEIE=1]
3. Set RB0 as External Interrupt .[INTCON.INT0IE=1]
4. Disable Pull-Ups on PortB [INTCON2.RBPU=1].
5. Finally set RB0 interrupt flag [INTCON.INT0IF=0]
6. Setup interrupt on RB0 as Falling Edge (You may use rising Edge) [INTCON2.INTEDG0=0]

  INTCON.GIE=1;  // Enabling Global Interrupt
  INTCON.PEIE=1;  // Enables Peripheral Interrupt
  INTCON.INT0IE=1;  // Enables RB0 pin as external interrupt
  INTCON.INT0IF=0;  // Set the RB0 interrupt flag as 0 . that means no interrupt happens . 
  INTCON2.RBPU=1; // DISABLE PULUP REGISTER  
  INTCON2.INTEDG0=0;  // Set Interrupt on RB0(INT0) as Falling Edge . 
IR (Infrared) Remote Control Communication Between Two Microcontroller -Step By Step Tutorial : Part-2
INTCON2 Register

Interrupt on Rising Edge and Falling Edge

It's required to set up, exactly when the MCU will consider the interrupt. That means , which condition will be considered as an interrupt . 
Is it for from 0 to 1 Condition ? or from 1 to 0 Condition ?

Interrupt Flag:

Now we will create a project where a push button will be connected with RB0 pin . There , when button will be pressed, the RB0 pin receive a falling edge signal of external interrupt . At that time the MCU follows the interrupt function routine .

Listen to me , carefully . Flag is one kind of indicator .When any interrupt occurs at RB0 pin , this will make INTCON.INT0IF=1;
That means RB0 pin related interrupt flag will become 1 .This get our microcontroller understand that interrupt has happened . Until we set  INTCON.INT0IF=0 , microcontroller continues in interrupt function routine . That's why we set INTCON.INT0IF=0; in interrupt function and microcontroller go to perform general routine . 
If again interrupt occurs , flag will be INTCON.INT0IF=1. After that interrupt function makes INTCON.INT0IF=0 and microcontroller goes to general routine .
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-2
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-2

Note : To write interrupt function routine . We just need to write a function like this ,

void interrupt(){

......

......

..}

Your microcontroller will automatically understand that it is a interrupt routine .

 


MikroC Source Code for External Interrupt :


 // 






  
 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,xr=0;  
 void interrupt()  
 {  
 if(INTCON.INT0IF){  
  PORTC=0xFF;  
  chk=3;  
  INTCON.INT0IF=0;  
 }  
 }  
 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;  
  while(1){  
  PORTC=0x00;  
      if(chk==7){  
     Lcd_Cmd(_LCD_CLEAR);  
     Lcd_Out(1,1,"General");  
      Lcd_Out(2,3,"Routine");  
       
       } else{  
      Lcd_Cmd(_LCD_CLEAR);  
     Lcd_Out(1,1,"Interrupt");  
      Lcd_Out(2,3,"Routine");  
      
      chk=7;  
        }  
  }  
 }  

It will be wise , if we skip delay_ms() function for this case .

Proteus Circuit for External Interrupt :

 
IR (Infrared) Remote Control Communication Between Two Microcontroller -Step By Step Tutorial : Part-2
IR (Infrared) Remote Control Communication Between Two Microcontroller -Step By Step Tutorial : Part-2

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-2



Download this project

>>Continue to 3rd PART<<

Bookmark this Blog!

Tags

: (1) 18F2550 (1) 36KHz (5) 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) Db9 (1) DC Motor (1) digital (2) Digital Voting Machine (1) display (2) DS1307 (1) electronic (1) embedded c programming tutorial (12) embedded c tutorial (13) experiment kit (4) external interrupt (4) flash (1) flashing (1) Gas Leakage detector (1) HC-06 (1) home (1) how (1) How to (9) i2c tutorial (1) in (1) indicator (2) infrared Connection (5) interface (10) interfacing (3) Interrupt (4) Introduction (1) IR Connection (5) IR Receiver (5) IR Transmitter (5) key pad (1) keypad (1) lavel (1) Lcd 16x2 (2) lcd 2x16 (2) led (1) level (1) lm35 (2) LPG (1) machine (1) make (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 (4) Microcontroller Project (6) Microcontroller Projects (1) microcontroller_project (3) microcontrollers (4) Microprocessor (2) mikroC (6) mikroc pro for pic (1) module (2) Motion detector (1) MQ-9 Gas Sensor (1) musical (1) NEC Protocol (5) pcb (5) PIC (3) pic controller (11) pic microcontroller (12) pic microcontroller tutorial (13) pic programming (1) pic programming in c (14) pic proteus (1) Pic Tutorial (14) pic18 (2) pic18f2550 (12) picmicrocontroller (4) PIR Motion Sensor (1) printed circuit board (1) proteus (6) push (1) real (1) RF Module (2) 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) step by step (7) step bystep (1) Stepper Motor (2) tank (1) text (2) Thief Detector (1) time (1) timer (4) timer0 (4) tone (1) TSOP38236 Receiver (5) tutorial (2) two pic microcontroller (2) Unipolar (1) USART Connection (1) USB (1) usb 1.0 (1) using (9) voltmeter (1) voting (1) Water (1) Wireless Radio Frequency (2) with (2) work (1)

Google+ Followers