專(zhuān)注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

單片機(jī)外中斷匯編語(yǔ)言和C語(yǔ)言程序

作者:于老師   來(lái)源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年03月28日   【字體:

      以下兩個(gè)程序效果相同,一個(gè)使用匯編語(yǔ)言,一個(gè)使用C語(yǔ)言,從仿真效果看,c語(yǔ)言程序運(yùn)行比較流暢。
兩個(gè)外中斷跑馬燈匯編語(yǔ)言程序
             ORG 0000H
             LJMP MAIN
             ORG 0003H
             LJMP WZD0
             ORG 0013H
             LJMP WZD1
             ORG 0040H
WZD0: MOV DPTR,#0220H
            MOV R3,#42D
CB0:    CLR A
            MOVC A,@A+DPTR
            MOV P1,A
            INC DPTR
            LCALL DELAY2
            DJNZ R3,CB0
            RETI
WZD1: MOV DPTR,#0220H
           MOV R3,#42D
CB1:  CLR A
          MOVC A,@A+DPTR
          MOV P2,A
          INC DPTR
          LCALL DELAY1
          DJNZ R3,CB1
          RETI
         ORG 0150H
MAIN: MOV P1,#00H
           MOV P2,#00H
           SETB EA
           SETB IT0
           SETB EX0
           CLR PX0
           SETB IT1
           SETB EX1
           SETB PX1
ST:    MOV DPTR,#0270H
          MOV R4,#17D
 DD:   CLR A
          MOVC A,@A+DPTR
          MOV P0,A
         INC DPTR
         LCALL DELAY1
         DJNZ R4,DD
         SJMP ST
         DELAY1: MOV R7,#00H
         DD1: MOV R6,#00H
         DD2: MOV R5,#01H
         DJNZ R5,$
         DJNZ R6,DD2
         DJNZ R7,DD1
         RET
 DELAY2: MOV R7,#00H
    DDD1: MOV R6,#00H
               DJNZ R6,$
               DJNZ R7,DDD1
               RET
  ORG 0220H
  DB 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80
  DB 0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01
  DB 0x18,0x24,0x42,0x81,0x00,0x81,0x42,0x24,0x18
  DB 0x18,0x24,0x42,0x81,0x00,0x81,0x42,0x24,0x18
  DB 0x18,0x00,0x3c,0x00,0x7e,0x00,0xff,0x00
  ORG 0270H   
  DB  0xfe,0xfc,0xf8,0xe0,0xe0,0xc0,0x80,0x00
  DB  0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff, 0x00
  END
 
兩個(gè)外中斷跑馬燈C語(yǔ)言程序
#include<reg51.h>
int code yu[]={
     0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,
  0x00,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,
  0x00,0x18,0x24,0x42,0x81,0x00,0x81,0x42,0x24,0x18,
  0x00,0x18,0x24,0x42,0x81,0x00,0x81,0x42,0x24,0x18,
  0x00,0x18,0x00,0x3c,0x00,0x7e,0x00,0xff,0x00,0x00,
     0x00     };
int code yu1[]={
               0xfe,0xfc,0xf8,0xe0,0xe0,0xc0,0x80,0x00,
      0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
               0x00};
void delay(int ms)
{
 while(ms--)
   {
    int i;
    for(i=0;i<120;i++);
   }
}
void ddd() interrupt 0
   {
    int k;
    for(k=0;k<47;k++)
      {
    P2=yu[k];
    delay(100);
   }
   }
void dd() interrupt 1
   {
    int k;
    for(k=0;k<48;k++)
      {
    P1=yu[k];
    delay(300);
   }
   P1=0x00;
   }
void main()
{
  int n;
  IT0=0;
  EX0=1;
  PX0=1;
  IT1=0;
  EX1=1;
  PX1=0;
  EA=1;
  P1=0x00;
  P2=0x00;
  while(1)
   {
    for(n=0;n<17;n++)
     {
   P0=yu1[n];
      delay(100);
     }
   }

 

關(guān)閉窗口

相關(guān)文章