📄 main.c
字号:
#include<REG922.h>
#include <absacc.h>
#include "string.h"
#include "intrins.h"
#include "ports.h"
#include "osc.h"
#include "uart.h"
#include "rtc.h"
#include "power.h"
#include "watchdogrtc.h "
#include "IAP_LITE.H"
#include "crc.h"
bit T_flag=0,set=0,set_ok;
bit beep_flg=0;
void Send_ok(unsigned char key);
unsigned char ADDR,RX_addr=0,key_tem=01;
unsigned char wring_send_times=0;
//************************程序用的变量********************
unsigned char data RXtem[7];//程序接收数据缓冲区
unsigned char data RXNOM,RXSTAT=0;
unsigned char data RXTimerOUT,i,RX_BEEP=0; //接收超时
bit rxok; // 接收完成标志
sbit BEEP =P1^4;
unsigned int xxxx,beep_delay=0;
/****************************************************************************/
union charint{
unsigned char chrX[2];
unsigned int intX;
};
union charint data CRC;
void Delay100ms(unsigned char x)
{
unsigned char i,j;
while(x-- != 0)
{
for (j = 0;j < 114; j++)
{
if(rxok)
{
rxok=0;
Send_ok(key_tem);
i=0;
}
for (i = 0;i < 88; i++){_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;
_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;
_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;
_nop_() ;_nop_() ;_nop_() ;};
}
}
}
/****************************************************************************/
void Delay1ms(unsigned char idata x)
{
unsigned char i;
while(x-- != 0)
{
if(rxok)
{
rxok=0;
Send_ok(key_tem);
i=0;
}
for (i = 0;i < 89; i++){_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;
_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;
_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;
_nop_() ;_nop_() ;_nop_() ;};
}
}
/****************************************************************************/
void Delay1msa(unsigned char idata x)
{
unsigned char i;
while(x-- != 0)
{
for (i = 0;i < 89; i++){_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;
_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;
_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;_nop_() ;
_nop_() ;_nop_() ;_nop_() ;};
}
}
/***********************************************************************
DESC: Initializes UART for mode 1
Baudrate: 9600
RETURNS: Nothing
CAUTION: If interrupts are being used then EA must be set to 1
after calling this function
************************************************************************/
void uart_init(void)
{
// configure UART
// clear SMOD0
PCON &= ~0x40;
SCON = 0x50;
// set or clear SMOD1
PCON &= 0x7f;
PCON |= (0 << 8);
SSTAT = 0x00;
// enable break detect
AUXR1 |= 0x40;
// configure baud rate generator
BRGCON = 0x00;
BRGR0 = 0xF0;
BRGR1 = 0x02;
BRGCON = 0x03;
// TxD = push-pull, RxD = input
P1M1 &= ~0x01;
P1M2 |= 0x01;
P1M1 |= 0x02;
P1M2 &= ~0x02;
// initially not busy
// set isr priority to 0
IP0 &= 0xEF;
IP0H &= 0xEF;
// enable uart interrupt
ES = 0;
} // uart_init
/***********************************************************************
DESC: Initializes the ports
RETURNS: Nothing
CAUTION: Call before the initialization functions of other peripherals
************************************************************************/
void ports_init(void)
{
P0M1 &= 0xF0;
P0M2 &= 0xF0;
/*P1M1 |= 0x2C;
P1M2 &= 0xDF;
P1M2 |= 0x0C;*/
P1M1 &= 0xEC;
P1M1 |= 0x2C;
P1M2 &= 0xDF;
P1M2 |= 0x1F;
} // ports_init
//*******************add**********************************************
/****************************************************************************
* *
* Function: START_T0 *
* *
* Input: - *
* Output: - *
* *
* Description: *
* *
* *
****************************************************************************/
void START_T0(unsigned char x)
{
ET0 = 0 ;
TR0 = 0;
RXTimerOUT = x ;
TL0 = 0;
TH0 = 0;
TR0 = 1;
ET0 = 1 ;
}
/****************************************************************************
* *
* Function: CALL_isr_T0 *
* *
* Input: - *
* Output: - *
* *
* Description: *
* *
* *
****************************************************************************/
void CALL_isr_T0(void)
{
TR0 = 0;
RXTimerOUT = 0;
TF0 = 1;
}
//********************串口中断处理程序**************************************************************
void ub1ISR(void) interrupt 4 using 1
{
unsigned char c;
// unsigned int data crcx;
if ( RI ) // Receive Command
{
// RXTimerOUT=10;
RI = 0;
c = SBUF;
switch(RXSTAT)
{
case 0: //接受帧头02
if(c==0x55)
{RXSTAT=1;START_T0(20);RXtem[0]=c;}
else
CALL_isr_T0();
break;
case 1: //接收
if(c==0xaa)
{
RXSTAT=2;
START_T0(20);
RXtem[1]=c;
}
else
{
if(c==0xbb)
{
if(!ISP)
{
set=1;
RXSTAT=2;
START_T0(20);
}
}
else
{
RXSTAT=0;
CALL_isr_T0();
}
}
// RXSTAT=0;
break;
case 2:
if(c==ADDR)
{ // LED= !LED;
RXtem[2]=c;
RXSTAT=3;
START_T0(20);
}
else
{
RXSTAT=0;
CALL_isr_T0();
}
i=0;
break;
case 3:
RX_BEEP=c;
START_T0(20);
RXSTAT=4;
RXtem[3]=c;
/*if(c==ADDR)
{
if(set)
{
RXSTAT=4;
START_T0(20);
}
else
{
RXSTAT=0;
rxok=1;
CALL_isr_T0();
}
}*/
break;
case 4:
if(set)
{
RX_addr=c;
set_ok=1;
set=0;
RXSTAT=0;
CALL_isr_T0();
}
else
{
CRC.chrX[0]=c;
//SBUF=CRC.chrX[0];
// while(!TI);
// TI=0;
RXSTAT=5;
START_T0(20);
}
break;
case 5:
CRC.chrX[1]=c;
//SBUF=CRC.chrX[1];
//while(!TI);
// TI=0;
// if(crcx==CRC.intX)//crc交验通过
{
rxok=1;
}
RXSTAT=0;
CALL_isr_T0();
break;
default:
RXSTAT=0;
break;
}
}
else
{
TI=0;
}
}
/*************************原有的函数需改变*************************************
* *
* Function: isr_timer0 *
* *
* Input: - *
* Output: - *
* *
* Description: *
* *
* *
****************************************************************************/
void isr_timer0(void) interrupt 1 using 2
{
TR0=0;
if(RXTimerOUT!=0) //此if{}else{} 为新加入的判断超时功能
{
RXTimerOUT--;
}
else
{
RXSTAT=0;
RXNOM=0;
set=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -