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

📄 system.c

📁 一体化锁主程序
💻 C
📖 第 1 页 / 共 5 页
字号:
/*
+---------------------------------------------------+
| Project: SYSTEM with STC89C5X                     |
| File:    SYSTEM.C                                 |
| Data:    2008-12-04                               |
| Version: V 1.0                                    |
| Initial Editor: Li Jifeng                         |
+---------------------------------------------------+
| following environments are supported              |
| Processor: STC89C5X                               |
| Compiler: WAVE V                                  |
| hardware: JF-2008                                 |
+---------------------------------------------------+
|    Copyright (C) JF Ltd, 2008                     |
|       All Rights reserved                         |
+---------------------------------------------------+
| 功能简介: the system function                     |
+---------------------------------------------------+
*/

#include "system.h"

extern UINT8 idata card1[12];
extern bit nopen_flag;

bit LED_flag;
bit D0_flag;    //不连续
bit D7_flag;    //满  覆盖
bit purview_flag;  //权限确认标志
bit uart_rcv_finish;
bit uart_start_flag;
//bit timer0_out;
bit relay_flag,timer_flag;
//static UINT8 data timer0_n=10;
UINT16 timer2_tick;
bit timer2_out;

UINT8 xdata addr;

//权限确认

static UINT16 data purview_timer;
UINT16 data LOADP,LOADPP;
UINT16 data SAVEP;
static UINT8 xdata relay_timer,relay_timer1;
static UINT16 xdata open_timer;
static UINT8 xdata open_timer1;
static UINT8 xdata infrared_alarm_timer,infrared_alarm_timer1;
static UINT8 xdata exotic_alarm_timer,exotic_alarm_timer1;
static UINT8 xdata infrared_comm_timer,infrared_comm_timer1;
static UINT8 xdata col_flag1,col_flag2,col_flag3;
static UINT8 data LED_timer;
static UINT8 data LED_n;

UINT8 data card[14];
static UINT8 data uart_rcv_flag = 0;
UINT8 data rec_buf[8] = {"1001804"};          //接收数据区
static UINT8 data CID2;                              //命令码
static UINT8 data LTH[4];                            //数据长度
static UINT8 data LCHKSUM;                           //长度校验
static UINT8 data com_n;
static UINT8 data uart_rcv_len;
static UINT8 idata DataBuf[78];                        //信息区
static UINT8 data sum_buf[4];

//static UINT8 idata DataBuf[74] = "~100180000000FDB6\r";	          //发送数据区

xdata user user_tmp;
static UINT16  Txsum;
UINT16  Rxsum = 0xcd;
UINT16  Rxsum1;
static UINT16  Rxsum2;

static UINT8 data user_n;
xdata user user_buf[60];

xdata festa holiday_buf[14];

static UINT8 data holiday_n;


UINT8 data I2C_rcv[16];                        //信息区

code UINT8 VER[37]= "45533830364120202C2020564552312E3020" ;
code UINT8 OK[3]= "OK";
code UINT8 ERR[4]= "ERR";

/***********************************************************************************
函数名称:asc_hex()
功能描述:ASCII码转十六进制子程序
函数参数:asc
***********************************************************************************/
UINT8 asc_hex(UINT8 uData)
{
    if ((uData > 0x2F) && (uData < 0x3A))
    {
       return(uData - 0x30);
    }
    else if ((uData > 0x40) && (uData < 0x47))
    {
       return(uData - 0x37);
    }
    else return(0);
}
/***********************************************************************************
函数名称:hex_asc()
功能描述:十六进制转ASCII码子程序
函数参数:hex
***********************************************************************************/
UINT8 hex_asc(UINT8 uData)
{
    if (uData < 10)
    {
      return(0x30 + uData);
    }
    else if(uData < 16)
    {
      return(0x37 + uData);
    }
    else return(0);
}
/***********************************************************************************
函数名称:timer0_isr()
功能描述:定时器0中断服务程序
参    数:无
***********************************************************************************/
static void timer0_isr (void) interrupt TF0_VECTOR using 2
{
    TL0 = TL0_VALUE;             //如果是工作方式2,8位自动重装
    TH0 = TH0_VALUE;             //TL0和TH0就不用重新赋值

    /*if(--timer0_n == 0)
    {
       timer0_n = 10;
       timer0_out = 1;
    }*/
    if(purview_flag)
    {
        if(--purview_timer == 0)
           purview_flag = 0;
    }
    if(relay_flag)
    {
       if(--relay_timer == 0)
       {
          relay_flag = 0;
          relay = 1;
          LED = 1;
          timer_flag = 0;
       }
    }
    if(LED_flag)
    {
       if(--LED_timer == 0)
       {
          LED_timer = 4;
          LED = ~LED;
          if(--LED_n == 0)
          {
             LED_flag = 0;
             LED_n = 5;
             LED = 1;
             timer_flag = 0;
          }
       }
    }
    if(nopen_flag)
    {
       if (--timer2_tick == 0)     //timer2_N=9,1秒
       {
          timer2_tick = 20;
          timer2_out = 1;
          nopen_flag = 0;
       }
    }
}
/***********************************************************************************
函数名称:timer2_isr()
功能描述:定时器2中断服务程序
参    数:无
***********************************************************************************/
//static UINT8 timer2_tick,timer2_N;
static void timer2_isr (void) interrupt TF2_VECTOR using 2
{
    //TR2 = 0;
    TF2 = 0;                     //必须由软件清零
    //EXF2 = 0;                  //定时器2外部中断标志,必须由软件清零
    //TH2 = TH2_VALUE;           //如果是工作方式0,16位自动重装
    //TL2 = TL2_VALUE;           //TL2和TH2就不用重新赋值
    //TR2 = 1;
    if (--open_timer == 0)     //timer2_N=9,1秒
    {
       TR2 = 0;
       open_timer = open_timer1*2;
       timer2_out = 1;
    }
}
/***********************************************************************************
函数名称:uart_send_byte()
功能:串口发送函数
函数参数:udata:发送的数据
***********************************************************************************/
void uart_send_byte(UINT8 udata)
{
    TI = 0;
    SBUF = udata;
    while (TI == 0);
    TI = 0;
}
/***********************************************************************************
函数名称:uart_send1()
功能:串口发送函数
函数参数:*DataBuf:发送的数据
         n:数据个数
***********************************************************************************/
void uart_send1(UINT8 *DataBuf,UINT16 n)
{
    while (n--)
    {
        uart_send_byte(*DataBuf);
        DataBuf++;
    }
}
/***********************************************************************************
函数名称:uart_isr()
功能描述:串口中断服务子程序
函数参数:无
10018048200EF0E00000
***********************************************************************************/
static void uart_isr(void)  interrupt SIO_VECTOR using 3
{
    UINT8 tmp;
    if (RI)
    {
        RI = 0;
        tmp = SBUF;
        if (tmp == '~')                            //帧头
        {
            uart_start_flag = 1;
            uart_rcv_flag = 0;
            com_n = 0;
        }
        else if (uart_start_flag)
        {
            switch (uart_rcv_flag)
            {
                case 0:if (tmp == rec_buf[com_n])
                       {
                          com_n++;
                       }
                       else
                       {
                         uart_start_flag = 0;
                       }
                       if (com_n == 7)
                       {
                           uart_rcv_flag = 1;
                       }
                       break;
                case 1:CID2 = tmp;
                       if ((CID2 == '8') || (CID2 == 'A'))
                       {
                          uart_rcv_flag = 2;
                          com_n = 0;
                          LCHKSUM = 0;
                          Rxsum2 = Rxsum1 + tmp;
                       }
                       else if(purview_flag)
                       {
                          if(CID2 == '9')
                          {
                             uart_rcv_flag = 2;
                             com_n = 0;
                             LCHKSUM = 0;
                             Rxsum2 = Rxsum1 + tmp;
                          }
                          else
                          {
                             uart_start_flag = 0;
                          }
                       }
                       else
                       {
                          uart_start_flag = 0;
                       }
                       break;
                case 2:if(com_n < 3)
                       {
                          LTH[com_n] = asc_hex(tmp);
                          LCHKSUM += LTH[com_n];
                          Rxsum2 += tmp;
                          com_n++;
                       }
                       else
                       {
                          LTH[com_n] = asc_hex(tmp);
                          LCHKSUM += LTH[com_n];
                          Rxsum2 += tmp;
                          if((LCHKSUM & 0x0f) == 0)
                          {
                             uart_rcv_flag = 3;
                             uart_rcv_len = (LTH[2]<<4) + LTH[3];
                             com_n = 0;
                          }
                          else uart_start_flag = 0;
                       }
                       break;
                case 3:if (com_n < uart_rcv_len - 1)              //DATA
                       {
                           DataBuf[com_n] = asc_hex(tmp);
                           Rxsum2 += tmp;
                           com_n++;
                       }
                       else
                       {
                           uart_rcv_flag = 4;
                           DataBuf[com_n] = asc_hex(tmp);
                           com_n = 0;
                           Rxsum2 += tmp;
                           Rxsum2 = 0x10000 - Rxsum2;
                           sum_buf[0] = hex_asc((Rxsum2 >> 12) & 0x000f);
                           sum_buf[1] = hex_asc((Rxsum2 >> 8) & 0x000f);
                           sum_buf[2] = hex_asc((Rxsum2 >> 4) & 0x000f);
                           sum_buf[3] = hex_asc(Rxsum2 & 0x000f);
                       }
                       break;
                case 4:if(com_n < 4)
                       {
                          if (tmp == sum_buf[com_n])                  //FCS
                          {
                             com_n++;
                          }
                          else
                          {
                             uart_start_flag = 0;
                          }
                       }
                       else
                       {
                           if(tmp == 0x0D)
                           {
                              uart_rcv_finish = 1;
                              ES = 0;
                              uart_start_flag = 0;
                              //control();
                           }
                           uart_start_flag = 0;
                       }
                       break;
               default:uart_start_flag = 0;
                       break;
            }
        }

⌨️ 快捷键说明

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