写了一个 51单片机外部中断程序

来源:互联网 发布:单片机led灯不亮 编辑:程序博客网 时间:2024/05/16 19:22

/*以下是能在keilC 中变异的源码,直接粘贴过去就行,外部中断0(INT0)和外部中断1都有(INT1)*/

/*程序很简单,但是对新手来说还是很实用的*/

/*******************************************************************/
/*                                                                 */
/* 单片机开发系统演示程序 - INT0 INT1 中断计数                     */
/*                                                                 */
/* 6位数码管显示                                                   */
/*                                                                 */
/* 作者:曾露337  店铺:http://shop58430712.taobao.com             */
/*                                                                 */
/*【声明】此程序仅用于学习与参考,引用请注明版权和作者信息!       */
/*                                                                 */
/*******************************************************************/
 
#include < reg51.h >
#include <intrins.h>

#define uchar unsigned char
#define uint  unsigned int

unsigned char code  LEDData[ ] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,
                                  0x82,0xF8,0x80,0x90,0xff};
                                    
unsigned char data  display[8] = {0x00,0x00,0x00,0xff,0xff,0x00,0x00,0x00};

unsigned char code  scan_bit[8] = {0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};

unsigned char count0,count1,temp=0 ;

/********************************************************
*                                                       *
* 延时函数                                              *
*                                                       *
********************************************************/
void delay(uint ms) 
// 延时子程序
{
 uchar k;
 while(ms--)
 {
  for(k = 0; k < 100; k++);
 }
}

/********************************************************
*                                                       *
*  数据处理与显示函数                                   *
*                                                       *
********************************************************/
void  disp_count()
{
    char n; 

    temp=count0;

    display[2]=temp/100;    //数据处理
    temp=temp%100;
    display[1]=temp/10;
    display[0]=temp%10;

 if(display[2]==0)        //高位为0,不显示 
 { 
   display[2]=0x0a;
   if(display[1]==0)
   display[1]=0x0a;  
    }

    temp=count1;

    display[7]=temp/100;    //数据处理
    temp=temp%100;
    display[6]=temp/10;
    display[5]=temp%10;

 if(display[7]==0)        //高位为0,不显示 
 { 
   display[7]=0x0a;
   if(display[6]==0)
   display[6]=0x0a;  
    } 
     
    for(n=0;n<8;n++)
    {  
      P0 =LEDData[display[n]] ;  //显示段码
      P2 =scan_bit[n];           //输出位码
      delay(1);
   P2 = 0xff;                 //关闭显示
    } 
}

/********************************************************
*                                                       *
* 主程序                                               *
*                                                       *
********************************************************/
void main(void)
{  
   P0=0xff;
   P1=0xff;
   P2=0xff;   
   
   IT0=0;          //低电平触发
//   IT0=1;        //下降沿触发
   IT1=0;          //低电平触发
//   IT1=1;        //下降沿触发
   PX0=1;
   EA=1;
   EX1=1;
   EX0=1;

   while(1)
   {
      disp_count();
   }
}

/********************************************************
*                                                       *
* INT0中断函数                                          *
*                                                       *
********************************************************/
void  counter0(void) interrupt 0
{
   uchar  x;
   EX0=0;
   count0++;
    
   for(x=0;x<10;x++)
   {
     disp_count();
   }
   EX0=1;
}

/********************************************************
*                                                       *
* INT1中断函数                                          *
*                                                       *
********************************************************/
void  counter1(void) interrupt 2 
{
   uchar  x;
   EX1=0;
   count1++;
    
   for(x=0;x<10;x++)
   {
     disp_count();
   }
   EX1=1;
}

/********************************************************/

0 0