⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ints.c

📁 压缩包中包括多个常用的汇编
💻 C
字号:
/****************************************************************************/
/*                                                                          */
/*                Copyright (c) 2005, 老树工作室                            */
/*                     All rights reserved.                                 */
/*                                                                          */
/*      Email:laoshu0902@163.com                                            */
/*      www.laoshu0902.bokee.com                                            */
/*                                                                          */
/****************************************************************************/
/****************************************************************************/
/* 文件名:ints.c                                                           */
/* 版  本:Version 1.0                                                      */
/* 描  述:51中断编程演示代码                                               */
/*                                                                          */
/* 作  者:spot                                                             */
/* 函  数:                                                                 */
/*         com_command_receive                                              */
/*         com_int_proc                                                     */
/*         com_send_command                                                 */
/*         ext0_int_proc                                                    */
/*         ext1_int_proc                                                    */
/*         system_init                                                      */
/*         time0_over_int                                                   */
/*                                                                          */
/* 历史记录:                                                               */
/*  spot          2005-07-10     Creat Inital version. (Version 1.0)        */
/****************************************************************************/

#include <reg52.h>
#include <absacc.h>
#include <intrins.h>
#include <string.h>

#define MAX_RINTL        16   /* 串口接收缓冲区长度   */
typedef unsigned char u_char;
typedef unsigned int  u_int;

u_char pint_buf[MAX_RINTL];   /* 串口接收缓冲区       */
u_char pint_read;             /* 串口缓冲区读指针     */
u_char pint_write;            /* 串口缓冲区写指针     */
u_char psend_int;             /* 串口发送允许标志     */

u_int ext1_count;             /* 外部中断1中断计数    */
u_char timer_count;           /* 定时计数             */
bit   timer_int;              /* 定时标志             */

char idata str_test[18]   = "MCS51 INTERRUPTS:";
char idata str_ext0[15]   = "EXT0 INTERRUPT";
char idata str_timer0[17] = "TIMER0 INTERRUPT";
char str_hello[6]   = "HELLO";
char str_world[6]   = "WORLD";

/* 函数声明 */
void ext0_int_proc(void); /* 外部中断0中断服务程序,使用第2组寄存器 */

void time0_over_int(void);/* 定时器0中断服务程序 ,使用第1组寄存器  */

void ext1_int_proc(void); /* 外部中断1中断服务程序,使用第2组寄存器 */

void com_int_proc(void);  /* 串口中断服务程序,使用第3组寄存器      */

void system_init(void);   /* 系统上电初始化 */

void send_message(char str_message[]);/* 单片机串口发出消息 */
/* 声明结束 */

/* 外部中断0中断服务程序,使用第2组寄存器 */
void ext0_int_proc(void) interrupt 0 using 2
{
    EX0 = 0;       /* 关闭外部中断0 */
    send_message(str_ext0);
    EX0 = 1;       /* 开启外部中断0 */
}

/* 定时器0中断服务程序 ,使用第1组寄存器 */
void time0_over_int(void) interrupt 1 using 1
{
    TF0=0;
    timer_int=1;
    TH0 = -(46080 / 256);
    TL0 = -(46080 % 256);
}

/* 外部中断1中断服务程序,使用第2组寄存器 */
void ext1_int_proc(void) interrupt 2 using 2
{
    ext1_count++;

    if (ext1_count >= 3000)
    {
        ext1_count = 0;
        P1 = ~P1;
    }
}

/* 串口中断服务程序,使用第3组寄存器 */
void com_int_proc(void) interrupt 4 using 3
{
    u_char temp;
    u_char temp1;

    if (TI == 1)                /* 是发送中断       */
    {
        TI = 0;
        psend_int = 1;          /* 可以发送         */
    }

    if (RI == 1)                /* 是接收中断       */
    {
        RI = 0;                 /* 清串口接收中断   */
        temp1 = SBUF;
        temp  = pint_write + 1; /* 判断是否可以写入 */
        if (temp == MAX_RINTL)
        {
            temp=0;
        }

        if (temp != pint_read)
        {
            pint_buf[pint_write] = temp1; /* 读取数据 */
            pint_write = temp;
        }
    }
}

/* 串口发送一个字节 */
void com_send_command(char onebyte)
{
    psend_int = 0;
    SBUF = onebyte;
    while (psend_int != 1);
}

/* 串口接收数据处理 */
void com_command_receive(void)
{
    u_char var1,var2;
    
    var2 = pint_read;
    if (var2 != pint_write)
    {
        var1=pint_buf[var2];
        var2=var2+1;

        if (var2 >= MAX_RINTL)
            var2=0;

        pint_read=var2;

        if (var1 == 0x0A)    /* 收到0x0A */
        {
            send_message(str_hello); /* 发送字符串HELLO */                
            return;
        }
        if (var1 ==0x0D)     /* 收到0x0D */
        {
            send_message(str_world); /* 发送字符串WORLD */               
            return;
        }
    }
}

/* 系统上电初始化 */
void system_init(void)
{
    u_char loop;

    EA = 0;         /* CPU关中断      */
    timer_int = 0;
    timer_count = 0;
    
    pint_read  = 0; /* 串口缓冲读指针 */
    pint_write = 0; /* 串口缓冲写指针 */

    SCON = 0x48;
    PCON = 0x80;
    TMOD = 0x21;    /*定时器0方式1,定时器1方式2 */
    TCON = 0x54;
    TH1  = 0xFD;    /* 波特率为19200  */
    TL1  = 0xFD;
    TR1  = 1;       /* 定时器1启动计数*/

    TH0 = -(46080 / 256); /*定时50毫秒*/
    TL0 = -(46080 % 256);
    TR0 = 1;
    ES  = 1;       /* 串口开中断     */
    PS  = 1;       /* 串口低优先级   */
    PT0 = 1;
    REN = 1;       /* 串口接收允许   */
    IE  = 0x97;    /* 开中断         */

    loop = SBUF;    /* 清串口缓冲区   */
    for (loop=0; loop<MAX_RINTL; loop++)
    {
        pint_buf[loop] = 0;
    }
}

/* 单片机串口发出消息 */
void send_message(char str_message[])
{
    u_char i = 0;

    for (i=0; i<strlen(str_message); i++)
    {
        com_send_command(str_message[i]);  /* 发送字符串 */
    }
    com_send_command(0x0D);

}

main()
{
    u_char i = 0;
        
    system_init();

    send_message(str_test);

    while(1)
    {
        if (timer_int == 1) /* 50毫秒定时到 */
        {
            timer_int = 0;
            timer_count++;

            if (timer_count == 100)  /* 5秒定时到 */
            {               
               timer_count = 0;
               send_message(str_timer0);
            }
        }

        com_command_receive();/* 串口接收数据处理 */
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -