標(biāo)題:
51單片機(jī)串口通信程序,支持自定義協(xié)議
[打印本頁(yè)]
作者:
不拼不活
時(shí)間:
2019-12-5 17:40
標(biāo)題:
51單片機(jī)串口通信程序,支持自定義協(xié)議
51串口通信,支持自定義協(xié)議,代碼注釋非常全,求黑幣
單片機(jī)源程序如下:
#include"reg52.h" /*51串口通信by沸騰的冰塊/不拼不活*/
typedef unsigned int u16;
typedef unsigned char u8;
#define Data_SIZE 7 //存入數(shù)組RevBuf數(shù)據(jù)長(zhǎng)度==uart_Data_SIZE-2
#define uart_Data_SIZE 5//協(xié)議實(shí)際長(zhǎng)度(一個(gè)漢字占兩位)
u8 RevBuf[Data_SIZE]; //數(shù)據(jù)接收緩沖區(qū)
u8 indata[uart_Data_SIZE]; //防數(shù)據(jù)抵消緩沖區(qū)
u16 data_count=0; //數(shù)據(jù)長(zhǎng)度,串口中斷自增
u16 temp_length=0; //數(shù)據(jù)長(zhǎng)度,協(xié)議長(zhǎng)度中轉(zhuǎn)
u8 flished_flag=0; //數(shù)據(jù)接收符合要求標(biāo)志
u8 uart_flished_flag=0; //串口成功標(biāo)志
u8 i=0;//延時(shí)用
u16 temp=0,count=0;//temp緩存,count用于判斷是否接受到數(shù)據(jù)
sbit LED1=P1^0;sbit k1=P2^0;
sbit LED2=P1^1;sbit k2=P2^1;
sbit LED3=P1^2;sbit k3=P2^2;
sbit LED4=P1^3;sbit k4=P2^3;
sbit LED5=P1^4;sbit k5=P2^4;
sbit LED6=P1^5;sbit k6=P2^5;
sbit LED7=P1^6;sbit k7=P2^6;
sbit LED8=P1^7;sbit k8=P2^7;
void InitUART(void)
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFA;//在此修改波特率
TL1 = TH1;
PCON = 0x80;
EA = 1;
ES = 1;
TR1 = 1;
}
void delay(u16 i)
{
while(i--);
}
void keypros()//按鍵處理
{
if(k1==0)
{
delay(1000);
if(k1==0)
{
LED1=~LED1;
}
while(!k1);
}
if(k2==0)
{
delay(1000);
if(k2==0)
{
LED2=~LED2;
}
while(!k2);
}
if(k3==0)
{
delay(1000);
if(k3==0)
{
LED3=~LED3;
}
while(!k3);
}
if(k4==0)
{
delay(1000);
if(k4==0)
{
LED4=~LED4;
}
while(!k4);
}
if(k5==0)
{
delay(1000);
if(k5==0)
{
LED5=~LED5;
}
while(!k5);
}
if(k6==0)
{
delay(1000);
if(k6==0)
{
LED6=~LED6;
}
while(!k6);
}
if(k7==0)
{
delay(1000);
if(k7==0)
{
LED7=~LED7;
}
while(!k7);
}
if(k8==0)
{
delay(1000);
if(k8==0)
{
LED8=~LED8;
}
while(!k8);
}
}
void UartSendByte(unsigned char c)//發(fā)送一個(gè)字符
{
SBUF = c;
while(!TI);
TI = 0;
}
void UartSendString(u8 *string)//發(fā)送一個(gè)字符串
{
while(*string)
{
UartSendByte(*string++);
}
}
void main(void)
{
InitUART();
UartSendString("Uart_test is OK \r\n");
while(1)
{
keypros();
if(count<data_count)
{
count=data_count;
if(temp!='\n') //判斷是否接收到結(jié)束符
{
RevBuf[data_count-1]=temp;// 否,就存到RevBuf【】數(shù)組中
flished_flag=0;//未接收完
}
else
{
temp_length=data_count;//是,記錄其數(shù)據(jù)長(zhǎng)度
data_count=count=0;
flished_flag=1;//接收完
}
}
if(temp_length == Data_SIZE&&flished_flag==1) //判斷數(shù)據(jù)長(zhǎng)度是否滿足要求,是否接收完。
{
for(i=0;i<uart_Data_SIZE;i++)
{
indata[i]=RevBuf[i]; //緩沖區(qū)
}
uart_flished_flag=1; //數(shù)據(jù)接收成功標(biāo)志
flished_flag=0;
}
if(uart_flished_flag==1) //數(shù)據(jù)接收完整成功
{
UartSendString("你發(fā)送的是\r\n");
for(i=0;i<uart_Data_SIZE;i++)
{
SBUF = indata[i];
while(!TI);
TI=0;
}
UartSendString("\r\n");
uart_flished_flag=0;
if(indata[1]=='n')//發(fā)送:on :1
switch(indata[4])
{
case'1':LED1=0;break;
case'2':LED2=0;break;
case'3':LED3=0;break;
case'4':LED4=0;break;
case'5':LED5=0;break;
case'6':LED6=0;break;
case'7':LED7=0;break;
case'8':LED8=0;break;
}
if(indata[1]=='f')//發(fā)送:off:1
switch(indata[4])
{
case'1':LED1=1;break;
case'2':LED2=1;break;
case'3':LED3=1;break;
case'4':LED4=1;break;
case'5':LED5=1;break;
case'6':LED6=1;break;
case'7':LED7=1;break;
case'8':LED8=1;break;
}
}
}
}
void UARTInterrupt(void) interrupt 4
{
if(RI)
{
temp=SBUF;
data_count++;
RI=0;
}
}
復(fù)制代碼
51串口通信帶協(xié)議.rar
2019-12-5 17:38 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
1.41 KB, 下載次數(shù): 21, 下載積分: 黑幣 -5
作者:
不拼不活
時(shí)間:
2019-12-7 10:11
#include"reg52.h" /*51串口通信高級(jí)版by沸騰的冰塊/不拼不活*/
typedef unsigned int u16;
typedef unsigned char u8;
#define Data_SIZE 7 //存入數(shù)組RevBuf數(shù)據(jù)長(zhǎng)度==協(xié)議長(zhǎng)度-2
u8 RevBuf[Data_SIZE]; //數(shù)據(jù)接收緩沖區(qū)
u8 indata[2*Data_SIZE]; //防數(shù)據(jù)抵消緩沖區(qū)
u16 data_count=0; //數(shù)據(jù)長(zhǎng)度,串口中斷自增
u16 temp_length=0; //數(shù)據(jù)長(zhǎng)度,協(xié)議長(zhǎng)度中轉(zhuǎn)
u8 flished_flag=0; //數(shù)據(jù)接收符合要求標(biāo)志
u8 uart_flished_flag=0; //串口成功標(biāo)志
u8 i=0;//延時(shí)用,循環(huán)用
u8 sign;//協(xié)議開(kāi)頭標(biāo)志位
u16 temp=0,count=0;//temp緩存,count用于判斷是否接受到數(shù)據(jù)
sbit LED1=P1^0;sbit k1=P2^0;
sbit LED2=P1^1;sbit k2=P2^1;
sbit LED3=P1^2;sbit k3=P2^2;
sbit LED4=P1^3;sbit k4=P2^3;
sbit LED5=P1^4;sbit k5=P2^4;
sbit LED6=P1^5;sbit k6=P2^5;
sbit LED7=P1^6;sbit k7=P2^6;
sbit LED8=P1^7;sbit k8=P2^7;
void InitUART(void)
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFA;//在此修改波特率
TL1 = TH1;
PCON = 0x80;
EA = 1;
ES = 1;
TR1 = 1;
}
void delay(u16 i)
{
while(i--);
}
void keypros()//按鍵處理
{
if(k1==0)
{
delay(1000);
if(k1==0)
{
LED1=~LED1;
}
while(!k1);
}
if(k2==0)
{
delay(1000);
if(k2==0)
{
LED2=~LED2;
}
while(!k2);
}
if(k3==0)
{
delay(1000);
if(k3==0)
{
LED3=~LED3;
}
while(!k3);
}
if(k4==0)
{
delay(1000);
if(k4==0)
{
LED4=~LED4;
}
while(!k4);
}
if(k5==0)
{
delay(1000);
if(k5==0)
{
LED5=~LED5;
}
while(!k5);
}
if(k6==0)
{
delay(1000);
if(k6==0)
{
LED6=~LED6;
}
while(!k6);
}
if(k7==0)
{
delay(1000);
if(k7==0)
{
LED7=~LED7;
}
while(!k7);
}
if(k8==0)
{
delay(1000);
if(k8==0)
{
LED8=~LED8;
}
while(!k8);
}
}
void UartSendByte(unsigned char c)//發(fā)送一個(gè)字符
{
SBUF = c;
while(!TI);
TI = 0;
}
void UartSendString(u8 *string)//發(fā)送一個(gè)字符串
{
while(*string)
{
UartSendByte(*string++);
}
}
void main(void)
{
InitUART();
UartSendString("Uart_test is OK \r\n");
while(1)
{
keypros();
if(count<data_count)
{
count=data_count;
if(temp!='\n') //判斷是否接收到結(jié)束符
{
RevBuf[data_count-1]=temp;//否,存到RevBuf【】數(shù)組中
flished_flag=0;//未接收完
}
else
{
temp_length=data_count;//是,記錄其數(shù)據(jù)長(zhǎng)度
data_count=count=0;
flished_flag=1;//接收完
}
}
if(temp_length >= Data_SIZE&&flished_flag==1) //判斷數(shù)據(jù)長(zhǎng)度是否滿足要求,是否接收完。
{
for(i=0;i<(temp_length-2);i++)
{
indata[i]=RevBuf[i]; //緩沖區(qū)
}
uart_flished_flag=1; //數(shù)據(jù)接收成功標(biāo)志
flished_flag=0;
}
if(uart_flished_flag==1) //數(shù)據(jù)接收完整成功
{
UartSendString("你發(fā)送的是\r\n");
for(i=0;i<(temp_length-2);i++)
{
SBUF = indata[i];
while(!TI);
TI=0;
}
UartSendString("\r\n");
uart_flished_flag=0;
for(i=0;i<(temp_length-2);i++)//定位協(xié)議頭
{
if(indata[i]=='o')
{
sign=i;break;
}
}
if(indata[sign]=='o'&&indata[sign+1]=='n')//發(fā)送:on :1
switch(indata[sign+4])
{
case'1':LED1=0;break;
case'2':LED2=0;break;
case'3':LED3=0;break;
case'4':LED4=0;break;
case'5':LED5=0;break;
case'6':LED6=0;break;
case'7':LED7=0;break;
case'8':LED8=0;break;
}
if(indata[sign]=='o'&&indata[sign+1]=='f'&&indata[sign+2]=='f')//發(fā)送:off:1
switch(indata[sign+4])
{
case'1':LED1=1;break;
case'2':LED2=1;break;
case'3':LED3=1;break;
case'4':LED4=1;break;
case'5':LED5=1;break;
case'6':LED6=1;break;
case'7':LED7=1;break;
case'8':LED8=1;break;
}
}
}
}
void UARTInterrupt(void) interrupt 4
{
if(RI)
{
temp=SBUF;
data_count++;
RI=0;
}
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1