📄 mmm.c
字号:
#include <reg52.h>
#include <absacc.h>
#include "string.h"
#include "intrins.h"
//***********************串口初始化波特率定义*****原有********************
#define OSC_FREQ 11059200L
#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[6], RXdata[6];//程序接收数据缓冲区
unsigned char data RXNOM,RXSTAT;
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 wdog=P1^0;
/****************************************************************************/
void Delay100ms(unsigned char x)
{
unsigned char i,j;
while(x-- != 0)
{ wdog=! wdog;
for (j = 0;j < 114; 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 < 45; 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;
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)
{
rxok=1;
RXSTAT=0;
CALL_isr_T0();
}
else
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;
}
//*************************************************************************
unsigned char Send(unsigned char *p,unsigned char len)
{
unsigned char i,add=0;
ES=0;
TX_EN=1;
Delay1ms(6);
SBUF=0X55;
while(!TI);
TI=0;
SBUF=0XAA;
while(!TI);
TI=0;
SBUF=len;
while(!TI);
TI=0;
for(i=0;i<len;i++)
{
SBUF=p[i];
while(!TI);
TI=0;
add=add+p[i];
}
SBUF=add; //发送校验
while(!TI);
TI=0;
TX_EN=0;
ES=1;
EA=1;
Delay1ms(15);
if(rxok)
{
ES=0;
rxok=0;
TX_EN=1;
Delay1ms(4);
return 1;
}
else
{
ES=0;
TX_EN=1;
Delay1ms(4);
return 0;
}
}
//**************************************************************************
void main(void)
{
// unsigned int xx;
unsigned char i,*sdata;
unsigned long yyy=0;
Test_led=!Test_led;
Delay100ms(5);
Test_led=!Test_led;
wdog=! wdog;
TX_EN=1;
init_com();
ES=0;
i=0;
PWR_UP=1;
CS=1;
while(1)
{
wdog=! wdog;
sdata="12";
if( Send(sdata,2))
{
Test_led=!Test_led;
Delay1ms(60);
Test_led=!Test_led;
}
else
Delay1ms(10);
// Delay100ms(1);
//
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -