STC15W204S单片机为啥外部中断0不能进入,不能计数?

2020-03-12 19:47发布

#include "config.h"
#include "delay.h"
#include "uart.h"
unsigned int INT0_cnt=0, INT1_cnt=0; //测试用的计数变量
sbit key=P5^5;
/************* 功能说明 **************

******************************************/
/************* 本地常量声明 **************/

/************* 本地变量声明 **************/

/************* 本地函数声明 **************/

/*************  外部函数和变量声明 *****************/

/******************** IO配置函数 **************************/
void GPIO_config(void)
{
P5M1 = 0x40;   //设置准双向口
P5M0 = 0x00;
P0M1 = 0; P0M0 = 0; //设置为准双向口
P1M1 = 0; P1M0 = 0; //设置为准双向口
P2M1 = 0; P2M0 = 0; //设置为准双向口
P3M1 = 0; P3M0 = 0; //设置为准双向口
P4M1 = 0; P4M0 = 0; //设置为准双向口
P5M1 = 0; P5M0 = 0; //设置为准双向口
P6M1 = 0; P6M0 = 0; //设置为准双向口
P7M1 = 0; P7M0 = 0; //设置为准双向口
}
/******************** 主函数 **************************/
void main(void)
{
int count = 0;
GPIO_config();
UartInit();

INT0_cnt = 0;
INT1_cnt = 0;
IE1  = 0; //外中断1标志位
IE0  = 0; //外中断0标志位
EX1 = 1; //INT1 Enable
EX0 = 1; //INT0 Enable
IT0 = 1;  //INT0 下降沿中断  
// IT0 = 0;  //INT0 上升,下降沿中断
IT1 = 1;  //INT1 下降沿中断  
// IT1 = 0;  //INT1 上升,下降沿中断
EA = 1;  //允许总中断
while(1)
{

   
   SendData(1+0x30);

   if(INT0_cnt >= 65536)
    INT0_cnt = 0;
   if(key==0)
   {
    delay_ms(10);
    if(key==0)
    {
   
     while(!key);
     SendData(INT1_cnt/10000+0x30);
     SendData(INT1_cnt000/1000+0x30);
     SendData(INT1_cnt00000/100+0x30);
     SendData(INT1_cnt000000/10+0x30);
     SendData(INT1_cnt+0x30);
     SendString(" ");
     INT1_cnt=0;
     
    }
   
   }
   
}
}
/********************* INT0中断函数 *************************/
void INT0_int (void) interrupt 0  //进中断时已经清除标志
{
EX0=0;
INT0_cnt++; //中断+1
EX0=1;
}
/********************* INT1中断函数 *************************/
void INT1_int (void) interrupt 2 //进中断时已经清除标志
{
EX1=0;
INT1_cnt++; //中断+1
}

/*********************************************************/
//#define MAIN_Fosc  22118400L //定义主时钟
//#define MAIN_Fosc  12000000L //定义主时钟
#define MAIN_Fosc  11059200L //定义主时钟
//#define MAIN_Fosc   5529600L //定义主时钟
//#define MAIN_Fosc  24000000L //定义主时钟
#define BaudRate1 9600UL
#define Timer2_Reload (65536UL-(MAIN_Fosc/4)/BaudRate1)
/*********************************************************/
#include "uart.h"

void UartInit(void)  [url=]//9600bps@11.0592MHz[/url]
{
SCON = 0x50;                //8位可变波特率
AUXR&=~(1<<4 AUXR|=0x10; AUXR|=(1 T2H =(u8)(Timer2_Reload>>8);
T2L=(u8)(Timer2_Reload);
AUXR|=(1<<4 REN=1; ES=1;EA SBUF = ch;                 //写数据到UART数据寄存器 xss=removed TI = 0;>


2条回答
coody
1楼 · 2020-03-13 04:56.采纳回答

官方例程:

#include "reg51.h"

//-----------------------------------------------

sbit P10 = P1^0;

//-----------------------------------------
//中断服务程序
void exint0() interrupt 0       //INT0中断入口
{
    P10 = !P10;                 //将测试口取反
}

//-----------------------------------------------

void main()
{
    INT0 = 1;
    IT0 = 1;                    //设置INT0的中断类型 (1:仅下降沿 0:上升沿和下降沿)
    EX0 = 1;                    //使能INT0中断
    EA = 1;

    while (1);
}


qq986433936
2楼-- · 2020-03-13 00:16
 精彩回答 2  元偷偷看……

一周热门 更多>