pic,串口怎样发送和接收连续字符?
单片机怎样发送和接收连续的字符?比如发送AT命令:“AT+????”,返回的字符又怎么处理? unsigned char chr[]="AT+CGMI";unsigned char i;//, j;
// j=sizeof(chr);
if(Comm_Receive_OK)//接收完成
{ Comm_Receive_OK = 0;
EN485=1;
asm("nop"); asm("nop");//发送
for(i=0;chr!='\0';i++)
// for(i=0;i<j;i++)
{TXREG1 = chr;//发送
}
asm("nop"); asm("nop");
EN485=0;
}
} 中断接收放到缓存,主程序里判断是否有期望的返回字符串或错误返回,同时要作超时出错处理 平时很少扫盲的,今天发2个代码:
#include "Use.h"
#include "Ver.H"
/**************************************************************
UART接收程序在这里完成P0.4接收!
协议:
正常使用后,GPIO3=RXD;GPIO5=TXD!!GPIO4=0 进入下载模式!
波特率:9600 8 n 1
**************************************************************/
#define Uart_IN_IOGPIO3 //GPIO3=接收!
//********************************************************************
#define MAX_RS232_CNT 6 //通讯的字节数
//-----------------------------------------------------------------------
static unsigned char getBuf,cnt=0; //接收缓存!
static unsigned char tRSok=0;
//********************************************************************
// 通讯格式: 05 HL SUM
unsigned char Get_Uart_Rece_Dat (void ){
unsigned char i,bt;
BPS_Delay();
//************************************************************
bt=8;
i=0;
while(bt--){
i>>=1;
if(Uart_IN_IO) i |=0x80;
//*************************************************************
//延时等待下一个位
BPS_Delay();
}
//************************************************************
//等待结束位 (1)
//BPS_Delay();
return i;
}
//***********************************************************
//产生中断,开始接收!查询!
void Uart_Rece (void ){
getBuf=Get_Uart_Rece_Dat();
tRSok=5;
if(++cnt >= MAX_RS232_CNT) cnt=0;
}
//***********************************************************
//查询中断数据,是否接收完毕!!
unsigned char Get_Receive_DATA_COM0(void){
unsigned char i;
if(tRSok==0) return 0;
if(tRSok--==1){
i=cnt;
cnt=0;
return i;
}
return 0;
}
//***********************************************************
//返回合成ASC的BCC
unsigned char Get_BCC(unsigned char *p,unsigned char Long){
unsigned char Bcc;
Bcc=0;
while(Long--) Bcc +=*p++;
return (Bcc);
}
//***********************************************************
//查询中断数据
unsigned charGetPLC_Do(void){
unsigned char i,bcc;
i=Get_Receive_DATA_COM0();
if(i==0) return 0; //返回错误,未收到数据!!
//****************************************************
//02 05 46 4D
bcc=Get_BCC(getBuf,3);
//----------------------------------------------------
if (bcc != getBuf) return 2; //校验码出错
if (getBuf !=0x02) return 3; //特征字不对!
//*****************************************************************
PRM.PRM0 =(getBuf<<8 | getBuf);
//*****************************************************************
SendToPC();
return 1; //数据OK!!
}
/***********************************************************************
通讯的数据
02 03 20 25//800
02 03 52 57//850
02 03 84 89//900
02 03 e8 ed//1000
02 04 b0 b6//1200
02 05 46 4D//1350
02 05 dc e3//1500
02 06 40 48//1600
02 07 08 11//1800
*************************************************************************/
#include "Use.h"
#include "Ver.H"
/********************************************************************************
正常使用后,GPIO3=RXD;GPIO5=TXD!!GPIO4=0 进入下载模式!
波特率:9600 8 n 1
********************************************************************************/
#defineUART_SEND_Pin GPIO4 //GPIO4=TXD
//********************************************************************************
/*
void BPS_Delay(void){
TMR2IF=0;
TMR2ON=1;
while(!TMR2IF);
TMR2ON=0;
PR2=TtK;
}
*/
//********************************************************************************
/*
void UART_Out(unsigned char Dat){
unsigned char i;
i=8; //1200 8.n 1 P d8 S
UART_SEND_Pin=0;
PR2=TtK;
BPS_Delay();
while(i--){
UART_SEND_Pin=(bit)(Dat & 0x01);
Dat>>=1;
BPS_Delay();
}
UART_SEND_Pin=1;
BPS_Delay();
}
*/
//*********************************************************************************
//发送一串数据
//*********************************************************************************
void SendOut(unsigned char *p,unsigned char Long){
unsigned char i,Dat;
while(Long--){
CLRWDT(); //定时喂狗.
i=8; //9600 8.n 1 P d8 S
Dat=*p++;
BPS_Delay();
UART_SEND_Pin=0; //开始位
BPS_Delay();
while(i--){
UART_SEND_Pin=(bit)(Dat & 0x01);
Dat>>=1;
BPS_Delay();
}
UART_SEND_Pin=1;
BPS_Delay();
}
}
//*************************************************************
//发送频率到PC机
void SendToPC(void){
unsigned char i,buf;
static unsigned int delay=0;
//if(delay++ >100){
//发送到PC机
i=sprintf(buf,"F=%uU=%u \r\n",(unsigned int)PRM.PRM,(unsigned int)(PRM.Ua*1000));
SendOut(buf,i);
//delay=0;
// }
}
页:
[1]