📄 mmm.c
字号:
#include <reg52.h>
#include <absacc.h>
#include "string.h"
#include "intrins.h"
//***********************串口初始化波特率定义*****原有********************
#define OSC_FREQ 11059200L
#define BAUD_115200 256 - (OSC_FREQ/192L)/115200L
#define BAUD_57600 256 - (OSC_FREQ/192L)/57600L
#define BAUD_38400 256 - (OSC_FREQ/192L)/38400L
#define BAUD_28800 256 - (OSC_FREQ/192L)/28800L
#define BAUD_19200 256 - (OSC_FREQ/192L)/19200L
#define BAUD_14400 256 - (OSC_FREQ/192L)/14400L
#define BAUD_9600 256 - (OSC_FREQ/192L)/9600L
#define BAUD_4800 256 - (OSC_FREQ/192L)/4800L
#define BAUD_2400 256 - (OSC_FREQ/192L)/2400L
#define BAUD_1200 256 - (OSC_FREQ/192L)/1200L
//*********************************************************
union charint{
unsigned char chrX[2];
unsigned int intX;
};
union charint ad_temp;
//************************程序用的变量********************
unsigned char data RXtem[16], RXdata[6];//程序接收数据缓冲区
unsigned char data RXNOM,RXSTAT,RX_len=0;
unsigned char data RXTimerOUT; //接收超时
bit rxok; // 接收完成标志
sbit CS=P2^4;
sbit PWR_UP=P2^5;
sbit TX_EN=P2^6;
sbit Test_led=P1^4;
sbit TX=P3^1;
sbit RX=P3^0;
sbit wdog=P1^0;
/****************************************************************************/
void Delay100ms(unsigned char x)
{
unsigned char i,j;
while(x-- != 0)
{ wdog=! wdog;
for (j = 0;j < 67; j++)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)
{wdog=! wdog;
for (i = 0;i <23 ; 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_() ;};
}
}
/****************************************************************************
* *
* Function: init_com *
* *
* Input: - *
* Output: - *
* *
* Description: *
****************************************************************************/
void init_com(void)
{
PCON = 0x80; // SMOD = 1; bps = Time1/16
SCON = 0x50; // Mode 1, 1 star bit, 8 data bit, 1,stop bit UART, enable receive
TMOD = 0x21; // Timer 1, mode 2, 8-bit auto reload,
// Timer 0, mode 1, 16-bit counter
TH1 =BAUD_19200; //BAUD_2400;
TL1 = TH1;
TR1 = 1; // Timer 1 run
ES = 0; // Enable serail interrupts
EA=1;
RXSTAT=0;
RXNOM=0;
rxok=0;
RXTimerOUT=10;
}
/****************************************************************************
* *
* 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 int4() interrupt 4 using 1
{
unsigned char c,add,i;
if ( RI ) // Receive Command
{
// RXTimerOUT=10;
RI = 0;
c = SBUF;
switch(RXSTAT)
{
case 0: //接受帧头02
if(c==0x55){RXSTAT=1;START_T0(20);}
else
CALL_isr_T0();
break;
case 1: //接收
//if(c==0xaa)
if(c==0xff)
{
RXSTAT=2;
START_T0(20);
}
else
{
CALL_isr_T0();
RXSTAT=0;
}
break;
case 2: //接收长度
RX_len=c;
if(RX_len>16)
{
RX_len=0;
RXSTAT=0;
CALL_isr_T0();
}
else
{
RXNOM=0;
START_T0(20);
RXSTAT=3;
}
break;
case 3:
RXtem[RXNOM]=c;
RXNOM++;
if(RXNOM==RX_len)
{
RXSTAT=4;
}
START_T0(20);
break;
case 4:
add=0;
for(i=0;i<RXNOM;i++)
{
add=add+RXtem[i];
}
if(c==add)
{
rxok=1;
}
CALL_isr_T0();
RXSTAT=0;
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;
}
TH0=0X4c;
TL0=0X95;
TR0=1;
}
//***********************************************************
void Send_ok(void)
{
char i;
ES=0;
for(i=0;i<2;i++)
{
SBUF=0Xff;
while(!TI);
TI=0;
SBUF=0X55;
while(!TI);
TI=0;
SBUF=0XAA;
while(!TI);
TI=0;
}
ES=1;
}
//***************************************************
void test(void)
{
while(1)
{
TX_EN=! TX_EN;
Delay1ms(5);
TX_EN=! TX_EN;
Delay1ms(5);
}
}
//**************************************************************************
void main(void)
{
// unsigned int xx;
unsigned char i;
unsigned long yyy=0;
Test_led=!Test_led;
Delay100ms(5);
Test_led=!Test_led;
wdog=! wdog;
TX_EN=0;
init_com();
// ES=0;
//test();
ES=1;
i=0;
PWR_UP=1;
CS=01;
EA=1;
while(1)
{
wdog=! wdog;
TX_EN=0;
RX=1;
if(rxok)
{
rxok=0;
TX_EN=1;
RX=1;
TX=1;
Delay1ms(3);
Send_ok();
// Delay1ms(1);
TX_EN=0;
RX=1;
Delay1ms(3);
Test_led=!Test_led;
Delay1ms(60);
Test_led=!Test_led;
}
// Test_led=!Test_led;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -