INT0_Interrupt_Counter_with_Lcd_On_18f4550

port change interrupt မွာေတာ့ portB rb4-rb7 (kb10 - kb13) က pin ေတြမွာ rising edge ျဖစ္ေစ၊ falling edge ျဖစ္ေစ ေတြ႕တာနဲ႔ interrupt ျဖစ္ပါတယ္။ pin မွာ high to low ျဖစ္ေစ low to high ျဖစ္ေစ transition ျဖစ္တာနဲ႔ interrupt ျဖစ္ေစတာပါ။

သူ႔ကိုသံုးဖို႔ သက္ဆိုင္ရာ Pin ကို  digital input အျဖစ္ configure လုပ္ေပးရပါတယ္။

အခုစမ္းမွာကေတာ့ RB0 မွာ transition တစ္ႀကိမ္ျဖစ္တိုင္း interrupt service routine ထဲက cnt ကို 1 တိုးပါတယ္။  led ကိုလဲ လင္း၊ မွိတ္ ေျပာင္းေပးပါတယ္။ transition ဘယ္ႏွစ္ႀကိမ္ျဖစ္တယ္ဆိုတာလဲ LCD ေလးနဲ႔ျပပါမယ္။
ဒီေနရာမွာ INT0 ကို မ=rising edge တိုင္းမွာ interrupt ျဖစ္ေစဖို႔ INTCON နဲ႔  INTCON2 register ႏွစ္ခုကို configure လုပ္ရမွာပါ။
INTCON  ဘက္မွာဆိုရင္. . . .
GIE ကို enable
INT0IE ကို enable
INT0IF ကို clear(0) လုပ္ရပါမယ္။

INTCON2 ဘက္မွာဆိုရင္. . . .
INTEDG0 ကို  set(1) ထားျပီး rising edge မွာ Interrupt ျဖစ္ဖို႔ေရြးေပးလိုက္ပါတယ္။

main ထဲက  forever loop မွာပတ္ေနခ်ိန္ INT0 မွာ rising edge ေတြ႕ရင္ လက္ရွိေရာက္ေနရာကေန  Interrupt Service Routine (void interrupt()) စီကိခုန္ကူးျပီး အထဲက ေရးထားတာေတြကို လုပ္ပါတယ္။
ဒီနမူနာမွာေတာ့ led ကိုလင္းမွိတ္ အလွည့္က်လုပ္ခိုင္းတယ္။ cnt ကို 1 တိုးတယ္။ အထြက္မွာ INT0IF ကို clear လုပ္ပါတယ္။  ၿပီးရင္ မူလခုန္ကူးရာေနရာကေန လုပ္လက္စအလုပ္ကို ျပန္လုပ္ပါတယ္။ တစ္ခ်ိဳ႕ code ေတြမွာေတာ့ ISR ရဲ့အစမွာတင္  GIE နဲ႔ သက္ဆိုင္ရာ IE bit ေတြကို disable လုပ္တာေတြ႔ဖူးပါတယ္။ ဘယ္လိုကြာသလဲဆိုတာေတာ့ ပိုနက္နက္ေလ႔လာ စမ္းၾကည့္ရမယ္ထင္ပါတယ္။






     
 // Int0 Test
 //Input at RB0

#define led PORTb.f1

sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

 char txt1[] = "INT0_RISING_EDGES";
 char str[12],lcd[12],i,j;
 int cnt;

void main()
     {

      OSCCON=0xff;
      TRISB=0x01;
      ADCON1 = 0x0F;
      CMCON = 0x07;

      INTCON2.INTEDG0=1; //interrupt on rising edge
      INTCON.GIE=1;
      INTCON.INT0IE=1;
      INTCON.INT0IF=0;

      Lcd_Init();

      Lcd_Cmd(_LCD_CLEAR);

      Lcd_Cmd(_LCD_CURSOR_OFF);

      Lcd_Out(1,1,txt1);

 

   

      while(1)
      {
  if(cnt==255)  //refresh led
           {
           cnt=0;
           Lcd_Cmd(_LCD_CLEAR);
           Lcd_Cmd(_LCD_CURSOR_OFF);
           Lcd_Out(1,1,txt1)
           }

       IntToStr(cnt,str);     // long to string
        j=0;
      for(i=0;i<=9;i++)
         {
         if(str[i]!=' ')       // remove blanks
         {
         lcd[j]=str[i];
          j=j+1;
         }
         }
        Lcd_Out(2,7,lcd);
     }
     }

    void interrupt()
 {
 led=~led  ;
 cnt=cnt+1;
 INTCON.INT0IF=0;
 }

Comments

Popular posts from this blog

Inductance caculator

Pointer to object array