📄 main.c
字号:
/////////////////////////////////////
// Generated Initialization File //
/////////////////////////////////////
#include "includes.h"
//接收数据状态机计数器,在以下几种情况下会自动清零:
UINT8 getmode = 0;
//数据接收BUFFER
UINT8 getdatabuf[128];
UINT8 *pdatabuf;
TIMER_PARAMETER TIMER0_PARAMETER;
UINT16 delay_num = 0;
// Peripheral specific initialization functions,
// Called from the Init_Device() function
void Reset_Sources_Init()
{
WDTCN = 0xDE;
WDTCN = 0xAD;
}
void Timer_Init()
{
CKCON = 0x10;
TCON = 0x40;
TMOD = 0x21;
TH0 = 0xB8;
TH1 = 0xB8;
}
void UART_Init()
{
SCON1 = 0x50;
}
void Port_IO_Init()
{
P0MDOUT = 0x01;
XBR0 = 0x05;
XBR2 = 0x44;
}
void Oscillator_Init()
{
int i = 0;
OSCXCN = 0x67;
for (i = 0; i < 3000; i++); // Wait 1ms for initialization
while ((OSCXCN & 0x80) == 0);
OSCICN = 0x0C;
}
void Interrupts_Init()
{
IE = 0x82;
EIE2 = 0x40;
}
// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
Reset_Sources_Init();
Timer_Init();
UART_Init();
Port_IO_Init();
Oscillator_Init();
Interrupts_Init();
}
/*****************************************************************************************
** 函数名称: Enable_Timer0
** 功能描述: 启动定时器0,清零内部计数器
** 输 入: 超时多少个时间单位。
** 输 出: 无
** 全局变量: TIMER0_PARAMETER,delay_num
** 调用模块: 无
******************************************************************************************/
void Enable_timer0(unsigned int num)
{
TIMER0_PARAMETER.num_unit = num;
delay_num = 0; //清零内部计数器
TCON |= 0x10;
}
/*****************************************************************************************
** 函数名称: Disable_Timer0
** 功能描述: 关闭定时器0
** 输 入: 无
** 输 出: 无
** 全局变量: 无
** 调用模块: 无
******************************************************************************************/
void Disable_timer0()
{
TCON &= ~0x10;
}
/*****************************************************************************************
** 函数名称: Get_writeone()
** 功能描述: 关闭定时器0
** 输 入: 无
** 输 出: 返回接收结束状态
0、接收正常;1、接收到变频器发送回来的操作错误数据;2、接收超时
** 全局变量: getmode
** 调用模块: 无
******************************************************************************************/
UINT8 Get_writeone(void)
{
UINT8 return_data = 0;
getmode = 0;
Enable_timer0(100);
while(1)
{
if(TIMER0_PARAMETER.timerout_flag == 1)
{
Disable_timer0();
TIMER0_PARAMETER.timerout_flag = 0;
getmode = TIMEROUT;
return_data = 2;//接收超时
break;
}
else if(getmode == RIGHT_LF)
{
Disable_timer0();
return_data = 0; //正确接收
break;
}
else if(getmode == WRONG_LF)
{
Disable_timer0();
return_data = 1; //接收到错误回复信号
break;
}
}
getmode = 0;
return return_data;
}
void main(void)
{
// volatile UINT8 num; //n代表发送或接收的数据字数
UINT16 count,i;
UINT8 loopnum = 0;
Init_Device();
count = 0;
pdatabuf = getdatabuf;
//测试程序
while(1)
{
while(count < 0x1770)
{
for(loopnum=0;loopnum<3;loopnum++)
{
Send_writeone(0x01,0x2001,count);
if(!Get_writeone())break;
}
if(loopnum >= 3)
{
Send_writeone(0x01,0x2000,2);//停止指令
if(!Get_writeone())while(1);
while(1);
}
for(i=0;i<65534;i++);
count++;
}
count = 0;
}
//测试程序完
}
void Timer0_ISR(void) interrupt 1
{
TH0 = 0xB8;
delay_num++;
//Timer 可以硬件清除溢出标志位
if (delay_num > TIMER0_PARAMETER.num_unit)
{
TIMER0_PARAMETER.timerout_flag = 1;
delay_num = 0;
}
}
void UART1_ISR(void) interrupt 20
{
static UINT8 getdata;
if(SCON1&0x01)
{
SCON1 &= 0xfe;
getdata = SBUF1 & 0x7f;//接收时屏蔽最高位,当作停止位
switch(getdata)
{
case T_STR:
getmode = SOF;break;//STR
case T_CR:
if(getmode <= 9)
{
getmode = WRONG_CR;
break;//提前接收CR,说明通信错误
}
else
{
getmode = RIGHT_CR;
break;//正确接收CR
}
case T_LF:
if(WRONG_CR == getmode)
{
getmode = WRONG_LF;//错误接收结束
break;//LF
}
else
{
getmode = RIGHT_LF;//正确接收结束
break;//LF
}
}
switch(getmode)
{
case SOF: pdatabuf = getdatabuf;getmode = 2;break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:*pdatabuf = getdata;++pdatabuf;++getmode;break;
case RIGHT_CR:break;
case WRONG_CR:break;
case RIGHT_LF:break;//正确接收结束
case WRONG_LF:break;//错误接收结束,保留getmode,以供识别
case TIMEROUT:break;//外部定时器溢出
default: *pdatabuf = getdata;++pdatabuf;;break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -