📄 uart.c
字号:
#define UART_GLOBALS
#include "includes.h"
/****************************************************************/
/*description: */
/* */
/* */
/*input parameters: */
/* */
/*output argument: */
/*relative globe variables: */
/***************************************************************/
void SCI3a_ini(void)
{
HF_SPCR_SPC32=1;
HB_SMR=0x20;/*asy, 8bit, even,1stop*/
HB_BRR=32;/*bps is 4800 when 10M crystal when using 38024F*/
HB_SCR3=0;
Set_P_485NEnDe_Output;
}
/****************************************************************/
/*description: this function send some bytes on sci2a; */
/* */
/* */
/*input argument: PP_str: point to the string which is for sending */
/* PB_ssum: the sum of the send byts */
/* PB_0d,PB_0a:the terminal signs */
/*output argument: none */
/*relative globe variables: none */
/***************************************************************/
void SCI3a_send_somebytes(uchar *PP_str,uchar PB_ssum,uchar PB_0d,uchar PB_0a)
{
uchar i,j;
En_485Tr;
HF_SCR3_TE=1;j=1;while(HF_SSR_TDRE==0)nop();
for(j=0;j!=PB_ssum;j++)
{
i=*PP_str;
HB_TDR=i;
nop();
PP_str++;
while(HF_SSR_TDRE==0);
}
if(PB_0d)
{
HB_TDR=PB_0d;
while(HF_SSR_TDRE==0);
}
if(PB_0a)
{
HB_TDR=PB_0a;
while(HF_SSR_TDRE==0);
}
while(HF_SSR_TEND==0);
En_485Re;
HF_SCR3_TE=0;
}
/****************************************************************/
/*description: this function receive a command for host; */
/* */
/* */
/*input argument: none */
/* */
/* */
/*output argument: none */
/*relative globe variables:GB_Sci3a_WCnt,GF_sci3a_wait_tri */
/*GF_sci3a_rec_tout,GF_sci3a_comm_active*/
/*GSTRU_receive_buf.buff:saving for the cmd*/
/***************************************************************/
void wait_for_command(void)
{
uchar LB_data;
while(1)
{
HF_SSR_RDRF=0;HF_SSR_OER=0;HF_SSR_FER=0;HF_SSR_PER=0;
HF_SCR3_RE=1;
GB_Sci3a_WCnt=Uconst_sci3a_recwait;
GF_sci3a_wait_tri=1;
GF_sci3a_rec_tout=0;
GSTRU_receive_buf.Re_Cnt=0;
GF_sci3a_comm_active=0;
while(1)
{
if(HF_SSR_FER)
{
HF_SSR_FER=0;
continue;
}
if(HF_SSR_RDRF)/*whether received a byte*/
{
LB_data=HB_RDR;
HF_SSR_RDRF=0;HF_SSR_OER=0;HF_SSR_FER=0;HF_SSR_PER=0;
if(GSTRU_receive_buf.Re_Cnt==0)
{
if(LB_data==0x55)/*Is the first is the lead code */
{
GB_checksum=0x55;
GSTRU_receive_buf.buff[0]=0x55;
GSTRU_receive_buf.Re_Cnt=1;
GB_Sci3a_WCnt=Uconst_sci3a_recwait;
GF_sci3a_wait_tri=1;
GF_sci3a_rec_tout=0;
}
}
else if(GSTRU_receive_buf.Re_Cnt==1)
{
GB_checksum+=LB_data;
GSTRU_receive_buf.buff[1]=LB_data;
GSTRU_receive_buf.Re_Cnt=2;
GB_Sci3a_WCnt=Uconst_sci3a_recwait;
GF_sci3a_wait_tri=1;
GF_sci3a_rec_tout=0;
}
else if(GSTRU_receive_buf.buff[1]==GSTRU_receive_buf.Re_Cnt-2)
{
if(LB_data==GB_checksum)/*is the chk sum right?*/
{
GSTRU_receive_buf.buff[GSTRU_receive_buf.Re_Cnt]=LB_data;
GSTRU_receive_buf.Re_Cnt+=1;
GB_Sci3a_WCnt=Uconst_sci3a_recwait;
GF_sci3a_wait_tri=0;
GF_sci3a_comm_active=1;
break;
}
}
else
{
GB_checksum+=LB_data;
GSTRU_receive_buf.buff[GSTRU_receive_buf.Re_Cnt]=LB_data;
GSTRU_receive_buf.Re_Cnt+=1;
GB_Sci3a_WCnt=Uconst_sci3a_recwait;
GF_sci3a_wait_tri=1;
GF_sci3a_rec_tout=0;
}
}
else
{
if(GF_sci3a_rec_tout)
{
nop();
break;
}
}
}
if(GF_sci3a_comm_active)
{
switch(GSTRU_receive_buf.buff[2])
{
case 1:
prcess_cmd01_wr();
break;
case 2:
prcess_cmd02_rd();
break;
default:
break;
}
nop();
toggle_led1();
break;
}
}
}
void Sci3a_CmmPrc(void)
{
uchar i,LB_data;
if(GSTRU_receive_buf.Re_Cnt>4)
{
if(GSTRU_receive_buf.buff[0]==0x55)//55 02 02 01 5A
{
if(GSTRU_receive_buf.Re_Cnt>(GSTRU_receive_buf.buff[1]+2))
{
LB_data=0;
for(i=0;i!=(GSTRU_receive_buf.buff[1]+2);i++)
{
LB_data+=GSTRU_receive_buf.buff[i];
}
GSTRU_receive_buf.Re_Cnt=0;
if(LB_data==GSTRU_receive_buf.buff[i])
{
GF_sci3a_comm_active=1;
}
}
else
{
if(GF_sci3a_rec_tout)
{
GSTRU_receive_buf.Re_Cnt=0;
}
}
}
else
{
GSTRU_receive_buf.Re_Cnt=0;
}
}
}
void prcess_cmd01_wr(void)
{
Data_wr7758(GSTRU_receive_buf.buff[3], GSTRU_receive_buf.buff+4);
}
void prcess_cmd02_rd(void)
{
uchar LB_data;
LB_data=GSTRU_receive_buf.buff[3];
Data_rd7758(LB_data);
SCI3a_send_somebytes(GA_buf_rdwr7758, 3, 0x0d, 0x0a);
}
void Sci3a_int_process(void)
{
uchar LB_data;
if((HB_SSR&0x38)!=0)
{
HB_SSR&=0x0c7;
}
if(HF_SSR_RDRF)
{
*(GP_Sci3a_ReBuf+GSTRU_receive_buf.Re_Cnt)=HB_RDR;
GSTRU_receive_buf.Re_Cnt++;
GF_Re1Byte=1;
GB_Sci3a_WCnt=Uconst_sci3a_recwait;
GF_sci3a_wait_tri=1;
GF_sci3a_rec_tout=0;
if(GB_Sci3a_ReSum==GSTRU_receive_buf.Re_Cnt)
{
HF_SCR3_RE=0;
HF_SCR3_RIE=0;
GF_ReEnd=1;
}
}
if(HF_SSR_TDRE)
{
if(GB_Sci3a_TCnt<GB_Sci3a_TSum)
{
HB_TDR=*(GP_Sci3a_TrBuf+GB_Sci3a_TCnt);
GB_Sci3a_TCnt++;
if(GB_Sci3a_TCnt==GB_Sci3a_TSum)
GF_TrEnd=1;
HF_SCR3_TE=1;
HF_SCR3_TIE=1;
En_485Re;
}
}
/*
if(HF_SSR_TEND)
{
if(GB_Sci3a_TCnt==GB_Sci3a_TSum)
{
nop();
HF_SCR3_TE=0;
HF_SCR3_TIE=0;
}
} */
}
void Sci3a_TrEn(uchar *PP_addr,uchar PB_sum)
{
HF_SCR3_TE=1; HB_SSR&=0x0c7;
GB_Sci3a_TCnt=0;GB_Sci3a_TSum=PB_sum;
GP_Sci3a_TrBuf=PP_addr;
GF_TrEnd=0;
En_485Tr;
HF_SCR3_TIE=1;
}
void Sci3a_ReEn(uchar *PP_addr,uchar PB_sum)
{
HF_SCR3_RE=1; HB_SSR&=0x0c7;
GSTRU_receive_buf.Re_Cnt=0;
GB_Sci3a_ReSum=PB_sum;
GP_Sci3a_ReBuf=PP_addr;
GF_ReEnd=0;
En_485Re;
HF_SCR3_RIE=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -