📄 uart.c.bak
字号:
#include "sfr_r811.h"
#include "sms.h"
#include "string.h"
#include "int.h"
/***************************/
static const char tab[]="0123456789";
/*****************************************************/
void send_str(char ui,const char* sendat);
char sch_str(char ui,const char * strptr);
char wait_str(char ui,const char *strptr);
void clr_rxbuf(char ui);
void delay(unsigned int nms);
/**************************/
char news_index[3];
char news_mem[5];
char uirxcount[2],uitxcount[2],uinumoftx[2];
char *u0txptr;
char *u1txptr;
//#pragma ROM tab
//extern static const char tab[]="0123456789ABCDEF";
#pragma ROM csend
static const char csend[]={0x1a,'\0'};
struct{
char lrd_mem[5];
char rw_mem[5];
char rec_mem[5];
unsigned char lrd_used;
unsigned char lrd_totle;
unsigned char rw_used;
unsigned char rw_totle;
unsigned char rec_used;
unsigned char rec_totle;
}gsmstat;
//extern SM_STAT stat;
/*------------------------------------------------------*/
#pragma BIT flag
union {
struct{
char u0txbusy:1;
char u0usetxbuf:1;
char u1txbusy:1;
char u1usetxbuf:1;
char gsmerror:1;
char newmsg:1;
char chargestar:1;
char chargeend:1;
}bit;
char byte;
}flag;
char txbuf[TXBUFSIZE];
char u0rxbuf[u0RXBUFSIZE];
char u1rxbuf[u1RXBUFSIZE];
void sfr_ref(void);
typedef struct{
unsigned char index;
unsigned char mstat[2];
}MADDR;
MADDR m_addr[16];
unsigned char n_phone[16];
unsigned char max_addr,max_phone;
init_uart0(int baud,char eon)
{ s0tic=0x3;
s0ric=0x4;
if(eon=='n')
{
u0mr=0x05; /*set uar0 MODE 8 1*/
/*setting uart0 register*/
}
else if(eon=='e')
{
u0mr=0x65;
}
else if(eon=='o')
{
u0mr=0x45;
}
else
{
u0mr=0x05;
}
if(baud==1200)
{
u0c0=0x05; //0x85 for 645 /*internal clock ;txdi pin fuction select; lsb first;f1sio 0x00 f1 0x01 f8*/
u0brg=129; /*(n+1)fi*/
}
else if(baud==2400)
{
u0c0=0x05; //0x85 for 645 /*internal clock ;txdi pin fuction select; lsb first;f1sio 0x00 f1 0x01 f8*/
u0brg=64; /*(n+1)fi*/
}
else if(baud==4800)
{
u0c0=0x05; //0x85 for 645 /*internal clock ;txdi pin fuction select; lsb first;f1sio 0x00 f1 0x01 f8*/
u0brg=32; /*(n+1)fi*/
}
else if(baud==9600)
{
u0c0=0x04; //0x85 for 645 /*internal clock ;txdi pin fuction select; lsb first;f1sio 0x00 f1 0x01 f8*/
u0brg=129; /*(n+1)fi*/
}
else if(baud==19200)
{
u0c0=0x04; //0x85 for 645 /*internal clock ;txdi pin fuction select; lsb first;f1sio 0x00 f1 0x01 f8*/
u0brg=64; /*(n+1)fi*/
}
else
{
u0c0=0x05; //0x85 for 645 /*internal clock ;txdi pin fuction select; lsb first;f1sio 0x00 f1 0x01 f8*/
u0brg=129; /*(n+1)fi*/
}
u0c1=0x00; /*stop tx;stop rx*/
ucon=0x20; /*uart0/1 int source select;uart1 txd1 selection, txd1 enable*/
}
init_uart1(void)
{
/*setting uart1 register*/
s1tic=0x5;
s1ric=0x6;
u1mr=0x05;
u1c0=0x04;
u1brg=129;
u1c1=0x00;
}
void init_io(void)
{
/* Setting port registers */
prc2=1;
pd0|=0x19;
pd1=0xdf;
pd3=0x0f;
pd4=0x20;
pur0=0x00;
pur1=0x00;
drr=0x1f;
p0 = 0x00;
p1 = 0x04;
p3 = 0x0;
p4 = 0x20;
}
void init_int(void)
{
/*setting interrupt control register*/
kupic=0x00;
adic=0x00;
cmp1ic=0x00;
int2ic=0x0;
txic=0x1;
tyic=0x2;
tzic=0x0;
int1ic=0x0;
int2ic=0x0;
tcic=0x00;
cmp0ic=0x0;
int0ic=0x00;
/*setting INT0 registers*/
inten=0x00;
int0f=0x00;
/*setting timer mode register*/
}
void init_timer(void)
{
txmr=0x00;
tyzmr = 0x00; /* Timer Z mode = Programmable waveform generation mode */
pum = 0; /* Timer Y, Z waveform output control register */
tys=0; /*Timer y count start flag= stop*/
tzs = 0; /* Timer Z count start flag = stop */
tcss = 0x26; /* division = X=32 Y=8 Z:y/
/* Setting main cycle timer */
/* 20MHz* 1/8 * 250 * 100 =10ms */
prex = 250; /* Setting Prescaler X register */
prey =250;
tx = 250; /* Setting timer X register */
tysc=0;
typr=10;
txs = 1; /* Timer X count start flag = start */
tys =1 ;
tcc0=0x00;
tcc1=0x00;
}
void init_key(void)
{
/*setting key input register*/
kien=0;
aier=0;
}
void init_wdc(void)
{
/*setting wdt register*/
wdc=0;
wdtr=0;
wdts=0;
}
void init_adc(void)
{
adcon2 = 0x01;
adcon1 = 0x10; /* 8/10-bit mode select bit = 8-bit mode */
adcon0 = 0x80; /* fAD/2 and AN0 is selected */
adst = 0; /* A-D conversion STOPED */
}
/************************************************************************************
Name: sfr_init
Parameters: None
Returns: None
Description: Initial setting of SFR registers
************************************************************************************/
void sfr_init(void){
/*setting vcr registers*/
// prcr=0x04;
// vcr2=0;
// d4int=0;
// prcr=0x00;
/*setting clock generare register*/
// prcr=0x01;
// cm0=0x78;
// cm1=0x18;
// ocd=0x00;
// prcr=0x00;
//hr0=0x00;
//pcr0=1;
//hr1=0x00;
//pcr0=0;
tcout=0x00;
}
/************************************************************************************
Name: sfr_ref
Parameters: None
Returns: None
Description: Refresh of SFR registers
************************************************************************************/
void sfr_ref(void){
/* Setting port direction registers */
prcr = 0x04;
pd0 = 0;
prcr = 0;
pd1 = 0x1E;
pd3 = 0x02;
pd4 = 0;
}
/************************************************************************************
Name: send_n_byte
Parameters: ui,send n bytes data through uart0/1;
chptr,point to data array that will be send;
n,the char number in the string;
Returns: none
Description: send n bytes data through uart0/1
************************************************************************************/
void send_n_byte(char ui,char *chptr,char n)
{ // RTS0=1;
stop_timery();
timycount[ui]=0;
clr_rxbuf(ui);
if(ui==0)
{REDI=0;
//RTS0=0;
//while(CTS0==1){}
flag.bit.u0txbusy=1;
u0txptr=chptr;
u0tb=*u0txptr;
*u0txptr=0;
u0txptr++;
uitxcount[0]=1;
uinumoftx[0]=n;
uirxcount[0]=0;
start_u0tx();
}
else if(ui==1)
{
flag.bit.u1txbusy=1;
u1txptr=chptr;
u1tb=*u1txptr;
*u1txptr=0;
u1txptr++;
uitxcount[1]=1;
uinumoftx[1]=n;
uirxcount[1]=0;
start_u1tx();
}
}
/************************************************************************************
Name: sch_645_frame
Parameters: ui, uart0/1,search in the u0/1rxbuf
Returns: 0:not find;1:have fond
Description: search dl645 frame in the u0/1rxbuf,search result store txbuf;
************************************************************************************/
char sch_645_frame(char ui,unsigned char *pdst)
{
char byte_count,frame_len,result,k;
result=0;
if(ui==0)
{
for(byte_count=0;byte_count<u0RXBUFSIZE;byte_count++)
{
if(u0rxbuf[byte_count]==0x68)
{ frame_len=u0rxbuf[byte_count+9]+12;
if(u0rxbuf[byte_count+frame_len-1]==0x16)
{ for(k=0;k<=frame_len-1;k++)
{
pdst[k]=u0rxbuf[byte_count+k];
u0rxbuf[byte_count+k]=0;
}
result=frame_len;
uirxcount[0]=0;
break;
}
}
}
}
else if(ui==1)
{
for(byte_count=0;byte_count<u1RXBUFSIZE;byte_count++)
{
if(u1rxbuf[byte_count]==0x68)
{ frame_len=u0rxbuf[byte_count+9]+12;
if(u1rxbuf[byte_count+frame_len-1]==0x16)
{ for(k=0;k<=frame_len-1;k++)
{
pdst[k]=u1rxbuf[byte_count+k];
u1rxbuf[byte_count+k]=0;
}
result=frame_len;
uirxcount[1]=0;
break;
}
}
}
}
return(result);
}
/************************************************************************************
Name: send_str
Parameters: ui,send a string through uart0/1;
sendat,point to a string that will be send;
Returns: none
Description: none
************************************************************************************/
void send_str(char ui,char* sendat)
{
//strncpy(txbuf,sendat,strlen(sendat));
send_n_byte(ui,sendat,strlen(sendat));
}
/************************************************************************************
Name: int2str
Parameters: src:int number
pDst:dest string
len:length of string
Returns: None
Description: transform a integer number to string which length is equal to len;
************************************************************************************/
void int2str(int src, char* pDst,char len)
{
int index;
char i;
char nzero;
nzero=5-len;
//char *ptr;
i=0;
//ptr=pDst;
index=(int)src/10000;
src=src%10000;
if(i++>=nzero)*pDst++=tab[index];
index=(int)src/1000;
src=src%1000;
if(i++>=nzero)*pDst++=tab[index];
index=(int)src/100;
src=src%100;
if(i++>=nzero)*pDst++=tab[index];
index=(int)src/10;
src=src%10;
if(i++>=nzero)*pDst++=tab[index];
if(i++>=nzero)*pDst++=tab[src];
*pDst= '\0';
//for(i=0;i<=lenint;i++)ptr[i]=ptr[5-lenint+i];
}
void int2str1(int src, char* pDst)
{
int index;
char i;
char nzero,len;
if(src<10)len=1;
else if(src<100)len=2;
else if(src<1000)len=3;
else if(src<10000)len=4;
nzero=5-len;
i=0;
index=(int)src/10000;
src=src%10000;
if(i++>=nzero)*pDst++=tab[index];
index=(int)src/1000;
src=src%1000;
if(i++>=nzero)*pDst++=tab[index];
index=(int)src/100;
src=src%100;
if(i++>=nzero)*pDst++=tab[index];
index=(int)src/10;
src=src%10;
if(i++>=nzero)*pDst++=tab[index];
if(i++>=nzero)*pDst++=tab[src];
*pDst= '\0';
}
/************************************************************************************
Name: sch_str
Parameters: ui, uart0/1,search in the u0/1rxbuf
strptr,point to the const char
Returns: none
Description: search const char in the u0/1rxbuf,search result store txbuf;
************************************************************************************/
char sch_str(char ui,const char * strptr)
{
char byte_count,k,result,LEN;
result=0;
LEN=strlen(strptr);
if(ui==0)
{
for(byte_count=0;byte_count<u0RXBUFSIZE;byte_count++)
{
for(k=0;k<LEN;k++)
{
if(u0rxbuf[byte_count+k]!=*(strptr+k))break;
}
if(k==LEN)
{
//for(j=0;j<u0RXBUFSIZE;j++)u0rxbuf[j]=0x00;
result=1;
break;
}
}
}
else if(ui==1)
{
for(byte_count=0;byte_count<u1RXBUFSIZE;byte_count++)
{
for(k=0;k<LEN;k++)
{
if(u1rxbuf[byte_count+k]!=*(strptr+k))break;
}
if(k==LEN)
{
//for(j=0;j<u1RXBUFSIZE;j++)u1rxbuf[j]=0x00;
result=1;
break;
}
}
}
return(result);
}
/************************************************************************************
Name: sendATcom
Parameters: ui, uart0/1,search in the u0/1rxbuf
com,AT command string
ack,AT command ack string
err,AT error ack string
timeout,if GSM has no ack,seth timeout number :(ms)
Returns: -1,0/1,AT command is successful executed/fail to executed
Description: send a AT command to GSM modle ,then wait for ack
************************************************************************************/
char sendATcom(char ui,const char *com,const char *ack,const char *err,unsigned int timeout)
{
send_str(ui,(char *)com);
while(sch_str(ui,ack)==0)
{
if(sch_str(ui,err)==1)
{
return(0);
break;
}
if(timycount[ui]>=timeout)
{
return(-1);
break;
}
}
return(1);
}
/************************************************************************************
Name: sendATdata
Parameters: ui, uart0/1,search in the u0/1rxbuf
data,AT command or data string
ack,AT command ack string
err,AT error ack string
timeout,if GSM has no ack,seth timeout number :(ms)
Returns: -1,0/1,AT command is successful executed/fail to executed
Description: send a AT command or data to GSM modle ,then wait for ack
************************************************************************************/
char sendATdata(char ui,char *data,const char *ack,const char *err,unsigned int timeout)
{
send_n_byte(ui,data,strlen(data));
while(sch_str(ui,ack)==0)
{
if(sch_str(ui,err)==1)
{
return(0);
break;
}
if(timycount[ui]>=timeout)
{
return(-1);
break;
}
}
return(1);
}
/************************************************************************************
Name: wait_str
Parameters: ui, uart0/1,search in the u0/1rxbuf
strptr,point to a const string that is the ack of GSM modle
Returns: 0/1,have not searched/searched
Description: wait a GSM modle string,note:not clear the u0/1rxbuf
************************************************************************************/
char wait_str(char ui,const char *strptr)
{char byte_count,k,j,result,LEN;
result=0;
LEN=strlen(strptr);
if(ui==0)
{
for(byte_count=0;byte_count<u0RXBUFSIZE;byte_count++)
{
for(k=0;k<LEN;k++)
{
if(u0rxbuf[byte_count+k]!=*(strptr+k))break;
}
if(k==LEN)
{
result=1;
break;
}
}
}
else if(ui==1)
{
for(byte_count=0;byte_count<u1RXBUFSIZE;byte_count++)
{
for(k=0;k<LEN;k++)
{
if(u1rxbuf[byte_count+k]!=*(strptr+k))break;
}
if(k==LEN)
{
result=1;
break;
}
}
}
return(result);
}
/************************************************************************************
Name: clr_rxbuf
Parameters: ui, uart0/1,search in the u0/1rxbuf
Returns: none
Description: clear uirxbuf;
************************************************************************************/
void clr_rxbuf(char ui)
{int i;
if(ui==0)
{
for(i=0;i<u0RXBUFSIZE;i++)u0rxbuf[i]=0;
uirxcount[0]=0;
}
else if(ui==1)
{
for(i=0;i<u1RXBUFSIZE;i++)u1rxbuf[i]=0;
uirxcount[1]=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -